// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_long_equal_tips_assembly(asg_t*, ma_hit_t_alloc*, long long, R_to_U*)
int asg_arc_cut_long_equal_tips_assembly(asg_t *g, ma_hit_t_alloc* reverse_sources, 
long long miniedgeLen, 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, is_hap;
    long long ll, base_maxLen, base_maxLen_i;

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

    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t i, n_arc = 0, nv = asg_arc_n(g, v), n_tips;
        asg_arc_t *av = asg_arc_a(g, v);
        ///some node could be deleted
        if (nv < 2 || g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
        n_arc = get_real_length(g, v, NULL);
        if (n_arc < 2) continue;

        base_maxLen = -1;
        base_maxLen_i = -1;
        n_tips = 0;
        is_hap = 0;

        for (i = 0; i < nv; i++)
        {
            if (!av[i].del)
            {
                flag = detect_single_path_with_dels_contigLen(g, av[i].v, &convex, &ll, NULL);
                
                if(base_maxLen < ll)
                {
                    base_maxLen = ll;
                    base_maxLen_i = i;
                }

                if(flag == END_TIPS)
                {
                    n_tips++;
                }
            }
        }

        ///at least one tip
        if(n_tips > 0)
        {
            for (i = 0; i < nv; i++)
            {
                if(i == base_maxLen_i) continue;
                if (!av[i].del)
                {
                    b.b.n = 0;
                    if(detect_single_path_with_dels_contigLen(g, av[i].v, &convex, &ll, &b) 
                    != END_TIPS)
                    {
                        continue;
                    }
                    //we can only cut tips
                    /****************************may have bugs********************************/
                    if(check_if_diploid(av[base_maxLen_i].v, av[i].v, g, 
                    reverse_sources, miniedgeLen, ruIndex)==1)
                    {/****************************may have bugs********************************/
                        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]);
                        }
                        is_hap++;
                    }
                }
            }
        }


        if(is_hap > 0)
        {
            i = base_maxLen_i;
            b.b.n = 0;
            detect_single_path_with_dels_contigLen(g, av[i].v, &convex, &ll, &b);

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


    }


    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 $0xc8, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %r12d
addl %r12d, %r12d
xorps %xmm0, %xmm0
leaq 0x60(%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 %r8d, %r8d
movl $0x0, 0xc(%rsp)
movq %r13, 0x48(%rsp)
movq %rbp, 0x40(%rsp)
movq %r12, 0x30(%rsp)
cmpq %r12, %r8
je 0x80c0f
movq 0x28(%rbx), %rax
movq (%rax,%r8,8), %r14
testl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
je 0x80c07
movq 0x20(%rbx), %rax
movl %r8d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x80c07
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x80c07
movq %r8, %rsi
movq 0x8(%rbx), %r15
movq %rbx, %rdi
movq %r8, 0x18(%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jb 0x80c02
movq %r14, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%r15,%rax), %rcx
movq %rcx, 0x20(%rsp)
movl %r14d, %edx
leaq (%r15,%rax), %r12
addq $0xc, %r12
xorl %r13d, %r13d
pushq $-0x1
popq %r15
movq %r15, %r14
xorl %ebp, %ebp
movq %rdx, 0x28(%rsp)
cmpq %r13, %rdx
je 0x80a84
cmpl $0x0, (%r12)
js 0x80a7b
movl -0x4(%r12), %esi
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x38(%rsp), %rcx
xorl %r8d, %r8d
callq 0x70b8f
movq 0x28(%rsp), %rdx
movq 0x38(%rsp), %rcx
cmpq %rcx, %r14
cmovleq %rcx, %r14
cmovlq %r13, %r15
xorl %ecx, %ecx
cmpl $0x5, %eax
sete %cl
addl %ecx, %ebp
incq %r13
addq $0x18, %r12
jmp 0x80a36
testl %ebp, %ebp
je 0x80bfd
imulq $0x18, %r15, %rax
movq 0x20(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
movq %rax, 0x58(%rsp)
movq %rcx, %rax
xorl %r12d, %r12d
movl $0x0, 0x10(%rsp)
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
cmpq %rdx, %r12
je 0x80b96
cmpq %r12, %r15
je 0x80b8e
imulq $0x18, %r12, %r14
cmpl $0x0, 0xc(%rax,%r14)
js 0x80b8e
addq %rax, %r14
andq $0x0, 0x98(%rsp)
movl 0x8(%r14), %esi
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x60(%rsp), %r8
callq 0x70b8f
cmpl $0x5, %eax
jne 0x80b84
movq 0x58(%rsp), %rax
movl (%rax), %edi
movl 0x8(%r14), %esi
movq %rbx, %rdx
movq 0x50(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x70fd8
cmpq $0x1, %rax
jne 0x80b84
movq 0x98(%rsp), %rax
movq 0xa8(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x80b53
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incq %rdx
jmp 0x80b3d
xorl %r14d, %r14d
cmpq %rax, %r14
jae 0x80b7c
movq 0xa8(%rsp), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
callq 0x80870
incq %r14
movq 0x98(%rsp), %rax
jmp 0x80b56
incl 0xc(%rsp)
incl 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
incq %r12
jmp 0x80ab9
cmpl $0x0, 0x10(%rsp)
je 0x80bfd
andq $0x0, 0x98(%rsp)
movq %rax, %rcx
movl %r15d, %eax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %esi
movq %rbx, %rdi
leaq 0x14(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x60(%rsp), %r8
callq 0x70b8f
movq 0x98(%rsp), %rax
movq 0xa8(%rsp), %rcx
xorl %edx, %edx
movq 0x30(%rsp), %r12
movq 0x18(%rsp), %r8
cmpq %rdx, %rax
je 0x80c07
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
movb $0x2, 0x4(%rsi,%rdi,8)
incq %rdx
jmp 0x80be7
movq 0x30(%rsp), %r12
movq 0x18(%rsp), %r8
incq %r8
jmp 0x809ab
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0xa8(%rsp), %rdi
callq 0x2220
movl 0xc(%rsp), %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq