// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rdx, 0x88(%rsp)
movq %rdi, %r14
xorl %edx, %edx
callq 0xa6dbe
movl 0x14(%r14), %eax
leal (%rax,%rax), %ebx
testl %eax, %eax
js 0xf2063
movq %r14, %rdi
callq 0xa0333
movq %r14, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x80(%rsp)
movl %ebx, %edi
movl $0x1c, %esi
movq %rdi, 0x90(%rsp)
callq 0x2120
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
testl %ebx, %ebx
je 0xf2411
xorl %r13d, %r13d
xorl %r11d, %r11d
xorl %edi, %edi
movq %r14, 0x8(%rsp)
movq 0x20(%r14), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xf23f5
movq 0x30(%r14), %rax
cmpb $0x0, (%rax,%r13)
jne 0xf23f5
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %rax
andl $-0x2, %eax
je 0xf23f5
movq %r11, %rbp
movq %rdi, %r15
movq %r14, %rdi
movl %r13d, %esi
movq 0x88(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0xae409
cmpq $0x1, %rax
jne 0xf23ef
movq 0x58(%rsp), %rbx
testq %rbx, %rbx
movq %r15, %rdi
je 0xf216e
movq 0x38(%rsp), %rax
movq 0x68(%rsp), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
cmpq %rsi, %r13
je 0xf2164
cmpl (%rax), %esi
jne 0xf2149
xorl $0x1, %esi
movl %esi, (%rcx,%rdx,4)
jmp 0xf2164
movq 0x30(%r14), %r8
movb $0x2, %r9b
movb %r9b, (%r8,%rsi)
movq 0x30(%r14), %rsi
movl (%rcx,%rdx,4), %r8d
xorq $0x1, %r8
movb %r9b, (%rsi,%r8)
incq %rdx
movl %edx, %esi
cmpq %rsi, %rbx
ja 0xf2135
movq 0x30(%r14), %rax
movb $0x2, %dl
movb %dl, (%rax,%r13)
movq 0x30(%r14), %rax
movq 0x38(%rsp), %rcx
movl (%rcx), %ecx
xorq $0x1, %rcx
movb %dl, (%rax,%rcx)
movq 0x60(%rsp), %rax
cmpq %rax, %rbx
jne 0xf21bf
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %r15, %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq %rbx, %rcx
incq %rcx
movq %rcx, 0x58(%rsp)
movl %r13d, (%rax,%rbx,4)
movq %rbp, %r11
je 0xf23f5
xorl %edx, %edx
movq %r13, 0x10(%rsp)
movq 0x28(%r14), %rax
movq 0x68(%rsp), %rcx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xf23e0
movq %rdx, (%rsp)
movq %r12, 0x18(%rsp)
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsi, 0x98(%rsp)
movl %eax, %r13d
leaq (%rcx,%rdx,8), %r14
addq $0x12, %r14
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %eax, %eax
movl -0x6(%r14), %r12d
testl %r12d, %r12d
js 0xf2293
cmpb $0x0, (%r14)
je 0xf2249
movl $0x1, %eax
jmp 0xf2293
cmpq %r11, %rbp
jne 0xf2285
leaq (%r11,%r11), %r15
testq %r11, %r11
movq %rbp, 0xa0(%rsp)
movl %eax, %ebp
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
callq 0x2370
movq %rax, %rdi
movl %ebp, %eax
movq 0xa0(%rsp), %rbp
movq %r15, %r11
shlq $0x20, %r12
addq %rbx, %r12
movq %r12, (%rdi,%rbp,8)
incq %rbp
incq %rbx
addq $0x18, %r14
cmpq %rbx, %r13
jne 0xf2233
testl %eax, %eax
sete %al
testq %rbp, %rbp
sete %cl
orb %al, %cl
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
je 0xf22c8
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
jmp 0xf23dc
cmpq $0x2, %rbp
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r12
jb 0xf22ed
leaq (%rdi,%rbp,8), %rsi
movq %rdi, %rbx
movq %r11, %r13
callq 0x9d53f
movq %r13, %r11
movq %rbx, %rdi
testq %rbp, %rbp
movq 0x10(%rsp), %r13
je 0xf23dc
xorl %eax, %eax
movq 0x98(%rsp), %rbx
movl (%rdi,%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 %r15, %rsi
je 0xf23ce
movq 0x8(%r14), %r8
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%r8,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r10d, %r10d
xorl %r8d, %r8d
movl (%rdx,%r10), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %r10
cmpq %r10, %rsi
jne 0xf234a
cmpl $0x2, %r8d
jl 0xf23ce
orb $-0x80, 0xf(%rbx,%rcx,8)
movl (%rdi,%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 %r15, %rdx
je 0xf23cb
movl 0x4(%rbx,%rcx,8), %ecx
movq 0x8(%r14), %r8
shrq $0x20, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rsi,2), %rsi
leaq (%r8,%rsi,8), %rsi
addq $0xc, %rsi
xorl %r9d, %r9d
movl -0x4(%rsi,%r9), %r8d
xorl %ecx, %r8d
cmpl $0x1, %r8d
jne 0xf23c2
orb $-0x80, 0x3(%rsi,%r9)
addq $0x18, %r9
cmpq %r9, %rdx
jne 0xf23ae
incl %r12d
incq %rax
movl %eax, %ecx
cmpq %rcx, %rbp
ja 0xf2305
movq (%rsp), %rdx
incl %edx
cmpq %rdx, 0x58(%rsp)
ja 0xf21e3
jmp 0xf23f5
movq %r15, %rdi
movq %rbp, %r11
incq %r13
cmpq 0x90(%rsp), %r13
jne 0xf20c1
testl %r12d, %r12d
sete %bl
movq %rdi, %r15
jmp 0xf2416
movb $0x1, %bl
xorl %r15d, %r15d
movq 0x20(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
testb %bl, %bl
je 0xf2469
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa02fa