// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_trio_long_tip_primary_complex(asg_t*, ma_ug_t*, asg_t*, ma_hit_t_alloc*, R_to_U*, unsigned int, float, unsigned int, hap_cov_t*, utg_trans_t*, unsigned int)
int asg_arc_cut_trio_long_tip_primary_complex(asg_t *g, ma_ug_t *ug, asg_t *read_sg, ma_hit_t_alloc* reverse_sources,
R_to_U* ruIndex, uint32_t min_edge_length, float drop_ratio, uint32_t stops_threshold, hap_cov_t *cov, utg_trans_t *o, uint32_t trio_flag)
{
    double startTime = Get_T();
    uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, convex, in, flag, operation;
    uint32_t return_flag, convex_i, k;
    long long ll, tmp, max_stop_nodeLen, max_stop_baseLen;

    buf_t b, b_0, b_1;
    memset(&b, 0, sizeof(buf_t));
    memset(&b_0, 0, sizeof(buf_t));
    memset(&b_1, 0, sizeof(buf_t));
    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t i;
        if(g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
        ///tip
        if (get_real_length(g, v, NULL) != 0) continue;
        if(get_real_length(g, v^1, NULL) != 1) continue;

        b.b.n = 0;   
        return_flag = get_unitig(g, ug, v^1, &convex, &ll, &tmp, &max_stop_nodeLen, 
        &max_stop_baseLen, 1, &b);

        if(return_flag != MUL_INPUT) continue;
        if(trio_check(ug, b.b.a, b.b.n, trio_flag)) continue;
        in = convex^1;
        get_real_length(g, convex, &convex);
        convex = convex^1;
        uint32_t n_convex = asg_arc_n(g, convex), convexLen = ll;
        asg_arc_t *a_convex = asg_arc_a(g, convex);
        for (i = 0; i < n_convex; i++)
        {
            if(a_convex[i].del) continue;
            if(a_convex[i].v == in) break;
        }
        convex_i = i;
        ///if(convex_i == n_convex) fprintf(stderr, "ERROR\n");


        for (i = 0; i < n_convex; i++)
        {
            if(a_convex[i].del) continue;
            if(i == convex_i) continue;

            return_flag = get_unitig(g, ug, a_convex[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, 
            &max_stop_baseLen, stops_threshold, NULL);
            
            if(convexLen < ll*drop_ratio && max_stop_nodeLen >= ll*MAX_STOP_RATE)
            {
                n_reduced++;
                operation = TRIM;
                flag = check_different_haps(g, ug, read_sg, a_convex[convex_i].v, a_convex[i].v,
                reverse_sources, &b_0, &b_1, ruIndex, cov->is_r_het, min_edge_length, stops_threshold);
                // #define UNAVAILABLE (uint32_t)-1
                // #define PLOID 0
                // #define NON_PLOID 1
                if(flag == NON_PLOID) operation = CUT;

                for (k = 0; k < b.b.n; k++)
                {
                    g->seq[b.b.a[k]>>1].c = ALTER_LABLE;
                }

                for (k = 0; k < b.b.n; k++)
                {
                    asg_seq_drop(g, b.b.a[k]>>1);
                }

                if(operation == CUT)
                {
                    for (k = 0; k < b.b.n; k++)
                    {
                        g->seq[b.b.a[k]>>1].c = CUT;
                    }
                }

                ///note: we need to remove b_0, insetad of b_1 here
                if(cov && operation != CUT)
                {
                    collect_trans_cov(__func__, &b_1, a_convex[i].ol, &b_0, a_convex[convex_i].ol, ug, read_sg, cov);
                } 

                if(o && operation != CUT)
                {
                    collect_trans_ovlp(__func__, &b_1, a_convex[i].ol, &b_0, a_convex[convex_i].ol, ug, o);
                }
                
                
                break;
            }
        }
    }


    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %d long tips\n", 
        __func__, n_reduced);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }

    asg_cleanup(g);
    asg_symm(g);
    free(b.b.a);
    free(b_0.b.a);
    free(b_1.b.a);

    return n_reduced;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movl %r9d, -0x44(%rbp)
movss %xmm0, -0x48(%rbp)
callq 0x2510
movsd %xmm0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x168(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x1d0(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x117aea
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x1175ec
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x1175f1
jmp 0x117adc
movq -0x20(%rbp), %rdi
movl -0x54(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
je 0x11760b
jmp 0x117adc
movq -0x20(%rbp), %rdi
movl -0x54(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
je 0x117628
jmp 0x117adc
movq $0x0, -0xc8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x54(%rbp), %edx
xorl $0x1, %edx
leaq -0x60(%rbp), %rcx
leaq -0x80(%rbp), %r8
leaq -0x88(%rbp), %r9
leaq -0x90(%rbp), %r11
leaq -0x98(%rbp), %r10
leaq -0x100(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x70(%rbp)
cmpl $0x3, -0x70(%rbp)
je 0x11768e
jmp 0x117adc
movq -0x28(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rax
movl %eax, %edx
movl 0x28(%rbp), %ecx
callq 0x167d40
cmpl $0x0, %eax
je 0x1176b4
jmp 0x117adc
movl -0x60(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rdi
movl -0x60(%rbp), %esi
leaq -0x60(%rbp), %rdx
callq 0x166b50
movl -0x60(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x60(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x1d8(%rbp)
movq -0x80(%rbp), %rax
movl %eax, -0x1dc(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x60(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1e8(%rbp)
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1d8(%rbp), %eax
jae 0x117788
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x117757
jmp 0x117777
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x64(%rbp), %eax
jne 0x117775
jmp 0x117788
jmp 0x117777
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x117728
movl -0x1d4(%rbp), %eax
movl %eax, -0x74(%rbp)
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1d8(%rbp), %eax
jae 0x117ada
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1177d1
jmp 0x117ac6
movl -0x1d4(%rbp), %eax
cmpl -0x74(%rbp), %eax
jne 0x1177e1
jmp 0x117ac6
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rax,%rcx,8), %edx
movl 0x10(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
leaq -0x98(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x90(%rbp), %rcx
movq %rcx, (%rax)
movq $0x0, 0x18(%rax)
leaq -0x60(%rbp), %rcx
leaq -0x80(%rbp), %r8
leaq -0x88(%rbp), %r9
callq 0x166c30
movl %eax, -0x70(%rbp)
movl -0x1dc(%rbp), %eax
cvtsi2ss %rax, %xmm1
cvtsi2ssq -0x80(%rbp), %xmm0
mulss -0x48(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x117ac4
cvtsi2sdq -0x90(%rbp), %xmm0
cvtsi2sdq -0x80(%rbp), %xmm1
movsd 0x26a99f(%rip), %xmm2   # 0x382210
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x117ac4
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl $0xa, -0x6c(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x1e8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %r8d
imulq $0x18, %r8, %r8
addq %r8, %rax
movl 0x8(%rax), %r8d
movq -0x38(%rbp), %r9
movq -0x40(%rbp), %rbx
movq 0x18(%rbp), %rax
movq 0xa0(%rax), %r11
movl -0x44(%rbp), %r10d
movl 0x10(%rbp), %eax
leaq -0x168(%rbp), %r15
leaq -0x1d0(%rbp), %r14
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movl %r10d, 0x20(%rsp)
movl %eax, 0x28(%rsp)
callq 0x1674d0
movl %eax, -0x68(%rbp)
cmpl $0x1, -0x68(%rbp)
jne 0x117921
movl $0xb, -0x6c(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x11795d
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xb8(%rbp), %rcx
movl -0x78(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x117928
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x117993
movq -0x20(%rbp), %rdi
movq -0xb8(%rbp), %rax
movl -0x78(%rbp), %ecx
movl (%rax,%rcx,4), %esi
shrl %esi
callq 0xf6800
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x117964
cmpl $0xb, -0x6c(%rbp)
jne 0x1179d7
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x1179d5
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xb8(%rbp), %rcx
movl -0x78(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0xb, 0x4(%rax,%rcx,8)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x1179a0
jmp 0x1179d7
cmpq $0x0, 0x18(%rbp)
je 0x117a51
cmpl $0xb, -0x6c(%rbp)
je 0x117a51
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x1e8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
movq -0x28(%rbp), %r9
movq -0x30(%rbp), %r10
movq 0x18(%rbp), %rax
leaq 0x2704b1(%rip), %rdi    # 0x387ee6
leaq -0x1d0(%rbp), %rsi
leaq -0x168(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xfab70
cmpq $0x0, 0x20(%rbp)
je 0x117ac2
cmpl $0xb, -0x6c(%rbp)
je 0x117ac2
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x1e8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
movq -0x28(%rbp), %r9
movq 0x20(%rbp), %rax
leaq 0x27043b(%rip), %rdi    # 0x387ee6
leaq -0x1d0(%rbp), %rsi
leaq -0x168(%rbp), %rcx
movq %rax, (%rsp)
callq 0x2721e0
jmp 0x117ada
jmp 0x117ac6
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x11779b
jmp 0x117adc
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1175ad
movq -0x20(%rbp), %rdi
callq 0xcdd10
movq -0x20(%rbp), %rdi
callq 0xce020
movq -0xb8(%rbp), %rdi
callq 0x2210
movq -0x120(%rbp), %rdi
callq 0x2210
movq -0x188(%rbp), %rdi
callq 0x2210
movl -0x5c(%rbp), %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)