// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: drop_inexact_edegs_at_bubbles(asg_t*, bub_label_t*, unsigned long)
void drop_inexact_edegs_at_bubbles(asg_t *r_g, bub_label_t* b_mask_t, uint64_t bubble_dist)
{
    asg_arc_identify_simple_bubbles_multi(r_g, b_mask_t, 0);

    uint32_t v, k, i, n_vtx = r_g->n_seq * 2, nv, flag, n_reduce = 0;
    uint64_t oLen;
    asg_arc_t *av = NULL;
    
    if (!r_g->is_symm) asg_symm(r_g);

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

    kvec_t(uint64_t) e;
    kv_init(e);

    b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    for (v = 0; v < n_vtx; ++v) 
    {
        nv = asg_arc_n(r_g, v);
        if(r_g->seq[v>>1].del) continue;
        if(r_g->seq_vis[v] != 0) continue;
        if(nv < 2) continue;
        

        ///if this is a bubble
        if(asg_bub_finder_without_del_advance(r_g, v, bubble_dist, &b) == 1)
        {
            //beg is v, end is b.S.a[0]
            //note b.b include end, does not include beg
            for (i = 0; i < b.b.n; i++)
            {
                if(b.b.a[i] == v) continue;
                //note b.b include end, does not include beg
                if(b.b.a[i] == b.S.a[0])
                {
                    b.b.a[i] = b.b.a[i]^1;
                    continue;
                }

                r_g->seq_vis[b.b.a[i]] = 2;
                r_g->seq_vis[b.b.a[i]^1] = 2;
            }

            r_g->seq_vis[v] = 2;
            r_g->seq_vis[b.S.a[0]^1] = 2;
            

            //note b.b include end, does not include beg
            //so push beg into b.b
            kv_push(uint32_t, b.b, v);
            for (i = 0; i < b.b.n; i++)
            {
                nv = asg_arc_n(r_g, b.b.a[i]);
                if(nv <= 1) continue;
                av = asg_arc_a(r_g, b.b.a[i]);
                e.n = 0;
                flag = 0;
                for (k = 0; k < nv; k++)
                {
                    if(av[k].del) continue;
                    if(av[k].el)
                    {
                        flag = 1;
                        continue;
                    }
                    oLen = av[k].ol;
                    oLen = oLen << 32;
                    oLen = oLen | (uint64_t)(k);
                    kv_push(uint64_t, e, oLen);
                }

                if(flag == 0 || e.n == 0) continue;

                if(e.n>1) radix_sort_arch64(e.a, e.a + e.n);

                for (k = 0; k < e.n; k++)
                {
                    if(get_real_length(r_g, av[(uint32_t)e.a[k]].v^1, NULL)<=1) continue;
                    av[(uint32_t)e.a[k]].del = 1;
                    asg_arc_del(r_g, av[(uint32_t)e.a[k]].v^1, av[(uint32_t)e.a[k]].ul>>32^1, 1);
                    n_reduce++;
                }

            }
        }
    }

    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    kv_destroy(e);
    if(n_reduce > 0) asg_cleanup(r_g);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x38(%rsp)
movq %rdi, %r14
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x6ef72
movl 0x14(%r14), %eax
leal (%rax,%rax), %ebx
testl %eax, %eax
js 0xb50c1
movq %r14, %rdi
callq 0x69638
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r12
movups %xmm0, 0x58(%r12)
movups %xmm0, 0x48(%r12)
movups %xmm0, 0x38(%r12)
movups %xmm0, 0x28(%r12)
movups %xmm0, 0x18(%r12)
movups %xmm0, 0x8(%r12)
movl %ebx, %edi
pushq $0x1c
popq %rsi
movq %rdi, 0x40(%rsp)
callq 0x2120
movq %rax, (%r12)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r14, 0x8(%rsp)
cmpq 0x40(%rsp), %r12
je 0xb53b9
movq 0x20(%r14), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xb53b1
movq 0x30(%r14), %rax
cmpb $0x0, (%rax,%r12)
jne 0xb53b1
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rax
andl $-0x2, %eax
je 0xb53b1
movq %r14, %rdi
movl %r12d, %esi
movq 0x38(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x75519
cmpq $0x1, %rax
jne 0xb53b1
movq 0x68(%rsp), %rax
movq 0x88(%rsp), %rbx
movq 0x98(%rsp), %rdi
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rbx
jbe 0xb51ba
movl (%rdi,%rcx,4), %edx
cmpq %rdx, %r12
je 0xb51b6
cmpl (%rax), %edx
jne 0xb519c
xorl $0x1, %edx
movl %edx, (%rdi,%rcx,4)
jmp 0xb51b6
movq 0x30(%r14), %rsi
movb $0x2, %r8b
movb %r8b, (%rsi,%rdx)
movq 0x30(%r14), %rdx
movl (%rdi,%rcx,4), %esi
xorq $0x1, %rsi
movb %r8b, (%rdx,%rsi)
incl %ecx
jmp 0xb5181
movq 0x30(%r14), %rcx
movb $0x2, %dl
movb %dl, (%rcx,%r12)
movq 0x30(%r14), %rcx
movl (%rax), %eax
xorq $0x1, %rax
movb %dl, (%rcx,%rax)
cmpq 0x90(%rsp), %rbx
jne 0xb5205
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x90(%rsp)
shlq $0x2, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x98(%rsp)
leaq 0x1(%rbx), %rax
movq %rax, 0x88(%rsp)
movl %r12d, (%rdi,%rbx,4)
xorl %edx, %edx
movq %r12, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movl %edx, %edx
cmpq %rdx, %rax
jbe 0xb53b1
movq 0x28(%r14), %rax
movl (%rdi,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xb5315
movq %rdx, 0x20(%rsp)
movq %r13, (%rsp)
movq %r15, 0x28(%rsp)
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x30(%rsp)
movl %eax, %r15d
leaq (%rcx,%rdx), %rbx
addq $0x12, %rbx
xorl %r14d, %r14d
xorl %ecx, %ecx
xorl %r13d, %r13d
cmpq %r14, %r15
je 0xb52e9
movl -0x6(%rbx), %r12d
testl %r12d, %r12d
js 0xb52e0
cmpb $0x0, (%rbx)
je 0xb5293
pushq $0x1
popq %rcx
jmp 0xb52e0
movq (%rsp), %rax
cmpq %rax, %r13
jne 0xb52d1
movq %rbp, %rdi
leaq (%rax,%rax), %rbp
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq %r13, 0x48(%rsp)
movl %ecx, %r13d
callq 0x2370
movl %r13d, %ecx
movq 0x48(%rsp), %r13
movq %rbp, (%rsp)
movq %rax, %rbp
shlq $0x20, %r12
addq %r14, %r12
movq %r12, (%rbp,%r13,8)
incq %r13
incq %r14
addq $0x18, %rbx
jmp 0xb527b
testl %ecx, %ecx
sete %al
testq %r13, %r13
sete %cl
orb %al, %cl
je 0xb5324
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %r12
movq (%rsp), %r13
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
incl %edx
movq 0x88(%rsp), %rax
jmp 0xb5221
cmpq $0x2, %r13
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %r15
jb 0xb5347
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x66c25
xorl %ebx, %ebx
movq 0x30(%rsp), %rcx
movl %ebx, %ebx
cmpq %rbx, %r13
jbe 0xb5302
movl (%rbp,%rbx,8), %eax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %esi
xorl $0x1, %esi
movq %r14, %rdi
xorl %edx, %edx
movq %rcx, %r12
callq 0xc2e7a
cmpl $0x2, %eax
jl 0xb53ad
movl (%rbp,%rbx,8), %eax
imulq $0x18, %rax, %rax
orl $0x80000000, 0xc(%r12,%rax) # imm = 0x80000000
movl (%rbp,%rbx,8), %eax
imulq $0x18, %rax, %rax
movl 0x4(%r12,%rax), %edx
movl 0x8(%r12,%rax), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %r14, %rdi
pushq $0x1
popq %rcx
callq 0x6db50
incl %r15d
incl %ebx
jmp 0xb5349
incq %r12
jmp 0xb510d
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
movq 0xb0(%rsp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
testl %r15d, %r15d
je 0xb541a
movq %r14, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x694eb
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq