// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movss %xmm0, -0xc(%rbp)
movl %esi, -0x10(%rbp)
callq 0x2510
movsd %xmm0, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0xf67ce
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x1c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf6552
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x1c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0xf6557
jmp 0xf67c0
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
je 0xf6571
jmp 0xf67c0
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
je 0xf658e
jmp 0xf67c0
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
xorl $0x1, %esi
leaq -0x28(%rbp), %rdx
leaq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdb140
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
je 0xf65be
cmpl $0x3, -0x2c(%rbp)
je 0xf65be
jmp 0xf67c0
movl -0x28(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0xb4(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x28(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jae 0xf67be
movq -0xc0(%rbp), %rax
movl -0xac(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf67a8
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rax
movl -0xac(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movl -0x10(%rbp), %eax
leaq -0x28(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x40(%rbp), %r8
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0xdb440
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0xf669c
jmp 0xf67aa
cvtsi2ssq -0x38(%rbp), %xmm0
movss -0xc(%rbp), %xmm1
mulss %xmm1, %xmm0
movl -0xb4(%rbp), %eax
cvtsi2ss %rax, %xmm1
ucomiss %xmm1, %xmm0
jbe 0xf67a6
movq -0x40(%rbp), %rax
shlq %rax
cmpq -0x38(%rbp), %rax
jle 0xf67a6
movq $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
xorl $0x1, %esi
leaq -0x28(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0xa8(%rbp), %r8
callq 0xdb140
movl %eax, -0x2c(%rbp)
cmpq $0x2, -0x70(%rbp)
jae 0xf6705
jmp 0xf67be
movq -0x70(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x70(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0xf6761
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x60(%rbp), %rcx
movq -0xc8(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc8(%rbp)
jmp 0xf6725
movq $0x0, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0xf67a4
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0xf6800
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc8(%rbp)
jmp 0xf676c
jmp 0xf67be
jmp 0xf67a8
jmp 0xf67aa
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xf6625
jmp 0xf67c0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xf6513
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movq -0x60(%rbp), %rdi
callq 0x2210
movl -0x24(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)