// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movq $0x0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x33bd
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %ecx
movl $0x2d, %eax
cmpl %ecx, %eax
jne 0x33a7
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %ecx
movl $0x69, %eax
cmpl %ecx, %eax
jne 0x32bd
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x20a0
movl %eax, -0x90(%rbp)
movslq -0x90(%rbp), %rdi
callq 0x2180
movq %rax, 0x3f05(%rip)      # 0x7168
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x90(%rbp), %eax
jge 0x32bb
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x94(%rbp), %rcx
movb (%rax,%rcx), %dl
movq 0x3ec8(%rip), %rax      # 0x7168
movslq -0x94(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x326d
jmp 0x32bd
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %ecx
movl $0x6f, %eax
cmpl %ecx, %eax
jne 0x3369
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x20a0
movl %eax, -0x98(%rbp)
movslq -0x98(%rbp), %rdi
callq 0x2180
movq %rax, 0x3e61(%rip)      # 0x7170
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x98(%rbp), %eax
jge 0x3367
movq -0x10(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x9c(%rbp), %rcx
movb (%rax,%rcx), %dl
movq 0x3e24(%rip), %rax      # 0x7170
movslq -0x9c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x3319
jmp 0x3369
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %ecx
movl $0x68, %eax
cmpl %ecx, %eax
jne 0x3396
callq 0x3120
movl $0x0, -0x4(%rbp)
jmp 0x3abd
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x33a9
jmp 0x33a9
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x31e3
movq 0x3da4(%rip), %rsi      # 0x7168
leaq 0x1dc2(%rip), %rdi      # 0x518d
movb $0x0, %al
callq 0x2030
cmpq $0x0, 0x3d8e(%rip)      # 0x7168
jne 0x33ed
callq 0x3120
movl $0x0, -0x4(%rbp)
jmp 0x3abd
cmpq $0x0, 0x3d7b(%rip)      # 0x7170
jne 0x3469
leaq 0x3d32(%rip), %rdi      # 0x7130
callq 0x20a0
movl %eax, -0xa0(%rbp)
movslq -0xa0(%rbp), %rdi
callq 0x2180
movq %rax, 0x3d54(%rip)      # 0x7170
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0xa0(%rbp), %eax
jge 0x3467
movslq -0xa4(%rbp), %rcx
leaq 0x3cee(%rip), %rax      # 0x7130
movb (%rax,%rcx), %dl
movq 0x3d24(%rip), %rax      # 0x7170
movslq -0xa4(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x3426
jmp 0x3469
leaq 0x3cf8(%rip), %rsi      # 0x7168
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x110(%rbp)
movl $0x2, %edx
callq 0x3b10
movq -0x110(%rbp), %rdi
callq 0x2190
movq %rax, -0x108(%rbp)
jmp 0x349d
movq -0x108(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3bf0
callq 0x20e0
movq %rax, -0xe8(%rbp)
movq 0x3c9e(%rip), %rdi      # 0x7168
leaq 0x1cca(%rip), %rsi      # 0x519b
callq 0x2150
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x3525
movq 0x3c80(%rip), %rsi      # 0x7168
leaq 0x1cae(%rip), %rdi      # 0x519d
movb $0x0, %al
callq 0x2030
movl $0x1, -0x4(%rbp)
jmp 0x3abd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3bf0
jmp 0x3ac9
movq 0x3c3c(%rip), %rsi      # 0x7168
movq -0x88(%rbp), %rdx
leaq 0x1c82(%rip), %rdi      # 0x51bc
movb $0x0, %al
callq 0x2030
movq -0x88(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2180
movq %rax, -0x30(%rbp)
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x35bb
movq -0x28(%rbp), %rdi
callq 0x21c0
movb %al, -0xdd(%rbp)
movsbl -0xdd(%rbp), %edx
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movsbl -0xdd(%rbp), %eax
cmpl $0x0, %eax
jge 0x35ae
movsbl -0xdd(%rbp), %edx
addl $0x100, %edx            # imm = 0x100
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x35b0
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x355c
movq -0x28(%rbp), %rdi
callq 0x2120
movl $0x0, 0x3bc2(%rip)      # 0x7190
movl $0x0, 0x3c08(%rip)      # 0x71e0
movl $0x0, -0xec(%rbp)
movl -0xec(%rbp), %eax
cmpl 0x3b4e(%rip), %eax      # 0x713c
jge 0x3957
movq -0x88(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2180
movq %rax, -0x48(%rbp)
movq -0x88(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2180
movq %rax, -0x50(%rbp)
movq -0x88(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2180
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x3641
movl $0x1, %edi
callq 0x2170
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x2900
movl %eax, -0x78(%rbp)
movl $0x0, -0xf0(%rbp)
movl -0xf0(%rbp), %eax
cmpl -0x78(%rbp), %eax
jge 0x3696
movl -0xf0(%rbp), %edx
movq -0x58(%rbp), %rax
movslq -0xf0(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0x3666
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movl -0x78(%rbp), %r8d
callq 0x2b10
cmpl $0x0, -0xec(%rbp)
jne 0x36f0
movq -0x88(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2180
movq %rax, -0x38(%rbp)
movl -0x78(%rbp), %eax
shll $0x3, %eax
movslq %eax, %rdi
callq 0x2180
movq %rax, -0x68(%rbp)
movl -0x78(%rbp), %eax
shll $0x3, %eax
movslq %eax, %rdi
callq 0x2180
movq %rax, -0x70(%rbp)
movl -0x78(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x2180
movq %rax, -0x60(%rbp)
movl -0x78(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x2180
movq %rax, -0x40(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x60(%rbp), %r9
movl -0x78(%rbp), %eax
movl %eax, (%rsp)
callq 0x2ea0
movl %eax, %edx
movslq -0x7c(%rbp), %rcx
leaq 0x3a93(%rip), %rax      # 0x71e0
movl %edx, (%rax,%rcx,4)
movslq -0x7c(%rbp), %rcx
leaq 0x3a85(%rip), %rax      # 0x71e0
movl (%rax,%rcx,4), %eax
addl $0x2, %eax
movl %eax, 0x39d1(%rip)      # 0x7138
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x3a69(%rip), %rax      # 0x71e0
movl (%rax,%rcx,4), %edx
movslq -0x7c(%rbp), %rcx
leaq 0x3a5b(%rip), %rax      # 0x71e0
addl (%rax,%rcx,4), %edx
leaq 0x3a51(%rip), %rax      # 0x71e0
movl %edx, (%rax,%rcx,4)
movl 0x39ec(%rip), %edx      # 0x7184
movslq -0x7c(%rbp), %rcx
leaq 0x39ed(%rip), %rax      # 0x7190
movl %edx, (%rax,%rcx,4)
movl 0x398c(%rip), %esi      # 0x7138
movl -0x7c(%rbp), %edx
movslq -0x7c(%rbp), %rcx
leaq 0x3a26(%rip), %rax      # 0x71e0
movl (%rax,%rcx,4), %ecx
movq -0x88(%rbp), %r8
movl -0x78(%rbp), %r9d
leaq 0x1a12(%rip), %rdi      # 0x51e1
movb $0x0, %al
callq 0x2030
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x38(%rbp), %r9
movq -0x68(%rbp), %r11
movq -0x70(%rbp), %r10
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x2c70
movslq -0x7c(%rbp), %rcx
leaq 0x39c7(%rip), %rax      # 0x71e0
movl (%rax,%rcx,4), %eax
shll $0x2, %eax
shll %eax
movslq -0x7c(%rbp), %rdx
leaq 0x3964(%rip), %rcx      # 0x7190
movl (%rcx,%rdx,4), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl -0x78(%rbp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
cltq
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x19b3(%rip), %rdi      # 0x5204
movb $0x0, %al
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2160
movq -0x30(%rbp), %rdi
callq 0x2160
movq -0x48(%rbp), %rdi
callq 0x2160
movq -0x50(%rbp), %rdi
callq 0x2160
movq -0x60(%rbp), %rdi
callq 0x2160
cmpl $0x0, -0xec(%rbp)
jle 0x38ac
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x38ac
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, 0x3895(%rip)      # 0x713c
jmp 0x3957
cmpl $0x63, -0xec(%rbp)
jne 0x38c9
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, 0x3878(%rip)      # 0x713c
jmp 0x3957
movl -0xec(%rbp), %eax
movl 0x3867(%rip), %ecx      # 0x713c
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3941
movslq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
shlq $0x2, %rdi
callq 0x2180
movq %rax, -0x30(%rbp)
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x3930
movq -0x40(%rbp), %rax
movslq -0x74(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x3902
movq -0x40(%rbp), %rdi
callq 0x2160
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x3943
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x35e2
movq -0x38(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl -0x78(%rbp), %r8d
callq 0x2fe0
callq 0x20e0
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
subq -0xe8(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x1671(%rip), %xmm1     # 0x5008
divsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0x100(%rbp), %xmm0
leaq 0x1878(%rip), %rdi      # 0x522a
movb $0x1, %al
callq 0x2030
movslq 0x377c(%rip), %rcx      # 0x713c
leaq 0x3819(%rip), %rax      # 0x71e0
movl (%rax,%rcx,4), %esi
shll $0x2, %esi
shll %esi
movslq 0x3766(%rip), %rcx      # 0x713c
leaq 0x37b3(%rip), %rax      # 0x7190
movl (%rax,%rcx,4), %eax
shll $0x2, %eax
addl %eax, %esi
movl -0x78(%rbp), %eax
shll $0x2, %eax
addl %eax, %esi
movslq 0x3748(%rip), %rcx      # 0x713c
leaq 0x37e5(%rip), %rax      # 0x71e0
movl (%rax,%rcx,4), %edx
movslq 0x3737(%rip), %rcx      # 0x713c
leaq 0x3784(%rip), %rax      # 0x7190
movl (%rax,%rcx,4), %ecx
movl -0x78(%rbp), %r8d
leaq 0x182d(%rip), %rdi      # 0x5247
movb $0x0, %al
callq 0x2030
movl 0x3715(%rip), %esi      # 0x713c
leaq 0x1835(%rip), %rdi      # 0x5263
movb $0x0, %al
callq 0x2030
movslq 0x3700(%rip), %rcx      # 0x713c
leaq 0x374d(%rip), %rax      # 0x7190
movl (%rax,%rcx,4), %eax
movslq 0x36ef(%rip), %rsi      # 0x713c
leaq 0x378c(%rip), %rcx      # 0x71e0
cltd
idivl (%rcx,%rsi,4)
movl %eax, %esi
addl $0x1, %esi
leaq 0x1809(%rip), %rdi      # 0x526d
movb $0x0, %al
callq 0x2030
movq -0x68(%rbp), %rax
movslq 0x36c6(%rip), %rdx      # 0x713c
leaq 0x3763(%rip), %rcx      # 0x71e0
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x17ee(%rip), %rdi      # 0x5279
movb $0x0, %al
callq 0x2030
movq -0x40(%rbp), %rdi
callq 0x2160
movq -0x38(%rbp), %rdi
callq 0x2160
movq -0x68(%rbp), %rdi
callq 0x2160
movq -0x70(%rbp), %rdi
callq 0x2160
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0xd8(%rbp), %rdi
callq 0x21e0
nopw %cs:(%rax,%rax)
nop