// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r12
movq %rdx, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0xc0(%rsp)
addl %eax, %eax
je 0xbb679
movl %eax, %r8d
xorl %r9d, %r9d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq %r12, 0x48(%rsp)
movq %r8, 0x28(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%r9,8), %rdi
cmpl $0x2, %edi
jb 0xbb66b
movq 0x20(%rbx), %rax
movl %r9d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xbb66b
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xbb66b
movq %rdi, %rax
andq %rbp, %rax
je 0xbb66b
movq 0x8(%rbx), %rcx
movq %rdi, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsi, 0x20(%rsp)
leaq (%rcx,%rdx,8), %r13
addq $0xc, %r13
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r13,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0xbb2f8
cmpl $0x2, %edx
jb 0xbb66b
movq %r9, 0x30(%rsp)
movl %r14d, 0x4(%rsp)
cmpl $0x1, %edi
movq %rdi, 0x38(%rsp)
adcl $0x0, %edi
movq %rdi, 0x8(%rsp)
movq $-0x1, %r12
xorl %r14d, %r14d
movq $-0x1, %rdx
movq $-0x1, %r15
cmpl $0x0, (%r13)
js 0xbb3ac
movq %rdx, 0x10(%rsp)
movl -0x4(%r13), %esi
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
leaq 0x40(%rsp), %rcx
xorl %r8d, %r8d
callq 0xa96f6
andl $0x5, %eax
cmpl $0x1, %eax
jne 0xbb3c1
cmpq $-0x1, %r12
sete %cl
movl 0x18(%rsp), %eax
cmpl %r12d, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0xbb3c1
movl %eax, %eax
cmpq $-0x1, %r12
cmoveq %rax, %r12
movq 0x40(%rsp), %rax
cmpq %rax, %r15
cmovleq %rax, %r15
movq 0x10(%rsp), %rdx
cmovlq %r14, %rdx
incq %r14
addq $0x18, %r13
cmpq %r14, 0x8(%rsp)
jne 0xbb348
movq 0x8(%rsp), %r14
jmp 0xbb3c6
movq 0x10(%rsp), %rdx
cmpl 0x38(%rsp), %r14d
movq 0x48(%rsp), %r12
jne 0xbb65c
leaq (%rdx,%rdx,2), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movl 0x8(%rsp), %esi
xorl %r15d, %r15d
movl $0x0, 0x1c(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
cmpq %r15, %rdx
je 0xbb5e0
leaq (%r15,%r15,2), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
js 0xbb5e0
leaq (%rcx,%rax,8), %r14
movq $0x0, 0x98(%rsp)
movl 0x8(%r14), %esi
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x60(%rsp), %r8
callq 0xa96f6
movq 0x98(%rsp), %rax
cmpq $0x2, %rax
jb 0xbb5d1
decq %rax
movq %rax, 0x98(%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %edi
movl 0x8(%r14), %esi
movq %rbx, %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq %r12, %r9
callq 0xa9c9f
cmpq $0x1, %rax
jne 0xbb5d1
movq 0x98(%rsp), %rax
testq %rax, %rax
je 0xbb4ba
movq 0x20(%rbx), %rcx
movq 0xa8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movb $0x1, 0x4(%rcx,%rdi,8)
incq %rsi
cmpq %rsi, %rax
jne 0xbb4aa
cmpq $0x0, 0x98(%rsp)
je 0xbb5c4
xorl %eax, %eax
movq 0xa8(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x20(%rbx), %rdx
cmpb $0x1, 0x4(%rdx,%rcx,8)
jne 0xbb5b3
addl %ecx, %ecx
xorl %edx, %edx
leaq (%rdx,%rcx), %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %rbp, %rdi
je 0xbb5a3
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%rbx), %r8
xorl %r9d, %r9d
leaq (%r9,%r9,2), %r11
movl 0xc(%r8,%r11,8), %r10d
testl %r10d, %r10d
js 0xbb597
leaq (%r8,%r11,8), %r14
movq 0x20(%rbx), %r13
movl 0x8(%r14), %r12d
movl %r12d, %r11d
shrl %r11d
cmpb $0x1, 0x4(%r13,%r11,8)
je 0xbb597
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%r14)
xorq $0x1, %r12
movq 0x28(%rbx), %r10
movq (%r10,%r12,8), %r11
movq %r11, %r10
andq %rbp, %r10
je 0xbb597
movq 0x8(%rbx), %r14
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%r14,%r11,8), %r11
addq $0xc, %r11
xorl %r14d, %r14d
movl -0x4(%r11,%r14), %r12d
xorl %esi, %r12d
cmpl $0x1, %r12d
jne 0xbb58e
orb $-0x80, 0x3(%r11,%r14)
addq $0x18, %r14
cmpq %r14, %r10
jne 0xbb57a
incq %r9
cmpq %rdi, %r9
jne 0xbb514
leaq 0x1(%rdx), %rsi
testq %rdx, %rdx
movq %rsi, %rdx
je 0xbb4e9
incq %rax
cmpq 0x98(%rsp), %rax
jb 0xbb4cb
incl 0x4(%rsp)
incl 0x1c(%rsp)
movq 0x48(%rsp), %r12
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
incq %r15
cmpq %rsi, %r15
jne 0xbb405
cmpl $0x0, 0x1c(%rsp)
je 0xbb65c
movq $0x0, 0x98(%rsp)
movl %edx, %eax
leaq (%rax,%rax,2), %rax
movl 0x8(%rcx,%rax,8), %esi
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x60(%rsp), %r8
callq 0xa96f6
movq 0x98(%rsp), %rax
testq %rax, %rax
movl 0x4(%rsp), %r14d
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
je 0xbb66b
movq 0x20(%rbx), %rcx
movq 0xa8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movb $0x2, 0x4(%rcx,%rdi,8)
incq %rsi
cmpq %rsi, %rax
jne 0xbb64a
jmp 0xbb66b
movl 0x4(%rsp), %r14d
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
incq %r9
cmpq %r8, %r9
jne 0xbb291
jmp 0xbb67c
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq 0xa8(%rsp), %rdi
callq 0x2220
movl %r14d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq