// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_trio_long_tip_primary(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*)
int asg_arc_cut_trio_long_tip_primary(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 trio_flag, hap_cov_t *cov, utg_trans_t *o)
{
    double startTime = Get_T();
    ///the reason is that each read has two direction (query->target, target->query)
    uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, convex, v_maxLen_i = (uint32_t)-1, flag, operation;
    uint32_t return_flag, n_tips;
    long long ll, v_maxLen, 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, k, n_arc = 0, nv = asg_arc_n(g, v);
        asg_arc_t *av = asg_arc_a(g, v);
        ///some node could be deleted
        if (nv < 2 || g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
        n_arc = get_real_length(g, v, NULL);
        if (n_arc < 2) continue;

        v_maxLen = -1;
        v_maxLen_i = (uint32_t)-1;
        n_tips = 0;

        for (i = 0, n_arc = 0; i < nv; i++)
        {
            if (!av[i].del)
            { 
                return_flag = get_unitig(g, ug, av[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, 
                &max_stop_baseLen, 1, NULL);

                if(return_flag==LOOP) continue;
                if(return_flag==END_TIPS) n_tips++;
                
                if(v_maxLen < ll)
                {
                    v_maxLen = ll;
                    v_maxLen_i = i;
                }
            }
        }

        if(n_tips==0) continue;

        for (i = 0, n_arc = 0; i < nv; i++)
        {
            if (!av[i].del)
            {
                ///skip the longest way
                if(v_maxLen_i == i) continue;

                b.b.n = 0;
                if(get_unitig(g, ug, av[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, 
                &max_stop_baseLen, 1, &b)!=END_TIPS)
                {
                    continue;
                } 

                if(ll >= (v_maxLen*drop_ratio)) continue;
                if(trio_check(ug, b.b.a, b.b.n, trio_flag)) continue;
                n_reduced++;

                operation = TRIM;
                flag = check_different_haps(g, ug, read_sg, av[v_maxLen_i].v, av[i].v, reverse_sources, 
                &b_0, &b_1, ruIndex, cov->is_r_het, min_edge_length, 1);
                // #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;
                    }
                }

                if(cov && operation != CUT)
                {
                    collect_trans_cov(__func__, &b_0, av[v_maxLen_i].ol, &b_1, av[i].ol, ug, read_sg, cov);
                } 

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


    asg_cleanup(g);
    asg_symm(g);
    free(b.b.a);
    free(b_0.b.a);
    free(b_1.b.a);
    
    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);
    }

    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movss %xmm0, 0x50(%rsp)
movq %r9, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0x110(%rsp)
movq %rcx, 0x120(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1d0(%rsp)
movaps %xmm0, 0x1e0(%rsp)
movaps %xmm0, 0x1f0(%rsp)
movq %rcx, 0x200(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x170(%rsp)
movaps %xmm0, 0x180(%rsp)
movq $0x0, 0x20(%rsp)
movq %rcx, 0x190(%rsp)
addl %eax, %eax
je 0xcebd0
movl 0x240(%rsp), %ecx
addl $-0x3, %ecx
movl %ecx, 0x54(%rsp)
movl %eax, %eax
movq %rax, 0x90(%rsp)
xorl %ebp, %ebp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq $0x0, 0x20(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xcebbf
movq 0x20(%rbx), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xcebbf
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xcebbf
movq %rax, %r15
andq %r14, %r15
je 0xcebbf
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsi, 0x40(%rsp)
leaq (%rcx,%rdx,8), %r12
addq $0xc, %r12
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%r12,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xce647
cmpl $0x2, %esi
jb 0xcebbf
movq %rbp, 0x28(%rsp)
movl %eax, %eax
movq %rax, 0x98(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq $-0x1, %r13
movl $0x0, 0xc(%rsp)
cmpl $0x0, (%r12)
js 0xce6f3
movl -0x4(%r12), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x5c(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0xb0(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0xb0(%rsp), %rax
pushq %rax
leaq 0xc0(%rsp), %rax
pushq %rax
callq 0x1053a7
addq $0x20, %rsp
cmpl $0x7, %eax
je 0xce6f3
cmpl $0x5, %eax
jne 0xce6de
incl 0xc(%rsp)
movq 0x48(%rsp), %rax
cmpq %rax, %r13
movl (%rsp), %ecx
cmovll %ebp, %ecx
movl %ecx, (%rsp)
cmovleq %rax, %r13
incq %rbp
addq $0x18, %r12
cmpq %rbp, %r15
jne 0xce68b
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rbp
je 0xcebbf
xorps %xmm0, %xmm0
cvtsi2ss %r13, %xmm0
mulss 0x50(%rsp), %xmm0
movss %xmm0, 0x58(%rsp)
movl (%rsp), %eax
movq %rax, 0x88(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rax
movq 0x40(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
js 0xcebae
cmpq 0x88(%rsp), %rdx
je 0xcebae
movq %rdx, 0x10(%rsp)
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq $0x0, 0xf8(%rsp)
movq %rax, 0x38(%rsp)
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x5c(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0xb0(%rsp), %r9
leaq 0xc0(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0xb0(%rsp), %rax
pushq %rax
leaq 0xc0(%rsp), %rax
pushq %rax
callq 0x1053a7
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0xceba9
xorps %xmm0, %xmm0
cvtsi2ssq 0x48(%rsp), %xmm0
ucomiss 0x58(%rsp), %xmm0
jae 0xceba9
cmpl $-0x2, 0x54(%rsp)
movq 0x18(%rsp), %rsi
jb 0xce920
movl 0xf8(%rsp), %ebp
testq %rbp, %rbp
je 0xce886
movq 0x108(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%rsi), %rax
xorl %r15d, %r15d
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %edi, 0xc(%rsp)
movq (%rsp), %rcx
movl (%rcx,%r15,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rdi
movl 0x240(%rsp), %esi
leaq 0x64(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0xbc(%rsp), %r8
callq 0xd0648
movl 0xc(%rsp), %edi
movq 0x18(%rsp), %rsi
addl 0x64(%rsp), %r12d
addl 0x60(%rsp), %r13d
movq 0x10(%rsi), %rax
movq (%rsp), %rcx
movl (%rcx,%r15,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
addl 0x10(%rax,%rcx,8), %edi
incq %r15
cmpq %r15, %rbp
jne 0xce81f
jmp 0xce88e
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %edi, %edi
movl %r13d, %eax
addl %r12d, %eax
je 0xce91b
movl %r12d, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x162d79(%rip), %xmm1   # 0x231630
ucomisd %xmm0, %xmm1
jae 0xce91b
testl %r13d, %r13d
sete %al
cmpl $0x14, %r12d
setae %cl
testb %al, %cl
jne 0xceba4
cmpl $0x64, %edi
jb 0xce8eb
movl %edi, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x162327(%rip), %xmm1   # 0x230c10
jmp 0xce911
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
mulsd 0x162316(%rip), %xmm1   # 0x230c10
cmpl $0x32, %edi
jb 0xce909
mulsd 0x162ce9(%rip), %xmm1   # 0x2315f0
jmp 0xce911
mulsd 0x162cbf(%rip), %xmm1   # 0x2315d0
ucomisd %xmm0, %xmm1
jbe 0xceba4
movq 0x28(%rsp), %rbp
movq 0x30(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x38(%rsp), %rax
movl 0x8(%rax), %r8d
movq %rbx, %rdi
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %r9
pushq $0x1
pushq 0x88(%rsp)
movq 0x258(%rsp), %rax
pushq 0xa0(%rax)
pushq 0x90(%rsp)
leaq 0x150(%rsp), %rax
pushq %rax
leaq 0x1c8(%rsp), %rax
pushq %rax
callq 0x105913
addq $0x30, %rsp
movl %eax, (%rsp)
movq 0xf8(%rsp), %rdx
testq %rdx, %rdx
je 0xceae3
movq 0x20(%rbx), %rax
movq 0x108(%rsp), %rcx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
shrl %edi
movb $0x1, 0x4(%rax,%rdi,8)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rdx
ja 0xce999
xorl %eax, %eax
movq 0x108(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movl %ecx, %esi
shrl %esi
movq 0x20(%rbx), %rdi
cmpb $0x1, 0x4(%rdi,%rsi,8)
jne 0xceaab
andl $-0x2, %ecx
movb $0x1, %dl
xorl %esi, %esi
orq %rcx, %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %r14, %rdi
je 0xcea8b
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%rbx), %r8
xorl %r9d, %r9d
leaq (%r9,%r9,2), %r11
movl 0xc(%r8,%r11,8), %r10d
testl %r10d, %r10d
js 0xcea7f
leaq (%r8,%r11,8), %r15
movq 0x20(%rbx), %r12
movl 0x8(%r15), %r11d
movl %r11d, %r13d
shrl %r13d
cmpb $0x1, 0x4(%r12,%r13,8)
je 0xcea7f
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%r15)
xorq $0x1, %r11
movq 0x28(%rbx), %r10
movq (%r10,%r11,8), %r11
movq %r11, %r10
andq %r14, %r10
je 0xcea7f
movq 0x8(%rbx), %r15
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%r15,%r11,8), %r11
addq $0xc, %r11
xorl %r15d, %r15d
movl -0x4(%r11,%r15), %ebp
xorl %esi, %ebp
cmpl $0x1, %ebp
jne 0xcea76
orb $-0x80, 0x3(%r11,%r15)
addq $0x18, %r15
cmpq %r15, %r10
jne 0xcea64
incq %r9
cmpq %rdi, %r9
jne 0xce9fe
movl $0x1, %esi
testb $0x1, %dl
movl $0x0, %edx
jne 0xce9d4
movq 0xf8(%rsp), %rdx
movq 0x28(%rsp), %rbp
incl %eax
cmpq %rax, %rdx
ja 0xce9af
testq %rdx, %rdx
je 0xceae3
cmpl $0x1, (%rsp)
jne 0xceae3
movq 0x20(%rbx), %rax
movq 0x108(%rsp), %rcx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
shrl %edi
movb $0xb, 0x4(%rax,%rdi,8)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rdx
ja 0xceacf
cmpq $0x0, 0x248(%rsp)
je 0xceb39
cmpl $0x1, (%rsp)
je 0xceb39
movq 0x30(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x38(%rsp), %rcx
movl 0xc(%rcx), %r8d
andq %rax, %r8
leaq 0x1a0(%rsp), %rsi
leaq 0x130(%rsp), %rcx
movq 0x18(%rsp), %r9
pushq 0x248(%rsp)
pushq 0x70(%rsp)
callq 0xbe511
addq $0x10, %rsp
movq 0x20(%rsp), %rax
incl %eax
cmpq $0x0, 0x250(%rsp)
movq %rax, 0x20(%rsp)
je 0xceba9
cmpl $0x1, (%rsp)
je 0xceba9
movq 0x30(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x38(%rsp), %rcx
movl 0xc(%rcx), %r8d
andq %rax, %r8
subq $0x8, %rsp
leaq 0x16875c(%rip), %rdi    # 0x2372d9
leaq 0x1a8(%rsp), %rsi
leaq 0x138(%rsp), %rcx
movq 0x20(%rsp), %r9
pushq 0x258(%rsp)
callq 0x1990ae
addq $0x10, %rsp
jmp 0xceba9
movq 0x28(%rsp), %rbp
movq 0x10(%rsp), %rdx
incq %rdx
cmpq 0x98(%rsp), %rdx
jne 0xce742
incq %rbp
cmpq 0x90(%rsp), %rbp
jne 0xce5db
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
movq 0x108(%rsp), %rdi
callq 0x2220
movq 0x1e8(%rsp), %rdi
callq 0x2220
movq 0x178(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq