// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %r8, %r15
movq %rcx, %r13
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
movq %r8, %rdi
callq 0x2090
leaq 0x40(%rax), %rdi
callq 0x2280
movq %rax, %r14
leaq 0x118a13(%rip), %rsi    # 0x22e0f6
xorl %ebp, %ebp
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x11deae(%rip), %rsi    # 0x2335a7
movq %r14, %rdi
callq 0x2210
testq %rax, %rax
je 0x115847
movq %rax, %r12
leaq 0xf(%rsp), %rbp
movb $0x0, (%rbp)
movl $0x1, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %rax, %rcx
callq 0x22f0
cmpb $0x66, (%rbp)
je 0x115755
movzbl 0xf(%rsp), %eax
cmpl $0x66, %eax
je 0x11577b
cmpl $0x68, %eax
je 0x115767
movq %r14, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2190
jmp 0x115859
leaq 0xf(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14c3f
jmp 0x115731
movzbl %al, %edx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x14cae
testb $0x1, %al
jne 0x115740
leaq 0x24(%rsp), %rbx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x22f0
movl (%rbx), %eax
cmpl 0xf8(%r13), %eax
jne 0x115885
leaq 0xd0(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x22f0
leaq 0xd4(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x22f0
addq $0xe8, %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x22f0
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32681
testl %eax, %eax
je 0x115851
movq 0x28(%rbx), %rdi
movq 0x50(%rbx), %rdx
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x20e0
leaq 0x1188e2(%rip), %rsi    # 0x22e100
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
callq 0x2190
leaq 0x11dd6d(%rip), %rsi    # 0x2335a7
movq %r14, %rdi
callq 0x2210
testq %rax, %rax
jne 0x11586c
movq %r14, %rdi
callq 0x2220
jmp 0x11585b
movq %r14, %rdi
callq 0x2220
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50(%rbx), %rdi
movq %rax, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x14eb0
movl $0x1, %ebp
jmp 0x11585b
callq 0x14e8b