// File: /workspace/github_c_makefile_star2_O2/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 0xdce(%rip), %rbp       # 0x2034
leaq 0xd97(%rip), %r14       # 0x2004
movq 0x2d64(%rip), %rbx      # 0x3fd8
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1100
leal -0x68(%rax), %ecx
cmpl $0xb, %ecx
ja 0x12c0
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
jmpq *%rcx
movq (%rbx), %r15
jmp 0x1274
movb $0x2, 0x8(%rsp)
jmp 0x1274
movq (%rbx), %rdi
callq 0x1110
movl %eax, (%rsp)
jmp 0x1274
movq (%rbx), %rax
movq %rax, 0x18(%rsp)
jmp 0x1274
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
jmp 0x1274
cmpl $-0x1, %eax
jne 0x1378
testq %r15, %r15
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
je 0x15e8
testq %r14, %r14
je 0x15e8
testq %rbx, %rbx
je 0x15e8
movb 0x8(%rsp), %bpl
cmpb $0x2, %bpl
sete %al
cmpl $0x0, (%rsp)
setle %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x15f4
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
pushq $0x14
popq %rdx
xorl %edi, %edi
callq 0x1664
movq %rax, %r13
movq %rax, 0x20(%rsp)
leaq 0xd26(%rip), %rsi       # 0x2071
movq %r14, %rdi
callq 0x10f0
movq %rax, %rbx
movq 0x2c9b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
testq %rbx, %rbx
jne 0x13a7
leaq 0xd08(%rip), %rsi       # 0x2074
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
jmp 0x1392
movq 0x2c79(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xcb8(%rip), %rsi       # 0x2041
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
pushq $0x1
popq %rax
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 0xcd1(%rip), %rsi       # 0x208c
movq %r12, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x10c0
pushq $0x14
popq %rsi
pushq $0x1
popq %r12
movq %r14, %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1130
leaq 0x20(%rsp), %rdi
pushq $0x8
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1130
leaq 0xcbe(%rip), %rsi       # 0x20b5
movq %r15, %rdi
callq 0x10f0
testq %rax, %rax
je 0x14b4
movq %rax, %r14
movq %rax, %rdi
callq 0x10d0
cmpb $0x2, %bpl
je 0x14cd
movq %rax, %r13
leaq 0x50(%rsp), %r15
pushq $0xa
popq %rbp
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
je 0x15d1
movq %r15, %rdi
callq 0x163b
cmpl $-0x1, %eax
je 0x1428
movl %eax, %r12d
movq %r15, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x10a0
movslq %r12d, %rax
addq %rax, %r13
incq %r13
movq %r15, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x10a0
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x1070
notl %r12d
addl %r12d, %eax
shlq $0x28, %rax
orq %r13, %rax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1130
movq %r14, %rdi
callq 0x10d0
movq %rax, %r13
jmp 0x1428
movq 0x2b3d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbf2(%rip), %rsi       # 0x20b7
movq %r15, %rdx
jmp 0x136f
movslq (%rsp), %rdi
pushq $0x10
popq %rsi
movq %rdi, 0x10(%rsp)
callq 0x10b0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1612
movq %r14, %rdi
callq 0x10d0
movq %rax, %r12
movq $0x0, (%rsp)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
je 0x1581
movq %r13, %rdi
callq 0x163b
cmpl $-0x1, %eax
je 0x1504
movl %eax, %r15d
movq %r13, %rdi
callq 0x1070
movq %rbx, %rbp
movq %r12, %rbx
movq %rax, %r12
movslq %r15d, %rax
movb $0x0, 0x50(%rsp,%rax)
movq %r13, %rdi
callq 0x1694
movq (%rsp), %r15
movq %r15, %rcx
shlq $0x4, %rcx
movq 0x8(%rsp), %rdx
movq %rax, (%rdx,%rcx)
shlq $0x28, %r12
orq %rbx, %r12
movq %rbp, %rbx
movq %r12, 0x8(%rdx,%rcx)
movq %r14, %rdi
callq 0x10d0
movq %rax, %r12
incq %r15
movq %r15, (%rsp)
jmp 0x1504
leaq 0xcb(%rip), %rcx        # 0x1653
pushq $0x10
popq %r15
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x1050
movq 0x2a4d(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb4e(%rip), %rdi       # 0x2103
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
callq 0x1130
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1130
movq %rbx, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x1060
xorl %eax, %eax
jmp 0x1395
callq 0x162f
xorl %edi, %edi
callq 0x1120
movq 0x29fd(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa53(%rip), %rdi       # 0x2058
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x1130
jmp 0x15ed
leaq 0xab5(%rip), %rdi       # 0x20ce
leaq 0xabf(%rip), %rsi       # 0x20df
leaq 0xac5(%rip), %rcx       # 0x20ec
pushq $0x74
popq %rdx
callq 0x1080