// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/htab.cpp
// Function: load_pt_index(void**, ha_pt_s**, All_reads*, hifiasm_opt_t*, char*)
int load_pt_index(void **r_flt_tab, ha_pt_t **r_ha_idx, All_reads *r, hifiasm_opt_t* opt, char* file_name)
{
	char* gfa_name = (char*)malloc(strlen(file_name)+64);
    sprintf(gfa_name, "%s.pt_flt", file_name);
    FILE* fp = fopen(gfa_name, "r");
	if (!fp) {
		free(gfa_name);
        return 0;
    }

	ha_pt_t *ha_idx = NULL;
	char mode = 0;
	int f_flag = 0, absent, 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->a, g->n);
			f_flag += fread(g->a, sizeof(ha_idxpos_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;
	}


	f_flag += fread(&absent, sizeof(absent), 1, fp);
	if(absent != opt->number_of_round)
	{
		fprintf(stderr, "ERROR: different number of rounds!\n");
		exit(1);
	}

	f_flag += fread(&opt->hom_cov, sizeof(opt->hom_cov), 1, fp);
	f_flag += fread(&opt->het_cov, sizeof(opt->het_cov), 1, fp);
	f_flag += fread(&opt->max_n_chain, sizeof(opt->max_n_chain), 1, fp);


	// fclose(fp);
	
	if(!load_All_reads(r, gfa_name))
	{
		free(gfa_name);
		return 0;
	}

	memset(r->trio_flag, AMBIGU, r->total_reads*sizeof(uint8_t));

	sprintf(gfa_name, "%s.pt_flt.paf.bin", file_name);
	fclose(fp); fp = fopen(gfa_name, "r"); uint64_t k;
    if (!fp) {
        free(gfa_name);
        return 0;
    }
	f_flag += fread(&(r->total_reads), sizeof(r->total_reads), 1, fp);
	r->paf = (ma_hit_t_alloc*)malloc(sizeof(ma_hit_t_alloc)*r->total_reads);
    r->reverse_paf = (ma_hit_t_alloc*)malloc(sizeof(ma_hit_t_alloc)*r->total_reads);
	for (k = 0; k < r->total_reads; k++) {
        // init_ma_hit_t_alloc(&(r->paf[k]));
        init_ma_hit_t_alloc(&(r->reverse_paf[k]));

		f_flag += fread(&(r->paf[k].is_fully_corrected), sizeof(r->paf[k].is_fully_corrected), 1, fp);
        f_flag += fread(&(r->paf[k].is_abnormal), sizeof(r->paf[k].is_abnormal), 1, fp);
        f_flag += fread(&(r->paf[k].length), sizeof(r->paf[k].length), 1, fp);
        r->paf[k].size = r->paf[k].length;

        r->paf[k].buffer = NULL;
        if(r->paf[k].length == 0) continue;
        
        r->paf[k].buffer = (ma_hit_t*)malloc(sizeof(ma_hit_t)*r->paf[k].length);
        fread(r->paf[k].buffer, sizeof((*(r->paf[k].buffer))), r->paf[k].length, fp);
    }
	fclose(fp);

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

	free(gfa_name);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %rbp
movq %rcx, 0x50(%rsp)
movq %rdx, %r13
movq %rsi, 0x48(%rsp)
movq %rdi, %r15
movq %r8, %rdi
callq 0x2090
leaq 0x40(%rax), %rdi
callq 0x2280
movq %rax, %r14
leaq 0x114c48(%rip), %rsi    # 0x1ec1e6
xorl %ebx, %ebx
movq %rax, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x11a103(%rip), %rsi    # 0x1f16b7
movq %r14, %rdi
callq 0x2210
testq %rax, %rax
je 0xd78d4
movq %rax, %r12
movq %rbp, 0x40(%rsp)
leaq 0xf(%rsp), %rbx
movb $0x0, (%rbx)
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rdx, %rsi
movq %rax, %rcx
callq 0x22f0
movb (%rbx), %al
cmpb $0x66, %al
je 0xd7619
pushq $0x1
popq %rbx
cmpb $0x66, %al
movq %r13, 0x30(%rsp)
je 0xd7810
movzbl %al, %ecx
cmpl $0x68, %ecx
je 0xd7679
movq %r14, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2190
jmp 0xd78e6
callq 0xefea7
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0xd7a5f
leaq 0xf(%rsp), %rbx
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rdx, %rsi
movq %r12, %rcx
callq 0x22f0
movq 0x17b994(%rip), %rax    # 0x252fe0
movq (%rax), %r15
callq 0xefea7
subsd 0x20(%rsp), %xmm0
leaq 0x114bb0(%rip), %rsi    # 0x1ec211
leaq 0x114bcd(%rip), %rdx    # 0x1ec235
movq %r15, %rdi
movb $0x1, %al
callq 0x22b0
movb (%rbx), %al
jmp 0xd75ec
movl %ecx, 0x1c(%rsp)
movb %al, 0xe(%rsp)
movq %r14, 0x10(%rsp)
pushq $0x1
popq %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, %rbp
pushq $0x4
popq %r15
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
leaq 0x4(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
leaq 0x8(%rbp), %rdi
pushq $0x8
popq %r13
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
leaq 0x10(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x22f0
movb 0x4(%rbp), %cl
shll %cl, %ebx
movslq %ebx, %r15
movq %r15, %rdi
pushq $0x20
popq %rsi
callq 0x2120
movq %rax, %rbx
movq %rbp, 0x38(%rsp)
movq %rax, 0x18(%rbp)
addq $0x8, %rbx
xorpd %xmm0, %xmm0
xorl %r14d, %r14d
cmpq %r15, %r14
jge 0xd77be
movapd %xmm0, 0x20(%rsp)
callq 0xefea7
movapd %xmm0, 0x80(%rsp)
leaq -0x8(%rbx), %rdi
movq %r12, %rsi
callq 0xd7b72
callq 0xefea7
movapd %xmm0, 0x70(%rsp)
callq 0xefea7
movapd %xmm0, 0x60(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x22f0
movq (%rbx), %rbp
leaq (,%rbp,8), %rdi
callq 0x2280
movq %rax, 0x8(%rbx)
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x22f0
callq 0xefea7
movapd 0x70(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd 0x20(%rsp), %xmm0
movapd 0x80(%rsp), %xmm1
unpcklpd 0x60(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
subpd %xmm1, %xmm2
addpd %xmm2, %xmm0
incq %r14
addq $0x20, %rbx
jmp 0xd7711
movq 0x48(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, (%rax)
movq 0x17b80e(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
leaq 0x114a5f(%rip), %rsi    # 0x1ec243
leaq 0x114a4a(%rip), %rdx    # 0x1ec235
movb $0x2, %al
callq 0x22b0
cmpb $0x66, 0xe(%rsp)
movq 0x10(%rsp), %r14
movq 0x30(%rsp), %r13
movl 0x1c(%rsp), %eax
je 0xd7810
cmpl $0x68, %eax
jne 0xd7604
leaq 0x5c(%rsp), %rbx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x22f0
movl (%rbx), %eax
movq 0x50(%rsp), %rbp
cmpl 0xf8(%rbp), %eax
jne 0xd7a3b
leaq 0xd0(%rbp), %rdi
pushq $0x4
popq %rbx
pushq $0x1
popq %r15
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x22f0
leaq 0xd4(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x22f0
addq $0xe8, %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x22f0
movq %r13, %rdi
movq %r14, %rsi
callq 0x5bfa
testl %eax, %eax
je 0xd78de
movq 0x28(%r13), %rdi
movq 0x50(%r13), %rdx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x20e0
leaq 0x114947(%rip), %rsi    # 0x1ec1f0
movq %r14, %rdi
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
callq 0x2190
leaq 0x119df0(%rip), %rsi    # 0x1f16b7
movq %r14, %rdi
callq 0x2210
testq %rax, %rax
jne 0xd78fc
movq %r14, %rdi
callq 0x2220
jmp 0xd78e8
movq %r14, %rdi
callq 0x2220
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq %r14, 0x10(%rsp)
leaq 0x50(%r13), %rdi
pushq $0x8
popq %rsi
movq %r13, %r12
pushq $0x1
popq %r13
movq %rdi, 0x20(%rsp)
movq %r13, %rdx
movq %rax, %rcx
callq 0x22f0
movq 0x50(%r12), %r14
imulq $0x18, %r14, %rbx
movq %rbx, %rdi
callq 0x2280
movq %rax, 0x78(%r12)
movq %rbx, %rdi
callq 0x2280
movq %rax, 0x80(%r12)
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpq %r14, %r12
jae 0xd7a02
movq 0x30(%rsp), %rbx
movq 0x80(%rbx), %rdi
addq %rbp, %rdi
callq 0x6964f
movq 0x78(%rbx), %rax
leaq (%rax,%rbp), %rdi
addq $0x10, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
leaq (%rax,%rbp), %rdi
addq $0x11, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %rax
leaq (%rax,%rbp), %rdi
addq $0xc, %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x22f0
movq 0x78(%rbx), %r14
movl 0xc(%r14,%rbp), %ebx
andq $0x0, (%r14,%rbp)
movl %ebx, 0x8(%r14,%rbp)
testq %rbx, %rbx
je 0xd79ee
imulq $0x28, %rbx, %rdi
callq 0x2280
movq %rax, (%r14,%rbp)
movq %rax, %rdi
pushq $0x28
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x22f0
incq %r12
movq 0x20(%rsp), %rax
movq (%rax), %r14
addq $0x18, %rbp
jmp 0xd794d
movq %r15, %rdi
callq 0x2190
movq 0x17b5cf(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1147ab(%rip), %rsi    # 0x1ec1c6
leaq 0x114813(%rip), %rdx    # 0x1ec235
xorl %eax, %eax
callq 0x22b0
movq 0x10(%rsp), %rdi
callq 0x2220
pushq $0x1
popq %rbx
jmp 0xd78e8
movq 0x17b59e(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x11482c(%rip), %rdi    # 0x1ec278
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
pushq $0x1
popq %rdi
callq 0x2250