// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq %rdi, %r14
xorl %edx, %edx
callq 0xa7e08
movl 0x14(%r14), %eax
leal (%rax,%rax), %ebx
testl %eax, %eax
js 0xf4466
movq %r14, %rdi
callq 0xa0ab6
movq %r14, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0xa8(%rsp)
movups %xmm0, 0x98(%rsp)
movups %xmm0, 0x88(%rsp)
movups %xmm0, 0x78(%rsp)
movups %xmm0, 0x68(%rsp)
movups %xmm0, 0x58(%rsp)
movl %ebx, %edi
movl $0x1c, %esi
movq %rdi, 0x38(%rsp)
callq 0x2120
movq %rax, 0x50(%rsp)
testl %ebx, %ebx
je 0xf485f
xorl %r13d, %r13d
xorl %r11d, %r11d
xorl %r10d, %r10d
xorl %r12d, %r12d
movq %r14, 0x18(%rsp)
movq 0x20(%r14), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xf47da
movq 0x30(%r14), %rax
cmpb $0x0, (%rax,%r13)
jne 0xf47da
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %rax
andl $-0x2, %eax
je 0xf47da
movq %r11, %rbp
movq %r10, %r15
movq %r14, %rdi
movl %r13d, %esi
movq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0xaf281
cmpq $0x1, %rax
jne 0xf47d4
movq 0x68(%rsp), %rax
movq 0x88(%rsp), %rbx
movq 0x98(%rsp), %rdi
testq %rbx, %rbx
movb $0x2, %r8b
je 0xf456b
xorl %ecx, %ecx
movl (%rdi,%rcx,4), %edx
cmpq %rdx, %r13
je 0xf4561
cmpl (%rax), %edx
jne 0xf454a
xorl $0x1, %edx
movl %edx, (%rdi,%rcx,4)
jmp 0xf4561
movq 0x30(%r14), %rsi
movb %r8b, (%rsi,%rdx)
movq 0x30(%r14), %rdx
movl (%rdi,%rcx,4), %esi
xorq $0x1, %rsi
movb %r8b, (%rdx,%rsi)
incq %rcx
movl %ecx, %edx
cmpq %rdx, %rbx
ja 0xf4536
movq 0x30(%r14), %rcx
movb %r8b, (%rcx,%r13)
movq 0x30(%r14), %rcx
movl (%rax), %eax
xorq $0x1, %rax
movb %r8b, (%rcx,%rax)
cmpq 0x90(%rsp), %rbx
jne 0xf45b7
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x90(%rsp)
shlq $0x2, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x98(%rsp)
movq %r15, %r10
movq %rbp, %r11
leaq 0x1(%rbx), %rax
movq %rax, 0x88(%rsp)
movq %rdi, 0x48(%rsp)
movl %r13d, (%rdi,%rbx,4)
xorl %edx, %edx
movq %r13, 0x20(%rsp)
movq 0x28(%r14), %rax
movq 0x48(%rsp), %rcx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xf47c2
movq %rdx, 0x28(%rsp)
movl %r12d, 0xc(%rsp)
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsi, 0x40(%rsp)
movl %eax, %r13d
leaq (%rcx,%rdx,8), %rbp
addq $0x12, %rbp
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %eax, %eax
movl -0x6(%rbp), %r14d
testl %r14d, %r14d
js 0xf467f
cmpb $0x0, (%rbp)
je 0xf463e
movl $0x1, %eax
jmp 0xf467f
cmpq %r11, %r15
jne 0xf4671
leaq (%r15,%r15), %r12
testq %r15, %r15
movl %eax, 0x10(%rsp)
movl $0x2, %eax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq %r10, %rdi
callq 0x2370
movq %r12, %r11
movq %rax, %r10
movl 0x10(%rsp), %eax
shlq $0x20, %r14
addq %rbx, %r14
movq %r14, (%r10,%r15,8)
incq %r15
incq %rbx
addq $0x18, %rbp
cmpq %rbx, %r13
jne 0xf4628
testl %eax, %eax
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0xf46b3
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %r12d
movq 0x20(%rsp), %r13
jmp 0xf47bd
cmpq $0x2, %r15
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %r12d
jb 0xf46db
leaq (%r10,%r15,8), %rsi
movq %r10, %rdi
movq %r10, %rbx
movq %r11, %r13
callq 0x9dc69
movq %r13, %r11
movq %rbx, %r10
xorl %eax, %eax
movq 0x20(%rsp), %r13
movq 0x40(%rsp), %rbx
movl (%r10,%rax,8), %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rbx,%rcx,8), %edx
xorq $0x1, %rdx
movq 0x28(%r14), %rsi
movq (%rsi,%rdx,8), %rdx
movq %rdx, %rsi
andq %rbp, %rsi
je 0xf47af
movq 0x8(%r14), %rdi
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rdi,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rdx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xf472c
cmpl $0x2, %r8d
jl 0xf47af
orb $-0x80, 0xf(%rbx,%rcx,8)
movl (%r10,%rax,8), %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rbx,%rcx,8), %edx
xorq $0x1, %rdx
movq 0x28(%r14), %rsi
movq (%rsi,%rdx,8), %rsi
movq %rsi, %rdx
andq %rbp, %rdx
je 0xf47ac
movl 0x4(%rbx,%rcx,8), %ecx
movq 0x8(%r14), %rdi
shrq $0x20, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rsi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
movl -0x4(%rsi,%rdi), %r8d
xorl %ecx, %r8d
cmpl $0x1, %r8d
jne 0xf47a3
orb $-0x80, 0x3(%rsi,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xf4790
incl %r12d
incq %rax
movl %eax, %ecx
cmpq %rcx, %r15
ja 0xf46e7
movq 0x28(%rsp), %rdx
incl %edx
cmpq %rdx, 0x88(%rsp)
ja 0xf45d9
jmp 0xf47da
movq %r15, %r10
movq %rbp, %r11
incq %r13
cmpq 0x38(%rsp), %r13
jne 0xf44bf
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rbp
movq 0x80(%rsp), %r15
movq 0x98(%rsp), %r13
movq 0xb0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r10, %rbx
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
testl %r12d, %r12d
je 0xf4878
movq %r14, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa0a7d
movq %rax, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq