// File: /workspace/github_c_makefile_star2_O3/desion[P]terminal/src/hdict.c
// Function: hdict_open
hdict_t* hdict_open(const char *path, int *hdict_errnop)
{
    FILE *fp = NULL;
    char pathname[256];

    hdict_t *hdict = (hdict_t *)calloc(1, sizeof(hdict[0]));
    hdict->path = strdup(path);

    snprintf(pathname, sizeof(pathname), "%s/idx", hdict->path);
    if ((fp = fopen(pathname, "r")) == NULL) {
        *hdict_errnop = EHDICT_BAD_FILE;
        goto error;
    }
    /* get the meta in idx file */
    meta_t *hdict_meta = (meta_t*)calloc(1, sizeof(meta_t));
    if (hdict_meta == NULL) {
        *hdict_errnop = EHDICT_OUT_OF_MEMERY;
        fprintf(stderr, "open idx %s fail get meta error\n", pathname);
        goto error;
    }
    if (fread(hdict_meta, sizeof(meta_t), 1, fp) != 1) {
        *hdict_errnop = EHDICT_BAD_FILE;
        goto error;
    }
    uint64_t crc64_num;
    if (fread(&crc64_num, sizeof(uint64_t), 1, fp) != 1){
        *hdict_errnop = EHDICT_BAD_FILE;
        fprintf(stderr, "open idx %s fail get crc error\n", pathname);
        goto error;
    }
    uint64_t meta_crc64 = crc64(0, hdict_meta, sizeof(meta_t));
    if (meta_crc64 != crc64_num){
        *hdict_errnop = EHDICT_BAD_FILE;
        fprintf(stderr, "open idx %s fail crc error crc is:%lu-%lu\n", pathname, meta_crc64, crc64_num);
        goto error;
    }
    hdict->hdict_meta = hdict_meta;

    struct stat st;
    if (stat(pathname, &st) == -1 ||
        ((st.st_size - sizeof(meta_t) - sizeof(uint64_t)) % sizeof(hdict->idx[0])) != 0) {
        *hdict_errnop = EHDICT_BAD_FILE;
        goto error;
    }
    /*malloc the idx space*/
    hdict->idx_num = (st.st_size - sizeof(meta_t) - sizeof(uint64_t)) / sizeof(hdict->idx[0]);
    hdict->idx = (idx_t *)malloc(hdict->idx_num * sizeof(hdict->idx[0]));
    if (hdict->idx == NULL) {
        *hdict_errnop = EHDICT_OUT_OF_MEMERY;
        goto error;
    }

    /* load the idx data to memory*/
    if (fread(hdict->idx, sizeof(hdict->idx[0]), hdict->idx_num, fp) != hdict->idx_num) {
        fprintf(stderr, "open idx %s fail idx_num:%d\n", pathname, hdict->idx_num);
        *hdict_errnop = EHDICT_BAD_FILE;
        goto error;
    }

    fclose(fp);
    fp = NULL;

    snprintf(pathname, sizeof(pathname), "%s/dat", hdict->path);
    hdict->fd = open(pathname, O_RDWR);
    if (hdict->fd <= 0) {
        //*hdict_errnop = EHDICT_BAD_FILE;
        *hdict_errnop = EHDICT_OUT_OF_MEMERY;
        goto error;
    }
    fprintf(stderr, "open idx %s success num:%d\n", pathname, hdict->idx_num);
    hdict->open_time = time(NULL);
    return hdict;

error:
    if (fp) fclose(fp);
    if (hdict) hdict_close(hdict);
    fprintf(stderr, "open idx %s fail\n", pathname);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, %r14
movq %rdi, %r15
movl $0x1, %edi
movl $0x60, %esi
callq 0x2210
movq %rax, %rbx
movq %r15, %rdi
callq 0x2430
movq %rax, %r12
movq %rax, 0x20(%rbx)
leaq 0x2fc5(%rip), %rdx      # 0x5820
leaq 0x10(%rsp), %r15
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2160
leaq 0x2fae(%rip), %rsi      # 0x5827
movq %r15, %rdi
callq 0x2380
testq %rax, %rax
jne 0x2892
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
jmp 0x2a6f
movq %rax, %r15
movl $0x1, %edi
movl $0x14, %esi
callq 0x2210
testq %rax, %rax
jne 0x28d2
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
movq 0x6741(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2f68(%rip), %rsi      # 0x5829
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
jmp 0x2a67
movq %rax, %r13
movl $0x14, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rcx
callq 0x20c0
cmpq $0x1, %rax
jne 0x2a60
leaq 0x8(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x20c0
cmpq $0x1, %rax
jne 0x2997
movl $0x14, %edx
xorl %edi, %edi
movq %r13, %rsi
callq 0x27ec
movq 0x8(%rsp), %r8
cmpq %r8, %rax
jne 0x29b4
movq %r13, 0x58(%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x2260
cmpl $-0x1, %eax
je 0x2a60
movq 0x148(%rsp), %rax
addq $-0x1c, %rax
testb $0xf, %al
jne 0x2a60
movq %rax, %r13
shrq $0x4, %r13
movl %r13d, 0x28(%rbx)
movabsq $0xffffffff0, %rdi      # imm = 0xFFFFFFFF0
andq %rax, %rdi
callq 0x22c0
movq %rax, 0x30(%rbx)
testq %rax, %rax
jne 0x29e0
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
jmp 0x2a67
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
movq 0x6653(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2e9b(%rip), %rsi      # 0x584a
jmp 0x28c1
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
movq 0x6636(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x2e9e(%rip), %rsi      # 0x586a
leaq 0x10(%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2240
jmp 0x2a67
movl %r13d, %ebp
movl $0x10, %esi
movq %rax, %rdi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x20c0
cmpq %rbp, %rax
jne 0x2a40
movq %r15, %rdi
callq 0x2120
leaq 0x2ea8(%rip), %rdx      # 0x58b2
leaq 0x10(%rsp), %r15
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2160
movq %r15, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2370
movl %eax, 0x38(%rbx)
testl %eax, %eax
jg 0x2aab
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
jmp 0x2a6f
movq 0x65b1(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2e44(%rip), %rsi      # 0x5895
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2240
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
movq %r15, %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2ad8
movq 0x657a(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2e4d(%rip), %rsi      # 0x58d5
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
movq %rbx, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6546(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2dfd(%rip), %rsi      # 0x58b9
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x2290
movq %rax, 0x40(%rbx)
jmp 0x2a96