// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: clean_trio_untig_graph(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, long long, float, long long, R_to_U*, buf_t*, unsigned char*, float, unsigned int, unsigned int, float, int, int, float, unsigned int, float, int, int, int, hap_cov_t*, kvec_asg_arc_t_warp*)
void clean_trio_untig_graph(ma_ug_t *ug, asg_t *read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, 
long long tipsLen, float tip_drop_ratio, long long stops_threshold, 
R_to_U* ruIndex, buf_t* b_0, uint8_t* visit, float density, uint32_t miniHapLen, 
uint32_t miniBiGraph, float chimeric_rate, int is_final_clean, int just_bubble_pop, 
float drop_ratio, uint32_t trio_flag, float trio_drop_rate, int max_hang, int min_ovlp, 
int gap_fuzz, hap_cov_t *cov, kvec_asg_arc_t_warp *ae)
{
    asg_t *g = ug->g; rd_hamming_fly_simp_t *p = NULL; 
    uint32_t is_first = 1;
    // if(trio_flag == MOTHER) {
    //     print_debug_gfa(read_g, ug, coverage_cut, "debug_dups", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len, 0, 1, 0);
    //     exit(1);
    // }
    redo:
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-0:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-0:", 0);
    ///debug
    // if(!p) p = gen_rd_hamming_fly_simp_t(ug, read_g, sources, coverage_cut, max_hang, min_ovlp, gap_fuzz); 
    // fprintf(stderr, "[M::%s] 0\n", __func__); dbg_asys_gfa(g);
    asg_pop_bubble_primary_trio(ug, NULL, trio_flag, DROP, cov, NULL, 1, p);
    ///do not need to refine bubbles during the first round of cleaning
    if(!p) p = gen_rd_hamming_fly_simp_t(ug, read_g, sources, coverage_cut, max_hang, min_ovlp, gap_fuzz, ae); 

    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-1:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-1:", 0);
    // fprintf(stderr, "[M::%s] 1\n", __func__); dbg_asys_gfa(g);
    magic_trio_phasing(g, ug, read_g, coverage_cut, sources, reverse_sources, 2, ruIndex, trio_flag, trio_drop_rate);        
    // fprintf(stderr, "[M::%s] 2\n", __func__); dbg_asys_gfa(g);

    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-2:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-2:", 0);
    /**********debug**********/
    if(just_bubble_pop == 0)
    {
        cut_trio_tip_primary(g, ug, tipsLen, trio_flag, 0, read_g, reverse_sources, ruIndex, cov->is_r_het, 2);
    }
    // fprintf(stderr, "[M::%s] 3\n", __func__); dbg_asys_gfa(g);
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-3:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-3:", 0);
    /**********debug**********/
    long long pre_cons = get_graph_statistic(g);
    long long cur_cons = 0;
    while(pre_cons != cur_cons)
    {
        // fprintf(stderr, "[M::%s] 4\n", __func__); dbg_asys_gfa(g);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-4:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-4:", 0);
        pre_cons = get_graph_statistic(g);
        // fprintf(stderr, "[M::%s] 5\n", __func__); dbg_asys_gfa(g);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-5:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-5:", 0);
        ///need consider tangles
        asg_pop_bubble_primary_trio(ug, NULL, trio_flag, DROP, cov, NULL, 1, p);
        // fprintf(stderr, "[M::%s] 6\n", __func__); dbg_asys_gfa(g);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-6:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-6:", 0);
        /**********debug**********/
        if(just_bubble_pop == 0)
        {
            ///need consider tangles
            asg_arc_cut_trio_long_tip_primary(g, ug, read_g, reverse_sources, ruIndex, 2, tip_drop_ratio, trio_flag, cov, NULL);            
            // if(trio_flag == MOTHER) print_untig((ug), 9, "i-7:", 0);
            // if(trio_flag == MOTHER) print_untig((ug), 10, "i-7:", 0);
            // fprintf(stderr, "[M::%s] 7\n", __func__); dbg_asys_gfa(g);
            // if(trio_flag == MOTHER) print_debug_gfa(read_g, ug, coverage_cut, "debug_dups", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len);
            asg_arc_cut_trio_long_equal_tips_assembly(g, ug, read_g, reverse_sources, 2, ruIndex, trio_flag, cov, NULL);            
            // if(trio_flag == MOTHER) print_untig((ug), 9, "i-8:", 0);
            // if(trio_flag == MOTHER) print_untig((ug), 10, "i-8:", 0);
            // fprintf(stderr, "[M::%s] 8\n", __func__); dbg_asys_gfa(g);
            asg_arc_cut_trio_long_tip_primary_complex(g, ug, read_g, reverse_sources, ruIndex, 2, tip_drop_ratio, stops_threshold, cov, NULL, trio_flag);
            // if(trio_flag == MOTHER) print_untig((ug), 9, "i-9:", 0);
            // if(trio_flag == MOTHER) print_untig((ug), 10, "i-9:", 0);
            // fprintf(stderr, "[M::%s] 9\n", __func__); dbg_asys_gfa(g);
            asg_arc_cut_trio_long_equal_tips_assembly_complex(g, ug, read_g, reverse_sources, 2, ruIndex, stops_threshold, cov, NULL, trio_flag);
            // if(trio_flag == MOTHER) print_untig((ug), 9, "i-10:", 0);
            // if(trio_flag == MOTHER) print_untig((ug), 10, "i-10:", 0);
            // fprintf(stderr, "[M::%s] 10\n", __func__); dbg_asys_gfa(g);
            detect_chimeric_by_topo(g, ug, read_g, reverse_sources, 2, stops_threshold, chimeric_rate, ruIndex, NULL, cov->is_r_het);
            // if(trio_flag == MOTHER) print_untig((ug), 9, "i-11:", 0);
            // if(trio_flag == MOTHER) print_untig((ug), 10, "i-11:", 0);
            // fprintf(stderr, "[M::%s] 11\n", __func__); dbg_asys_gfa(g);
            ///need consider tangles
            ///note we need both the read graph and the untig graph
        }
        /**********debug**********/
        cur_cons = get_graph_statistic(g);
        // fprintf(stderr, "[M::%s] 12\n", __func__); dbg_asys_gfa(g);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-12:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-12:", 0);
    }
    if(just_bubble_pop == 0)
    {   
        // fprintf(stderr, "[M::%s] 13\n", __func__); dbg_asys_gfa(g);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-13:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-13:", 0);
        cut_trio_tip_primary(g, ug, tipsLen, trio_flag, 0, read_g, reverse_sources, ruIndex, cov->is_r_het, 2);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-14:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-14:", 0);
        // fprintf(stderr, "[M::%s] 14\n", __func__); dbg_asys_gfa(g);
    }

    // print_debug_gfa(read_g, ug, coverage_cut, "debug_dups", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len);
    // fprintf(stderr, "[M::%s] 15\n", __func__); dbg_asys_gfa(g);
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-15:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-15:", 0);
    magic_trio_phasing(g, ug, read_g, coverage_cut, sources, reverse_sources, 2, ruIndex, trio_flag, trio_drop_rate); 
    // fprintf(stderr, "[M::%s] 16\n", __func__); dbg_asys_gfa(g);
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-16:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-16:", 0);

    // print_debug_gfa(read_g, ug, coverage_cut, "resolve_tangles", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len, 0, 0, 0);
    // exit(1);
    ///bug here
    resolve_tangles(ug, read_g, reverse_sources, 20, 100, 0.05, 0.2, ruIndex, cov->is_r_het, trio_flag, drop_ratio);    
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-17:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-17:", 0);
    // fprintf(stderr, "[M::%s] 17\n", __func__); dbg_asys_gfa(g);
    drop_semi_circle(ug, g, read_g, reverse_sources, ruIndex, cov->is_r_het);
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-18:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-18:", 0);
    // fprintf(stderr, "[M::%s] 18\n", __func__); dbg_asys_gfa(g);
    all_to_all_deduplicate(ug, read_g, coverage_cut, sources, trio_flag, trio_drop_rate, reverse_sources, ruIndex, cov->is_r_het, DOUBLE_CHECK_THRES, asm_opt.trio_flag_occ_thres);
    // if(trio_flag == MOTHER) print_untig((ug), 9, "i-19:", 0);
    // if(trio_flag == MOTHER) print_untig((ug), 10, "i-19:", 0);
    // fprintf(stderr, "[M::%s] 19\n", __func__); dbg_asys_gfa(g);
    // if(trio_flag == MOTHER) print_untig_by_read(ug, "m54329U_190827_173812/30214441/ccs", (uint32_t)-1, NULL, NULL, "bf-16");
    if(is_first)
    {
        is_first = 0;
        unitig_arc_del_short_diploid_by_length(ug->g, drop_ratio);
        // if(trio_flag == MOTHER) print_untig((ug), 9, "i-20:", 0);
        // if(trio_flag == MOTHER) print_untig((ug), 10, "i-20:", 0);
        // fprintf(stderr, "[M::%s] 20\n", __func__); dbg_asys_gfa(g);
        goto redo;
    } 
    if(p) {
        fprintf(stderr, "[M::%s] # adjusted arcs::%u\n", __func__, p->n_insert);
        destroy_rd_hamming_fly_simp_t(p); free(p);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movss %xmm4, 0x1c(%rsp)
movss %xmm3, 0x3c(%rsp)
movss %xmm2, 0x74(%rsp)
movss %xmm0, 0x44(%rsp)
movq %r9, 0x78(%rsp)
movq %r8, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
movq 0x148(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x138(%rsp), %eax
movq %rax, 0x88(%rsp)
movl 0x130(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x128(%rsp), %eax
movl %eax, 0x68(%rsp)
movl 0x120(%rsp), %r15d
movl 0x118(%rsp), %r12d
movq 0xe8(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0xe0(%rsp), %eax
movq %rax, 0x98(%rsp)
movq 0x18(%rdi), %r14
xorl %ebx, %ebx
pushq $0x5
popq %r13
movzbl %r15b, %eax
movl %eax, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
movq %r15, 0x10(%rsp)
movl %r12d, 0x40(%rsp)
movq %r14, 0x8(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movl %r15d, %edx
movl %r13d, %ecx
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq %rbx
pushq $0x1
callq 0x9716c
popq %rcx
popq %rdx
testq %rbx, %rbx
jne 0x96dc2
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x48(%rsp), %rcx
movl 0x68(%rsp), %r8d
movl 0x6c(%rsp), %r9d
pushq 0x90(%rsp)
pushq 0x90(%rsp)
callq 0x968fc
popq %rcx
popq %rdx
movq %rax, %rbx
subq $0x20, %rsp
movl %r15d, 0x10(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq 0x40(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x78(%rsp), %r8
movss 0x3c(%rsp), %xmm0
callq 0x947c2
addq $0x20, %rsp
testl %r12d, %r12d
jne 0x96e32
movq %r14, %rdi
movq %rbp, %rsi
movq 0x78(%rsp), %rdx
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x20(%rsp), %r9
pushq $0x2
movq 0x30(%rsp), %rax
pushq 0xa0(%rax)
pushq 0x40(%rsp)
pushq 0x68(%rsp)
callq 0x93005
addq $0x20, %rsp
movq %r14, %rdi
callq 0x92d8d
movq %rax, %rcx
xorl %eax, %eax
movq %rbx, 0x60(%rsp)
cmpq %rax, %rcx
je 0x96faa
movq %r14, %rdi
callq 0x92d8d
movq %rax, 0xa0(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movl %r15d, %edx
movl %r13d, %ecx
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq %rbx
pushq $0x1
callq 0x9716c
popq %rcx
popq %rdx
testl %r12d, %r12d
jne 0x96f95
subq $0x8, %rsp
movq %r14, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %r15
movq %r15, %rdx
movq 0x58(%rsp), %r12
movq %r12, %rcx
movq 0x38(%rsp), %rbx
movq %rbx, %r8
pushq $0x2
popq %r13
movl %r13d, %r9d
movss 0x4c(%rsp), %xmm0
pushq $0x0
movq %rbp, %r14
movq 0x38(%rsp), %rbp
pushq %rbp
pushq 0x28(%rsp)
callq 0x9342f
addq $0x18, %rsp
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
pushq $0x2
popq %r8
movq %rbx, %r9
pushq $0x0
pushq %rbp
pushq 0x28(%rsp)
callq 0x94044
addq $0x20, %rsp
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rbx, %r8
movl %r13d, %r9d
movss 0x44(%rsp), %xmm0
pushq 0x10(%rsp)
pushq $0x0
pushq %rbp
movq 0xb0(%rsp), %r13
pushq %r13
callq 0x938d7
addq $0x20, %rsp
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
pushq $0x2
popq %r8
movq %rbx, %r9
pushq 0x10(%rsp)
pushq $0x0
pushq %rbp
pushq %r13
callq 0x94b2d
addq $0x18, %rsp
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %r15
movq %r12, %rcx
movl 0x48(%rsp), %r12d
pushq $0x2
popq %r8
movl %r13d, %r9d
pushq $0x5
popq %r13
movss 0x7c(%rsp), %xmm0
pushq 0xa0(%rbp)
movq %r14, %rbp
movq 0x18(%rsp), %r14
pushq $0x0
pushq %rbx
movq 0x80(%rsp), %rbx
callq 0x95038
addq $0x20, %rsp
movq %r14, %rdi
callq 0x92d8d
movq 0xa0(%rsp), %rcx
jmp 0x96e44
testl %r12d, %r12d
jne 0x96fe3
movq %r14, %rdi
movq %rbp, %rsi
movq 0x78(%rsp), %rdx
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x20(%rsp), %r9
pushq $0x2
movq 0x30(%rsp), %rax
pushq 0xa0(%rax)
pushq 0x40(%rsp)
pushq 0x68(%rsp)
callq 0x93005
addq $0x20, %rsp
subq $0x20, %rsp
movl %r15d, 0x10(%rsp)
movq 0x50(%rsp), %rbx
movq %rbx, 0x8(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq 0x40(%rsp), %r15
movq %r15, %rdx
movq 0x68(%rsp), %rcx
movq 0x78(%rsp), %r13
movq %r13, %r8
movss 0x3c(%rsp), %xmm0
callq 0x947c2
addq $0x20, %rsp
movq %rbp, %rdi
movq %r15, %rsi
movq 0x50(%rsp), %r12
movq %r12, %rdx
pushq $0x14
popq %rcx
pushq $0x64
popq %r8
movss 0x150c7f(%rip), %xmm0   # 0x1e7cbc
movss 0x150c7b(%rip), %xmm1   # 0x1e7cc0
movq %rbx, %r9
movss 0x3c(%rsp), %xmm2
pushq 0x10(%rsp)
movq %rbp, %r14
movq 0x30(%rsp), %rbp
pushq 0xa0(%rbp)
callq 0x974ef
popq %rax
popq %rcx
movq 0xa0(%rbp), %r9
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0x97ac1
leaq 0x1bc6cd(%rip), %rax    # 0x253758
movl 0x1c0(%rax), %eax
subq $0x8, %rsp
movq %r14, %rdi
movq %r15, %rsi
movq 0x50(%rsp), %rdx
movq %r13, %rcx
movl 0x78(%rsp), %r8d
movss 0x24(%rsp), %xmm0
movq %r12, %r9
movss 0x150bf7(%rip), %xmm1   # 0x1e7cb0
pushq %rax
pushq 0xa0(%rbp)
pushq %rbx
callq 0x97cf5
addq $0x20, %rsp
testb $0x1, 0x80(%rsp)
jne 0x9710d
movq 0x18(%r14), %rdi
movss 0x3c(%rsp), %xmm0
callq 0x7264d
movb $0x1, %al
movq %rax, 0x80(%rsp)
movq 0x10(%rsp), %r15
movl 0x40(%rsp), %r12d
movq %r14, %rbp
movq 0x60(%rsp), %rbx
movq 0x8(%rsp), %r14
pushq $0x5
popq %r13
jmp 0x96d6c
movq 0x60(%rsp), %rbx
testq %rbx, %rbx
jne 0x97129
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1bbeb0(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movl 0x40(%rbx), %ecx
leaq 0x1523e4(%rip), %rsi    # 0x1e9521
leaq 0x1523fa(%rip), %rdx    # 0x1e953e
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rdi
callq 0x969a7
movq %rbx, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220