// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/htab.cpp
// Function: uidx_load(void**, ha_pt_s**, char*, ma_ug_t*)
int uidx_load(void **r_flt_tab, ha_pt_t **r_ha_idx, char* file_name, ma_ug_t *ug)
{
    char* gfa_name = (char*)malloc(strlen(file_name)+25);
    sprintf(gfa_name, "%s.uidx.bin", file_name);
    FILE* fp = fopen(gfa_name, "r");
    if (!fp) {
        free(gfa_name);
        return 0;
    }

	if(ug && (!test_dbug(ug, fp))) {
        fprintf(stderr, "[M::%s] Renew UL Index\n", __func__);
        free(gfa_name); fclose(fp);
        return 0;
    }
	// fprintf(stderr, "[M::%s]\t%s\tftell::%ld\n", __func__, file_name, ftell(fp));

    ha_pt_t *ha_idx = NULL;
    char mode = 0;
    int f_flag = 0, i;
    double index_time, index_s_time, pos_time, pos_s_time;

    
    
    f_flag += fread(&mode, 1, 1, fp);
    if(mode == 'f')
    {
        index_time = yak_realtime();

        yak_ft_load((yak_ft_t **)r_flt_tab, fp);
        
        f_flag += fread(&mode, 1, 1, fp);

        fprintf(stderr, "[M::%s::%.3f] ==> Loaded flt table\n", __func__, yak_realtime()-index_time);
    }
    ///insert using multiple threads???
    if(mode == 'h')
    {
        pos_time = index_time = 0;

        CALLOC(ha_idx, 1);
        ha_pt1_t *g;
        f_flag += fread(&ha_idx->k, sizeof(ha_idx->k), 1, fp);
        f_flag += fread(&ha_idx->pre, sizeof(ha_idx->pre), 1, fp);
        f_flag += fread(&ha_idx->tot, sizeof(ha_idx->tot), 1, fp);
        f_flag += fread(&ha_idx->tot_pos, sizeof(ha_idx->tot_pos), 1, fp);
        CALLOC(ha_idx->h, 1<<ha_idx->pre);
        for (i = 0; i < 1<<ha_idx->pre; ++i) 
        {
            index_s_time = yak_realtime();

            g = &(ha_idx->h[i]);
            yak_pt_load(&(g->h), fp);

            index_time += yak_realtime() - index_s_time;
            
            pos_s_time = yak_realtime();

            f_flag += fread(&g->n, sizeof(g->n), 1, fp);
            MALLOC(g->al, g->n);
            f_flag += fread(g->al, sizeof(ha_idxposl_t), g->n, fp);

            pos_time += yak_realtime() - pos_s_time;
        }
        (*r_ha_idx) = ha_idx;

        fprintf(stderr, "[M::%s::%.3f(index)/%.3f(pos)] ==> Loaded pos table\n", __func__, index_time, pos_time);
    }

    if(mode != 'h' && mode != 'f')
    {
        free(gfa_name);
        fclose(fp);
        return 0;
    }

    fprintf(stderr, "[M::%s] Index has been loaded.\n", __func__);

	fclose(fp);
    free(gfa_name);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movq %rdx, %rdi
callq 0x2090
leaq 0x19(%rax), %rdi
callq 0x2280
movq %rax, %r12
leaq 0x11442f(%rip), %rsi    # 0x1ec29c
xorl %ebp, %ebp
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x119834(%rip), %rsi    # 0x1f16b7
movq %r12, %rdi
callq 0x2210
testq %rax, %rax
je 0xd80fc
movq %rax, %r14
testq %r15, %r15
je 0xd7eaf
movq %r15, %rdi
movq %r14, %rsi
callq 0x871ea
testl %eax, %eax
je 0xd8115
leaq 0xf(%rsp), %r15
movb $0x0, (%r15)
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rdx, %rsi
movq %r14, %rcx
callq 0x22f0
movb (%r15), %al
cmpb $0x66, %al
je 0xd7efa
pushq $0x1
popq %rbp
cmpb $0x66, %al
je 0xd80d5
movzbl %al, %eax
cmpl $0x68, %eax
je 0xd7f5a
movq %r12, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2190
xorl %ebp, %ebp
jmp 0xd8104
callq 0xefea7
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd7a5f
leaq 0xf(%rsp), %rbx
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rdx, %rsi
movq %r14, %rcx
callq 0x22f0
movq 0x17b0b3(%rip), %rax    # 0x252fe0
movq (%rax), %r15
callq 0xefea7
subsd 0x10(%rsp), %xmm0
leaq 0x1142cf(%rip), %rsi    # 0x1ec211
leaq 0x114382(%rip), %rdx    # 0x1ec2cb
movq %r15, %rdi
movb $0x1, %al
callq 0x22b0
movb (%rbx), %al
jmp 0xd7ed0
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
pushq $0x1
popq %r12
pushq $0x20
popq %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2120
movq %rax, %r13
pushq $0x4
popq %r15
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x4(%r13), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x8(%r13), %rdi
pushq $0x8
popq %r15
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x22f0
leaq 0x10(%r13), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x22f0
movb 0x4(%r13), %cl
shll %cl, %ebp
movslq %ebp, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2120
movq %rax, %rbp
movq %r13, 0x28(%rsp)
movq %rax, 0x18(%r13)
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
pushq $0x10
popq %rbx
cmpq %r15, %r13
jge 0xd8099
movapd %xmm0, 0x10(%rsp)
callq 0xefea7
movapd %xmm0, 0x60(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0xd7b72
callq 0xefea7
movapd %xmm0, 0x50(%rsp)
callq 0xefea7
movapd %xmm0, 0x40(%rsp)
leaq 0x8(%rbp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x22f0
movq 0x8(%rbp), %r12
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x2280
movq %rax, 0x18(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x22f0
callq 0xefea7
movapd 0x50(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd 0x10(%rsp), %xmm0
movapd 0x60(%rsp), %xmm1
unpcklpd 0x40(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
subpd %xmm1, %xmm2
addpd %xmm2, %xmm0
incq %r13
addq $0x20, %rbp
jmp 0xd7ff2
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0x17af33(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
leaq 0x114184(%rip), %rsi    # 0x1ec243
leaq 0x114205(%rip), %rdx    # 0x1ec2cb
movb $0x2, %al
callq 0x22b0
movq 0x38(%rsp), %r12
pushq $0x1
popq %rbp
movq 0x17af04(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1140e0(%rip), %rsi    # 0x1ec1c6
leaq 0x1141de(%rip), %rdx    # 0x1ec2cb
xorl %eax, %eax
callq 0x22b0
movq %r14, %rdi
callq 0x2190
movq %r12, %rdi
callq 0x2220
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17aec4(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x11418d(%rip), %rsi    # 0x1ec2b3
leaq 0x11419e(%rip), %rdx    # 0x1ec2cb
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x22b0
movq %r12, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2190
jmp 0xd8104