// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
callq 0x2510
movsd %xmm0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0xf6d84
movl $0x0, -0xc8(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xcc(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x2c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
cmpl $0x2, -0xcc(%rbp)
jb 0xf6a37
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf6a37
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0xf6a3c
jmp 0xf6d76
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0xc8(%rbp)
cmpl $0x2, -0xc8(%rbp)
jae 0xf6a60
jmp 0xf6d76
movq $-0x1, -0x50(%rbp)
movq $-0x1, -0x58(%rbp)
movl $0x0, -0xd0(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jae 0xf6b35
movq -0xd8(%rbp), %rax
movl -0xc4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf6b1f
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rax
movl -0xc4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
leaq -0x38(%rbp), %rdx
leaq -0x48(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdb740
movl %eax, -0x3c(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0xf6b08
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0xc4(%rbp), %eax
movq %rax, -0x58(%rbp)
cmpl $0x5, -0x3c(%rbp)
jne 0xf6b1d
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0xf6b1f
jmp 0xf6b21
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xf6a8b
cmpl $0x0, -0xd0(%rbp)
jbe 0xf6cda
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jae 0xf6cd8
movl -0xc4(%rbp), %eax
cmpq -0x58(%rbp), %rax
jne 0xf6b6f
jmp 0xf6cc4
movq -0xd8(%rbp), %rax
movl -0xc4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf6cc2
movq $0x0, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rax
movl -0xc4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
leaq -0x38(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0xc0(%rbp), %r8
callq 0xdb740
cmpl $0x5, %eax
je 0xf6bd6
jmp 0xf6cc4
movq -0xd8(%rbp), %rax
imulq $0x18, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %edi
movq -0xd8(%rbp), %rax
movl -0xc4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movq -0x8(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %r8
movq -0x20(%rbp), %r9
callq 0xdbff0
cmpq $0x1, %rax
jne 0xf6cc0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq $0x0, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0xf6c71
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x78(%rbp), %rcx
movq -0xe0(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0xf6c32
movq $0x0, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0xf6cb7
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rax
movq -0xe0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0xf6800
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0xf6c7c
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xf6cc2
jmp 0xf6cc4
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xf6b4c
jmp 0xf6cda
cmpl $0x0, -0x40(%rbp)
jbe 0xf6d74
movq -0x58(%rbp), %rax
movl %eax, -0xc4(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rax
movl -0xc4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
leaq -0x38(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0xc0(%rbp), %r8
callq 0xdb740
movq $0x0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0xf6d72
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x78(%rbp), %rcx
movq -0xe8(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movb $0x2, 0x4(%rax,%rcx,8)
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0xf6d33
jmp 0xf6d74
jmp 0xf6d76
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xf69a7
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movq -0x78(%rbp), %rdi
callq 0x2210
movl -0x34(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)