// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rdi, %r12
movq %rdx, %rdi
callq 0x2090
leaq 0x19(%rax), %rdi
callq 0x2280
movq %rax, %rbp
leaq 0x100768(%rip), %rsi    # 0x23b304
xorl %ebx, %ebx
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x104c45(%rip), %rsi    # 0x23f7f7
movq %rbp, %rdi
callq 0x2210
testq %rax, %rax
je 0x13aec0
movq %rax, %r15
movl $0x1, %edi
movl $0xc0, %esi
callq 0x2120
movq %rax, %r14
leaq 0x30(%rsp), %rbx
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x22f0
cmpq $0x41, (%rbx)
jne 0x13aed9
movq %rbp, 0x10(%rsp)
movq %r13, 0x18(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x18(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x20(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x28(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x30(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x38(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x40(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x50(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
leaq 0x58(%r14), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rdi, 0x28(%rsp)
movq %r15, %rcx
callq 0x22f0
movq %r14, %rdi
addq $0x60, %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x58(%r14), %rbx
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x2280
movq %r14, %r12
movq %rax, 0x78(%r14)
testq %rbx, %rbx
je 0x13ae8e
movq %rax, %r14
movl $0x18, %ebp
xorl %r13d, %r13d
leaq (%r14,%rbp), %rdi
addq $-0x10, %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%r12), %r14
leaq (%r14,%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq -0x10(%r14,%rbp), %rbx
leaq (,%rbx,8), %rdi
callq 0x2280
movq %rax, -0x8(%r14,%rbp)
movl $0x8, %esi
movq %rax, %rdi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x22f0
movl $0x1, %edi
movl $0x18, %esi
callq 0x2120
movq %rax, -0x18(%r14,%rbp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %r15, %rcx
callq 0x22f0
movq -0x18(%r14,%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22f0
movq -0x18(%r14,%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 0x13ae29
movl 0xc(%rsp), %eax
movl %eax, %edi
shrl $0x3, %edi
andl $-0x4, %edi
movl %eax, %ebx
shrl $0x5, %ebx
cmpl $0x20, %eax
movl $0x4, %eax
cmovbl %eax, %edi
movl $0x1, %eax
cmovbl %eax, %ebx
callq 0x2280
movq -0x18(%r14,%rbp), %rcx
movq %rax, 0x8(%rcx)
movq -0x18(%r14,%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x4, %esi
movq %rbx, %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 0x13ae79
movl 0xc(%rsp), %ebx
movq %rbx, %rdi
shlq $0x4, %rdi
callq 0x2280
movq -0x18(%r14,%rbp), %rcx
movq %rax, 0x10(%rcx)
movq -0x18(%r14,%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x10, %esi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x22f0
incq %r13
addq $0x20, %rbp
movq 0x28(%rsp), %rax
cmpq (%rax), %r13
jb 0x13ad04
movq 0x20(%rsp), %rbx
movq %r12, (%rbx)
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0xc3645
testl %eax, %eax
je 0x13aeec
movq %r15, %rdi
callq 0x2190
movl $0x1, %ebx
jmp 0x13aec8
movq %rbp, %rdi
callq 0x2220
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x1c1ff
jmp 0x13aefa
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1c252
xorl %ebx, %ebx
jmp 0x13aec8