// File: /workspace/github_cpp_makefile_star2_O2/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 0x251c
movl 0x14(%rbx), %r12d
addl %r12d, %r12d
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x60(%rax)
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpl %r12d, %r15d
je 0x8083e
movq 0x20(%rbx), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x807af
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x807af
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
jne 0x807af
movl %r15d, %ebp
xorl $0x1, %ebp
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x807af
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x70874
andl $0x5, %eax
cmpl $0x1, %eax
jne 0x807af
movl %ebp, 0x20(%rsp)
movq %r13, 0x30(%rsp)
movl 0x14(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rcx
decq %rcx
movq %rcx, 0x18(%rsp)
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rsi
movq (%rsi,%rax,8), %rax
movl %eax, %esi
shrq $0x20, %rax
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm0
movss %xmm0, 0x24(%rsp)
imulq $0x18, %rsi, %r13
imulq $0x18, %rax, %rax
leaq (%rdx,%rax), %rbp
addq $0xc, %rbp
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x807aa
cmpl $0x0, (%rbp,%r14)
js 0x807a4
movl -0x4(%rbp,%r14), %esi
movl 0x2c(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0x709b2
cmpl %r15d, 0x14(%rsp)
je 0x807a4
movq 0x18(%rsp), %rax
cvtsi2ss %rax, %xmm0
mulss 0x28(%rsp), %xmm0
ucomiss 0x24(%rsp), %xmm0
jbe 0x807a4
movq 0x38(%rsp), %rcx
addq %rcx, %rcx
cmpq %rax, %rcx
jg 0x807b7
addq $0x18, %r14
jmp 0x80746
movq 0x30(%rsp), %r13
incl %r15d
jmp 0x80678
andq $0x0, 0x78(%rsp)
movq %rbx, %rdi
movl 0x20(%rsp), %esi
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0x70874
movq 0x78(%rsp), %rax
cmpq $0x2, %rax
movq 0x30(%rsp), %r13
jb 0x807af
decq %rax
movq %rax, 0x78(%rsp)
movq 0x88(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x80810
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incq %rdx
jmp 0x807fa
xorl %r14d, %r14d
cmpq %rax, %r14
jae 0x80836
movq 0x88(%rsp), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
callq 0x80870
incq %r14
movq 0x78(%rsp), %rax
jmp 0x80813
incl %r13d
jmp 0x807af
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0x88(%rsp), %rdi
callq 0x2220
movl %r13d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq