// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbp
movq %rcx, 0x18(%rsp)
movq %rdx, %r14
movq %rsi, (%rsp)
movq %rdi, %r15
movq %r8, %rdi
callq 0x2090
leaq 0x40(%rax), %rdi
callq 0x2280
movq %rax, %r12
leaq 0x1150ec(%rip), %rsi    # 0x1ec1e6
xorl %ebx, %ebx
movq %rax, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0x123591(%rip), %rsi    # 0x1fa6a1
movq %r12, %rdi
callq 0x2210
testq %rax, %rax
je 0xd72c5
movq %rax, %r13
movq %rbp, 0x10(%rsp)
movq %r12, 0x8(%rsp)
testq %r15, %r15
je 0xd7149
pushq $0x66
popq %rdi
movq %r13, %rsi
callq 0x2270
movq %r15, %rdi
movq %r13, %rsi
callq 0xd7398
cmpq $0x0, (%rsp)
je 0xd721b
pushq $0x68
popq %rdi
movq %r13, %rsi
callq 0x2270
pushq $0x4
popq %r12
pushq $0x1
popq %r15
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
leaq 0x4(%rbx), %rdi
movq %rdi, 0x20(%rsp)
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
leaq 0x8(%rbx), %rdi
pushq $0x8
popq %r12
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
leaq 0x10(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
xorl %ebp, %ebp
movq 0x20(%rsp), %rax
movb (%rax), %cl
pushq $0x1
popq %rax
shll %cl, %eax
cltq
cmpq %rax, %rbp
jge 0xd721b
movq (%rsp), %rax
movq 0x18(%rax), %rbx
leaq (%rbx,%r12), %r15
movq -0x8(%rbx,%r12), %rdi
movq %r13, %rsi
callq 0xd747d
movq %r15, %rdi
pushq $0x8
popq %r15
movq %r15, %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x2410
movq (%rbx,%r12), %rdx
movq 0x8(%rbx,%r12), %rdi
movq %r15, %rsi
movq %r13, %rcx
callq 0x2410
incq %rbp
addq $0x20, %r12
jmp 0xd71bd
movq 0x18(%rsp), %r12
leaq 0xf8(%r12), %rdi
pushq $0x4
popq %rbx
pushq $0x1
popq %r15
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
leaq 0xd0(%r12), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
leaq 0xd4(%r12), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
addq $0xe8, %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2410
movq %r14, %rdi
movq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x5958
leaq 0x114f58(%rip), %rsi    # 0x1ec1f0
xorl %ebx, %ebx
movq %r12, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
movq %r13, %rdi
callq 0x2190
leaq 0x1233e9(%rip), %rsi    # 0x1fa6a1
movq %r12, %rdi
callq 0x2210
testq %rax, %rax
jne 0xd72de
movq %r12, %rdi
callq 0x2220
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
leaq 0x50(%r14), %rbx
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2410
pushq $0xc
popq %r12
xorl %r13d, %r13d
pushq $0x28
popq %rbp
cmpq (%rbx), %r13
jae 0xd735f
movq 0x78(%r14), %rax
movsbl 0x4(%rax,%r12), %edi
movq %r15, %rsi
callq 0x2270
movq 0x78(%r14), %rax
movsbl 0x5(%rax,%r12), %edi
movq %r15, %rsi
callq 0x2270
movq 0x78(%r14), %rdi
addq %r12, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2410
movq 0x78(%r14), %rax
movq -0xc(%rax,%r12), %rdi
movl (%rax,%r12), %edx
movq %rbp, %rsi
movq %r15, %rcx
callq 0x2410
incq %r13
addq $0x18, %r12
jmp 0xd7300
movq 0x17bc7a(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x114df2(%rip), %rsi    # 0x1ec162
leaq 0x114e8b(%rip), %rdx    # 0x1ec202
xorl %eax, %eax
callq 0x22b0
movq 0x8(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2190
pushq $0x1
popq %rbx
jmp 0xd72cd