// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_long_tip_primary_complex(asg_t*, float, unsigned int)
int asg_arc_cut_long_tip_primary_complex(asg_t *g, float drop_ratio, uint32_t stops_threshold)
{
    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, flag;
    long long ll, max_stopLen;

    buf_t b;
    memset(&b, 0, sizeof(buf_t));

    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t i;
        ///some node could be deleted
        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;      
        flag = detect_single_path_with_dels(g, v^1, &convex, &ll, NULL);
        if(flag != TWO_INPUT && flag != MUL_INPUT) continue;
        convex = convex^1;ll--;
        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)
            {
                ///if stops_threshold = 1, 
                ///detect_single_path_with_dels_n_stops() is detect_single_path_with_dels()
                detect_single_path_with_dels_n_stops(g, a_convex[i].v, &convex, &ll, &max_stopLen,
                NULL, stops_threshold);

                if(convex == v) continue;

                if(ll*drop_ratio > convexLen && max_stopLen*2>ll)
                {

                    b.b.n = 0; 
                    flag = detect_single_path_with_dels(g, v^1, &convex, &ll, &b);
                    if(b.b.n < 2) break;
                    b.b.n--;


                    n_reduced++;
                    uint64_t k;

                    for (k = 0; k < b.b.n; k++)
                    {
                        g->seq[b.b.a[k]].c = ALTER_LABLE;
                    }

                    for (k = 0; k < b.b.n; k++)
                    {
                        asg_seq_drop(g, b.b.a[k]);
                    }
                    break;
                }
            }
        }
    }


    asg_cleanup(g);
    asg_symm(g);
    free(b.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 $0xa8, %rsp
movl %esi, 0x2c(%rsp)
movss %xmm0, 0x28(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0xa0(%rsp)
addl %eax, %eax
je 0xbbdf0
movl %eax, %r15d
xorl %r14d, %r14d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq 0x20(%rbx), %rax
movl %r14d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xbbc6a
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xbbc6a
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, %rdx
andq %r12, %rdx
je 0xbbb1b
movq 0x8(%rbx), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xbbafc
testl %edi, %edi
jne 0xbbc6a
movl %r14d, %r13d
xorl $0x1, %r13d
movq (%rax,%r13,8), %rax
movq %rax, %rcx
andq %r12, %rcx
je 0xbbc6a
movq 0x8(%rbx), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xbbb52
cmpl $0x1, %esi
jne 0xbbc6a
movq %rbx, %rdi
movl %r13d, %esi
leaq 0xc(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0xaa2c3
andl $0x5, %eax
cmpl $0x1, %eax
jne 0xbbc6a
movl 0xc(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rcx
decq %rcx
movq %rcx, 0x10(%rsp)
movq 0x28(%rbx), %rdx
movq (%rdx,%rax,8), %rax
testl %eax, %eax
je 0xbbc6a
movq %r13, 0x30(%rsp)
movq %r15, 0x18(%rsp)
movl %ebp, 0x8(%rsp)
movq 0x8(%rbx), %rdx
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm0
movss %xmm0, 0x24(%rsp)
movq %rax, %rcx
shrq $0x20, %rcx
movl %eax, %eax
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rbp
addq $0xc, %rbp
shlq $0x3, %rax
leaq (%rax,%rax,2), %r13
xorl %r15d, %r15d
cmpl $0x0, (%rbp,%r15)
js 0xbbc58
movl -0x4(%rbp,%r15), %esi
movl 0x2c(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0xaa4f8
movl 0xc(%rsp), %eax
cmpq %rax, %r14
je 0xbbc58
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
mulss 0x28(%rsp), %xmm0
ucomiss 0x24(%rsp), %xmm0
jbe 0xbbc58
movq 0x38(%rsp), %rcx
addq %rcx, %rcx
cmpq %rax, %rcx
jg 0xbbc7b
addq $0x18, %r15
cmpq %r15, %r13
jne 0xbbbfa
movl 0x8(%rsp), %ebp
movq 0x18(%rsp), %r15
incq %r14
cmpq %r15, %r14
jne 0xbbaae
jmp 0xbbdf2
movq $0x0, 0x78(%rsp)
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0xaa2c3
movq 0x78(%rsp), %rdx
cmpq $0x2, %rdx
movq 0x18(%rsp), %r15
jae 0xbbcb6
movl 0x8(%rsp), %ebp
jmp 0xbbc6a
decq %rdx
movq %rdx, 0x78(%rsp)
movl 0x8(%rsp), %ebp
incl %ebp
movq 0x20(%rbx), %rax
movq 0x88(%rsp), %rcx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
movb $0x1, 0x4(%rax,%rdi,8)
incq %rsi
cmpq %rsi, %rdx
jne 0xbbcd2
xorl %eax, %eax
movl %ebp, 0x8(%rsp)
movq 0x88(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x20(%rbx), %rsi
cmpb $0x1, 0x4(%rsi,%rcx,8)
jne 0xbbddf
addl %ecx, %ecx
movb $0x1, %dl
xorl %esi, %esi
orq %rcx, %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %r12, %rdi
je 0xbbdbe
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 0xbbdb2
leaq (%r8,%r11,8), %r15
movq 0x20(%rbx), %r13
movl 0x8(%r15), %r11d
movl %r11d, %ebp
shrl %ebp
cmpb $0x1, 0x4(%r13,%rbp,8)
je 0xbbdb2
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 %r12, %r10
je 0xbbdb2
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 0xbbda9
orb $-0x80, 0x3(%r11,%r15)
addq $0x18, %r15
cmpq %r15, %r10
jne 0xbbd97
incq %r9
cmpq %rdi, %r9
jne 0xbbd32
movl $0x1, %esi
testb $0x1, %dl
movl $0x0, %edx
jne 0xbbd08
movq 0x78(%rsp), %rdx
movl 0x8(%rsp), %ebp
movq 0x18(%rsp), %r15
incq %rax
cmpq %rdx, %rax
jb 0xbbce8
jmp 0xbbc6a
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
movq 0x88(%rsp), %rdi
callq 0x2220
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq