// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: topo_ovlp_collect(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, ma_sub_t*, long long, float, long long, R_to_U*, float, float, int, int, hap_cov_t*)
utg_trans_t *topo_ovlp_collect(ma_ug_t *ug, asg_t *read_g, ma_hit_t_alloc* sources, 
ma_hit_t_alloc* reverse_sources, ma_sub_t* coverage_cut, long long tipsLen, float tip_drop_ratio, 
long long stops_threshold, R_to_U* ruIndex, float chimeric_rate, float drop_ratio, int max_hang, 
int min_ovlp, hap_cov_t *cov)
{
    utg_trans_t *o = init_utg_trans_t(ug, reverse_sources, coverage_cut, ruIndex, read_g, max_hang, min_ovlp);
    #define T_ROUND 2
    asg_t *g = ug->g;
    int round = T_ROUND;

    // print_debug_gfa(read_g, ug, coverage_cut, "debug_init", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len);

    redo:
    asg_pop_bubble_primary_trio(ug, NULL, (uint32_t)-1, DROP, cov, o, 1, NULL);
    cut_trio_tip_primary(g, ug, tipsLen, (uint32_t)-1, 0, read_g, reverse_sources, ruIndex, cov->is_r_het, 2);

    long long pre_cons = get_graph_statistic(g);
    long long cur_cons = 0;
    while(pre_cons != cur_cons)
    {
        while(pre_cons != cur_cons)
        {
            while(pre_cons != cur_cons)
            {    
                pre_cons = get_graph_statistic(g);
                asg_pop_bubble_primary_trio(ug, NULL, (uint32_t)-1, DROP, cov, o, 1, NULL);
                
                ///need consider tangles  
                asg_arc_cut_trio_long_tip_primary(g, ug, read_g, reverse_sources, ruIndex, 2, tip_drop_ratio, (uint32_t)-1, cov, o);              
                asg_arc_cut_trio_long_equal_tips_assembly(g, ug, read_g, reverse_sources, 2, ruIndex, (uint32_t)-1, cov, o);   
                asg_arc_cut_trio_long_tip_primary_complex(g, ug, read_g, reverse_sources, ruIndex, 2, tip_drop_ratio, stops_threshold, cov, o, (uint32_t)-1);   
                asg_arc_cut_trio_long_equal_tips_assembly_complex(g, ug, read_g, reverse_sources, 2, ruIndex, stops_threshold, cov, o, (uint32_t)-1);
                detect_chimeric_by_topo(g, ug, read_g, reverse_sources, 2, stops_threshold, chimeric_rate, ruIndex, o, cov->is_r_het);    
                
                cur_cons = get_graph_statistic(g);
            }    

            // if(asm_opt.polyploidy > 2) asg_arc_decompress(g, ug, read_g, reverse_sources, ruIndex, o);
            if(asm_opt.polyploidy > 2)
            {
                asg_arc_decompress_mul(g, ug, read_g, (uint32_t)-1, DROP, reverse_sources, ruIndex, o);
            }
            
            cur_cons = get_graph_statistic(g);
        }
        if(round != T_ROUND)
        {
            unitig_arc_del_short_diploid_by_length_topo(g, ug, drop_ratio, asm_opt.max_short_tip, 
            reverse_sources, 0, 1);
        }
        cur_cons = get_graph_statistic(g);
    }

    
    cut_trio_tip_primary(g, ug, tipsLen, (uint32_t)-1, 0, read_g, reverse_sources, ruIndex, cov->is_r_het, 2);
    
    resolve_tangles(ug, read_g, reverse_sources, 20, 100, 0.05, 0.2, ruIndex, cov->is_r_het, (uint32_t)-1, drop_ratio); 
    drop_semi_circle(ug, g, read_g, reverse_sources, ruIndex, cov->is_r_het); 
    // print_debug_gfa(read_g, ug, coverage_cut, "debug_clean_end", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len);
    unitig_arc_del_short_diploid_by_length_topo(g, ug, drop_ratio, asm_opt.max_short_tip, reverse_sources, 0, 1);    
    
    if(round > 0)
    {
        if(round != T_ROUND)
        {
            unitig_arc_del_short_diploid_by_length(ug->g, drop_ratio);        
        }
        round--;
        goto redo;
    }
    return o;    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movss %xmm2, 0x4(%rsp)
movss %xmm1, 0x34(%rsp)
movss %xmm0, 0x14(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, %rdx
movq %rcx, %r8
movq %rsi, %r10
movq %rdi, %rbx
movq 0x98(%rsp), %rcx
movl 0xa0(%rsp), %r9d
movl 0xa8(%rsp), %eax
subq $0x8, %rsp
movq %r8, 0x30(%rsp)
movq %r8, %rsi
movq %r10, 0x28(%rsp)
movq %r10, %r8
pushq %rax
callq 0x18e211
addq $0x10, %rsp
movq %rax, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x18(%rbx), %rbp
movl $0x2, %ecx
movq %rcx, 0x40(%rsp)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x5, %ecx
movq 0xb0(%rsp), %r13
movq %r13, %r8
movq 0x18(%rsp), %r9
pushq $0x0
pushq $0x1
callq 0xd23e2
addq $0x10, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq 0x20(%rsp), %r9
pushq $0x2
pushq 0xa0(%r13)
pushq 0xa8(%rsp)
pushq 0x40(%rsp)
callq 0xccb76
addq $0x20, %rsp
movq %rbp, %rdi
callq 0xcc8b8
testq %rax, %rax
je 0xd3edf
movq %rbp, %rdi
callq 0xcc8b8
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x5, %ecx
movq 0xb0(%rsp), %r8
movq 0x18(%rsp), %r13
movq %r13, %r9
pushq $0x0
pushq $0x1
callq 0xd23e2
addq $0x8, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x28(%rsp), %r12
movq %r12, %rdx
movq 0x30(%rsp), %r15
movq %r15, %rcx
movq 0xa0(%rsp), %r8
movl $0x2, %r9d
movss 0x1c(%rsp), %xmm0
pushq %r13
movq 0xc0(%rsp), %r14
pushq %r14
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
callq 0xcd19d
addq $0x18, %rsp
movl $0x2, %r8d
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq 0xa0(%rsp), %r9
pushq %r13
pushq %r14
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
callq 0xce4b8
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq 0x98(%rsp), %r8
movl $0x2, %r9d
movss 0x14(%rsp), %xmm0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
pushq %r13
pushq %r14
movq %rbp, %r14
movl 0xa8(%rsp), %ebp
pushq %rbp
callq 0xcd975
addq $0x20, %rsp
movl $0x2, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq 0x98(%rsp), %r9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
pushq %r13
movq 0xc0(%rsp), %rbp
pushq %rbp
movl 0xa8(%rsp), %eax
pushq %rax
callq 0xcf498
addq $0x18, %rsp
movl $0x2, %r8d
movq %r14, %rdi
movq %rbx, %rsi
movq 0x58(%rsp), %rbx
movq %r12, %rdx
movq %r15, %rcx
movl 0x98(%rsp), %r9d
movss 0x3c(%rsp), %xmm0
pushq 0xa0(%rbp)
pushq %r13
pushq 0xb0(%rsp)
callq 0xcfd37
addq $0x20, %rsp
movq %r14, %rbp
movq %r14, %rdi
callq 0xcc8b8
cmpq %rax, %rbx
jne 0xd3cd1
leaq 0x1b6905(%rip), %rax    # 0x28a758
cmpl $0x3, 0x1bc(%rax)
jl 0xd3e8d
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x5, %r8d
movq 0x28(%rsp), %r9
pushq 0x18(%rsp)
pushq 0xa0(%rsp)
callq 0x2392a
addq $0x10, %rsp
movq %rbp, %rdi
callq 0xcc8b8
cmpq %rax, %rbx
jne 0xd3cd1
cmpl $0x2, 0x40(%rsp)
je 0xd3ece
leaq 0x1b68ac(%rip), %rax    # 0x28a758
movl 0x11c(%rax), %edx
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movss 0x4(%rsp), %xmm0
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xab022
movq %rbp, %rdi
callq 0xcc8b8
cmpq %rax, %rbx
jne 0xd3cd1
movq %rbp, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq 0x20(%rsp), %r12
movq %r12, %r9
pushq $0x2
movq 0xb8(%rsp), %rax
pushq 0xa0(%rax)
movq 0xa8(%rsp), %r14
pushq %r14
movq 0x40(%rsp), %r15
pushq %r15
callq 0xccb76
addq $0x20, %rsp
movl $0x14, %ecx
movl $0x64, %r8d
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movss 0x155c7f(%rip), %xmm0   # 0x229bc4
movss 0x155c7b(%rip), %xmm1   # 0x229bc8
movq %r14, %r9
movss 0x4(%rsp), %xmm2
pushq $-0x1
movq 0xb8(%rsp), %r14
pushq 0xa0(%r14)
callq 0xd2794
addq $0x10, %rsp
movq 0xa0(%r14), %r9
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq 0x98(%rsp), %r8
callq 0xd2f0d
leaq 0x1b67c1(%rip), %rax    # 0x28a758
movl 0x11c(%rax), %edx
movq %rbp, %rdi
movq %rbx, %rsi
movss 0x4(%rsp), %xmm0
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xab022
xorl %ecx, %ecx
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rbx
testl %ebx, %ebx
jle 0xd3fe8
cmpl $0x2, %ebx
je 0xd3fe0
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movss 0x4(%rsp), %xmm0
callq 0xab54c
leal -0x1(%rbx), %ecx
movq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
testl %ebx, %ebx
jg 0xd3c55
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq