// File: chhylp123[P]hifiasm/htab.cpp
// Function: write_pt_index(void*, ha_pt_s*, All_reads*, hifiasm_opt_t*, char*)
int write_pt_index(void *flt_tab, ha_pt_t *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, "w");
	if (!fp) {
		free(gfa_name);
        return 0;
    }
	yak_ft_t *ha_flt_tab = (yak_ft_t*)flt_tab;

	if(ha_flt_tab)
	{
		fwrite("f", 1, 1, fp);
		yak_ft_save(ha_flt_tab, fp);
	}


	if(ha_idx)
	{
		int i;
		ha_pt1_t *g;
		fwrite("h", 1, 1, fp);
		fwrite(&ha_idx->k, sizeof(ha_idx->k), 1, fp);
		fwrite(&ha_idx->pre, sizeof(ha_idx->pre), 1, fp);
		fwrite(&ha_idx->tot, sizeof(ha_idx->tot), 1, fp);
		fwrite(&ha_idx->tot_pos, sizeof(ha_idx->tot_pos), 1, fp);

		for (i = 0; i < 1<<ha_idx->pre; ++i) 
		{
			g = &(ha_idx->h[i]);
			yak_pt_save(g->h, fp);
			fwrite(&g->n, sizeof(g->n), 1, fp);
			fwrite(g->a, sizeof(ha_idxpos_t), g->n, fp);
		}
	}

	fwrite(&opt->number_of_round, sizeof(opt->number_of_round), 1, fp);
	fwrite(&opt->hom_cov, sizeof(opt->hom_cov), 1, fp);
	fwrite(&opt->het_cov, sizeof(opt->het_cov), 1, fp);
	fwrite(&opt->max_n_chain, sizeof(opt->max_n_chain), 1, fp);


	write_All_reads(r, gfa_name);

	sprintf(gfa_name, "%s.pt_flt.paf.bin", file_name); 
	fclose(fp); fp = fopen(gfa_name, "w"); uint64_t k;
	if (!fp) {
		free(gfa_name);
        return 0;
    }
	fwrite(&(r->total_reads), sizeof(r->total_reads), 1, fp);
	for (k = 0; k < r->total_reads; k++) {
		fwrite(&(r->paf[k].is_fully_corrected), sizeof(r->paf[k].is_fully_corrected), 1, fp);
        fwrite(&(r->paf[k].is_abnormal), sizeof(r->paf[k].is_abnormal), 1, fp);
        fwrite(&(r->paf[k].length), sizeof(r->paf[k].length), 1, fp);
        fwrite(r->paf[k].buffer, sizeof((*(r->paf[k].buffer))), r->paf[k].length, fp);
	}

	fprintf(stderr, "[M::%s] Index has been written.\n", __func__);
    free(gfa_name);
	fclose(fp);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %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 0x200472(%rip), %rsi    # 0x38aec6
movb $0x0, %al
callq 0x2050
movq -0x38(%rbp), %rdi
leaq 0x20e96c(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x18aa8b
movq -0x38(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x18add1
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x18aabf
movq -0x40(%rbp), %rcx
leaq 0x2027f9(%rip), %rdi    # 0x38d29e
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2400
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x18ade0
cmpq $0x0, -0x18(%rbp)
je 0x18abd8
movq -0x40(%rbp), %rcx
leaq 0x1fb986(%rip), %rdi    # 0x38645b
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2400
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jge 0x18abd6
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x18ae10
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %rcx
movl $0x8, %esi
callq 0x2400
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x18ab51
jmp 0x18abd8
movq -0x28(%rbp), %rdi
addq $0xf8, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x28(%rbp), %rdi
addq $0xd0, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x28(%rbp), %rdi
addq $0xd4, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x28(%rbp), %rdi
addq $0xe8, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x75b0
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x200264(%rip), %rsi    # 0x38aed0
movb $0x0, %al
callq 0x2050
movq -0x40(%rbp), %rdi
callq 0x21a0
movq -0x38(%rbp), %rdi
leaq 0x20e74b(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x18acac
movq -0x38(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
jmp 0x18add1
movq -0x20(%rbp), %rdi
addq $0x50, %rdi
movq -0x40(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2400
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jae 0x18ad99
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
imulq $0x18, -0x60(%rbp), %rax
addq %rax, %rdi
addq $0x10, %rdi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2400
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
imulq $0x18, -0x60(%rbp), %rax
addq %rax, %rdi
addq $0x11, %rdi
movq -0x40(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2400
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rdi
imulq $0x18, -0x60(%rbp), %rax
addq %rax, %rdi
addq $0xc, %rdi
movq -0x40(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x2400
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, %edx
movq -0x40(%rbp), %rcx
movl $0x28, %esi
callq 0x2400
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x18accf
movq 0x239240(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x200098(%rip), %rsi    # 0x38ae42
leaq 0x200131(%rip), %rdx    # 0x38aee2
movb $0x0, %al
callq 0x22a0
movq -0x38(%rbp), %rdi
callq 0x2210
movq -0x40(%rbp), %rdi
callq 0x21a0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)