// File: /workspace/github_c_makefile_star2_O3/desion[P]terminal/src/idx_create.c
// Function: main
int main(int argc, char* argv[]){
    int c;
    char *input, *output;
    uint8_t idx_type = 1;
    char* label;
    int line_num = 0;
    char buf[4096];
    int index;
    while ((c = getopt(argc, argv, "i:o:hsl:i:n:")) != -1) {
        switch (c) {
        case 'i':
            input = optarg;
            break;
        case 'n':
            line_num = atoi(optarg);
            break;
        case 'o':
            output = optarg;
            break;
        case 'l':
            label = optarg;
            break;
        case 'h':
            usage();
            exit(EXIT_SUCCESS);
        case 's':                   //type 1 for integer key, type 2 for string key
            idx_type = 2;
            break;
        default:
            fprintf(stderr, "Illegal argument \"%c\"\n", c);
            return 1;
        }
    }
    if (NULL == input || NULL == output || NULL == label){
        usage();
        exit(EXIT_SUCCESS);
    }
    if (idx_type == 2 && line_num <= 0){
        fprintf(stderr, "Please set the line num\n");
        exit(EXIT_SUCCESS);
    }
    //write the meta head
    meta_t idx_meta;
    idx_meta.version = time(NULL);
    idx_meta.idx_type = idx_type;
    strcpy(idx_meta.label, label);
    uint64_t crc = crc64(0, &idx_meta, sizeof(meta_t));
    FILE *out_fp = fopen(output, "wb");
    if (NULL == out_fp){
        fprintf(stderr, "Illegal Output file %s\n", output);
        return 1;
    }

    fprintf(stderr, "version:%u idx_type:%d label:%s crc:%lu\n", idx_meta.version, idx_meta.idx_type, idx_meta.label, crc);
    fwrite(&idx_meta, sizeof(idx_meta), 1, out_fp);
    fwrite(&crc, sizeof(uint64_t), 1, out_fp);

    FILE *in_fp;
    uint64_t hash_key;
    long off;
    int pos;

    in_fp = fopen(input, "r");
    if (NULL == in_fp){
        fprintf(stderr, "Illegal Input file %s\n", input);
        return 1;
    }
    off = ftell(in_fp);
    if(idx_type == 2){
        //get the prime
        idx_t *idx_hash = (idx_t *)calloc(line_num, sizeof(idx_t));
        assert(idx_hash != NULL);
        off = ftell(in_fp);
        pos = 0;
        size_t len;
        while(fgets(buf, 4096, in_fp) != NULL){
            index = find_index(buf, ':');
            if (index == -1){
                continue;
            }
            len = strlen(buf);
            buf[index] = '\0';
            //string hash
            hash_key = bkdr_hash(buf);
            idx_hash[pos].key = hash_key;
            idx_hash[pos].pos = (len << 40) | off;
            off = ftell(in_fp);
            pos++;
        }
        qsort(idx_hash, line_num, sizeof(idx_t), cmp_idx_key);
        fprintf(stderr, "sort finish\n");
        fwrite(idx_hash, sizeof(idx_t), line_num, out_fp);
    }else{
        idx_t idx_data;
        while(fgets(buf, 4096, in_fp) != NULL){
            index = find_index(buf, ':');
            if (index == -1){
                continue;
            }
            idx_data.key = strtoull(buf, NULL, 10);
            off = off + index + 1;
            idx_data.key = strtoull(buf, NULL, 10);
            idx_data.pos = ((strlen(buf) - index - 1) << 40) | off;
            fwrite(&idx_data, sizeof(idx_t), 1, out_fp);
            off = ftell(in_fp);
        }
    }
    fclose(out_fp);
    fclose(in_fp);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1058, %rsp           # imm = 0x1058
movq %rsi, %r12
movl %edi, %r13d
movb $0x1, 0x8(%rsp)
movl $0x0, (%rsp)
leaq 0xd9e(%rip), %rbp       # 0x2034
movq 0x2d3b(%rip), %r14      # 0x3fd8
leaq 0xd60(%rip), %rbx       # 0x2004
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1100
cmpl $0x69, %eax
jne 0x12bc
movq (%r14), %r15
jmp 0x12a4
leal -0x68(%rax), %ecx
cmpl $0xb, %ecx
ja 0x12f5
movslq (%rbx,%rcx,4), %rcx
addq %rbx, %rcx
jmpq *%rcx
movq (%r14), %rax
movq %rax, 0x10(%rsp)
jmp 0x12a4
movb $0x2, 0x8(%rsp)
jmp 0x12a4
movq (%r14), %rax
movq %rax, 0x18(%rsp)
jmp 0x12a4
movq (%r14), %rdi
callq 0x1110
movl %eax, (%rsp)
jmp 0x12a4
cmpl $-0x1, %eax
jne 0x13af
testq %r15, %r15
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rbx
je 0x16ae
testq %r14, %r14
je 0x16ae
testq %rbx, %rbx
je 0x16ae
movb 0x8(%rsp), %bpl
cmpb $0x2, %bpl
sete %al
cmpl $0x0, (%rsp)
setle %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x16ba
xorl %edi, %edi
callq 0x10e0
leaq 0x41(%rsp), %r12
movl %eax, -0x5(%r12)
movb %bpl, -0x1(%r12)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1030
leaq 0x3c(%rsp), %rsi
movl $0x14, %edx
xorl %edi, %edi
callq 0x16f8
movq %rax, %r13
movq %rax, 0x20(%rsp)
leaq 0xcef(%rip), %rsi       # 0x2071
movq %r14, %rdi
callq 0x10f0
movq %rax, %rbx
movq 0x2c64(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
testq %rbx, %rbx
jne 0x13e0
leaq 0xcd1(%rip), %rsi       # 0x2074
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x13c9
movq 0x2c42(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xc81(%rip), %rsi       # 0x2041
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
movl $0x1, %eax
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3c(%rsp), %r14
movl (%r14), %edx
movzbl 0x4(%r14), %ecx
leaq 0xc98(%rip), %rsi       # 0x208c
movq %r12, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x10c0
movl $0x14, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1130
leaq 0x20(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1130
leaq 0xc81(%rip), %rsi       # 0x20b5
movq %r15, %rdi
callq 0x10f0
testq %rax, %rax
je 0x153b
movq %rax, %r14
movq %rax, %rdi
callq 0x10d0
cmpb $0x2, %bpl
je 0x1554
movq %rax, %r13
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
je 0x15ec
leaq 0x28(%rsp), %r12
movq %r15, %rcx
xorl %ebp, %ebp
movzbl (%rcx), %eax
testl %eax, %eax
je 0x1498
cmpl $0x3a, %eax
je 0x14ba
incl %ebp
incq %rcx
jmp 0x1485
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
movq %r15, %rcx
movl $0x0, %ebp
testq %rax, %rax
jne 0x1485
jmp 0x15ec
movq %r15, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10a0
movl %ebp, %eax
addq %rax, %r13
incq %r13
movq %r15, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10a0
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x1070
notl %ebp
addl %ebp, %eax
shlq $0x28, %rax
orq %r13, %rax
movq %rax, 0x30(%rsp)
movl $0x10, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x1130
movq %r14, %rdi
callq 0x10d0
movq %rax, %r13
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
jne 0x1480
jmp 0x15ec
movq 0x2ab6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb6b(%rip), %rsi       # 0x20b7
movq %r15, %rdx
jmp 0x13a6
movslq (%rsp), %rdi
movl $0x10, %esi
movq %rdi, 0x8(%rsp)
callq 0x10b0
movq %rax, (%rsp)
testq %rax, %rax
je 0x16bf
movq %r14, %rdi
callq 0x10d0
movq %rax, %r15
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
jne 0x1603
leaq 0x14a(%rip), %rcx       # 0x16ea
movl $0x10, %edx
movq (%rsp), %r12
movq %r12, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x1050
movq 0x2a38(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb39(%rip), %rdi       # 0x2103
movl $0xc, %esi
movl $0x1, %edx
callq 0x1130
movl $0x10, %esi
movq %r12, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1130
movq %rbx, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x1060
xorl %eax, %eax
jmp 0x13ce
xorl %r12d, %r12d
movq %rbx, 0x10(%rsp)
movq %r13, %rcx
xorl %ebp, %ebp
movzbl (%rcx), %eax
testl %eax, %eax
je 0x1623
cmpl $0x3a, %eax
je 0x1645
incl %ebp
incq %rcx
jmp 0x1610
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
movq %r13, %rcx
movl $0x0, %ebp
testq %rax, %rax
jne 0x1610
jmp 0x1599
movq %r13, %rdi
callq 0x1070
movq %r15, %rbx
movq %rax, %r15
movl %ebp, %eax
movb $0x0, 0x50(%rsp,%rax)
movq %r13, %rdi
callq 0x1728
movq %r12, %rcx
shlq $0x4, %rcx
movq (%rsp), %rdx
movq %rax, (%rdx,%rcx)
shlq $0x28, %r15
orq %rbx, %r15
movq 0x10(%rsp), %rbx
movq %r15, 0x8(%rdx,%rcx)
movq %r14, %rdi
callq 0x10d0
movq %rax, %r15
incq %r12
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
jne 0x160b
jmp 0x1599
callq 0x16de
xorl %edi, %edi
callq 0x1120
callq 0x1150
leaq 0xa08(%rip), %rdi       # 0x20ce
leaq 0xa12(%rip), %rsi       # 0x20df
leaq 0xa18(%rip), %rcx       # 0x20ec
movl $0x74, %edx
callq 0x1080