// File: /workspace/github_cpp_makefile_star2_O1/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, 0x34(%rsp)
movss %xmm0, 0x30(%rsp)
movq %rdi, %rbx
callq 0x300fc
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 0xbad69
movl %eax, %r9d
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %r9, 0x18(%rsp)
movq 0x20(%rbx), %rax
movl %r14d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xbabe4
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xbabe4
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %rdx
je 0xbaa9a
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 0xbaa7b
testl %edi, %edi
jne 0xbabe4
movl %r14d, %r15d
xorl $0x1, %r15d
movq (%rax,%r15,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xbabdf
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 0xbaad6
cmpl $0x1, %esi
jne 0xbabdf
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
callq 0xa9219
andl $0x5, %eax
cmpl $0x1, %eax
jne 0xbabdf
movl 0x14(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rcx
decq %rcx
movq %rcx, 0x20(%rsp)
movq 0x28(%rbx), %rdx
movq (%rdx,%rax,8), %rax
testl %eax, %eax
je 0xbabdf
movl %ebp, 0x10(%rsp)
movq 0x8(%rbx), %rdx
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm0
movss %xmm0, 0x2c(%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 %r12d, %r12d
cmpl $0x0, (%rbp,%r12)
js 0xbabd2
movl -0x4(%rbp,%r12), %esi
movl 0x34(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0xa9452
movl 0x14(%rsp), %eax
cmpq %rax, %r14
je 0xbabd2
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
mulss 0x30(%rsp), %xmm0
ucomiss 0x2c(%rsp), %xmm0
jbe 0xbabd2
movq 0x38(%rsp), %rcx
addq %rcx, %rcx
cmpq %rax, %rcx
jg 0xbabf5
addq $0x18, %r12
cmpq %r12, %r13
jne 0xbab74
movl 0x10(%rsp), %ebp
movq 0x18(%rsp), %r9
incq %r14
cmpq %r9, %r14
jne 0xbaa28
jmp 0xbad6b
movq $0x0, 0x78(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0xa9219
movq 0x78(%rsp), %rax
cmpq $0x2, %rax
jb 0xbabdb
decq %rax
movq %rax, 0x78(%rsp)
movl 0x10(%rsp), %ebp
movq 0x18(%rsp), %r9
je 0xbac54
movq 0x20(%rbx), %rcx
movq 0x88(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movb $0x1, 0x4(%rcx,%rdi,8)
incq %rsi
cmpq %rsi, %rax
jne 0xbac44
incl %ebp
cmpq $0x0, 0x78(%rsp)
je 0xbabe4
movl %ebp, 0x10(%rsp)
xorl %eax, %eax
movq 0x88(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x20(%rbx), %rdx
cmpb $0x1, 0x4(%rdx,%rcx,8)
jne 0xbad56
addl %ecx, %ecx
xorl %edx, %edx
leaq (%rdx,%rcx), %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %r9, %rdi
je 0xbad46
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 0xbad3a
leaq (%r8,%r11,8), %r15
movq 0x20(%rbx), %r12
movl 0x8(%r15), %r11d
movl %r11d, %r13d
shrl %r13d
cmpb $0x1, 0x4(%r12,%r13,8)
je 0xbad3a
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%r15)
xorq $0x1, %r11
movq 0x28(%rbx), %r10
movq (%r10,%r11,8), %r11
movq %r11, %r10
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
andq %r15, %r10
je 0xbad3a
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 0xbad31
orb $-0x80, 0x3(%r11,%r15)
addq $0x18, %r15
cmpq %r15, %r10
jne 0xbad1f
incq %r9
cmpq %rdi, %r9
jne 0xbacb3
leaq 0x1(%rdx), %rsi
testq %rdx, %rdx
movq %rsi, %rdx
je 0xbac82
incq %rax
cmpq 0x78(%rsp), %rax
jb 0xbac64
jmp 0xbabdb
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
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