// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movq %r8, %rdi
callq 0x2090
leaq 0x40(%rax), %rdi
callq 0x2280
movq %rax, %r14
leaq 0x11f0e2(%rip), %rsi    # 0x23a306
xorl %ebx, %ebx
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x1245bd(%rip), %rsi    # 0x23f7f7
movq %r14, %rdi
callq 0x2210
testq %rax, %rax
je 0x11b38b
movq %rax, %r12
leaq 0xe(%rsp), %rbx
movb $0x0, (%rbx)
movl $0x1, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x22f0
movb (%rbx), %al
cmpb $0x66, %al
je 0x11b292
cmpb $0x66, %al
je 0x11b2bf
movzbl %al, %edx
cmpl $0x68, %edx
je 0x11b2ae
movq %r14, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2190
jmp 0x11b39d
leaq 0xe(%rsp), %rdx
leaq 0xf(%rsp), %rbx
movq %rbp, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x14425
movb (%rbx), %al
jmp 0x11b271
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x1449c
testb $0x1, %al
jne 0x11b27d
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 0x11b3c9
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), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x31d8d
testl %eax, %eax
je 0x11b395
movq 0x28(%r13), %rdi
movq 0x50(%r13), %rdx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x20e0
leaq 0x11efae(%rip), %rsi    # 0x23a310
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
callq 0x2190
leaq 0x124479(%rip), %rsi    # 0x23f7f7
movq %r14, %rdi
callq 0x2210
testq %rax, %rax
jne 0x11b3b0
movq %r14, %rdi
callq 0x2220
jmp 0x11b39f
movq %r14, %rdi
callq 0x2220
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50(%r13), %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x1467e
movl $0x1, %ebx
jmp 0x11b39f
callq 0x14659