      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:24)
        INTEGER :: K_MB (0:24)
        INTEGER :: K_MV (0:13)
        INTEGER :: K_MPAD (0:15)
        INTEGER :: K_MSHAPE (0:13)
        
        INTEGER, DIMENSION (0:1) :: K_PP
        INTEGER, DIMENSION (0:3, 0:1) :: I_A
        INTEGER, DIMENSION (0:1, 0:9) :: I_B
        INTEGER, DIMENSION (0:9) :: I_V
        INTEGER, DIMENSION (0:19) :: I_PAD
        INTEGER :: I_I
        INTEGER :: I_J
        INTEGER, DIMENSION (0:1) :: I_SHAPE
        INTEGER, DIMENSION (0:1) :: I_ORDER
        
        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, 2, (/3, 2/))
        CALL AD_PROC_COORD (K_HP, K_PP)
        
        K_MA = (/66, 0, K_HP, 0, 0, 2, 3, 1, 1, 4, 2, 5, 4, 2, 3, 3, 5, 
     *30, 30, 10, 10, 2, 4, 0, 1/)
        K_MB = (/66, 0, K_HP, 0, 0, 2, 4, 1, 1, 2, 10, 0, 22, 2, 4, 1, -
     *2, 2, 30, 2, 10, 1, 4, -1, 1/)
        K_MV = (/65, 0, K_HP, 0, 6, 1, 10, 0, 6, 1, 30, 10, 2, 0/)
        K_MPAD = (/73, 0, K_HP, 0, 2, 1, 20, 27, 2, 1, 30, 10, 4, 1, 0, 
     *0/)
        K_MSHAPE = (/129, 0, 0, 0, 2, 1, 2, 0, 2, 1, 2, 2, 1, -1/)
        
        DO 10 I_I = 1, 6
          IF (K_PP(0) == (I_I-1)/10) THEN
            K_V0 = MOD(I_I-1,10)
            I_V (K_V0) = I_I
          ENDIF
          
   10   CONTINUE
        IF (K_PP(0) == 0 .AND. K_PP(1) == 0) THEN
          I_PAD (17) = 0
        ENDIF
        
        IF (K_PP(0) == 0 .AND. K_PP(1) == 0) THEN
          I_PAD (18) = 0
        ENDIF
        
        I_SHAPE (0) = 2
        
        I_SHAPE (1) = 3
        
        K_T0 = K_PTR;   K_PTR = K_PTR + 8
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        CALL AD_RESHAPE_I (K_TMP(K_T0), K_MA, I_V, K_MV, I_SHAPE, K_MSHA
     *PE, K_TMP(0), (/0/), .FALSE., K_TMP(0), (/0/), .FALSE.)
        
        DO K_C1 = 0, 0
          K_V0 = K_PP(0)*10
          K_V1 = MAX((K_V0-3)/3,0)
          K_V2 = (-K_V0+K_V1*3+5)/3
          K_V3 = (K_C1*2+K_PP(1))*10
          K_V4 = MAX(K_V3/5,0)
          K_V5 = K_C1*2+(-K_V3+K_V4*5+4)/5
          K_V6 = MIN(-K_V0+8,9)/3
          K_V7 = K_C1*2+MIN(-K_V3+14,9)/5
          K_T0_1 = K_T0 + (K_V2+K_V5*4)
          DO K_1 = K_V5, K_V7
            K_T0_0 = K_T0_1
            DO K_0 = K_V2, K_V6
              I_A (K_0,K_1) = K_TMP (K_T0_0)
              K_T0_0 = K_T0_0 + (1)
            ENDDO
            K_T0_1 = K_T0_1 + (4)
          ENDDO
        ENDDO
        
        K_PTR = K_PTR - 8
        IF (K_P0 == 0) THEN
          PRINT "(' a...')"
        ENDIF
        DO 20 I_I = 1, 2
          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, 3, 1, 3, 0, 3, 1, 
     *3, 3, 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_I-1, 0, -1, 3, 1, 1/)
          K_TMP (K_M1+9 : K_M1+24) = K_MA (9 : 24)
          
          CALL AD_REMAP_II (K_TMP(K_T0), K_TMP(K_M0), I_A, K_TMP(K_M1))
          K_PTR = K_PTR - 26
          
          IF (K_P0 == 0) THEN
            PRINT "(10i4)", K_TMP(K_T0:K_T0+(2))
          ENDIF
          K_PTR = K_PTR - 18
   20   CONTINUE
        I_SHAPE (0) = 2
        
        I_SHAPE (1) = 4
        
        I_ORDER (0) = 2
        
        I_ORDER (1) = 1
        
        K_T0 = K_PTR;   K_PTR = K_PTR + 20
        IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
        CALL AD_RESHAPE_I (K_TMP(K_T0), K_MB, I_V, K_MV, I_SHAPE, K_MSHA
     *PE, I_PAD, K_MPAD, .TRUE., I_ORDER, K_MSHAPE, .TRUE.)
        
        DO K_C1 = 0, 1
          K_V0 = (K_C1*2+K_PP(1))*10
          K_V1 = MAX(-(K_V0-14)/2,0)
          K_V2 = K_C1*5+(-K_V0-K_V1*2+22)/2
          K_V3 = K_C1*5+MAX(-K_V0+16,0)/2
          K_T0_1 = K_T0 + (K_V2*2)
          DO K_1 = K_V2, K_V3, -1
            K_T0_0 = K_T0_1
            DO K_0 = 0, 1
              I_B (K_0,K_1) = K_TMP (K_T0_0)
              K_T0_0 = K_T0_0 + (1)
            ENDDO
            K_T0_1 = K_T0_1 + (-2)
          ENDDO
        ENDDO
        
        K_PTR = K_PTR - 20
        IF (K_P0 == 0) THEN
          PRINT "(' b...')"
        ENDIF
        DO 30 I_I = 1, 2
          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 + 26
          IF (K_PTR .GT. 300000) CALL AD_STACK_ERR (300000)
          K_TMP (K_M1 : K_M1+8) = (/66, 0, K_HP, I_I-1, 0, -1, 4, 1, 1/)
          K_TMP (K_M1+9 : K_M1+24) = K_MB (9 : 24)
          
          CALL AD_REMAP_II (K_TMP(K_T0), K_TMP(K_M0), I_B, K_TMP(K_M1))
          K_PTR = K_PTR - 26
          
          IF (K_P0 == 0) THEN
            PRINT "(10i4)", K_TMP(K_T0:K_T0+(3))
          ENDIF
          K_PTR = K_PTR - 18
   30   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
