// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1d8, %rsp            # imm = 0x1D8
movss %xmm0, 0x44(%rsp)
movq %r9, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %r12d
addl %r12d, %r12d
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
andq $0x0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
leaq 0x170(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
andq $0x0, 0x60(%rax)
leaq 0x100(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
andq $0x0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
xorl %edx, %edx
movl $0x0, 0x1c(%rsp)
movq %r13, 0x10(%rsp)
movq %r12, 0x50(%rsp)
cmpq %r12, %rdx
je 0x9388a
movq 0x28(%rbx), %rax
movq (%rax,%rdx,8), %r14
testl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
je 0x93882
movq 0x20(%rbx), %rax
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x93882
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x93882
movq %rdx, %rsi
movq 0x8(%rbx), %r15
movq %rbx, %rdi
movq %rdx, 0x30(%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jb 0x9387d
movq %r14, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%r15,%rax), %rcx
movq %rcx, 0x8(%rsp)
movl %r14d, %r14d
leaq (%r15,%rax), %r12
addq $0xc, %r12
xorl %r15d, %r15d
pushq $-0x1
popq %rbp
movl $0x0, 0x20(%rsp)
pushq $-0x1
popq %r13
cmpq %r15, %r14
je 0x935eb
cmpl $0x0, (%r12)
js 0x935e2
movl -0x4(%r12), %edx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
leaq 0x4c(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x88(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x7, %eax
je 0x935e2
cmpl $0x5, %eax
jne 0x935d2
incl 0x20(%rsp)
movq 0x38(%rsp), %rax
cmpq %rax, %r13
cmovll %r15d, %ebp
cmovleq %rax, %r13
incq %r15
addq $0x18, %r12
jmp 0x9357a
cmpl $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rax
je 0x93878
cvtsi2ss %r13, %xmm0
mulss 0x44(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
movl %ebp, %ecx
movq %rcx, 0x20(%rsp)
imulq $0x18, %rcx, %rcx
addq %rax, %rcx
movq %rcx, 0x28(%rsp)
xorl %r12d, %r12d
leaq 0x90(%rsp), %r15
cmpq %r14, %r12
je 0x93878
imulq $0x18, %r12, %rbp
cmpl $0x0, 0xc(%rax,%rbp)
js 0x93849
cmpq 0x20(%rsp), %r12
je 0x93849
addq %rax, %rbp
andq $0x0, 0xc8(%rsp)
movl 0x8(%rbp), %edx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
leaq 0x4c(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x88(%rsp), %r9
pushq %r15
pushq $0x1
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0x93844
cvtsi2ssq 0x38(%rsp), %xmm0
ucomiss 0x48(%rsp), %xmm0
jae 0x93844
movq 0xd8(%rsp), %rsi
movl 0xc8(%rsp), %edx
movq 0x10(%rsp), %r13
movq %r13, %rdi
movl 0x210(%rsp), %ecx
callq 0xc3788
testl %eax, %eax
jne 0x93844
movq 0x28(%rsp), %rax
movl 0x8(%rax), %ecx
movl 0x8(%rbp), %r8d
movq %rbx, %rdi
movq %r13, %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %r9
pushq $0x1
pushq 0x78(%rsp)
movq 0x228(%rsp), %rax
pushq 0xa0(%rax)
pushq 0x80(%rsp)
leaq 0x120(%rsp), %rax
pushq %rax
leaq 0x198(%rsp), %rax
pushq %rax
callq 0xc3307
addq $0x30, %rsp
movl %eax, %r13d
movq 0xc8(%rsp), %rax
movq 0xd8(%rsp), %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x9375b
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x93742
incl 0x1c(%rsp)
xorl %ecx, %ecx
movl %ecx, %r15d
cmpq %r15, %rax
jbe 0x9378d
movq 0xd8(%rsp), %rax
movl (%rax,%r15,4), %esi
shrl %esi
movq %rbx, %rdi
callq 0x80870
leal 0x1(%r15), %ecx
movq 0xc8(%rsp), %rax
jmp 0x93761
cmpl $0x1, %r13d
leaq 0x90(%rsp), %r15
je 0x93851
cmpq $0x0, 0x218(%rsp)
je 0x937ee
cmpl $0x1, %r13d
je 0x937ee
movq 0x28(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movl 0xc(%rbp), %r8d
andq %rax, %r8
leaq 0x170(%rsp), %rsi
leaq 0x100(%rsp), %rcx
movq 0x10(%rsp), %r9
pushq 0x218(%rsp)
pushq 0x60(%rsp)
callq 0x82b96
popq %rax
popq %rcx
cmpq $0x0, 0x220(%rsp)
je 0x93844
cmpl $0x1, %r13d
je 0x93844
movq 0x28(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movl 0xc(%rbp), %r8d
andq %rax, %r8
subq $0x8, %rsp
leaq 0x1559b8(%rip), %rdi    # 0x1e91d9
leaq 0x178(%rsp), %rsi
leaq 0x108(%rsp), %rcx
movq 0x18(%rsp), %r9
pushq 0x228(%rsp)
callq 0x151bf8
popq %rax
popq %rcx
movq 0x8(%rsp), %rax
incq %r12
jmp 0x9362a
xorl %edx, %edx
movq 0xd8(%rsp), %rcx
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x9379f
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0xb, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x9385b
movq 0x50(%rsp), %r12
movq 0x30(%rsp), %rdx
incq %rdx
jmp 0x934ef
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0x1b8(%rsp), %rdi
callq 0x2220
movq 0x148(%rsp), %rdi
callq 0x2220
movl 0x1c(%rsp), %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq