// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10b0, %rsp           # imm = 0x10B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x1, -0x29(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xdb2(%rip), %rdx       # 0x2034
callq 0x1100
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x1340
movl -0x14(%rbp), %eax
addl $-0x68, %eax
movl %eax, %ecx
movq %rcx, -0x10b0(%rbp)
subl $0xb, %eax
ja 0x1314
movq -0x10b0(%rbp), %rax
leaq 0xd4f(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x2d13(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x133b
movq 0x2d03(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1110
movl %eax, -0x3c(%rbp)
jmp 0x133b
movq 0x2cef(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x133b
movq 0x2cdf(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x133b
callq 0x17d0
xorl %edi, %edi
callq 0x1130
movb $0x2, -0x29(%rbp)
jmp 0x133b
movq 0x2cdd(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0xd19(%rip), %rsi       # 0x2041
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x17b5
jmp 0x1274
xorl %eax, %eax
cmpq -0x20(%rbp), %rax
je 0x1358
xorl %eax, %eax
cmpq -0x28(%rbp), %rax
je 0x1358
xorl %eax, %eax
cmpq -0x38(%rbp), %rax
jne 0x1364
callq 0x17d0
xorl %edi, %edi
callq 0x1130
movzbl -0x29(%rbp), %eax
cmpl $0x2, %eax
jne 0x1392
cmpl $0x0, -0x3c(%rbp)
jg 0x1392
movq 0x2c7e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcd4(%rip), %rsi       # 0x2058
movb $0x0, %al
callq 0x10c0
xorl %edi, %edi
callq 0x1130
xorl %eax, %eax
movl %eax, %edi
callq 0x10e0
movl %eax, -0x1058(%rbp)
movb -0x29(%rbp), %al
movb %al, -0x1054(%rbp)
leaq -0x1058(%rbp), %rdi
addq $0x5, %rdi
movq -0x38(%rbp), %rsi
callq 0x1030
xorl %edi, %edi
leaq -0x1058(%rbp), %rsi
movl $0x14, %edx
callq 0x1890
movq %rax, -0x1060(%rbp)
movq -0x28(%rbp), %rdi
leaq 0xc8e(%rip), %rsi       # 0x2071
callq 0x10f0
movq %rax, -0x1068(%rbp)
xorl %eax, %eax
cmpq -0x1068(%rbp), %rax
jne 0x1422
movq 0x2bf7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
leaq 0xc65(%rip), %rsi       # 0x2074
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x17b5
movq 0x2bcf(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x1058(%rbp), %edx
movzbl -0x1054(%rbp), %ecx
leaq -0x1058(%rbp), %r8
addq $0x5, %r8
movq -0x1060(%rbp), %r9
leaq 0xc3a(%rip), %rsi       # 0x208c
movb $0x0, %al
callq 0x10c0
movq -0x1068(%rbp), %rcx
leaq -0x1058(%rbp), %rdi
movl $0x14, %esi
movl $0x1, %edx
callq 0x1140
movq -0x1068(%rbp), %rcx
leaq -0x1060(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x1140
movq -0x20(%rbp), %rdi
leaq 0xc17(%rip), %rsi       # 0x20b5
callq 0x10f0
movq %rax, -0x1070(%rbp)
xorl %eax, %eax
cmpq -0x1070(%rbp), %rax
jne 0x14dd
movq 0x2b3c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0xbed(%rip), %rsi       # 0x20b7
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x17b5
movq -0x1070(%rbp), %rdi
callq 0x10d0
movq %rax, -0x1080(%rbp)
movzbl -0x29(%rbp), %eax
cmpl $0x2, %eax
jne 0x1699
movslq -0x3c(%rbp), %rdi
movl $0x10, %esi
callq 0x10b0
movq %rax, -0x1090(%rbp)
cmpq $0x0, -0x1090(%rbp)
je 0x151e
jmp 0x153d
leaq 0xba9(%rip), %rdi       # 0x20ce
leaq 0xbb3(%rip), %rsi       # 0x20df
movl $0x74, %edx
leaq 0xbb4(%rip), %rcx       # 0x20ec
callq 0x1080
movq -0x1070(%rbp), %rdi
callq 0x10d0
movq %rax, -0x1080(%rbp)
movl $0x0, -0x1084(%rbp)
leaq -0x1040(%rbp), %rdi
movq -0x1070(%rbp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x1090
cmpq $0x0, %rax
je 0x1644
leaq -0x1040(%rbp), %rdi
movl $0x3a, %esi
callq 0x17f0
movl %eax, -0x1044(%rbp)
cmpl $-0x1, -0x1044(%rbp)
jne 0x159e
jmp 0x155a
leaq -0x1040(%rbp), %rdi
callq 0x1060
movq %rax, -0x1098(%rbp)
movslq -0x1044(%rbp), %rax
movb $0x0, -0x1040(%rbp,%rax)
leaq -0x1040(%rbp), %rdi
callq 0x1920
movq %rax, -0x1078(%rbp)
movq -0x1078(%rbp), %rcx
movq -0x1090(%rbp), %rax
movslq -0x1084(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x1098(%rbp), %rcx
shlq $0x28, %rcx
orq -0x1080(%rbp), %rcx
movq -0x1090(%rbp), %rax
movslq -0x1084(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x1070(%rbp), %rdi
callq 0x10d0
movq %rax, -0x1080(%rbp)
movl -0x1084(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1084(%rbp)
jmp 0x155a
movq -0x1090(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
movl $0x10, %edx
leaq 0x1f5(%rip), %rcx       # 0x1850
callq 0x1040
movq 0x2991(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa92(%rip), %rsi       # 0x2103
movb $0x0, %al
callq 0x10c0
movq -0x1090(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
movq -0x1068(%rbp), %rcx
movl $0x10, %esi
callq 0x1140
jmp 0x1796
jmp 0x169b
leaq -0x1040(%rbp), %rdi
movq -0x1070(%rbp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x1090
cmpq $0x0, %rax
je 0x1794
leaq -0x1040(%rbp), %rdi
movl $0x3a, %esi
callq 0x17f0
movl %eax, -0x1044(%rbp)
cmpl $-0x1, -0x1044(%rbp)
jne 0x16df
jmp 0x169b
leaq -0x1040(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x10a0
movq %rax, -0x10a8(%rbp)
movq -0x1080(%rbp), %rax
movslq -0x1044(%rbp), %rcx
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x1080(%rbp)
leaq -0x1040(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x10a0
movq %rax, -0x10a8(%rbp)
leaq -0x1040(%rbp), %rdi
callq 0x1060
movslq -0x1044(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
shlq $0x28, %rax
orq -0x1080(%rbp), %rax
movq %rax, -0x10a0(%rbp)
movq -0x1068(%rbp), %rcx
leaq -0x10a8(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1140
movq -0x1070(%rbp), %rdi
callq 0x10d0
movq %rax, -0x1080(%rbp)
jmp 0x169b
jmp 0x1796
movq -0x1068(%rbp), %rdi
callq 0x1050
movq -0x1070(%rbp), %rdi
callq 0x1050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10b0, %rsp           # imm = 0x10B0
popq %rbp
retq
nopw %cs:(%rax,%rax)