// File: chhylp123[P]hifiasm/Assembly.cpp
// Function: ha_assemble()
int ha_assemble(void)
{
    // debug_mc_g_t(MC_NAME);
    // debug_mc_gg_t(MC_NAME, 0, 0);
    // quick_debug_phasing(MC_NAME);
	extern void ha_extract_print_list(const All_reads *rs, int n_rounds, const char *o);
	int r, hom_cov = -1, ovlp_loaded = 0; uint64_t tot_b, tot_e;
	if (asm_opt.load_index_from_disk && load_all_data_from_disk(&R_INF.paf, &R_INF.reverse_paf, asm_opt.output_file_name)) {
		ovlp_loaded = 1;
		fprintf(stderr, "[M::%s::%.3f*%.2f] ==> loaded corrected reads and overlaps from disk\n", __func__, yak_realtime(), yak_cpu_usage());
		if (asm_opt.extract_list) {
			ha_extract_print_list(&R_INF, asm_opt.extract_iter, asm_opt.extract_list);
			exit(0);
		}
		if (asm_opt.flag & HA_F_WRITE_EC) {
            if(asm_opt.is_sc) Output_corrected_fastq();
            else Output_corrected_reads();
        }
		if (asm_opt.flag & HA_F_WRITE_PAF) Output_PAF();
        if (asm_opt.het_cov == -1024) hap_recalculate_peaks(asm_opt.output_file_name), ovlp_loaded = 2;
	}
	if (!ovlp_loaded) {
        ha_flt_tab = ha_idx = NULL;
        if((asm_opt.flag & HA_F_VERBOSE_GFA)) load_pt_index(&ha_flt_tab, &ha_idx, &R_INF, &asm_opt, asm_opt.output_file_name), load_ct_index(&ha_ct_table, asm_opt.output_file_name);

		// construct hash table for high occurrence k-mers
		if (!(asm_opt.flag & HA_F_NO_KMER_FLT) && ha_flt_tab == NULL) 
        {
			ha_flt_tab = ha_ft_gen(&asm_opt, &R_INF, &hom_cov, 0, 0);
			ha_opt_update_cov(&asm_opt, hom_cov);
		}
		// error correction
		assert(asm_opt.number_of_round > 0);
		for (r = ha_idx?asm_opt.number_of_round-1:0; r < asm_opt.number_of_round; ++r) {
			ha_opt_reset_to_round(&asm_opt, r); // this update asm_opt.roundID and a few other fields
            tot_b = tot_e = 0;
			// ha_overlap_and_correct(r);
            ha_ec(r, asm_opt.number_of_pround, (r<asm_opt.number_of_round-1)?1:0, &tot_b, &tot_e);
			fprintf(stderr, "[M::%s::%.3f*%.2f@%.3fGB] ==> corrected reads for round %d\n", __func__, yak_realtime(),
					yak_cpu_usage(), yak_peakrss_in_gb(), r + 1);
            fprintf(stderr, "[M::%s] # bases: %lu; # corrected bases: %lu\n", __func__, tot_b, tot_e);
			// fprintf(stderr, "[M::%s] # bases: %lld; # corrected bases: %lld; # recorrected bases: %lld\n", __func__,
			// 		asm_opt.num_bases, asm_opt.num_corrected_bases, asm_opt.num_recorrected_bases);
			// fprintf(stderr, "[M::%s] size of buffer: %.3fGB\n", __func__, asm_opt.mem_buf / 1073741824.0);
		}
		if (asm_opt.flag & HA_F_WRITE_EC) {
            if(asm_opt.is_sc) Output_corrected_fastq();
            else Output_corrected_reads();
        }
		// overlap between corrected reads
		ha_opt_reset_to_round(&asm_opt, asm_opt.number_of_round);
		// ha_overlap_final();
        ha_ec_ff(1/**0**/);
        fprintf(stderr, "[M::%s::%.3f*%.2f@%.3fGB] ==> found overlaps for the final round\n", __func__, yak_realtime(), yak_cpu_usage(), yak_peakrss_in_gb());
		// fprintf(stderr, "\n[M::%s::%.3f*%.2f@%.3fGB] ==> found overlaps for the final round\n", __func__, yak_realtime(), yak_cpu_usage(), yak_peakrss_in_gb());
		// ha_print_ovlp_stat(R_INF.paf, R_INF.reverse_paf, R_INF.total_reads);
		ha_ft_destroy(ha_flt_tab);
		if (asm_opt.flag & HA_F_WRITE_PAF) Output_PAF();
		ha_triobin(&asm_opt);

        // exit(1);
	}
    if(ovlp_loaded == 2) ovlp_loaded = 0;
    ha_opt_update_cov_min(&asm_opt, asm_opt.hom_cov, MIN_N_CHAIN);

    build_string_graph_without_clean(asm_opt.min_overlap_coverage, R_INF.paf, R_INF.reverse_paf, 
        R_INF.total_reads, R_INF.read_length, asm_opt.min_overlap_Len, asm_opt.max_hang_Len, asm_opt.clean_round, 
        asm_opt.gap_fuzz, asm_opt.min_drop_rate, asm_opt.max_drop_rate, asm_opt.output_file_name, asm_opt.large_pop_bubble_size, 0, !ovlp_loaded);
	destory_All_reads(&R_INF);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xa0, %rsp
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x2c(%rbp)
leaq 0x3a9391(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xf0(%rax)
je 0x1b58f
leaq 0x3a937d(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rdx
leaq 0x3a9e4a(%rip), %rsi    # 0x3c52d0
movq %rsi, %rdi
addq $0x78, %rdi
addq $0x80, %rsi
callq 0x133660
cmpl $0x0, %eax
je 0x1b58f
movl $0x1, -0x2c(%rbp)
movq 0x3a8b30(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x50(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x48(%rbp)
callq 0x1b7d20
movq -0x50(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0x48(%rbp), %xmm0
leaq 0x3667b7(%rip), %rsi    # 0x381c90
leaq 0x3667f6(%rip), %rdx    # 0x381cd6
movb $0x2, %al
callq 0x22a0
leaq 0x3a930a(%rip), %rax    # 0x3c47f8
cmpq $0x0, 0x48(%rax)
je 0x1b51d
leaq 0x3a92fc(%rip), %rax    # 0x3c47f8
movl 0x78(%rax), %esi
leaq 0x3a92f2(%rip), %rax    # 0x3c47f8
movq 0x48(%rax), %rdx
leaq 0x3a9dbf(%rip), %rdi    # 0x3c52d0
callq 0x1b6510
xorl %edi, %edi
callq 0x2240
leaq 0x3a92d4(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1b54d
leaq 0x3a92c3(%rip), %rax    # 0x3c47f8
cmpq $0x0, 0x2b0(%rax)
je 0x1b546
callq 0x15780
jmp 0x1b54b
callq 0x15610
jmp 0x1b54d
leaq 0x3a92a4(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x1b563
callq 0x198d0
leaq 0x3a928e(%rip), %rax    # 0x3c47f8
cmpl $0xfffffc00, 0xd4(%rax) # imm = 0xFFFFFC00
jne 0x1b58d
leaq 0x3a927b(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rdi
callq 0x1a810
movl $0x2, -0x2c(%rbp)
jmp 0x1b58f
cmpl $0x0, -0x2c(%rbp)
jne 0x1b894
leaq 0x3a9f38(%rip), %rax    # 0x3c54d8
movq $0x0, (%rax)
leaq 0x3a9f22(%rip), %rax    # 0x3c54d0
movq $0x0, (%rax)
leaq 0x3a923c(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1b609
leaq 0x3a922b(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %r8
leaq 0x3a9ef8(%rip), %rdi    # 0x3c54d0
leaq 0x3a9ef9(%rip), %rsi    # 0x3c54d8
leaq 0x3a9cea(%rip), %rdx    # 0x3c52d0
leaq 0x3a920b(%rip), %rcx    # 0x3c47f8
callq 0x18ae40
leaq 0x3a91ff(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %rsi
leaq 0x3a9eec(%rip), %rdi    # 0x3c54f0
callq 0x18a7a0
leaq 0x3a91e8(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x1b660
leaq 0x3a9eaf(%rip), %rax    # 0x3c54d0
cmpq $0x0, (%rax)
jne 0x1b660
leaq 0x3a91ca(%rip), %rdi    # 0x3c47f8
leaq 0x3a9c9b(%rip), %rsi    # 0x3c52d0
leaq -0x28(%rbp), %rdx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x189600
movq %rax, %rcx
leaq 0x3a9e82(%rip), %rax    # 0x3c54d0
movq %rcx, (%rax)
movl -0x28(%rbp), %esi
leaq 0x3a919d(%rip), %rdi    # 0x3c47f8
callq 0x3b60
leaq 0x3a9191(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xf8(%rax)
jle 0x1b672
jmp 0x1b691
leaq 0x3665a6(%rip), %rdi    # 0x381c1f
leaq 0x3665bb(%rip), %rsi    # 0x381c3b
movl $0x827, %edx            # imm = 0x827
leaq 0x366656(%rip), %rcx    # 0x381ce2
callq 0x2130
leaq 0x3a9e40(%rip), %rax    # 0x3c54d8
cmpq $0x0, (%rax)
je 0x1b6b3
leaq 0x3a9153(%rip), %rax    # 0x3c47f8
movl 0xf8(%rax), %eax
subl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b6ba
xorl %eax, %eax
movl %eax, -0x54(%rbp)
jmp 0x1b6ba
movl -0x54(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
leaq 0x3a912e(%rip), %rcx    # 0x3c47f8
cmpl 0xf8(%rcx), %eax
jge 0x1b7bd
movl -0x24(%rbp), %esi
leaq 0x3a9118(%rip), %rdi    # 0x3c47f8
callq 0x3b00
movq $0x0, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movslq -0x24(%rbp), %rdi
leaq 0x3a90f8(%rip), %rax    # 0x3c47f8
movl 0xfc(%rax), %esi
movl -0x24(%rbp), %ecx
leaq 0x3a90e8(%rip), %rax    # 0x3c47f8
movl 0xf8(%rax), %r8d
subl $0x1, %r8d
xorl %edx, %edx
movl $0x1, %eax
cmpl %r8d, %ecx
cmovll %eax, %edx
leaq -0x38(%rbp), %rcx
leaq -0x40(%rbp), %r8
callq 0x16b40
movq 0x3a88a4(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x70(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x60(%rbp)
callq 0x1b7d20
movsd %xmm0, -0x68(%rbp)
callq 0x1b7d00
movq -0x70(%rbp), %rdi
movsd -0x68(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x60(%rbp), %xmm0
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
leaq 0x36657a(%rip), %rsi    # 0x381cf4
leaq 0x366555(%rip), %rdx    # 0x381cd6
movb $0x3, %al
callq 0x22a0
movq 0x3a8851(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
leaq 0x36658f(%rip), %rsi    # 0x381d30
leaq 0x36652e(%rip), %rdx    # 0x381cd6
movb $0x0, %al
callq 0x22a0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1b6c0
leaq 0x3a9034(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1b7ed
leaq 0x3a9023(%rip), %rax    # 0x3c47f8
cmpq $0x0, 0x2b0(%rax)
je 0x1b7e6
callq 0x15780
jmp 0x1b7eb
callq 0x15610
jmp 0x1b7ed
leaq 0x3a9004(%rip), %rax    # 0x3c47f8
movl 0xf8(%rax), %esi
leaq 0x3a8ff7(%rip), %rdi    # 0x3c47f8
callq 0x3b00
movl $0x1, %edi
callq 0x1ad90
movq 0x3a87c9(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x78(%rbp)
callq 0x1b7d20
movsd %xmm0, -0x80(%rbp)
callq 0x1b7d00
movq -0x88(%rbp), %rdi
movsd -0x80(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x78(%rbp), %xmm0
leaq 0x366509(%rip), %rsi    # 0x381d5e
leaq 0x36647a(%rip), %rdx    # 0x381cd6
movb $0x3, %al
callq 0x22a0
leaq 0x3a9c66(%rip), %rax    # 0x3c54d0
movq (%rax), %rdi
callq 0x188df0
leaq 0x3a8f7f(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x1b888
callq 0x198d0
leaq 0x3a8f69(%rip), %rdi    # 0x3c47f8
callq 0x16a7f0
cmpl $0x2, -0x2c(%rbp)
jne 0x1b8a1
movl $0x0, -0x2c(%rbp)
leaq 0x3a8f50(%rip), %rax    # 0x3c47f8
movl 0xd0(%rax), %esi
leaq 0x3a8f43(%rip), %rdi    # 0x3c47f8
movl $0x64, %edx
callq 0x3bf0
leaq 0x3a8f32(%rip), %rax    # 0x3c47f8
movl 0x118(%rax), %edi
movq 0x3a9a75(%rip), %rsi    # 0x3c5348
movq 0x3a9a76(%rip), %rdx    # 0x3c5350
movq 0x3a9a3f(%rip), %rcx    # 0x3c5320
movq 0x3a9a00(%rip), %r8     # 0x3c52e8
leaq 0x3a8f09(%rip), %rax    # 0x3c47f8
movslq 0x114(%rax), %r9
leaq 0x3a8efb(%rip), %rax    # 0x3c47f8
movslq 0x10c(%rax), %r15
leaq 0x3a8eed(%rip), %rax    # 0x3c47f8
movslq 0x104(%rax), %r14
leaq 0x3a8edf(%rip), %rax    # 0x3c47f8
movslq 0x110(%rax), %rbx
leaq 0x3a8ed1(%rip), %rax    # 0x3c47f8
movss 0x15c(%rax), %xmm0
leaq 0x3a8ec2(%rip), %rax    # 0x3c47f8
movss 0x160(%rax), %xmm1
leaq 0x3a8eb3(%rip), %rax    # 0x3c47f8
movq 0x10(%rax), %r11
leaq 0x3a8ea8(%rip), %rax    # 0x3c47f8
movq 0x188(%rax), %r10
cmpl $0x0, -0x2c(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
xorl %r12d, %r12d
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl %eax, 0x30(%rsp)
callq 0x165da0
leaq 0x3a9938(%rip), %rdi    # 0x3c52d0
callq 0x73f0
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop