// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
testl %edi, %edi
jle 0x2d82
movq %rsi, %rbx
movl %edi, %ebp
xorl %r12d, %r12d
movslq %r12d, %r13
movq (%rbx,%r13,8), %rax
cmpb $0x2d, (%rax)
jne 0x2d76
cmpb $0x69, 0x1(%rax)
jne 0x2d14
movq 0x8(%rbx,%r13,8), %rdi
callq 0x2070
movq %rax, %r14
movslq %r14d, %r15
movq %r15, %rdi
callq 0x2160
movq %rax, 0x346c(%rip)      # 0x6158
testl %r15d, %r15d
jle 0x2d14
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movq 0x8(%rbx,%r13,8), %rcx
movb (%rcx,%rax), %cl
movq 0x344f(%rip), %rdx      # 0x6158
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2cfa
movq (%rbx,%r13,8), %rax
cmpb $0x6f, 0x1(%rax)
jne 0x2d65
movq 0x8(%rbx,%r13,8), %rdi
callq 0x2070
movq %rax, %r14
movslq %r14d, %r15
movq %r15, %rdi
callq 0x2160
movq %rax, 0x3423(%rip)      # 0x6160
testl %r15d, %r15d
jle 0x2d65
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movq 0x8(%rbx,%r13,8), %rcx
movb (%rcx,%rax), %cl
movq 0x3406(%rip), %rdx      # 0x6160
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2d4b
movq (%rbx,%r13,8), %rax
cmpb $0x68, 0x1(%rax)
je 0x316e
incl %r12d
incl %r12d
cmpl %ebp, %r12d
jl 0x2cb7
movq 0x33cf(%rip), %rsi      # 0x6158
leaq 0x1334(%rip), %rdi      # 0x40c4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2040
cmpq $0x0, 0x33b7(%rip)      # 0x6158
je 0x3164
cmpq $0x0, 0x33b1(%rip)      # 0x6160
jne 0x2df8
leaq 0x3368(%rip), %rbx      # 0x6120
movq %rbx, %rdi
callq 0x2070
movq %rax, %r14
movslq %r14d, %r15
movq %r15, %rdi
callq 0x2160
movq %rax, 0x338b(%rip)      # 0x6160
testl %r15d, %r15d
jle 0x2df8
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movb (%rax,%rbx), %cl
movq 0x3373(%rip), %rdx      # 0x6160
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2de3
leaq 0x3359(%rip), %rsi      # 0x6158
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl $0x2, %edx
callq 0x32e2
movq %rbx, %rdi
callq 0x2170
movq %rax, %rbp
leaq 0x70(%rsp), %rdi
callq 0x336a
callq 0x2090
movq %rax, %rbx
movq 0x3323(%rip), %rdi      # 0x6158
leaq 0x1296(%rip), %rsi      # 0x40d2
callq 0x2130
movq 0x3310(%rip), %rsi      # 0x6158
testq %rax, %rax
je 0x317a
movq %rax, %r14
movq %rbx, 0x50(%rsp)
leaq 0x1293(%rip), %rdi      # 0x40f3
xorl %r15d, %r15d
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2040
leaq (,%rbp,4), %rdi
callq 0x2160
movq %rax, (%rsp)
testq %rbp, %rbp
jle 0x2e9f
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x21b0
movzbl %al, %eax
movq (%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
incq %rbx
cmpq %rbx, %rbp
jne 0x2e85
movq %r14, %rdi
callq 0x2100
movl %r15d, 0x32d2(%rip)     # 0x6180
movl %r15d, 0x331b(%rip)     # 0x61d0
cmpl %r15d, 0x3270(%rip)     # 0x612c
jle 0x3192
xorl %ebx, %ebx
xorl %r12d, %r12d
leaq (,%rbp,4), %r14
movq %r14, %rdi
callq 0x2160
movq %rax, %r15
movq %r14, %rdi
callq 0x2160
movq %rax, %r13
movq %r14, %rdi
callq 0x2160
testq %rax, %rax
je 0x32c2
movq %rax, %r14
movq (%rsp), %rdi
movq %r15, 0x28(%rsp)
movq %r15, %rsi
movq %r13, 0x20(%rsp)
movq %r13, %rdx
movq %rbp, 0x10(%rsp)
movq %rbp, %rcx
callq 0x2668
movl %eax, %r15d
testl %eax, %eax
jle 0x2f32
movl %r15d, %eax
xorl %ecx, %ecx
movl %ecx, (%r14,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x2f26
movq %r12, 0x68(%rsp)
movq (%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
movl %r15d, %r8d
callq 0x27e7
testl %ebx, %ebx
movq %rbx, %rbp
jne 0x2f9b
movq 0x10(%rsp), %rax
leaq (,%rax,8), %rdi
callq 0x2160
movq %rax, 0x40(%rsp)
leal (,%r15,8), %eax
movslq %eax, %r13
movq %r13, %rdi
callq 0x2160
movq %rax, 0x38(%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %r12
callq 0x2160
movq %rax, 0x30(%rsp)
leal (,%r15,4), %eax
movslq %eax, %r13
movq %r13, 0x60(%rsp)
movq %r13, %rdi
callq 0x2160
movq %rax, %rbx
movq %rax, 0x58(%rsp)
movq %r13, %rdi
callq 0x2160
leaq 0x1(%rbp), %rcx
movq %rcx, 0x48(%rsp)
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, 0x10(%rsp)
movq %rax, %r8
movq %rbx, %r9
pushq %r15
callq 0x2a14
addq $0x10, %rsp
leaq 0x31d3(%rip), %r13      # 0x61d0
movl %eax, 0x4(%r13,%rbp,4)
leal 0x2(%rax), %esi
addl (%r13,%rbp,4), %eax
movl %esi, 0x3118(%rip)      # 0x6128
movl %eax, 0x4(%r13,%rbp,4)
movl 0x3159(%rip), %ecx      # 0x6174
leaq 0x315e(%rip), %rdx      # 0x6180
movl %ecx, 0x4(%rdx,%rbp,4)
leaq 0x10eb(%rip), %rdi      # 0x4118
movq 0x48(%rsp), %rdx
movl %eax, %ecx
movq 0x10(%rsp), %r8
movq %r15, 0x18(%rsp)
movl %r15d, %r9d
xorl %eax, %eax
callq 0x2040
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
movq 0x60(%rsp), %r12
movq %r12, %rcx
movq 0x48(%rsp), %r9
pushq %rbp
pushq 0x40(%rsp)
pushq 0x50(%rsp)
callq 0x28a1
addq $0x20, %rsp
movl 0x4(%r13,%rbp,4), %eax
leaq 0x30f9(%rip), %rcx      # 0x6180
movl 0x4(%rcx,%rbp,4), %ecx
movq 0x60(%rsp), %rdx
leal (%rdx,%rax,8), %eax
leal (%rax,%rcx,4), %eax
movslq %eax, %r13
leaq 0x109b(%rip), %rdi      # 0x413b
movq %r13, %rsi
movq 0x68(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x2140
movq (%rsp), %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x2140
movq 0x20(%rsp), %rdi
callq 0x2140
movq %r12, %rdi
callq 0x2140
movq %r15, %r12
testl %ebp, %ebp
je 0x30ed
cmpq %r13, %r12
jl 0x3194
cmpq $0x63, %rbp
je 0x3199
movl 0x302f(%rip), %eax      # 0x612c
decl %eax
cmpl %ebp, %eax
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %r14
jle 0x3149
movslq %r15d, %rax
movq %rax, 0x10(%rsp)
leaq (,%rax,4), %rdi
callq 0x2160
movq %rax, (%rsp)
testl %r15d, %r15d
jle 0x313e
movl %r15d, %edx
shlq $0x2, %rdx
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x20d0
movq %r14, %rdi
callq 0x2140
movq %r13, %r12
movq 0x48(%rsp), %rax
movq %rax, %rbx
cmpl %eax, 0x2fd5(%rip)      # 0x612c
movq 0x10(%rsp), %rbp
jg 0x2ec7
jmp 0x31ae
callq 0x2c41
jmp 0x32ae
callq 0x2c41
xorl %ebx, %ebx
jmp 0x32ae
leaq 0xf53(%rip), %rdi       # 0x40d4
xorl %eax, %eax
callq 0x2040
movl $0x1, %ebx
jmp 0x32ae
jmp 0x31ae
leal 0x1(%rbp), %eax
jmp 0x319e
movl $0x64, %eax
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %r14
movl %eax, 0x2f7e(%rip)      # 0x612c
movq %r14, 0x8(%rsp)
movq 0x40(%rsp), %r12
movq %r12, %rdi
movq 0x38(%rsp), %r13
movq %r13, %rsi
movq 0x30(%rsp), %rbp
movq %rbp, %rdx
movq %r14, %rcx
movl %r15d, %r8d
callq 0x2b0e
callq 0x2090
subq 0x50(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0xe1b(%rip), %xmm0      # 0x4008
leaq 0xf6d(%rip), %rdi       # 0x4161
movb $0x1, %al
callq 0x2040
movslq 0x2f2a(%rip), %rax      # 0x612c
movq %r15, %r8
leaq 0x2fc4(%rip), %r15      # 0x61d0
movl (%r15,%rax,4), %edx
leal (,%rdx,8), %esi
leaq 0x2f62(%rip), %r14      # 0x6180
movl (%r14,%rax,4), %ecx
leal (%rcx,%r8), %eax
leal (%rsi,%rax,4), %esi
leaq 0xf4e(%rip), %rdi       # 0x417e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2040
movl 0x2eed(%rip), %esi      # 0x612c
leaq 0xf54(%rip), %rdi       # 0x419a
xorl %eax, %eax
callq 0x2040
movslq 0x2ed8(%rip), %rcx      # 0x612c
movl (%r14,%rcx,4), %eax
cltd
idivl (%r15,%rcx,4)
leal 0x1(%rax), %esi
leaq 0xf3d(%rip), %rdi       # 0x41a4
xorl %eax, %eax
callq 0x2040
movslq 0x2eb7(%rip), %rax      # 0x612c
movslq (%r15,%rax,4), %rax
movl (%r13,%rax,4), %esi
leaq 0xf2b(%rip), %rdi       # 0x41b0
xorl %eax, %eax
callq 0x2040
movq 0x8(%rsp), %rdi
callq 0x2140
movq %r12, %rdi
callq 0x2140
movq %r13, %rdi
callq 0x2140
movq %rbp, %rdi
callq 0x2140
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x2150
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x336a
movq %rbx, %rdi
callq 0x21d0
nop