// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movss %xmm1, -0x38(%rbp)
movss %xmm2, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq 0x18(%rbp), %rcx
movq -0x10(%rbp), %r8
movl 0x20(%rbp), %r9d
movl 0x28(%rbp), %eax
movl %eax, (%rsp)
callq 0x26e9b0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x2, -0x54(%rbp)
movq -0x8(%rbp), %rdi
movq 0x30(%rbp), %r8
movq -0x48(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x5, %ecx
movl $0x1, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x11c760
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %r9
movq -0x20(%rbp), %r11
movq 0x18(%rbp), %r10
movq 0x30(%rbp), %rax
movq 0xa0(%rax), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x2, 0x18(%rsp)
callq 0x1168e0
movq -0x50(%rbp), %rdi
callq 0x116380
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
je 0x11e847
jmp 0x11e5fa
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
je 0x11e803
jmp 0x11e60a
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
je 0x11e7b0
movq -0x50(%rbp), %rdi
callq 0x116380
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq 0x30(%rbp), %r8
movq -0x48(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x5, %ecx
movl $0x1, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x11c760
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x18(%rbp), %r8
movss -0x34(%rbp), %xmm0
movq 0x30(%rbp), %r10
movq -0x48(%rbp), %rax
movl $0x2, %r9d
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x116eb0
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x18(%rbp), %r9
movq 0x30(%rbp), %r10
movq -0x48(%rbp), %rax
movl $0x2, %r8d
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x117ee0
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x18(%rbp), %r8
movss -0x34(%rbp), %xmm0
movq 0x10(%rbp), %rax
movl %eax, %r11d
movq 0x30(%rbp), %r10
movq -0x48(%rbp), %rax
movl $0x2, %r9d
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
callq 0x117510
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x18(%rbp), %r9
movq 0x10(%rbp), %rax
movl %eax, %r11d
movq 0x30(%rbp), %r10
movq -0x48(%rbp), %rax
movl $0x2, %r8d
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
callq 0x118f40
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x10(%rbp), %rax
movl %eax, %r9d
movss -0x38(%rbp), %xmm0
movq 0x18(%rbp), %r11
movq -0x48(%rbp), %r10
movq 0x30(%rbp), %rax
movq 0xa0(%rax), %rax
movl $0x2, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x1195b0
movq -0x50(%rbp), %rdi
callq 0x116380
movq %rax, -0x68(%rbp)
jmp 0x11e60a
leaq 0x2a6041(%rip), %rax    # 0x3c47f8
cmpl $0x2, 0x1bc(%rax)
jle 0x11e7f1
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %r9
movq 0x18(%rbp), %r10
movq -0x48(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x5, %r8d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x273eb0
movq -0x50(%rbp), %rdi
callq 0x116380
movq %rax, -0x68(%rbp)
jmp 0x11e5fa
cmpl $0x2, -0x54(%rbp)
je 0x11e835
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movss -0x3c(%rbp), %xmm0
leaq 0x2a5fdb(%rip), %rax    # 0x3c47f8
movl 0x11c(%rax), %edx
movq -0x20(%rbp), %rcx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xddd20
movq -0x50(%rbp), %rdi
callq 0x116380
movq %rax, -0x68(%rbp)
jmp 0x11e5ea
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %r9
movq -0x20(%rbp), %r11
movq 0x18(%rbp), %r10
movq 0x30(%rbp), %rax
movq 0xa0(%rax), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x2, 0x18(%rsp)
callq 0x1168e0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq 0x18(%rbp), %r9
movq 0x30(%rbp), %rax
movq 0xa0(%rax), %rax
movss -0x3c(%rbp), %xmm2
movl $0x14, %ecx
movl $0x64, %r8d
movss 0x267fe2(%rip), %xmm0   # 0x3868a4
movss 0x2607a2(%rip), %xmm1   # 0x37f06c
movq %rax, (%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
callq 0x11cca0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x30(%rbp), %rax
movq 0xa0(%rax), %r9
callq 0x11d700
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movss -0x3c(%rbp), %xmm0
leaq 0x2a5ee5(%rip), %rax    # 0x3c47f8
movl 0x11c(%rax), %edx
movq -0x20(%rbp), %rcx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xddd20
cmpl $0x0, -0x54(%rbp)
jle 0x11e957
cmpl $0x2, -0x54(%rbp)
je 0x11e949
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movss -0x3c(%rbp), %xmm0
callq 0xde540
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x11e55e
movq -0x48(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)