// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movss %xmm0, 0x44(%rsp)
movq %r9, 0x68(%rsp)
movq %r8, %r12
movq %rcx, 0x60(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
andq $0x0, 0x60(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x50(%rax)
leaq 0x160(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x50(%rax)
andq $0x0, 0x60(%rax)
leaq 0xf0(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
andq $0x0, 0x60(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x50(%rax)
movq $0x0, 0x28(%rsp)
xorl %r14d, %r14d
cmpl %r15d, %r14d
je 0x93d64
movq 0x20(%rbx), %rax
movl %r14d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x93b9e
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x93b9e
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
jne 0x93b9e
movl %r14d, %r13d
xorl $0x1, %r13d
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x93b9e
andq $0x0, 0xb8(%rsp)
movq %rbx, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
movl %r13d, %edx
leaq 0xc(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x78(%rsp), %r9
leaq 0x80(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x3, %eax
jne 0x93b9e
movq 0xc8(%rsp), %rsi
movl 0xb8(%rsp), %edx
movq %rbp, %rdi
movl 0x218(%rsp), %ecx
callq 0xc3788
testl %eax, %eax
jne 0x93b9e
movl 0xc(%rsp), %r13d
movq %rbx, %rdi
movl %r13d, %esi
leaq 0xc(%rsp), %rdx
callq 0xc2e7a
movl 0xc(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0xc(%rsp)
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rcx
movq (%rcx,%rax,8), %rsi
movl 0x38(%rsp), %eax
movl %esi, %ecx
shrq $0x20, %rsi
imulq $0x18, %rsi, %rsi
leaq (%rdx,%rsi), %rdi
movq %rdi, 0x58(%rsp)
leaq (%rdx,%rsi), %r9
addq $0xc, %r9
movq %r9, %rsi
xorl %edx, %edx
movq %r12, 0x20(%rsp)
cmpq %rdx, %rcx
je 0x93ae7
cmpl $0x0, (%rsi)
js 0x93ade
movl -0x4(%rsi), %edi
xorl %r13d, %edi
cmpl $0x1, %edi
je 0x93aea
incq %rdx
addq $0x18, %rsi
jmp 0x93ac9
movq %rcx, %rdx
movl %eax, %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, 0x18(%rsp)
imulq $0x18, %rcx, %r12
imulq $0x18, %rdx, %rbp
xorl %r13d, %r13d
movq %r9, 0x30(%rsp)
cmpq %r13, %r12
je 0x93b99
cmpq %r13, %rbp
je 0x93b90
cmpl $0x0, (%r9,%r13)
js 0x93b90
movl -0x4(%r9,%r13), %edx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x78(%rsp), %r9
pushq $0x0
movl 0x208(%rsp), %eax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
callq 0xc2ebf
movq 0x50(%rsp), %r9
addq $0x20, %rsp
movq 0x38(%rsp), %rax
cvtsi2ss %rax, %xmm0
mulss 0x44(%rsp), %xmm0
ucomiss 0x18(%rsp), %xmm0
jbe 0x93b90
cvtsi2sdq 0x50(%rsp), %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x14f94e(%rip), %xmm1   # 0x1e34d8
ucomisd %xmm1, %xmm0
jae 0x93ba6
addq $0x18, %r13
jmp 0x93b07
movq 0x20(%rsp), %r12
incl %r14d
jmp 0x939a2
movq 0x58(%rsp), %rcx
leaq (%rcx,%rbp), %rax
movq %rax, 0x18(%rsp)
movl 0x8(%rcx,%rbp), %ecx
movl -0x4(%r9,%r13), %r8d
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x60(%rsp), %r9
movl 0x200(%rsp), %eax
pushq %rax
pushq 0x70(%rsp)
movq 0x218(%rsp), %rax
pushq 0xa0(%rax)
movq 0x38(%rsp), %r12
pushq %r12
leaq 0x110(%rsp), %rax
pushq %rax
leaq 0x188(%rsp), %rax
pushq %rax
callq 0xc3307
addq $0x30, %rsp
movl %eax, %ebp
movq 0xb8(%rsp), %rax
movq 0xc8(%rsp), %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x93c38
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x93c1f
movq 0x28(%rsp), %rcx
incl %ecx
movq %rcx, 0x28(%rsp)
xorl %ecx, %ecx
movl %ecx, %r12d
cmpq %r12, %rax
jbe 0x93c73
movq 0xc8(%rsp), %rax
movl (%rax,%r12,4), %esi
shrl %esi
movq %rbx, %rdi
callq 0x80870
leal 0x1(%r12), %ecx
movq 0xb8(%rsp), %rax
jmp 0x93c46
cmpl $0x1, %ebp
movq 0x20(%rsp), %r12
je 0x93d3d
cmpq $0x0, 0x208(%rsp)
je 0x93cd5
cmpl $0x1, %ebp
je 0x93cd5
movq 0x30(%rsp), %rax
movl (%rax,%r13), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x18(%rsp), %rcx
movl 0xc(%rcx), %r8d
andq %rax, %r8
leaq 0xf0(%rsp), %rsi
leaq 0x160(%rsp), %rcx
movq 0x10(%rsp), %r9
pushq 0x208(%rsp)
pushq 0x50(%rsp)
callq 0x82b96
popq %rax
popq %rcx
cmpq $0x0, 0x210(%rsp)
movq 0x10(%rsp), %r9
je 0x93b9e
cmpl $0x1, %ebp
je 0x93b9e
movq 0x30(%rsp), %rax
movl (%rax,%r13), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdx
movq 0x18(%rsp), %rcx
movl 0xc(%rcx), %r8d
andq %rax, %r8
subq $0x8, %rsp
leaq 0x1554e1(%rip), %rdi    # 0x1e91fb
leaq 0xf8(%rsp), %rsi
leaq 0x168(%rsp), %rcx
pushq 0x218(%rsp)
callq 0x151bf8
popq %rax
popq %rcx
jmp 0x93b9e
xorl %edx, %edx
movq 0xc8(%rsp), %rcx
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x93c81
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0xb, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x93d47
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0x1a8(%rsp), %rdi
callq 0x2220
movq 0x138(%rsp), %rdi
callq 0x2220
movq 0x28(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq