// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_simple_large_bubbles(asg_t*, ma_hit_t_alloc*, long long, R_to_U*)
int asg_arc_simple_large_bubbles(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, all_covex;

    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);
        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;
        all_covex = -1;
        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(flag != TWO_INPUT && flag != MUL_INPUT)
                {
                    break;
                }

                if(all_covex != -1 && (uint32_t)all_covex != convex)
                {
                    break;
                }

                if(all_covex == -1)
                {
                    all_covex = convex;
                }
                
                if(base_maxLen < ll)
                {
                    base_maxLen = ll;
                    base_maxLen_i = i;
                }

            }
        }


        if(i == nv)
        {
            for (i = 0; i < nv; i++)
            {
                if(i == base_maxLen_i) continue;
                if (!av[i].del)
                {
                    b.b.n = 0;
                    detect_single_path_with_dels_contigLen(g, av[i].v, &convex, &ll, &b);
                    if(b.b.n < 2) continue;
                    b.b.n--;

                    //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 -0xc8(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0xf7217
movl $0x0, -0xd0(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xd4(%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, -0xe0(%rbp)
cmpl $0x2, -0xd4(%rbp)
jb 0xf6e97
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 0xf6e97
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 0xf6e9c
jmp 0xf7209
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0xd0(%rbp)
cmpl $0x2, -0xd0(%rbp)
jae 0xf6ec0
jmp 0xf7209
movq $-0x1, -0x50(%rbp)
movq $-0x1, -0x58(%rbp)
movq $-0x1, -0x60(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jae 0xf6fac
movq -0xe0(%rbp), %rax
movl -0xcc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf6f96
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movl -0xcc(%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)
cmpl $0x1, -0x3c(%rbp)
je 0xf6f58
cmpl $0x3, -0x3c(%rbp)
je 0xf6f58
jmp 0xf6fac
cmpq $-0x1, -0x60(%rbp)
je 0xf6f6a
movq -0x60(%rbp), %rax
cmpl -0x38(%rbp), %eax
je 0xf6f6a
jmp 0xf6fac
cmpq $-0x1, -0x60(%rbp)
jne 0xf6f78
movl -0x38(%rbp), %eax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0xf6f94
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0xcc(%rbp), %eax
movq %rax, -0x58(%rbp)
jmp 0xf6f96
jmp 0xf6f98
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0xf6ee9
movl -0xcc(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jne 0xf7207
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jae 0xf716b
movl -0xcc(%rbp), %eax
cmpq -0x58(%rbp), %rax
jne 0xf6feb
jmp 0xf7157
movq -0xe0(%rbp), %rax
movl -0xcc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf7155
movq $0x0, -0x90(%rbp)
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movl -0xcc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
leaq -0x38(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0xc8(%rbp), %r8
callq 0xdb740
cmpq $0x2, -0x90(%rbp)
jae 0xf7057
jmp 0xf7157
movq -0x90(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x90(%rbp)
movq -0xe0(%rbp), %rax
imulq $0x18, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %edi
movq -0xe0(%rbp), %rax
movl -0xcc(%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 0xf7153
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq $0x0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0xf7104
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movq -0xe8(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0xf70c5
movq $0x0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0xf714a
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movq -0xe8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0xf6800
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0xf710f
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xf7155
jmp 0xf7157
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0xf6fc8
cmpl $0x0, -0x40(%rbp)
jbe 0xf7205
movq -0x58(%rbp), %rax
movl %eax, -0xcc(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rax
movl -0xcc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
leaq -0x38(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0xc8(%rbp), %r8
callq 0xdb740
movq $0x0, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0xf7203
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movq -0xf0(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movb $0x2, 0x4(%rax,%rcx,8)
movq -0xf0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xf0(%rbp)
jmp 0xf71c4
jmp 0xf7205
jmp 0xf7207
jmp 0xf7209
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xf6e07
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movq -0x80(%rbp), %rdi
callq 0x2210
movl -0x34(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nop