// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: load_hc_pt_index(ha_ug_index**, ma_ug_t*, char*)
int load_hc_pt_index(ha_ug_index** r_idx, ma_ug_t *ug, char* file_name)
{
    uint64_t flag = 0;
    // double index_time = yak_realtime();
    char* gfa_name = (char*)malloc(strlen(file_name)+25);
    sprintf(gfa_name, "%s.hic.tlb.bin", file_name);
    FILE* fp = fopen(gfa_name, "r");
    if (!fp) {
        free(gfa_name);
        return 0;
    }
    ha_ug_index* idx = NULL; CALLOC(idx, 1);
    uint64_t i;

    flag += fread(&i, sizeof(i), 1, fp);
    if(i != HC_PT_MA)
    {
        free(gfa_name);
        destory_hc_pt_index(idx);
        free(idx);
        (*r_idx) = NULL;
        fclose(fp);
        fprintf(stderr, "[M::%s::] ==> Renew Hi-C index\n", __func__);
        return 0;
    }
    flag += fread(&idx->uID_bits, sizeof(idx->uID_bits), 1, fp);
    flag += fread(&idx->uID_mode, sizeof(idx->uID_mode), 1, fp);
    flag += fread(&idx->pos_bits, sizeof(idx->pos_bits), 1, fp);
    flag += fread(&idx->pos_mode, sizeof(idx->pos_mode), 1, fp);
    flag += fread(&idx->rev_mode, sizeof(idx->rev_mode), 1, fp);
    flag += fread(&idx->k, sizeof(idx->k), 1, fp);
    flag += fread(&idx->pre, sizeof(idx->pre), 1, fp);
    flag += fread(&idx->tot, sizeof(idx->tot), 1, fp);
    flag += fread(&idx->tot_pos, sizeof(idx->tot_pos), 1, fp);
    MALLOC(idx->idx_buf, idx->tot);

    for (i = 0; i < idx->tot; i++)
    {
        flag += fread(&idx->idx_buf[i].n, sizeof(idx->idx_buf[i].n), 1, fp);
        flag += fread(&idx->idx_buf[i].end, sizeof(idx->idx_buf[i].end), 1, fp);
        MALLOC(idx->idx_buf[i].a, idx->idx_buf[i].n);
        flag += fread(idx->idx_buf[i].a, sizeof(uint64_t), idx->idx_buf[i].n, fp);
        hc_pt_load(&(idx->idx_buf[i].h), fp);
    }


    (*r_idx) = idx;

    free(gfa_name);
    if(!test_dbug(ug, fp))
    {
        destory_hc_pt_index(idx);
        free(idx);
        (*r_idx) = NULL;
        fclose(fp);
        fprintf(stderr, "[M::%s::] ==> Renew Hi-C index\n", __func__);
        return 0;
    }

    fclose(fp);
    // fprintf(stderr, "[M::%s::%.3f] ==> HiC index has been loaded\n", __func__, yak_realtime()-index_time);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %r12
movq %rdx, %rdi
callq 0x2090
leaq 0x19(%rax), %rdi
callq 0x2280
movq %rax, %r13
leaq 0xfa63e(%rip), %rsi     # 0x22f0b4
xorl %ebx, %ebx
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0xfeb1b(%rip), %rsi     # 0x2335a7
movq %r13, %rdi
callq 0x2210
testq %rax, %rax
je 0x134d70
movq %rax, %r15
movq %r13, 0x10(%rsp)
movl $0x1, %edi
movl $0xc0, %esi
callq 0x2120
movq %rax, %r13
leaq 0x30(%rsp), %rbx
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x22f0
cmpq $0x41, (%rbx)
jne 0x134dc3
movq %rbp, 0x18(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x18(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x20(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x28(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x30(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x38(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x40(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x50(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x58(%r13), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rdi, 0x28(%rsp)
movq %r15, %rcx
callq 0x22f0
movq %r13, %rdi
addq $0x60, %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x58(%r13), %rbx
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x2280
movq %rax, 0x78(%r13)
testq %rbx, %rbx
je 0x134d7a
movl $0x18, %ebp
xorl %r14d, %r14d
movq 0x78(%r13), %rax
leaq (%rax,%rbp), %rdi
addq $-0x10, %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%r13), %rdi
addq %rbp, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%r13), %rbx
movq -0x10(%rbx,%rbp), %rdi
shlq $0x3, %rdi
callq 0x2280
movq %rax, -0x8(%rbx,%rbp)
movq -0x10(%rbx,%rbp), %rdx
movl $0x8, %esi
movq %rax, %rdi
movq %r15, %rcx
callq 0x22f0
movq 0x78(%r13), %rbx
movl $0x1, %edi
movl $0x18, %esi
callq 0x2120
movq %rax, -0x18(%rbx,%rbp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %r15, %rcx
callq 0x22f0
movq -0x18(%rbx,%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq -0x18(%rbx,%rbp), %rdi
addq $0x4, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movl $0x1, %esi
movl $0x1, %edx
leaq 0xb(%rsp), %rdi
movq %r15, %rcx
callq 0x22f0
cmpb $0x0, 0xb(%rsp)
je 0x134d08
movl 0xc(%rsp), %eax
movl %eax, %edi
shrl $0x3, %edi
andl $-0x4, %edi
movl %eax, %r12d
shrl $0x5, %r12d
cmpl $0x20, %eax
movl $0x4, %eax
cmovbl %eax, %edi
movl $0x1, %eax
cmovbl %eax, %r12d
callq 0x2280
movq -0x18(%rbx,%rbp), %rcx
movq %rax, 0x8(%rcx)
movq -0x18(%rbx,%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x4, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x22f0
movl $0x1, %esi
movl $0x1, %edx
leaq 0xb(%rsp), %rdi
movq %r15, %rcx
callq 0x22f0
cmpb $0x0, 0xb(%rsp)
je 0x134d59
movl 0xc(%rsp), %r12d
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x2280
movq -0x18(%rbx,%rbp), %rcx
movq %rax, 0x10(%rcx)
movq -0x18(%rbx,%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x10, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x22f0
incq %r14
addq $0x20, %rbp
movq 0x28(%rsp), %rax
cmpq (%rax), %r14
jb 0x134bd8
jmp 0x134d7d
movq %r13, %rdi
callq 0x2220
jmp 0x134db2
xorl %r14d, %r14d
movq %r14, 0x30(%rsp)
movq 0x20(%rsp), %rbx
movq %r13, (%rbx)
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0xc270e
testl %eax, %eax
je 0x134dd8
movq %r15, %rdi
callq 0x2190
movl $0x1, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x1c8dc
jmp 0x134de6
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1c92f
xorl %ebx, %ebx
jmp 0x134db2