// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_long_equal_tips_assembly_complex(asg_t*, ma_hit_t_alloc*, long long, unsigned int, R_to_U*)
int asg_arc_cut_long_equal_tips_assembly_complex(asg_t *g, ma_hit_t_alloc* reverse_sources, 
long long miniedgeLen, uint32_t stops_threshold, R_to_U* ruIndex)
{
    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_contigLen(g, v^1, &convex, &ll, NULL);
        if(flag != TWO_INPUT && flag != MUL_INPUT) continue;
        convex = convex^1;
        ///uint32_t n_convex = asg_arc_n(g, convex), convexLen = ll;
        uint32_t n_convex = asg_arc_n(g, convex), convexLen = (uint32_t)-1, convex_i = (uint32_t)-1;
        asg_arc_t *a_convex = asg_arc_a(g, convex);
        for (i = 0; i < n_convex; i++)
        {
            if (!a_convex[i].del)
            {
                detect_single_path_with_dels_contigLen(g, a_convex[i].v, &convex, &ll, NULL);
                if(convex == v)
                {
                    convexLen = ll;
                    convex_i = i;
                    break;
                }
            }
        }

        
        for (i = 0; i < n_convex; i++)
        {
            if (!a_convex[i].del)
            {
                if(i == convex_i) continue;
                
                detect_single_path_with_dels_contigLen_complex(g, a_convex[i].v, &convex, &ll, 
                &max_stopLen, NULL, stops_threshold);
                ///threshold = 0.8
                if(ll > convexLen  && max_stopLen*1.25>ll)
                {
                    ///keep all nodes of this tip
                    b.b.n = 0; 
                    detect_single_path_with_dels_contigLen(g, v^1, &convex, &ll, &b);
                    if(b.b.n < 2) break;
                    b.b.n--;
                    ///keep all nodes of this tip

                     //we can only cut tips
                    if(check_if_diploid_primary_complex(v^1, a_convex[i].v, g, 
                    reverse_sources, miniedgeLen, stops_threshold, 1, ruIndex)==1)
                    {
                        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]);
                        }


                        ///lable the primary one
                        b.b.n = 0; 
                        detect_single_path_with_dels_contigLen_complex(g, a_convex[i].v, &convex, 
                        &ll, &max_stopLen, &b, stops_threshold);
                        for (k = 0; k < b.b.n; k++)
                        {
                            g->seq[b.b.a[k]].c = HAP_LABLE;
                        }

                        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 $0xd8, %rsp
movq %r8, 0x60(%rsp)
movl %ecx, 0x1c(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %ebp
addl %ebp, %ebp
xorpd %xmm0, %xmm0
leaq 0x70(%rsp), %rax
movapd %xmm0, 0x50(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x20(%rax)
andq $0x0, 0x60(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpl %ebp, %r13d
je 0x83263
movq 0x20(%rbx), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x831a1
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x831a1
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
jne 0x831a1
movl %r13d, %r15d
xorl $0x1, %r15d
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x831a1
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
callq 0x70b8f
andl $0x5, %eax
cmpl $0x1, %eax
jne 0x831a1
movl %r15d, 0x3c(%rsp)
movl %ebp, 0x18(%rsp)
movq %r14, 0x28(%rsp)
movl 0x14(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0x14(%rsp)
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movl %eax, %r14d
shrq $0x20, %rax
imulq $0x18, %rax, %rbp
movq %rdx, 0x48(%rsp)
leaq (%rdx,%rbp), %r15
addq $0xc, %r15
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x8306c
cmpl $0x0, (%r15)
js 0x83063
movl -0x4(%r15), %esi
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
callq 0x70b8f
cmpl %r13d, 0x14(%rsp)
je 0x8307e
incq %r12
addq $0x18, %r15
jmp 0x83038
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x30(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x83087
movl 0x20(%rsp), %eax
movq %rax, 0x30(%rsp)
imulq $0x18, %r14, %r14
imulq $0x18, %r12, %rax
movq %rax, 0x68(%rsp)
movq 0x48(%rsp), %rax
leaq (%rax,%rbp), %r15
addq $0x8, %r15
xorl %ebp, %ebp
movq 0x30(%rsp), %r12
cmpq %rbp, %r14
je 0x83198
cmpq %rbp, 0x68(%rsp)
je 0x8318f
cmpl $0x0, 0x4(%r15,%rbp)
js 0x8318f
movl (%r15,%rbp), %esi
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %r9d, %r9d
callq 0x70d81
movq 0x20(%rsp), %rax
cmpq %r12, %rax
jle 0x8318f
cvtsi2sdq 0x40(%rsp), %xmm0
mulsd 0x164b56(%rip), %xmm0   # 0x1e7c60
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8318f
movq %r15, %r12
andq $0x0, 0xa8(%rsp)
movq %rbx, %rdi
movl 0x3c(%rsp), %r15d
movl %r15d, %esi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x70(%rsp), %r8
callq 0x70b8f
movq 0xa8(%rsp), %rax
cmpq $0x2, %rax
jb 0x83198
decq %rax
movq %rax, 0xa8(%rsp)
movl (%r12,%rbp), %esi
movq 0x60(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r15d, %edi
movq %r12, %r15
movq %rbx, %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movl 0x1c(%rsp), %r9d
callq 0x71242
cmpq $0x1, %rax
movq 0x30(%rsp), %r12
je 0x831a9
addq $0x18, %rbp
jmp 0x830a8
movq 0x28(%rsp), %r14
movl 0x18(%rsp), %ebp
incl %r13d
jmp 0x82f7a
movq 0xa8(%rsp), %rax
movq 0xb8(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x831d1
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incq %rdx
jmp 0x831bb
xorl %r14d, %r14d
cmpq %rax, %r14
jae 0x831fa
movq 0xb8(%rsp), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
callq 0x80870
incq %r14
movq 0xa8(%rsp), %rax
jmp 0x831d4
andq $0x0, 0xa8(%rsp)
movl (%r15,%rbp), %esi
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x70(%rsp), %r9
callq 0x70d81
movq 0xa8(%rsp), %rax
movq 0xb8(%rsp), %rcx
xorl %edx, %edx
movq 0x28(%rsp), %r14
movl 0x18(%rsp), %ebp
cmpq %rdx, %rax
je 0x8325b
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
movb $0x2, 0x4(%rsi,%rdi,8)
incq %rdx
jmp 0x83245
incl %r14d
jmp 0x831a1
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0xb8(%rsp), %rdi
callq 0x2220
movl %r14d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq