// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rdi
pushq $0x60
popq %rsi
callq 0x2200
movq %rax, %rbx
movq %r15, %rdi
callq 0x2420
movq %rax, %r12
movq %rax, 0x20(%rbx)
leaq 0x3229(%rip), %rdx      # 0x5820
leaq 0x10(%rsp), %r15
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2160
leaq 0x3212(%rip), %rsi      # 0x5827
movq %r15, %rdi
callq 0x2370
testq %rax, %rax
jne 0x262e
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
jmp 0x27e8
movq %rax, %r15
pushq $0x1
popq %rdi
pushq $0x14
popq %rsi
callq 0x2200
testq %rax, %rax
jne 0x2667
orl $-0x1, (%r14)
movq 0x69ac(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x31d3(%rip), %rsi      # 0x5829
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2230
jmp 0x27e0
movq %rax, %r13
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r15, %rcx
callq 0x20c0
cmpq $0x1, %rax
jne 0x27d9
leaq 0x8(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x20c0
cmpq $0x1, %rax
jne 0x2717
pushq $0x14
popq %rdx
xorl %edi, %edi
movq %r13, %rsi
callq 0x258c
movq 0x8(%rsp), %r8
cmpq %r8, %rax
jne 0x2734
movq %r13, 0x58(%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x2250
cmpl $-0x1, %eax
je 0x27d9
movq 0x148(%rsp), %rax
addq $-0x1c, %rax
testb $0xf, %al
jne 0x27d9
movq %rax, %r13
shrq $0x4, %r13
movl %r13d, 0x28(%rbx)
movabsq $0xffffffff0, %rdi      # imm = 0xFFFFFFFF0
andq %rax, %rdi
callq 0x22b0
movq %rax, 0x30(%rbx)
testq %rax, %rax
jne 0x2760
orl $-0x1, (%r14)
jmp 0x27e0
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
movq 0x68d3(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x311b(%rip), %rsi      # 0x584a
jmp 0x2656
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
movq 0x68b6(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x311e(%rip), %rsi      # 0x586a
leaq 0x10(%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2230
jmp 0x27e0
movl %r13d, %ebp
pushq $0x10
popq %rsi
movq %rax, %rdi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x20c0
cmpq %rbp, %rax
jne 0x27b9
movq %r15, %rdi
callq 0x2120
leaq 0x312a(%rip), %rdx      # 0x58b2
leaq 0x10(%rsp), %r15
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2160
pushq $0x2
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x2360
movl %eax, 0x38(%rbx)
testl %eax, %eax
jg 0x2824
orl $-0x1, (%r14)
jmp 0x27e8
movq 0x6838(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x30cb(%rip), %rsi      # 0x5895
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2230
movl $0xfffffffe, (%r14)     # imm = 0xFFFFFFFE
movq %r15, %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2851
movq 0x6801(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x30d4(%rip), %rsi      # 0x58d5
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2230
movq %rbx, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x67cd(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x3084(%rip), %rsi      # 0x58b9
leaq 0x10(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2230
xorl %edi, %edi
callq 0x2280
movq %rax, 0x40(%rbx)
jmp 0x280f