// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rsi, %rbx
movl %edi, %ebp
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpl %ebp, %r12d
jge 0x1cc4
movslq %r12d, %r13
movq (%rbx,%r13,8), %rax
cmpb $0x2d, (%rax)
jne 0x1cbc
movb 0x1(%rax), %al
cmpb $0x69, %al
jne 0x1c65
movq 0x8(%rbx,%r13,8), %rdi
callq 0x1060
movslq %eax, %r14
movq %r14, %rdi
callq 0x1140
movq %rax, 0x350f(%rip)      # 0x5148
testl %r14d, %r14d
cmovlel %r15d, %r14d
xorl %eax, %eax
cmpq %rax, %r14
je 0x1c5e
movq 0x8(%rbx,%r13,8), %rcx
movb (%rcx,%rax), %cl
movq 0x34f2(%rip), %rdx      # 0x5148
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1c42
movq (%rbx,%r13,8), %rax
movb 0x1(%rax), %al
cmpb $0x6f, %al
jne 0x1cb1
movq 0x8(%rbx,%r13,8), %rdi
callq 0x1060
movslq %eax, %r14
movq %r14, %rdi
callq 0x1140
movq %rax, 0x34cb(%rip)      # 0x5150
testl %r14d, %r14d
cmovlel %r15d, %r14d
xorl %eax, %eax
cmpq %rax, %r14
je 0x1caa
movq 0x8(%rbx,%r13,8), %rcx
movb (%rcx,%rax), %cl
movq 0x34ae(%rip), %rdx      # 0x5150
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1c8e
movq (%rbx,%r13,8), %rax
movb 0x1(%rax), %al
cmpb $0x68, %al
je 0x1daa
incl %r12d
incl %r12d
jmp 0x1bfd
movq 0x347d(%rip), %rsi      # 0x5148
leaq 0x13f2(%rip), %rdi      # 0x30c4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1030
cmpq $0x0, 0x3465(%rip)      # 0x5148
je 0x2096
cmpq $0x0, 0x345f(%rip)      # 0x5150
je 0x20a0
leaq 0x344a(%rip), %rsi      # 0x5148
leaq 0x80(%rsp), %rbx
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x2242
movq %rbx, %rdi
callq 0x1150
movq %rax, 0x10(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x22ae
callq 0x10a0
movq %rax, %rbx
movq 0x340e(%rip), %rdi      # 0x5148
leaq 0x1391(%rip), %rsi      # 0x30d2
callq 0x1110
movq 0x33fb(%rip), %rsi      # 0x5148
testq %rax, %rax
je 0x20e5
movq %rax, %r14
movq %rbx, 0x30(%rsp)
leaq 0x138e(%rip), %rdi      # 0x30f3
xorl %ebx, %ebx
movq 0x10(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x1030
leaq (,%r15,4), %rdi
callq 0x1140
movq %rax, (%rsp)
testq %r15, %r15
cmovleq %rbx, %r15
cmpq %rbx, %r15
je 0x1db6
movq %r14, %rdi
callq 0x1190
movzbl %al, %eax
movq (%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
incq %rbx
jmp 0x1d8e
callq 0x1b8f
xorl %ebx, %ebx
jmp 0x220d
movq %r14, %rdi
callq 0x1100
andl $0x0, 0x33ab(%rip)      # 0x5170
andl $0x0, 0x33f4(%rip)      # 0x51c0
movl 0x3349(%rip), %r15d     # 0x511c
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
xorl %edx, %edx
movq 0x10(%rsp), %rax
leaq (,%rax,4), %rcx
movq %rcx, 0x70(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x60(%rsp)
movq %r12, %rbx
movq %rdx, 0x68(%rsp)
cmpl %ebx, %r15d
jle 0x2109
movq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x1140
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x1140
movq %rax, %r12
movq %r14, %rdi
callq 0x1140
testq %rax, %rax
je 0x2221
movq %rax, %r15
movq (%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r12, 0x18(%rsp)
movq %r12, %rdx
movq 0x10(%rsp), %rcx
callq 0x1634
movl %eax, %r12d
testl %eax, %eax
movl $0x0, %eax
cmovgl %r12d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1e7b
movl %ecx, (%r15,%rcx,4)
incq %rcx
jmp 0x1e6d
movq (%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
movl %r12d, %r8d
callq 0x1793
testl %ebx, %ebx
movq %r12, 0x8(%rsp)
jne 0x1ed6
movq 0x60(%rsp), %rdi
callq 0x1140
movq %rax, 0x48(%rsp)
leal (,%r12,8), %eax
movslq %eax, %r14
movq %r14, %rdi
callq 0x1140
movq %rax, 0x40(%rsp)
movq %r14, %rdi
movq 0x8(%rsp), %r12
callq 0x1140
movq %rax, 0x28(%rsp)
leal (,%r12,4), %eax
movslq %eax, %r14
movq %r14, 0x78(%rsp)
movq %r14, %rdi
callq 0x1140
movq %rax, %r13
movq %rax, 0x58(%rsp)
movq %r14, %rdi
callq 0x1140
leaq 0x1(%rbx), %rcx
movq %rcx, 0x50(%rsp)
subq $0x8, %rsp
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
movq 0x20(%rsp), %rdx
movq %r15, %rcx
movq %rax, 0x40(%rsp)
movq %rax, %r8
movq %r13, %r9
pushq %r12
callq 0x198a
popq %rcx
popq %rdx
leal 0x2(%rax), %esi
movq %r14, %r13
movq %rbp, %r14
leaq 0x3279(%rip), %rbp      # 0x51c0
addl (%rbp,%rbx,4), %eax
movl %esi, 0x31c7(%rip)      # 0x5118
movl %eax, 0x4(%rbp,%rbx,4)
movl 0x3209(%rip), %ecx      # 0x5164
leaq 0x320e(%rip), %rdx      # 0x5170
movl %ecx, 0x4(%rdx,%rbx,4)
leaq 0x11ab(%rip), %rdi      # 0x3118
movq 0x50(%rsp), %rdx
movl %eax, %ecx
movq 0x10(%rsp), %r8
movl %r12d, %r9d
xorl %eax, %eax
callq 0x1030
subq $0x8, %rsp
movq %r14, %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x50(%rsp), %r9
pushq %rbx
pushq 0x38(%rsp)
pushq 0x58(%rsp)
callq 0x1843
addq $0x20, %rsp
movl 0x4(%rbp,%rbx,4), %eax
leaq 0x31b7(%rip), %rcx      # 0x5170
movl 0x4(%rcx,%rbx,4), %ecx
movq 0x78(%rsp), %rdx
leal (%rdx,%rax,8), %eax
leal (%rax,%rcx,4), %eax
movslq %eax, %rbp
leaq 0x1169(%rip), %rdi      # 0x313b
movq %rbp, %rsi
movq 0x68(%rsp), %r12
movq %r12, %rdx
xorl %eax, %eax
callq 0x1030
movq %r15, %rdi
callq 0x1120
movq %r14, %rdi
callq 0x1120
movq 0x20(%rsp), %rdi
callq 0x1120
movq 0x18(%rsp), %rdi
callq 0x1120
movq 0x58(%rsp), %rdi
callq 0x1120
testl %ebx, %ebx
je 0x201f
cmpq %rbp, %r12
jl 0x20fb
cmpq $0x63, %rbx
je 0x2100
movl 0x30ec(%rip), %r15d     # 0x511c
leal -0x1(%r15), %eax
cmpl %ebx, %eax
movq 0x50(%rsp), %r12
movq %r12, %rbx
jle 0x1e08
movq 0x8(%rsp), %rbx
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
leaq (,%rax,4), %rdi
callq 0x1140
movq %rax, (%rsp)
testl %ebx, %ebx
movl %ebx, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
movq 0x38(%rsp), %rdi
cmpq %rcx, %rax
je 0x2089
movl (%rdi,%rcx,4), %edx
movq (%rsp), %rsi
movl %edx, (%rsi,%rcx,4)
incq %rcx
jmp 0x2075
callq 0x1120
movq %rbp, %rdx
jmp 0x1de1
callq 0x1b8f
jmp 0x220d
leaq 0x3069(%rip), %rbx      # 0x5110
movq %rbx, %rdi
callq 0x1060
movslq %eax, %r14
movq %r14, %rdi
callq 0x1140
movq %rax, 0x308f(%rip)      # 0x5150
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
cmpq %rax, %r14
je 0x1cf7
movb (%rax,%rbx), %cl
movq 0x3073(%rip), %rdx      # 0x5150
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x20ca
leaq 0xfe8(%rip), %rdi       # 0x30d4
xorl %eax, %eax
callq 0x1030
pushq $0x1
popq %rbx
jmp 0x220d
leal 0x1(%rbx), %eax
jmp 0x2103
pushq $0x64
popq %rax
movl %eax, 0x3013(%rip)      # 0x511c
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r14
movq 0x48(%rsp), %r15
movq %r15, %rdi
movq 0x40(%rsp), %r12
movq %r12, %rsi
movq 0x28(%rsp), %rdx
movq 0x38(%rsp), %r13
movq %r13, %rcx
movl %r14d, %r8d
callq 0x1a66
callq 0x10a0
subq %rbx, %rax
cvtsi2sd %rax, %xmm0
divsd 0xebb(%rip), %xmm0      # 0x3008
leaq 0x100d(%rip), %rdi      # 0x3161
movb $0x1, %al
callq 0x1030
movslq 0x2fba(%rip), %rax      # 0x511c
movq %r14, %r8
leaq 0x3054(%rip), %r14      # 0x51c0
movl (%r14,%rax,4), %edx
leal (,%rdx,8), %esi
leaq 0x2ff2(%rip), %rbp      # 0x5170
movl (%rbp,%rax,4), %ecx
leal (%rcx,%r8), %eax
leal (%rsi,%rax,4), %esi
leaq 0xfee(%rip), %rdi       # 0x317e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1030
movl 0x2f7d(%rip), %esi      # 0x511c
leaq 0xff4(%rip), %rdi       # 0x319a
xorl %eax, %eax
callq 0x1030
movslq 0x2f68(%rip), %rcx      # 0x511c
movl (%rbp,%rcx,4), %eax
cltd
idivl (%r14,%rcx,4)
leal 0x1(%rax), %esi
leaq 0xfdd(%rip), %rdi       # 0x31a4
xorl %eax, %eax
callq 0x1030
movslq 0x2f47(%rip), %rax      # 0x511c
movslq (%r14,%rax,4), %rax
movl (%r12,%rax,4), %esi
leaq 0xfcc(%rip), %rdi       # 0x31b0
xorl %eax, %eax
callq 0x1030
movq %r13, %rdi
callq 0x1120
movq %r15, %rdi
callq 0x1120
movq %r12, %rdi
callq 0x1120
movq 0x28(%rsp), %rdi
callq 0x1120
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x1130
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x22ae
movq %rbx, %rdi
callq 0x11b0
nop