// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
jmp 0x12a4
movb $0x2, 0x8(%rsp)
jmp 0x12a4
movq (%r14), %rax
movq %rax, 0x10(%rsp)
jmp 0x12a4
movq (%r14), %rdi
callq 0x1110
movl %eax, (%rsp)
jmp 0x12a4
cmpl $-0x1, %eax
jne 0x13af
testq %r15, %r15
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
je 0x1659
testq %r14, %r14
je 0x1659
testq %rbx, %rbx
je 0x1659
movb 0x8(%rsp), %bpl
cmpb $0x2, %bpl
sete %al
cmpl $0x0, (%rsp)
setle %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x1665
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 0x16a4
movq %rax, %r13
movq %rax, 0x30(%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 0x30(%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 0x150d
movq %rax, %r14
movq %rax, %rdi
callq 0x10d0
cmpb $0x2, %bpl
je 0x1526
movq %rax, %r13
leaq 0x50(%rsp), %r15
leaq 0x20(%rsp), %r12
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
je 0x1642
xorl %ebp, %ebp
movzbl 0x50(%rsp,%rbp), %eax
testl %eax, %eax
je 0x1495
cmpl $0x3a, %eax
je 0x149a
incq %rbp
jmp 0x1482
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %ebp
je 0x1467
movq %r15, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10a0
movq %rax, 0x20(%rsp)
movslq %ebp, %rax
addq %rax, %r13
incq %r13
movq %r15, %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10a0
movq %rax, 0x20(%rsp)
movq %r15, %rdi
callq 0x1070
notl %ebp
addl %ebp, %eax
shlq $0x28, %rax
orq %r13, %rax
movq %rax, 0x28(%rsp)
movl $0x10, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x1130
movq %r14, %rdi
callq 0x10d0
movq %rax, %r13
jmp 0x1467
movq 0x2ae4(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb99(%rip), %rsi       # 0x20b7
movq %r15, %rdx
jmp 0x13a6
movslq (%rsp), %rdi
movl $0x10, %esi
movq %rdi, 0x10(%rsp)
callq 0x10b0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x166a
movq %r14, %rdi
callq 0x10d0
movq %rax, %r15
movq $0x0, (%rsp)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x1090
testq %rax, %rax
je 0x15ee
xorl %ebp, %ebp
movzbl 0x50(%rsp,%rbp), %eax
testl %eax, %eax
je 0x1589
cmpl $0x3a, %eax
je 0x158e
incq %rbp
jmp 0x1576
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %ebp
je 0x155f
movq %r13, %rdi
callq 0x1070
movq %rbx, %r12
movq %r15, %rbx
movq %rax, %r15
movslq %ebp, %rax
movb $0x0, 0x50(%rsp,%rax)
movq %r13, %rdi
callq 0x16d4
movq (%rsp), %rbp
movq %rbp, %rcx
shlq $0x4, %rcx
movq 0x8(%rsp), %rdx
movq %rax, (%rdx,%rcx)
shlq $0x28, %r15
orq %rbx, %r15
movq %r12, %rbx
movq %r15, 0x8(%rdx,%rcx)
movq %r14, %rdi
callq 0x10d0
movq %rax, %r15
incq %rbp
movq %rbp, (%rsp)
jmp 0x155f
leaq 0xa0(%rip), %rcx        # 0x1695
movl $0x10, %edx
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x1050
movq 0x29e2(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xae3(%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
callq 0x1689
xorl %edi, %edi
callq 0x1120
callq 0x1150
leaq 0xa5d(%rip), %rdi       # 0x20ce
leaq 0xa67(%rip), %rsi       # 0x20df
leaq 0xa6d(%rip), %rcx       # 0x20ec
movl $0x74, %edx
callq 0x1080