      SUBROUTINE AD_MAIN ()
        IMPLICIT INTEGER (I-K), REAL (R-T), DOUBLE PRECISION (D-F), COMP
     *LEX (X-Z), CHARACTER (A-C), LOGICAL (L-N)
        
        INTEGER :: K_MA (0:13)
        INTEGER :: K_MB (0:24)
        INTEGER :: K_MC (0:26)
        
        INTEGER, DIMENSION (0:0) :: K_PP
        INTEGER, DIMENSION (0:3) :: I_A
        REAL, DIMENSION (0:3, 0:0) :: R_B
        REAL, DIMENSION (0:4, 0:3) :: R_C
        INTEGER :: I_I
        INTEGER :: I_J
        
        LOGICAL  N_FIRSTCALL
        DATA     N_FIRSTCALL /.TRUE./
        INTEGER  ::  K_Q, K_H0, K_H1, K_H2, K_H3, K_H4, K_H5, K_H6, K_H7
     *, K_P0, K_P1 (0:0), K_P2 (0:1), K_P3 (0:2), K_P4 (0:3), K_P5 (0:4)
     *, K_P6 (0:5), K_P7 (0:6)
        COMMON /Q_PROCS/ K_Q, K_H0, K_H1, K_H2, K_H3, K_H4, K_H5, K_H6, 
     *K_H7, K_P0, K_P1, K_P2, K_P3, K_P4, K_P5, K_P6, K_P7
        
        INTEGER           ::  K_PTR, K_Z, K_TMP (0 : 299999)
        REAL              ::  T_TMP (0 : 299999)
        DOUBLE PRECISION  ::  F_TMP (0 : 149999)
        COMPLEX           ::  Z_TMP (0 : 149999)
        LOGICAL           ::  N_TMP (0 : 299999)
        EQUIVALENCE (K_TMP, T_TMP, F_TMP, Z_TMP, N_TMP)
        COMMON /Q_STACK/ K_PTR, K_Z, K_TMP
        
        K_PTR = 0
        
        CALL AD_INIT_PROCS (K_H1, 1, (/1/))
        CALL AD_PROC_COORD (K_H1, K_P1)
        CALL AD_INIT_PROCS (K_H2, 2, (/1, 1/))
        CALL AD_PROC_COORD (K_H2, K_P2)
        CALL AD_INIT_PROCS (K_H3, 3, (/1, 1, 1/))
        CALL AD_PROC_COORD (K_H3, K_P3)
        CALL AD_INIT_PROCS (K_H4, 4, (/1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H4, K_P4)
        CALL AD_INIT_PROCS (K_H5, 5, (/1, 1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H5, K_P5)
        CALL AD_INIT_PROCS (K_H6, 6, (/1, 1, 1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H6, K_P6)
        CALL AD_INIT_PROCS (K_H7, 7, (/1, 1, 1, 1, 1, 1, 1/))
        CALL AD_PROC_COORD (K_H7, K_P7)
        CALL AD_INIT_PROCS (K_HP, 1, (/6/))
        CALL AD_PROC_COORD (K_HP, K_PP)
        
        K_MA = (/65, 0, K_HP, 0, 5, 1, 4, 0, 5, 1, 20, 4, 2, 0/)
        K_MB = (/66, 0, K_HP, 0, 0, 4, 5, 1, 1, 4, 1, 0, 3, 4, 5, 1, 4, 
     *4, 20, 4, 4, 1, 2, -1, 0/)
        K_MC = (/74, 0, K_HP, 0, 0, 5, 4, 1, 1, 5, 4, 0, 0, 5, 4, 1, 1, 
     *5, 4, 5, 4, 1, 1, -1, -1, 0, 0/)
        
        DO 10 I_I = 1, 5
          IF (K_PP(0) == (I_I-1)/4) THEN
            K_V0 = MOD(I_I-1,4)
            I_A (K_V0) = I_I*10
          ENDIF
          
   10   CONTINUE
        IF (K_P0 == 0) THEN
          PRINT "(' a...')"
        ENDIF
        DO 20 I_I = 1, 5
          K_M0 = K_PTR;   K_PTR = K_PTR + 4
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M0 : K_M0+2) = (/256, 0, K_H0/)
          
          K_M1 = K_PTR;   K_PTR = K_PTR + 14
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M1 : K_M1+5) = (/65, 0, K_HP, I_I-1, -1, 1/)
          K_TMP (K_M1+6 : K_M1+13) = K_MA (6 : 13)
          
          CALL AD_REMAP_II (K_V0, K_TMP(K_M0), I_A, K_TMP(K_M1))
          K_PTR = K_PTR - 14
          
          IF (K_P0 == 0) THEN
            PRINT "(i4)", K_V0
          ENDIF
          K_PTR = K_PTR - 4
   20   CONTINUE
        K_T0 = K_PTR;   K_PTR = K_PTR + 4
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        CALL AD_SPREAD_I (K_TMP(K_T0), K_MB, I_A, K_MA, 1, 4)
        
        K_V0 = K_PP(0)*4
        K_V1 = MAX(K_V0/4,0)
        K_V2 = (-K_V0+K_V1*4+3)/4
        K_V3 = MIN(-K_V0+19,3)/4
        K_T0_0 = K_T0 + (K_V2*4)
        DO K_1 = K_V2, K_V3
          DO K_0 = 0, 3
            R_B (K_0,K_1) = K_TMP (K_T0_0)
            K_T0_0 = K_T0_0 + (1)
          ENDDO
        ENDDO
        
        K_PTR = K_PTR - 4
        IF (K_P0 == 0) THEN
          PRINT "(' spread...')"
        ENDIF
        DO 30 I_J = 1, 4
          K_T0 = K_PTR;   K_PTR = K_PTR + 6
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_M0 = K_PTR;   K_PTR = K_PTR + 14
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M0 : K_M0+13) = (/257, 0, K_H0, 0, 5, 1, 5, 0, 5, 1, 
     *5, 5, 1, -1/)
          
          K_M1 = K_PTR;   K_PTR = K_PTR + 26
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M1 : K_M1+8) = (/66, 0, K_HP, I_J-1, 0, -1, 5, 1, 1/)
          K_TMP (K_M1+9 : K_M1+24) = K_MB (9 : 24)
          
          CALL AD_REMAP_RR (T_TMP(K_T0), K_TMP(K_M0), R_B, K_TMP(K_M1))
          K_PTR = K_PTR - 26
          
          IF (K_P0 == 0) THEN
            PRINT "(10f7.2)", T_TMP(K_T0:K_T0+(4))
          ENDIF
          K_PTR = K_PTR - 20
   30   CONTINUE
        DO 40 I_I = 1, 5
          DO 50 I_J = 1, 4
            IF (K_PP(0) == ((I_I-1)*4+3)/4) THEN
              K_V0 = I_J-1
              K_V1 = MOD((I_I-1)*4+3,4)/4
              R_B (K_V0,K_V1) = I_I+I_J*10
            ENDIF
            
   50     CONTINUE
   40   CONTINUE
        IF (K_P0 == 0) THEN
          PRINT "(' b...')"
        ENDIF
        DO 60 I_J = 1, 4
          K_T0 = K_PTR;   K_PTR = K_PTR + 6
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_M0 = K_PTR;   K_PTR = K_PTR + 14
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M0 : K_M0+13) = (/257, 0, K_H0, 0, 5, 1, 5, 0, 5, 1, 
     *5, 5, 1, -1/)
          
          K_M1 = K_PTR;   K_PTR = K_PTR + 26
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M1 : K_M1+8) = (/66, 0, K_HP, I_J-1, 0, -1, 5, 1, 1/)
          K_TMP (K_M1+9 : K_M1+24) = K_MB (9 : 24)
          
          CALL AD_REMAP_RR (T_TMP(K_T0), K_TMP(K_M0), R_B, K_TMP(K_M1))
          K_PTR = K_PTR - 26
          
          IF (K_P0 == 0) THEN
            PRINT "(10f7.2)", T_TMP(K_T0:K_T0+(4))
          ENDIF
          K_PTR = K_PTR - 20
   60   CONTINUE
        K_T0 = K_PTR;   K_PTR = K_PTR + 20
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        CALL AD_TRANSPOSE_R (T_TMP(K_T0), K_MC, R_B, K_MB)
        
        IF (K_PP(0) == 0) THEN
          K_T0_0 = K_T0
          DO K_1 = 0, 3
            DO K_0 = 0, 4
              R_C (K_0,K_1) = T_TMP (K_T0_0)
              K_T0_0 = K_T0_0 + (1)
            ENDDO
          ENDDO
        ENDIF
        
        K_PTR = K_PTR - 20
        IF (K_P0 == 0) THEN
          PRINT "(' transpose...')"
        ENDIF
        DO 70 I_I = 1, 5
          K_T0 = K_PTR;   K_PTR = K_PTR + 4
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_M0 = K_PTR;   K_PTR = K_PTR + 14
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M0 : K_M0+13) = (/257, 0, K_H0, 0, 4, 1, 4, 0, 4, 1, 
     *4, 4, 1, -1/)
          
          K_M1 = K_PTR;   K_PTR = K_PTR + 28
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M1 : K_M1+8) = (/74, 0, K_HP, I_I-1, 0, -1, 4, 1, 1/)
          K_TMP (K_M1+9 : K_M1+26) = K_MC (9 : 26)
          
          CALL AD_REMAP_RR (T_TMP(K_T0), K_TMP(K_M0), R_C, K_TMP(K_M1))
          K_PTR = K_PTR - 28
          
          IF (K_P0 == 0) THEN
            PRINT "(10f7.2)", T_TMP(K_T0:K_T0+(3))
          ENDIF
          K_PTR = K_PTR - 18
   70   CONTINUE
        CALL AD_DELETE_PROCS (K_HP)
        
        CALL AD_DELETE_PROCS (K_H7)
        CALL AD_DELETE_PROCS (K_H6)
        CALL AD_DELETE_PROCS (K_H5)
        CALL AD_DELETE_PROCS (K_H4)
        CALL AD_DELETE_PROCS (K_H3)
        CALL AD_DELETE_PROCS (K_H2)
        CALL AD_DELETE_PROCS (K_H1)
        CALL AD_DELETE_PROCS (K_H0)
        
      END
