// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movl %r9d, -0x3c(%rbp)
movss %xmm0, -0x40(%rbp)
callq 0x2510
movsd %xmm0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
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, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0x1174c1
movl $0x0, -0x1dc(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x1e0(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x4c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1e8(%rbp)
cmpl $0x2, -0x1e0(%rbp)
jb 0x116fdf
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x116fdf
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x116fe4
jmp 0x1174b3
movq -0x18(%rbp), %rdi
movl -0x4c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0x1dc(%rbp)
cmpl $0x2, -0x1dc(%rbp)
jae 0x117008
jmp 0x1174b3
movq $-0x1, -0x80(%rbp)
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x1d4(%rbp)
movl $0x0, -0x1dc(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1e0(%rbp), %eax
jae 0x117112
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
jne 0x1170fc
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq -0x58(%rbp), %rcx
leaq -0x78(%rbp), %r8
leaq -0x88(%rbp), %r9
leaq -0x90(%rbp), %r10
leaq -0x98(%rbp), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x68(%rbp)
cmpl $0x7, -0x68(%rbp)
jne 0x1170d0
jmp 0x1170fe
cmpl $0x5, -0x68(%rbp)
jne 0x1170df
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x1170fa
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x1d4(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x1170fc
jmp 0x1170fe
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x117032
cmpl $0x0, -0x6c(%rbp)
jne 0x11711d
jmp 0x1174b3
movl $0x0, -0x1d4(%rbp)
movl $0x0, -0x1dc(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1e0(%rbp), %eax
jae 0x1174b1
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
jne 0x11749b
movl -0x5c(%rbp), %eax
cmpl -0x1d4(%rbp), %eax
jne 0x117176
jmp 0x11749d
movq $0x0, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq -0x58(%rbp), %rcx
leaq -0x78(%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
cmpl $0x5, %eax
je 0x1171e9
jmp 0x11749d
cvtsi2ssq -0x78(%rbp), %xmm0
cvtsi2ssq -0x80(%rbp), %xmm1
mulss -0x40(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jb 0x117204
jmp 0x11749d
movq -0x20(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rax
movl %eax, %edx
movl 0x10(%rbp), %ecx
callq 0x167d40
cmpl $0x0, %eax
je 0x11722a
jmp 0x11749d
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl $0xa, -0x64(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x1e8(%rbp), %rax
movl -0x5c(%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 -0x30(%rbp), %r9
movq -0x38(%rbp), %r11
movq 0x18(%rbp), %rax
movq 0xa0(%rax), %r10
movl -0x3c(%rbp), %eax
leaq -0x168(%rbp), %r14
leaq -0x1d0(%rbp), %rbx
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
callq 0x1674d0
movl %eax, -0x60(%rbp)
cmpl $0x1, -0x60(%rbp)
jne 0x1172cb
movl $0xb, -0x64(%rbp)
movl $0x0, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x117316
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xb8(%rbp), %rcx
movl -0x1d8(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x1172d5
movl $0x0, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x11735b
movq -0x18(%rbp), %rdi
movq -0xb8(%rbp), %rax
movl -0x1d8(%rbp), %ecx
movl (%rax,%rcx,4), %esi
shrl %esi
callq 0xf6800
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x117320
cmpl $0xb, -0x64(%rbp)
jne 0x1173ae
movl $0x0, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x1173ac
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xb8(%rbp), %rcx
movl -0x1d8(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0xb, 0x4(%rax,%rcx,8)
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x11736b
jmp 0x1173ae
cmpq $0x0, 0x18(%rbp)
je 0x117428
cmpl $0xb, -0x64(%rbp)
je 0x117428
movq -0x1e8(%rbp), %rax
movl -0x5c(%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 -0x1d4(%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 -0x20(%rbp), %r9
movq -0x28(%rbp), %r10
movq 0x18(%rbp), %rax
leaq 0x270ab8(%rip), %rdi    # 0x387ec4
leaq -0x168(%rbp), %rsi
leaq -0x1d0(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xfab70
cmpq $0x0, 0x20(%rbp)
je 0x117499
cmpl $0xb, -0x64(%rbp)
je 0x117499
movq -0x1e8(%rbp), %rax
movl -0x5c(%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 -0x1d4(%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 -0x20(%rbp), %r9
movq 0x20(%rbp), %rax
leaq 0x270a42(%rip), %rdi    # 0x387ec4
leaq -0x168(%rbp), %rsi
leaq -0x1d0(%rbp), %rcx
movq %rax, (%rsp)
callq 0x2721e0
jmp 0x11749b
jmp 0x11749d
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x117131
jmp 0x1174b3
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x116f4f
movq -0x18(%rbp), %rdi
callq 0xcdd10
movq -0x18(%rbp), %rdi
callq 0xce020
movq -0xb8(%rbp), %rdi
callq 0x2210
movq -0x120(%rbp), %rdi
callq 0x2210
movq -0x188(%rbp), %rdi
callq 0x2210
movl -0x54(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)