// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r13
movq %rdx, %rdi
callq 0x2090
leaq 0x19(%rax), %rdi
callq 0x2280
movq %rax, %rbp
leaq 0xf904e(%rip), %rsi     # 0x1ed1f4
xorl %r15d, %r15d
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0xfd4fa(%rip), %rsi     # 0x1f16b7
movq %rbp, %rdi
callq 0x2210
testq %rax, %rax
je 0xf448f
movq %rax, %r14
movq %r13, 0x10(%rsp)
pushq $0x1
popq %rbx
movl $0xc0, %esi
movq %rbx, %rdi
callq 0x2120
movq %rax, %r13
leaq 0x40(%rsp), %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
cmpq $0x41, (%r15)
jne 0xf44a9
movq %rbp, 0x18(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x18(%r13), %rdi
pushq $0x8
popq %r15
pushq $0x1
popq %rbx
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x20(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x28(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x30(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x38(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x40(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x50(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x58(%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
movq %r13, %rdi
addq $0x60, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x22f0
movq 0x58(%r13), %rdi
movq %rdi, 0x30(%rsp)
shlq $0x5, %rdi
callq 0x2280
movq %rax, %rbp
movq %rax, 0x78(%r13)
pushq $0x18
popq %r15
xorl %eax, %eax
pushq $0x4
popq %rbx
movq %r13, 0x28(%rsp)
cmpq 0x30(%rsp), %rax
jae 0xf445e
movq %rax, 0x38(%rsp)
leaq (%r15,%rbp), %rdi
addq $-0x10, %rdi
pushq $0x8
popq %rsi
movq %r14, %r12
pushq $0x1
popq %r14
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
movq 0x78(%r13), %rbp
leaq (%r15,%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
movq -0x10(%rbp,%r15), %r13
leaq (,%r13,8), %rdi
callq 0x2280
movq %rax, -0x8(%rbp,%r15)
movq %rax, %rdi
pushq $0x8
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x22f0
callq 0x120359
movq %rax, -0x18(%rbp,%r15)
leaq 0xc(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
movq -0x18(%rbp,%r15), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
movq -0x18(%rbp,%r15), %rdi
addq $0x4, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
leaq 0xb(%rsp), %rdi
movq %r14, %rsi
movq %r14, %rdx
movq %r12, %r14
movq %r12, %rcx
callq 0x22f0
cmpb $0x0, 0xb(%rsp)
je 0xf43fd
movl 0xc(%rsp), %eax
movl %eax, %edi
shrl $0x3, %edi
andl $-0x4, %edi
movl %eax, %r13d
shrl $0x5, %r13d
cmpl $0x20, %eax
pushq $0x4
popq %rax
cmovbl %eax, %edi
pushq $0x1
popq %rax
cmovbl %eax, %r13d
callq 0x2280
movq -0x18(%rbp,%r15), %rcx
movq %rax, 0x8(%rcx)
movq -0x18(%rbp,%r15), %rax
movq 0x8(%rax), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0xb(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %r14, %rcx
callq 0x22f0
cmpb $0x0, 0xb(%rsp)
je 0xf4448
movl 0xc(%rsp), %r13d
movq %r13, %rdi
shlq $0x4, %rdi
callq 0x2280
movq -0x18(%rbp,%r15), %rcx
movq %rax, 0x10(%rcx)
movq -0x18(%rbp,%r15), %rax
movq 0x10(%rax), %rdi
pushq $0x10
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x22f0
movq 0x38(%rsp), %rax
incq %rax
addq $0x20, %r15
movq 0x28(%rsp), %r13
jmp 0xf42e6
movq 0x10(%rsp), %rbx
movq %r13, (%rbx)
movq 0x18(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x871ea
testl %eax, %eax
je 0xf44cc
movq %r14, %rdi
callq 0x2190
pushq $0x1
popq %r15
jmp 0xf4497
movq %rbp, %rdi
callq 0x2220
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
callq 0x2220
movq %r13, %rdi
callq 0xf450c
movq %r13, %rdi
callq 0x2220
movq 0x10(%rsp), %rax
andq $0x0, (%rax)
jmp 0xf44e0
movq %r13, %rdi
callq 0xf450c
movq %r13, %rdi
callq 0x2220
andq $0x0, (%rbx)
movq %r14, %rdi
callq 0x2190
movq 0x15eaf1(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xf8d1c(%rip), %rsi     # 0x1ed215
leaq 0xf8d35(%rip), %rdx     # 0x1ed235
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x22b0
jmp 0xf4497