// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x2230
movq %rax, -0x128(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2450
movq %rax, %rcx
movq -0x128(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq -0x120(%rbp), %rdi
movq -0x128(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x494e(%rip), %rdx      # 0x7004
movb $0x0, %al
callq 0x2170
leaq -0x120(%rbp), %rdi
leaq 0x4940(%rip), %rsi      # 0x700b
callq 0x23a0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x26e9
movq -0x18(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x29f2
movl $0x1, %edi
movl $0x14, %esi
callq 0x2230
movq %rax, -0x130(%rbp)
cmpq $0x0, -0x130(%rbp)
jne 0x2737
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x78de(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq -0x120(%rbp), %rdx
leaq 0x48e2(%rip), %rsi      # 0x700d
movb $0x0, %al
callq 0x2260
jmp 0x29f2
movq -0x130(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x14, %esi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
je 0x2766
movq -0x18(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x29f2
movq -0x20(%rbp), %rcx
leaq -0x138(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
je 0x27b4
movq -0x18(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x7861(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq -0x120(%rbp), %rdx
leaq 0x4886(%rip), %rsi      # 0x702e
movb $0x0, %al
callq 0x2260
jmp 0x29f2
movq -0x130(%rbp), %rsi
xorl %edi, %edi
movl $0x14, %edx
callq 0x25c0
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
cmpq -0x138(%rbp), %rax
je 0x281a
movq -0x18(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x7809(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq -0x120(%rbp), %rdx
movq -0x140(%rbp), %rcx
movq -0x138(%rbp), %r8
leaq 0x4840(%rip), %rsi      # 0x704e
movb $0x0, %al
callq 0x2260
jmp 0x29f2
movq -0x130(%rbp), %rcx
movq -0x128(%rbp), %rax
movq %rcx, 0x58(%rax)
leaq -0x120(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x2280
cmpl $-0x1, %eax
je 0x285d
movq -0x1a0(%rbp), %rax
subq $0x14, %rax
subq $0x8, %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x286c
movq -0x18(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x29f2
movq -0x1a0(%rbp), %rax
subq $0x14, %rax
subq $0x8, %rax
shrq $0x4, %rax
movl %eax, %ecx
movq -0x128(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x128(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %edi
shlq $0x4, %rdi
callq 0x22e0
movq %rax, %rcx
movq -0x128(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x128(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x28cb
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x29f2
movq -0x128(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x128(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %edx
movq -0x20(%rbp), %rcx
movl $0x10, %esi
callq 0x20b0
movq -0x128(%rbp), %rcx
movl 0x28(%rcx), %ecx
cmpq %rcx, %rax
je 0x2937
movq 0x76f2(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq -0x120(%rbp), %rdx
movq -0x128(%rbp), %rax
movl 0x28(%rax), %ecx
leaq 0x4758(%rip), %rsi      # 0x7079
movb $0x0, %al
callq 0x2260
movq -0x18(%rbp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x29f2
movq -0x20(%rbp), %rdi
callq 0x2110
movq $0x0, -0x20(%rbp)
leaq -0x120(%rbp), %rdi
movq -0x128(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x4730(%rip), %rdx      # 0x7096
movb $0x0, %al
callq 0x2170
leaq -0x120(%rbp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x2390
movl %eax, %ecx
movq -0x128(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x128(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jg 0x29a5
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x29f2
movq 0x764c(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq -0x120(%rbp), %rdx
movq -0x128(%rbp), %rax
movl 0x28(%rax), %ecx
leaq 0x46d6(%rip), %rsi      # 0x709d
movb $0x0, %al
callq 0x2260
xorl %eax, %eax
movl %eax, %edi
callq 0x22b0
movq %rax, %rcx
movq -0x128(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x128(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2a3f
cmpq $0x0, -0x20(%rbp)
je 0x2a02
movq -0x20(%rbp), %rdi
callq 0x2110
cmpq $0x0, -0x128(%rbp)
je 0x2a18
movq -0x128(%rbp), %rdi
callq 0x2a50
movq 0x75d9(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq -0x120(%rbp), %rdx
leaq 0x4689(%rip), %rsi      # 0x70b9
movb $0x0, %al
callq 0x2260
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
nopl (%rax)