// File: /workspace/github_cpp_makefile_star2_O1/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 $0x218, %rsp            # imm = 0x218
movss %xmm0, 0x60(%rsp)
movq %r9, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xe0(%rsp)
movapd %xmm0, 0xf0(%rsp)
movapd %xmm0, 0x100(%rsp)
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x120(%rsp)
movq %rcx, 0x130(%rsp)
movapd %xmm0, 0x1b0(%rsp)
movapd %xmm0, 0x1c0(%rsp)
movapd %xmm0, 0x1d0(%rsp)
movapd %xmm0, 0x1e0(%rsp)
movapd %xmm0, 0x1f0(%rsp)
movapd %xmm0, 0x200(%rsp)
movq %rcx, 0x210(%rsp)
movapd %xmm0, 0x140(%rsp)
movapd %xmm0, 0x150(%rsp)
movapd %xmm0, 0x160(%rsp)
movapd %xmm0, 0x170(%rsp)
movapd %xmm0, 0x180(%rsp)
movapd %xmm0, 0x190(%rsp)
movq $0x0, 0x38(%rsp)
movq %rcx, 0x1a0(%rsp)
addl %eax, %eax
je 0xcd91b
movl 0x250(%rsp), %ecx
addl $-0x3, %ecx
movl %ecx, 0x64(%rsp)
movl %eax, %r12d
xorl %r13d, %r13d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq $0x0, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rdi
cmpl $0x2, %edi
jb 0xcd90f
movq 0x20(%rbx), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xcd90f
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xcd90f
movq %rdi, %rax
andq %r14, %rax
je 0xcd90f
movq 0x8(%rbx), %rcx
movq %rdi, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsi, 0x28(%rsp)
leaq (%rcx,%rdx,8), %r15
addq $0xc, %r15
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r15,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0xcd341
cmpl $0x2, %edx
jb 0xcd90f
movq %r13, 0x40(%rsp)
cmpl $0x1, %edi
adcl $0x0, %edi
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq $-0x1, %r12
movl $0x0, 0xc(%rsp)
leaq 0xb0(%rsp), %r13
movq %rdi, 0x10(%rsp)
cmpl $0x0, (%r15)
js 0xcd3fc
movl -0x4(%r15), %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
leaq 0x6c(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0xc0(%rsp), %r9
pushq $0x0
pushq $0x1
pushq %r13
leaq 0xd0(%rsp), %rax
pushq %rax
callq 0x10109f
addq $0x20, %rsp
cmpl $0x7, %eax
je 0xcd3f7
cmpl $0x5, %eax
movq 0x10(%rsp), %rdi
jne 0xcd3de
incl 0xc(%rsp)
movq 0x58(%rsp), %rax
cmpq %rax, %r12
movl 0x8(%rsp), %ecx
cmovll %ebp, %ecx
movl %ecx, 0x8(%rsp)
cmovleq %rax, %r12
jmp 0xcd3fc
movq 0x10(%rsp), %rdi
incq %rbp
addq $0x18, %r15
cmpq %rbp, %rdi
jne 0xcd38f
cmpl $0x0, 0xc(%rsp)
movq 0x40(%rsp), %r13
leaq 0xd0(%rsp), %rbp
je 0xcd90a
cvtsi2ss %r12, %xmm0
mulss 0x60(%rsp), %xmm0
movss %xmm0, 0x68(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0x98(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x28(%rsp), %r15
leaq (%r15,%rax,8), %rax
movq %rax, 0x48(%rsp)
xorl %ecx, %ecx
movq 0x30(%rsp), %r12
leaq (%rcx,%rcx,2), %rax
cmpl $0x0, 0xc(%r15,%rax,8)
js 0xcd8ed
cmpq 0x98(%rsp), %rcx
je 0xcd8ed
movq %rcx, 0x18(%rsp)
leaq (%r15,%rax,8), %rax
movq $0x0, 0x108(%rsp)
movq %rax, 0x50(%rsp)
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
leaq 0x6c(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0xc0(%rsp), %r9
pushq %rbp
pushq $0x1
leaq 0xc0(%rsp), %rax
pushq %rax
leaq 0xd0(%rsp), %rax
pushq %rax
callq 0x10109f
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0xcd597
xorps %xmm0, %xmm0
cvtsi2ssq 0x58(%rsp), %xmm0
ucomiss 0x68(%rsp), %xmm0
movq 0x10(%rsp), %rdi
jae 0xcd8e8
cmpl $-0x2, 0x64(%rsp)
jb 0xcd63a
movl 0x108(%rsp), %r13d
testq %r13, %r13
je 0xcd5a1
movq 0x118(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xa0(%rsp)
xorl %r12d, %r12d
xorl %edx, %edx
xorl %ebp, %ebp
xorl %esi, %esi
movl %edx, 0xc(%rsp)
movl %esi, 0x8(%rsp)
movq 0xa8(%rsp), %rax
movl (%rax,%r12,4), %eax
shrl %eax
leaq (%rax,%rax,4), %rax
movq 0xa0(%rsp), %rdx
leaq (%rdx,%rax,8), %r15
movq %r15, %rdi
movl 0x250(%rsp), %esi
leaq 0x74(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0xcc(%rsp), %r8
callq 0xcf440
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %edx
addl 0x74(%rsp), %esi
addl 0x70(%rsp), %ebp
addl 0x10(%r15), %edx
incq %r12
cmpq %r12, %r13
jne 0xcd532
jmp 0xcd5a7
movq 0x10(%rsp), %rdi
jmp 0xcd8e8
xorl %esi, %esi
xorl %ebp, %ebp
xorl %edx, %edx
movl %ebp, %eax
addl %esi, %eax
movq 0x30(%rsp), %r12
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r15
je 0xcd63a
movl %esi, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x157f58(%rip), %xmm1   # 0x225530
ucomisd %xmm0, %xmm1
jae 0xcd63a
testl %ebp, %ebp
sete %al
cmpl $0x14, %esi
setae %cl
testb %al, %cl
jne 0xcd8fb
cmpl $0x64, %edx
jb 0xcd60a
movl %edx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x1575c8(%rip), %xmm1   # 0x224bd0
jmp 0xcd630
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd 0x1575b7(%rip), %xmm1   # 0x224bd0
cmpl $0x32, %edx
jb 0xcd628
mulsd 0x157eca(%rip), %xmm1   # 0x2254f0
jmp 0xcd630
mulsd 0x157ea0(%rip), %xmm1   # 0x2254d0
ucomisd %xmm0, %xmm1
jbe 0xcd8fb
movq 0x48(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x50(%rsp), %rax
movl 0x8(%rax), %r8d
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %r9
pushq $0x1
pushq 0x98(%rsp)
movq 0x268(%rsp), %rax
pushq 0xa0(%rax)
pushq 0xa0(%rsp)
leaq 0x160(%rsp), %rax
pushq %rax
leaq 0x1d8(%rsp), %rax
pushq %rax
callq 0x1015d1
addq $0x30, %rsp
movl %eax, %r15d
movq 0x108(%rsp), %rax
testq %rax, %rax
je 0xcd6cb
movq 0x20(%rbx), %rcx
movq 0x118(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
shrl %edi
movb $0x1, 0x4(%rcx,%rdi,8)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0xcd6b7
movq 0x108(%rsp), %rcx
testq %rcx, %rcx
je 0xcd7dd
xorl %eax, %eax
movq 0x118(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movl %ecx, %edx
shrl %edx
movq 0x20(%rbx), %rsi
cmpb $0x1, 0x4(%rsi,%rdx,8)
jne 0xcd7ca
andl $-0x2, %ecx
xorl %edx, %edx
leaq (%rdx,%rcx), %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %r14, %rdi
je 0xcd7ba
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 0xcd7ae
leaq (%r8,%r11,8), %r12
movq 0x20(%rbx), %r13
movl 0x8(%r12), %r11d
movl %r11d, %ebp
shrl %ebp
cmpb $0x1, 0x4(%r13,%rbp,8)
je 0xcd7ae
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%r12)
xorq $0x1, %r11
movq 0x28(%rbx), %r10
movq (%r10,%r11,8), %r11
movq %r11, %r10
andq %r14, %r10
je 0xcd7ae
movq 0x8(%rbx), %r12
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%r12,%r11,8), %r11
addq $0xc, %r11
xorl %r12d, %r12d
movl -0x4(%r11,%r12), %ebp
xorl %esi, %ebp
cmpl $0x1, %ebp
jne 0xcd7a5
orb $-0x80, 0x3(%r11,%r12)
addq $0x18, %r12
cmpq %r12, %r10
jne 0xcd793
incq %r9
cmpq %rdi, %r9
jne 0xcd72c
leaq 0x1(%rdx), %rsi
testq %rdx, %rdx
movq %rsi, %rdx
je 0xcd701
incl %eax
movq 0x108(%rsp), %rcx
cmpq %rax, %rcx
ja 0xcd6de
testq %rcx, %rcx
je 0xcd80a
cmpl $0x1, %r15d
jne 0xcd80a
movq 0x20(%rbx), %rax
movq 0x118(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
shrl %edi
movb $0xb, 0x4(%rax,%rdi,8)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0xcd7f6
cmpq $0x0, 0x258(%rsp)
movq 0x30(%rsp), %r12
movq 0x40(%rsp), %r13
leaq 0xd0(%rsp), %rbp
je 0xcd875
cmpl $0x1, %r15d
je 0xcd875
movq 0x48(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x50(%rsp), %rcx
movl 0xc(%rcx), %r8d
andq %rax, %r8
leaq 0x1b0(%rsp), %rsi
leaq 0x140(%rsp), %rcx
movq 0x20(%rsp), %r9
pushq 0x258(%rsp)
pushq 0x80(%rsp)
callq 0xbd407
addq $0x10, %rsp
movq 0x38(%rsp), %rax
incl %eax
cmpq $0x0, 0x260(%rsp)
movq %rax, 0x38(%rsp)
je 0xcd8de
cmpl $0x1, %r15d
je 0xcd8de
movq 0x48(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x50(%rsp), %rcx
movl 0xc(%rcx), %r8d
andq %rax, %r8
subq $0x8, %rsp
leaq 0x15d830(%rip), %rdi    # 0x22b0e9
leaq 0x1b8(%rsp), %rsi
leaq 0x148(%rsp), %rcx
movq 0x28(%rsp), %r9
pushq 0x268(%rsp)
callq 0x1902f0
addq $0x10, %rsp
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %rcx
incq %rcx
cmpq %rdi, %rcx
jne 0xcd456
jmp 0xcd90f
movq 0x40(%rsp), %r13
leaq 0xd0(%rsp), %rbp
jmp 0xcd8e8
movq 0x30(%rsp), %r12
incq %r13
cmpq %r12, %r13
jne 0xcd2da
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq 0x118(%rsp), %rdi
callq 0x2220
movq 0x1f8(%rsp), %rdi
callq 0x2220
movq 0x188(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq