// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0xd8550
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x1510ae
movq -0x8(%rbp), %rdi
callq 0xce020
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xb8(%rbp)
movl -0x28(%rbp), %eax
movl %eax, %edi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0xb0(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x151587
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x1c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x15113c
jmp 0x151579
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x151155
jmp 0x151579
cmpl $0x2, -0x2c(%rbp)
jae 0x151160
jmp 0x151579
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq -0xb0(%rbp), %rcx
callq 0xe3010
cmpq $0x1, %rax
jne 0x151577
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpq -0x78(%rbp), %rax
jae 0x151211
movq -0x68(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x1c(%rbp), %eax
jne 0x1511a4
jmp 0x151203
movq -0x68(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x98(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x1511d2
movq -0x68(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %edx
xorl $0x1, %edx
movq -0x68(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x151203
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movb $0x2, (%rax,%rcx)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x2, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x15118a
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x1c(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x98(%rbp), %rcx
movl (%rcx), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x2, (%rax,%rcx)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x151289
cmpq $0x0, -0x70(%rbp)
je 0x15125b
movq -0x70(%rbp), %rax
shlq %rax
movq %rax, -0xd0(%rbp)
jmp 0x151269
movl $0x2, %eax
movq %rax, -0xd0(%rbp)
jmp 0x151269
movq -0xd0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, -0x68(%rbp)
movl -0x1c(%rbp), %edx
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x78(%rbp)
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpq -0x78(%rbp), %rax
jae 0x151575
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
ja 0x1512da
jmp 0x151567
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq -0x68(%rbp), %rdx
movl -0x24(%rbp), %esi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0xc8(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x15143b
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x15134a
jmp 0x15142d
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x12(%rax)
je 0x15136a
movl $0x1, -0x30(%rbp)
jmp 0x15142d
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
orq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jne 0x151407
cmpq $0x0, -0xc0(%rbp)
je 0x1513cd
movq -0xc0(%rbp), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x1513db
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x1513db
movq -0xd8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0xb8(%rbp)
movq -0x40(%rbp), %rdx
movq -0xb8(%rbp), %rax
movq -0xc8(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xc8(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x15142d
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x151320
cmpl $0x0, -0x30(%rbp)
je 0x15144b
cmpq $0x0, -0xc8(%rbp)
jne 0x151450
jmp 0x151567
cmpq $0x1, -0xc8(%rbp)
jbe 0x15147b
movq -0xb8(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0xc90e0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x151565
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
movl -0x20(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jg 0x1514ca
jmp 0x151557
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
movl -0x20(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
movl -0x20(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
movl -0x20(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
xorq $0x1, %rax
movl %eax, %edx
movl $0x1, %ecx
callq 0xd6530
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x151482
jmp 0x151567
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1512a9
jmp 0x151577
jmp 0x151579
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1510ff
movq -0xb0(%rbp), %rdi
callq 0x2210
movq -0x98(%rbp), %rdi
callq 0x2210
movq -0x80(%rbp), %rdi
callq 0x2210
movq -0x68(%rbp), %rdi
callq 0x2210
movq -0x50(%rbp), %rdi
callq 0x2210
movq -0xb8(%rbp), %rdi
callq 0x2210
cmpl $0x0, -0x34(%rbp)
jbe 0x1515d5
movq -0x8(%rbp), %rdi
callq 0xcdd10
addq $0xe0, %rsp
popq %rbp
retq
nop