// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
movq %rdx, %rdi
callq 0x2090
leaq 0x19(%rax), %rdi
callq 0x2280
movq %rax, %rbx
leaq 0x1184e0(%rip), %rsi    # 0x22e1ac
xorl %ebp, %ebp
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x11d8c5(%rip), %rsi    # 0x2335a7
movq %rbx, %rdi
callq 0x2210
testq %rax, %rax
je 0x115d53
movq %rax, %r15
testq %r13, %r13
je 0x115d0a
movq %r13, %rdi
movq %r15, %rsi
callq 0xc270e
testl %eax, %eax
je 0x115da2
leaq 0x7(%rsp), %r13
movb $0x0, (%r13)
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x22f0
cmpb $0x66, (%r13)
je 0x115d6c
movzbl 0x7(%rsp), %eax
cmpl $0x66, %eax
je 0x115d90
cmpl $0x68, %eax
je 0x115d7e
movq %rbx, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2190
xorl %ebp, %ebp
jmp 0x115d5b
movq %rbx, %rdi
callq 0x2220
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7(%rsp), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x1503c
jmp 0x115d30
movzbl %al, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x150ab
testb $0x1, %al
jne 0x115d3f
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1526e
movl $0x1, %ebp
jmp 0x115d5b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14ffc
jmp 0x115d4f