// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, %r14
movl $0x1, %edi
movl $0x60, %esi
callq 0x2210
movq %rax, %rbx
movq %r14, %rdi
callq 0x2430
movq %rax, %r12
movq %rax, 0x20(%rbx)
leaq 0x2f75(%rip), %rdx      # 0x5810
leaq 0x10(%rsp), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2160
leaq 0x2f5e(%rip), %rsi      # 0x5817
movq %r14, %rdi
callq 0x2380
testq %rax, %rax
jne 0x28d5
movl $0xfffffffe, (%r15)     # imm = 0xFFFFFFFE
xorl %r14d, %r14d
jmp 0x2aad
movq %rax, %r14
movl $0x1, %edi
movl $0x14, %esi
callq 0x2210
testq %rax, %rax
jne 0x2915
movl $0xffffffff, (%r15)     # imm = 0xFFFFFFFF
movq 0x66fe(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2f15(%rip), %rsi      # 0x5819
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
jmp 0x2aad
movq %rax, %r13
movl $0x14, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rcx
callq 0x20c0
cmpq $0x1, %rax
jne 0x2aa6
leaq 0x8(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x20c0
cmpq $0x1, %rax
jne 0x29da
movl $0x14, %edx
xorl %edi, %edi
movq %r13, %rsi
callq 0x282c
movq 0x8(%rsp), %r8
cmpq %r8, %rax
jne 0x29f7
movq %r13, 0x58(%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x2260
cmpl $-0x1, %eax
je 0x2aa6
movq 0x148(%rsp), %rax
addq $-0x1c, %rax
testb $0xf, %al
jne 0x2aa6
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 0x2a23
movl $0xffffffff, (%r15)     # imm = 0xFFFFFFFF
jmp 0x2aad
movl $0xfffffffe, (%r15)     # imm = 0xFFFFFFFE
movq 0x6610(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2e48(%rip), %rsi      # 0x583a
jmp 0x2904
movl $0xfffffffe, (%r15)     # imm = 0xFFFFFFFE
movq 0x65f3(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x2e4b(%rip), %rsi      # 0x585a
leaq 0x10(%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2240
jmp 0x2aad
movl %r13d, %ebp
movl $0x10, %esi
movq %rax, %rdi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x20c0
cmpq %rbp, %rax
jne 0x2a86
movq %r14, %rdi
callq 0x2120
leaq 0x2e55(%rip), %rdx      # 0x58a2
leaq 0x10(%rsp), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2160
movq %r14, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2370
movl %eax, 0x38(%rbx)
testl %eax, %eax
jg 0x2ad4
movl $0xffffffff, (%r15)     # imm = 0xFFFFFFFF
jmp 0x28cd
movq 0x656b(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2dee(%rip), %rsi      # 0x5885
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2240
movl $0xfffffffe, (%r15)     # imm = 0xFFFFFFFE
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x24b0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x651d(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2dc4(%rip), %rsi      # 0x58a9
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x2290
movq %rax, 0x40(%rbx)
jmp 0x2abf