// File: /workspace/github_cpp_makefile_star2_O3/TooruAkagi[P]GCIS_Index/gcis_nep_build.cpp
// Function: main
int main(int argc, char *argv[])
{
    long long int sps = 0;
    long long int spsod = 0;
    FILE *fp;
    unsigned int *T,*D,*NS;
    int *P,*P_2,*I,*K,*D_2,*D_3;
    int i = 0;
    int pn = 0;
    int round = 0;
    
    long long int fsize = 0;
    for(int i = 0;i<argc;i++){
      if('-'==argv[i][0]){ //符号
        if('i'==argv[i][1]){ //input
            int k = strlen(argv[i+1]);
            fname = (char*)malloc(k);
            for(int k2=0;k2<k;k2++){fname[k2]=argv[i+1][k2];}
        }
        if('o'==argv[i][1]){ //input
            int k = strlen(argv[i+1]);
            wname = (char*)malloc(k);
            for(int k2=0;k2<k;k2++){wname[k2]=argv[i+1][k2];}
        }
        if('h'==argv[i][1]){shelp();return 0;} //help
        i++;continue;
      }
    }
    printf("\ninput :[%s]\n",fname);
    if(fname==NULL){shelp();return 0;}
    if(wname==NULL){
      int k = strlen(defaultwname); 
      wname = (char*)malloc(k);
      for(int k2=0;k2<k;k2++){wname[k2]=defaultwname[k2];}
    }
    fsize = std::filesystem::file_size(fname);
    char chr;
    clock_t timesb = clock();
    fp = fopen(fname, "r"); // open file or return null
    if(fp == NULL) {
        printf("[%s] such file doesn't exist!\n", fname);
        return 1;
    }
    printf("\nfile %s 's size = %lld byte Last = ",fname,fsize);
    T= (unsigned int*)malloc(fsize*4);
    for(i=0;i<fsize;i++) {
        chr = fgetc(fp);
        T[i] = chr;
        if(chr<0){T[i]=chr + 256;}
    }
    fclose(fp);
    dcs[0] = 0;dc2s[0] = 0;
    
    for(int rs = 0;rs<loops;rs++){
        P = (int*)malloc(fsize*4); //for consutructing GCIS-index
        P_2 = (int*)malloc(fsize*4); 
        I = (int*)malloc(fsize*4); 
        if(I==NULL){exit(1);}
        pn = purse(T,P,P_2,fsize);
        for(int i = 0; i < pn ; i++){I[i] = i;}//put default number in I[]
        pnsort(T,P,P_2,I,pn);
        if(rs==0){ 
            D = (unsigned int*)malloc(fsize*8); //can save space with realloc() 
            D_2 = (int*)malloc(pn*8); //can save space with realloc() 
            D_3 = (int*)malloc(pn*8); //can save space with realloc() 
        }
        K = (int*)malloc(pn*4); 
        NS = (unsigned int*)malloc(pn*4); 
        round++;
        dc2s[round] = grammarmake(T,P,P_2,I,NS,K,pn); 
        tempC = dc2s[round]+2;
        dc2s[round] += dc2s[round-1];
        dcs[round] = codeA;
        printf("\nC=%d\ndc2s[%d]=%d\nfsize=%lld\npn=%d",tempC,round,dc2s[round],fsize,pn);
        makedic(T,P,P_2,K,NS,D,D_2,D_3,round-1);
        sps = dc2s[round]*4*2+dcs[round]*4+pn*4;
        printf("space : %lld << %lld bytes + O(log m)",sps,spsod);
        free(I);free(T);free(P);free(P_2);free(K); 
            if(rs>OVERTRANSFORM && spsod < sps){loops=rs+1;break;} // 
            if(rs==TRANSTIMES){loops=rs+1;break;}
            if(rs<loops-1){
                fsize = pn;
                T = (unsigned int*)malloc(fsize*4);
                for(i=0;i<fsize;i++){T[i]=NS[i];} //NS to T (NS is new input string)
                free(NS);
                spsod = sps;
            }
    }
    if(DEBUGFLAG==1){for(int yj = 0 ;yj<pn;yj++){printf("<%d>",NS[yj]);}printf("\n");}
    write6(D,D_2,D_3,NS,pn);
    clock_t timee = clock();
    
    const double time = static_cast<double>(timee - timesb) / CLOCKS_PER_SEC ;
    printf("\n[make index :  time %lf[s]\n",time);
    printf("space : %d bytes (%d,%d,%d)",dc2s[loops]*4*2+dcs[loops]*4+pn*4,dc2s[loops],dcs[loops],pn);
    printf("loop = %d",loops);
    printf("\n[u/n] = %d",1+dcs[loops]/dc2s[loops]);
    printf(" / dcs[16]=%d / ",D_2[dc2s[loops]]);
    free(NS); 
    free(D); 
    free(D_2); 
    free(D_3); 
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
testl %edi, %edi
jle 0x2d6b
movq %rsi, %rbx
movl %edi, %ebp
xorl %r12d, %r12d
movslq %r12d, %r13
movq (%rbx,%r13,8), %rax
cmpb $0x2d, (%rax)
jne 0x2d5f
cmpb $0x69, 0x1(%rax)
jne 0x2cfd
movq 0x8(%rbx,%r13,8), %rdi
callq 0x2070
movq %rax, %r14
movslq %r14d, %r15
movq %r15, %rdi
callq 0x2160
movq %rax, 0x3483(%rip)      # 0x6158
testl %r15d, %r15d
jle 0x2cfd
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movq 0x8(%rbx,%r13,8), %rcx
movb (%rcx,%rax), %cl
movq 0x3466(%rip), %rdx      # 0x6158
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2ce3
movq (%rbx,%r13,8), %rax
cmpb $0x6f, 0x1(%rax)
jne 0x2d4e
movq 0x8(%rbx,%r13,8), %rdi
callq 0x2070
movq %rax, %r14
movslq %r14d, %r15
movq %r15, %rdi
callq 0x2160
movq %rax, 0x343a(%rip)      # 0x6160
testl %r15d, %r15d
jle 0x2d4e
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movq 0x8(%rbx,%r13,8), %rcx
movb (%rcx,%rax), %cl
movq 0x341d(%rip), %rdx      # 0x6160
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2d34
movq (%rbx,%r13,8), %rax
cmpb $0x68, 0x1(%rax)
je 0x31e4
incl %r12d
incl %r12d
cmpl %ebp, %r12d
jl 0x2ca0
movq 0x33e6(%rip), %rsi      # 0x6158
leaq 0x1383(%rip), %rdi      # 0x40fc
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2040
cmpq $0x0, 0x33ce(%rip)      # 0x6158
je 0x31da
cmpq $0x0, 0x33c8(%rip)      # 0x6160
jne 0x2de1
leaq 0x337f(%rip), %rbx      # 0x6120
movq %rbx, %rdi
callq 0x2070
movq %rax, %r14
movslq %r14d, %r15
movq %r15, %rdi
callq 0x2160
movq %rax, 0x33a2(%rip)      # 0x6160
testl %r15d, %r15d
jle 0x2de1
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movb (%rax,%rbx), %cl
movq 0x338a(%rip), %rdx      # 0x6160
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2dcc
leaq 0x3370(%rip), %rsi      # 0x6158
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl $0x2, %edx
callq 0x3354
movq %rbx, %rdi
callq 0x2170
movq %rax, %rbp
leaq 0x80(%rsp), %rdi
callq 0x33dc
callq 0x2090
movq %rax, %r15
movq 0x3334(%rip), %rdi      # 0x6158
leaq 0x12df(%rip), %rsi      # 0x410a
callq 0x2130
movq 0x3321(%rip), %rsi      # 0x6158
testq %rax, %rax
je 0x31f0
movq %rax, %rbx
leaq 0x12e1(%rip), %rdi      # 0x412b
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2040
leaq (,%rbp,4), %rdi
callq 0x2160
movq %rax, %r12
testq %rbp, %rbp
jle 0x2e83
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x21b0
movzbl %al, %eax
movl %eax, (%r12,%r14,4)
incq %r14
cmpq %r14, %rbp
jne 0x2e6c
movq %r15, 0x50(%rsp)
movq %rbx, %rdi
callq 0x2100
xorl %r15d, %r15d
movl %r15d, 0x32e6(%rip)     # 0x6180
movl %r15d, 0x332f(%rip)     # 0x61d0
cmpl %r15d, 0x3284(%rip)     # 0x612c
jle 0x3208
movq %r12, 0x8(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
movq %rcx, 0x78(%rsp)
movq %rdx, %r13
leaq (,%rbp,4), %rbx
movq %rbx, %rdi
callq 0x2160
movq %rax, %r14
movq %rbx, %rdi
callq 0x2160
movq %rax, %r15
movq %rbx, %rdi
callq 0x2160
testq %rax, %rax
je 0x3331
movq %rax, %r12
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r15, 0x28(%rsp)
movq %r15, %rdx
movq %rbp, 0x10(%rsp)
movq %rbp, %rcx
callq 0x266a
movl %eax, %r15d
testl %eax, %eax
movdqa 0x1105(%rip), %xmm5     # 0x4020
pcmpeqd %xmm6, %xmm6
movdqa 0x1109(%rip), %xmm7     # 0x4030
movq %r13, %rsi
movq 0x78(%rsp), %r13
jle 0x2fae
movl %r15d, %ecx
leal 0x1(%r15), %eax
andl $-0x2, %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm5, %xmm0
xorl %ecx, %ecx
movdqa 0x10ba(%rip), %xmm1     # 0x4010
movdqa %xmm1, %xmm2
pxor %xmm5, %xmm2
movdqa %xmm2, %xmm3
pcmpgtd %xmm0, %xmm3
pcmpeqd %xmm0, %xmm2
pshufd $0xf5, %xmm2, %xmm4     # xmm4 = xmm2[1,1,3,3]
pand %xmm3, %xmm4
pshufd $0xf5, %xmm3, %xmm2     # xmm2 = xmm3[1,1,3,3]
por %xmm4, %xmm2
movd %xmm2, %edx
notl %edx
testb $0x1, %dl
je 0x2f8b
movl %ecx, (%r12,%rcx,4)
pxor %xmm6, %xmm2
pextrw $0x4, %xmm2, %edx
testb $0x1, %dl
je 0x2fa1
leal 0x1(%rcx), %edx
movl %edx, 0x4(%r12,%rcx,4)
addq $0x2, %rcx
paddq %xmm7, %xmm1
cmpq %rcx, %rax
jne 0x2f56
movq %rsi, 0x70(%rsp)
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rcx
movl %r15d, %r8d
callq 0x27d6
testl %r13d, %r13d
movq 0x10(%rsp), %rbp
jne 0x300c
leaq (,%rbp,8), %rdi
callq 0x2160
movq %rax, 0x38(%rsp)
leal (,%r15,8), %eax
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x2160
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
callq 0x2160
movq %rax, 0x18(%rsp)
leal (,%r15,4), %eax
movslq %eax, %rbp
movq %rbp, 0x68(%rsp)
movq %rbp, %rdi
callq 0x2160
movq %rax, %rbx
movq %rax, 0x48(%rsp)
movq %rbp, %rdi
callq 0x2160
leaq 0x1(%r13), %rbp
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
movq %rax, 0x48(%rsp)
movq %rax, %r8
movq %rbx, %r9
pushq %r15
callq 0x2a01
addq $0x10, %rsp
leal 0x2(%rax), %esi
movq %r14, 0x60(%rsp)
leaq 0x315f(%rip), %rbx      # 0x61d0
addl (%rbx,%r13,4), %eax
movl %esi, 0x30ad(%rip)      # 0x6128
movl %eax, 0x4(%rbx,%r13,4)
movl 0x30ee(%rip), %ecx      # 0x6174
leaq 0x30f3(%rip), %rdx      # 0x6180
movl %ecx, 0x4(%rdx,%r13,4)
leaq 0x10b7(%rip), %rdi      # 0x4150
movq %rbp, 0x58(%rsp)
movl %ebp, %edx
movl %eax, %ecx
movq 0x10(%rsp), %r8
movq %r15, 0x20(%rsp)
movl %r15d, %r9d
xorl %eax, %eax
callq 0x2040
subq $0x8, %rsp
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq 0x30(%rsp), %r14
movq %r14, %rdx
movq 0x50(%rsp), %rcx
movq 0x40(%rsp), %r9
pushq %r13
pushq 0x28(%rsp)
pushq 0x48(%rsp)
callq 0x2890
addq $0x20, %rsp
movl 0x4(%rbx,%r13,4), %eax
leaq 0x308a(%rip), %rcx      # 0x6180
movl 0x4(%rcx,%r13,4), %ecx
movq 0x68(%rsp), %rdx
leal (%rdx,%rax,8), %eax
leal (%rax,%rcx,4), %eax
movslq %eax, %rbx
leaq 0x1063(%rip), %rdi      # 0x4173
movq %rbx, %rsi
movq 0x70(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x2040
movq %r12, %rdi
callq 0x2140
movq %rbp, %rdi
callq 0x2140
movq 0x60(%rsp), %rdi
callq 0x2140
movq %r14, %rdi
callq 0x2140
movq 0x48(%rsp), %rdi
callq 0x2140
movq %r15, %rdx
testl %r13d, %r13d
je 0x315f
cmpq %rbx, %rdx
jl 0x320a
cmpq $0x63, %r13
je 0x3210
movq %rbx, %rbp
movl 0x2fba(%rip), %ebx      # 0x612c
leal -0x1(%rbx), %eax
cmpl %r13d, %eax
movq 0x20(%rsp), %r15
jle 0x31c3
movslq %r15d, %rax
movq %rax, 0x10(%rsp)
leaq (,%rax,4), %rdi
callq 0x2160
movq %rax, %r14
testl %r15d, %r15d
movq 0x40(%rsp), %r12
jle 0x31b3
movl %r15d, %edx
shlq $0x2, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x20d0
movq %r14, 0x8(%rsp)
movq %r12, %rdi
callq 0x2140
movq %rbp, %rdx
movq 0x58(%rsp), %rax
movq %rax, %rcx
cmpl %eax, %ebx
movq 0x10(%rsp), %rbp
jg 0x2eb7
jmp 0x3220
callq 0x2c2a
jmp 0x331d
callq 0x2c2a
xorl %ebx, %ebx
jmp 0x331d
leaq 0xf15(%rip), %rdi       # 0x410c
xorl %eax, %eax
callq 0x2040
movl $0x1, %ebx
jmp 0x331d
jmp 0x3220
leal 0x1(%r13), %eax
jmp 0x3215
movl $0x64, %eax
movq 0x20(%rsp), %r15
movl %eax, 0x2f0c(%rip)      # 0x612c
movq 0x38(%rsp), %r12
movq %r12, %rdi
movq 0x30(%rsp), %r13
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x40(%rsp), %rbp
movq %rbp, %rcx
movl %r15d, %r8d
callq 0x2af7
callq 0x2090
subq 0x50(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0xde4(%rip), %xmm0      # 0x4040
leaq 0xf36(%rip), %rdi       # 0x4199
movb $0x1, %al
callq 0x2040
movslq 0x2ebb(%rip), %rax      # 0x612c
leaq 0x2f58(%rip), %r14      # 0x61d0
movl (%r14,%rax,4), %edx
leal (,%rdx,8), %esi
movq %r15, %r8
leaq 0x2ef3(%rip), %r15      # 0x6180
movl (%r15,%rax,4), %ecx
leal (%rcx,%r8), %eax
leal (%rsi,%rax,4), %esi
leaq 0xf17(%rip), %rdi       # 0x41b6
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2040
movl 0x2e7e(%rip), %esi      # 0x612c
leaq 0xf1d(%rip), %rdi       # 0x41d2
xorl %eax, %eax
callq 0x2040
movslq 0x2e69(%rip), %rcx      # 0x612c
movl (%r15,%rcx,4), %eax
cltd
idivl (%r14,%rcx,4)
leal 0x1(%rax), %esi
leaq 0xf06(%rip), %rdi       # 0x41dc
xorl %eax, %eax
callq 0x2040
movslq 0x2e48(%rip), %rax      # 0x612c
movslq (%r14,%rax,4), %rax
movl (%r13,%rax,4), %esi
leaq 0xef4(%rip), %rdi       # 0x41e8
xorl %eax, %eax
callq 0x2040
movq %rbp, %rdi
callq 0x2140
movq %r12, %rdi
callq 0x2140
movq %r13, %rdi
callq 0x2140
movq 0x18(%rsp), %rdi
callq 0x2140
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x2150
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x33dc
movq %rbx, %rdi
callq 0x21d0
nop