// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0x40, %rdi
callq 0x2270
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x20003f(%rip), %rsi    # 0x38aec6
movb $0x0, %al
callq 0x2050
movq -0x38(%rbp), %rdi
leaq 0x20553d(%rip), %rsi    # 0x3903d6
callq 0x2200
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x18aebe
movq -0x38(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x18b585
movq $0x0, -0x48(%rbp)
movb $0x0, -0x49(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x40(%rbp), %rcx
leaq -0x49(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0x66, %eax
jne 0x18af6c
callq 0x1b7ca0
movsd %xmm0, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x18b5a0
movq -0x40(%rbp), %rcx
leaq -0x49(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq 0x2390a4(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x90(%rbp)
callq 0x1b7ca0
movq -0x90(%rbp), %rdi
subsd -0x60(%rbp), %xmm0
leaq 0x1fff93(%rip), %rsi    # 0x38aef1
leaq 0x1fffb0(%rip), %rdx    # 0x38af15
movb $0x1, %al
callq 0x22a0
movsbl -0x49(%rbp), %eax
cmpl $0x68, %eax
jne 0x18b191
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2140
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
addq $0x4, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movslq %eax, %rdi
movl $0x20, %esi
callq 0x2140
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x48(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jge 0x18b15d
callq 0x1b7ca0
movsd %xmm0, -0x68(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x18b5d0
callq 0x1b7ca0
subsd -0x68(%rbp), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x78(%rbp)
movq -0x80(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %rcx
movl $0x8, %esi
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
callq 0x1b7ca0
subsd -0x78(%rbp), %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x18b065
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq 0x238e71(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movsd -0x60(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
leaq 0x1ffda0(%rip), %rsi    # 0x38af23
leaq 0x1ffd8b(%rip), %rdx    # 0x38af15
movb $0x2, %al
callq 0x22a0
movsbl -0x49(%rbp), %eax
cmpl $0x68, %eax
je 0x18b1c1
movsbl -0x49(%rbp), %eax
cmpl $0x66, %eax
je 0x18b1c1
movq -0x38(%rbp), %rdi
callq 0x2210
movq -0x40(%rbp), %rdi
callq 0x21a0
movl $0x0, -0x4(%rbp)
jmp 0x18b585
movq -0x40(%rbp), %rcx
leaq -0x54(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movl -0x54(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0xf8(%rcx), %eax
je 0x18b216
movq 0x238de5(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1ffd53(%rip), %rsi    # 0x38af58
movb $0x0, %al
callq 0x22a0
movl $0x1, %edi
callq 0x2240
movq -0x28(%rbp), %rdi
addq $0xd0, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x28(%rbp), %rdi
addq $0xd4, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x28(%rbp), %rdi
addq $0xe8, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x79a0
cmpl $0x0, %eax
jne 0x18b2be
movq -0x38(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x18b585
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x1ffbe8(%rip), %rsi    # 0x38aed0
movb $0x0, %al
callq 0x2050
movq -0x40(%rbp), %rdi
callq 0x21a0
movq -0x38(%rbp), %rdi
leaq 0x2050d3(%rip), %rsi    # 0x3903d6
callq 0x2200
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x18b328
movq -0x38(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x18b585
movq -0x20(%rbp), %rdi
addq $0x50, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
imulq $0x18, 0x50(%rax), %rdi
callq 0x2270
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
imulq $0x18, 0x50(%rax), %rdi
callq 0x2270
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x80(%rax)
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x18b54d
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rdi
imulq $0x18, -0x88(%rbp), %rax
addq %rax, %rdi
callq 0xce060
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
imulq $0x18, -0x88(%rbp), %rax
addq %rax, %rdi
addq $0x10, %rdi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rsi
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
imulq $0x18, -0x88(%rbp), %rax
addq %rax, %rdi
addq $0x11, %rdi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rsi
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
imulq $0x18, -0x88(%rbp), %rax
addq %rax, %rdi
addq $0xc, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x22e0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movq $0x0, (%rax)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
cmpl $0x0, 0xc(%rax)
jne 0x18b4c2
jmp 0x18b536
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
imulq $0x28, %rax, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, %edx
movq -0x40(%rbp), %rcx
movl $0x28, %esi
callq 0x22e0
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x18b390
movq -0x40(%rbp), %rdi
callq 0x21a0
movq 0x238a83(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1ff93f(%rip), %rsi    # 0x38aea6
leaq 0x1ff9a7(%rip), %rdx    # 0x38af15
movb $0x0, %al
callq 0x22a0
movq -0x38(%rbp), %rdi
callq 0x2210
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)