// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_pop_bubble(ul_resolve_t*, ma_ug_t*, unsigned long*, unsigned int, unsigned int, unsigned int, asg64_v*, asg64_v*)
uint64_t ulg_pop_bubble(ul_resolve_t *uidx, ma_ug_t *ug, uint64_t* i_max_dist, uint32_t max_ext, uint32_t max_ext_hifi, uint32_t skip_hom, asg64_v *in, asg64_v *ib)
{
    // fprintf(stderr, "[M::%s::] Starting...\n", __func__);
    asg_t *g = ug->g; 
    asg64_v tx = {0,0,0}, tb = {0,0,0}, *ob = NULL, *ub = NULL; 
    uint32_t v, w, n_vtx = g->n_seq<<1, n_arc, nv, i, wc[2], wm[2], mm_c, mm_m, mm_v;
    uint64_t n_pop = 0, max_dist;
    asg_arc_t *av = NULL; if (!g->is_symm) asg_symm(g);
    buf_t b; memset(&b, 0, sizeof(buf_t));
    b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    if(i_max_dist) max_dist = (*i_max_dist);
    else max_dist = get_bub_pop_max_dist_advance(g, &b);
    ob = (in?(in):(&tx)); ub = (ib?(ib):(&tb)); ob->n = ub->n = 0;

    if(max_dist > 0) {
        for (v = 0; v < n_vtx; ++v) {
            nv = asg_arc_n(g, v); av = asg_arc_a(g, v);
            if (nv < 2 || g->seq[v>>1].del) continue;
            for (i = n_arc = 0; i < nv; ++i) {
                if (!av[i].del) ++n_arc;
            }
            if (n_arc < 2) continue;
            ///find a bubble
            ob->n = ub->n = 0;
            if(ulg_bub_pop1(uidx, ug, v, max_dist, &b, max_ext, max_ext_hifi, 1, 0, skip_hom, ob, ub, &(wc[0]), &(wm[0]))) {
                w = b.S.a[0]^1; mm_c = mm_m = mm_v = (uint32_t)-1;
                
                ob->n = ub->n = 0;
                if(ulg_bub_pop1(uidx, ug, v, max_dist, &b, max_ext, max_ext_hifi, 0, 0, skip_hom, 
                                                                                ob, ub, &(wc[0]), &(wm[0]))) {
                    mm_c = wc[0]; mm_m = wm[0]; mm_v = v;
                }

                ob->n = ub->n = 0;
                if(ulg_bub_pop1(uidx, ug, w, max_dist, &b, max_ext, max_ext_hifi, 0, 0, skip_hom, 
                                                                                ob, ub, &(wc[1]), &(wm[1]))) {
                    if((wc[1] < mm_c) && (wc[1] == mm_c && wm[1] > mm_m)) {
                        mm_c = wc[1]; mm_m = wm[1]; mm_v = w;
                    }                                                               
                }

                if(mm_v != (uint32_t)-1) {
                    ob->n = ub->n = 0;
                    w = ulg_bub_pop1(uidx, ug, mm_v, max_dist, &b, max_ext, max_ext_hifi, 0, 1, skip_hom, ob, ub, &(wc[0]), &(wm[0]));
                    assert(w);
                    n_pop += w;
                }
            }
        }
    }

    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    if(n_pop) asg_cleanup(g);
    if(!in) free(tx.a); if(!ib) free(tb.a);
    // fprintf(stderr, "[M::%s::] Done...\n", __func__);
    return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %r9, 0x50(%rsp)
movq %r8, 0x38(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdx, %r15
movq %rdi, 0x30(%rsp)
movq %rsi, 0x8(%rsp)
movq 0x18(%rsi), %rbx
xorl %eax, %eax
movq %rax, 0xa0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movq %rax, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl 0x14(%rbx), %eax
leal (%rax,%rax), %ebp
testl %eax, %eax
js 0x211e9e
movq %rbx, %rdi
callq 0xa0c1e
xorps %xmm0, %xmm0
movq 0x150(%rsp), %r14
movups %xmm0, 0x108(%rsp)
movups %xmm0, 0xf8(%rsp)
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0xd8(%rsp)
movups %xmm0, 0xc8(%rsp)
movups %xmm0, 0xb8(%rsp)
movl %ebp, %edi
movl $0x1c, %esi
movq %rdi, 0x28(%rsp)
callq 0x2120
movq %rax, 0xb0(%rsp)
testq %r15, %r15
je 0x211ef9
movq (%r15), %rcx
jmp 0x211f0c
leaq 0xb0(%rsp), %rsi
movq %rbx, %rdi
callq 0xc0162
movq %rax, %rcx
testq %r14, %r14
leaq 0x90(%rsp), %rdx
cmovneq %r14, %rdx
movq 0x158(%rsp), %rax
testq %rax, %rax
leaq 0x70(%rsp), %r10
cmovneq %rax, %r10
xorl %r13d, %r13d
movq %r13, (%r10)
movq %rdx, 0x60(%rsp)
movq %r13, (%rdx)
movq %rcx, 0x40(%rsp)
testq %rcx, %rcx
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
movl $0x0, %eax
movq %rax, 0x48(%rsp)
movq 0x8(%rsp), %r12
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdi
jne 0x2121b2
leaq 0x18(%rsp), %r11
leaq 0x20(%rsp), %r15
xorl %ebp, %ebp
movq $0x0, 0x48(%rsp)
movq %rbx, 0x58(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x2121a6
movq 0x20(%rbx), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x2121a6
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0xc, %rdx
xorl %ecx, %ecx
movl (%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %ecx
addq $0x18, %rdx
decq %rax
jne 0x211fc8
cmpl $0x2, %ecx
jb 0x2121a6
movq %r13, (%r10)
movq 0x60(%rsp), %rax
movq %r13, (%rax)
movq %r8, %r14
movq %r8, %rdi
movq %r12, %rsi
movl %ebp, %edx
movq 0x40(%rsp), %rcx
movq %rax, %r12
leaq 0xb0(%rsp), %r8
movl 0x14(%rsp), %r9d
pushq %r11
pushq %r15
pushq %r10
pushq %rax
pushq 0x70(%rsp)
pushq $0x0
pushq $0x1
pushq 0x70(%rsp)
movq %r10, %r15
callq 0x2114bd
addq $0x40, %rsp
testq %rax, %rax
je 0x212173
movq 0xc8(%rsp), %rax
xorl %ebx, %ebx
movl (%rax), %r13d
xorl $0x1, %r13d
movq %rbx, (%r15)
movq %rbx, (%r12)
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %edx
movq 0x40(%rsp), %rcx
leaq 0xb0(%rsp), %r8
movl 0x14(%rsp), %r9d
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq %r15
pushq %r12
movq 0x70(%rsp), %r12
pushq %r12
pushq %rbx
pushq %rbx
pushq 0x70(%rsp)
callq 0x2114bd
addq $0x40, %rsp
movq %r12, %r10
movq 0x60(%rsp), %r12
movq %rax, 0x68(%rsp)
movq %rbx, (%r15)
movq %rbx, (%r12)
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl %r13d, %edx
xorl %r13d, %r13d
movl 0x14(%rsp), %ebx
movq 0x40(%rsp), %rcx
leaq 0xb0(%rsp), %r14
movq %r14, %r8
movl %ebx, %r9d
leaq 0x1c(%rsp), %rax
pushq %rax
leaq 0x2c(%rsp), %rax
pushq %rax
pushq %r15
pushq %r12
pushq %r10
pushq %r13
pushq %r13
pushq 0x70(%rsp)
callq 0x2114bd
addq $0x40, %rsp
cmpq $0x0, 0x68(%rsp)
je 0x212185
movq %r13, (%r15)
movq %r13, (%r12)
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %edx
movq 0x40(%rsp), %rcx
movq %r14, %r8
movl %ebx, %r9d
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
movq %rax, %r14
pushq %rax
movq %r15, %r13
pushq %r15
pushq %r12
pushq 0x70(%rsp)
pushq $0x1
pushq $0x0
pushq 0x70(%rsp)
callq 0x2114bd
addq $0x40, %rsp
testq %rax, %rax
movq 0x58(%rsp), %rbx
je 0x21224a
addq %rax, 0x48(%rsp)
movq 0x8(%rsp), %r12
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdi
movq %r13, %r10
xorl %r13d, %r13d
leaq 0x18(%rsp), %r11
movq %r14, %r15
jmp 0x2121a6
movq 0x8(%rsp), %r12
movq %r14, %r8
movq 0x28(%rsp), %rdi
movq %r15, %r10
jmp 0x21219c
movq 0x8(%rsp), %r12
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdi
movq %r15, %r10
movq 0x58(%rsp), %rbx
leaq 0x18(%rsp), %r11
leaq 0x20(%rsp), %r15
incq %rbp
cmpq %rdi, %rbp
jne 0x211f88
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xf8(%rsp), %rdi
callq 0x2220
movq 0x110(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %r14
testq %r14, %r14
je 0x212205
movq %rbx, %rdi
callq 0xa0a7d
cmpq $0x0, 0x150(%rsp)
jne 0x21221d
movq 0xa0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x158(%rsp)
jne 0x212235
movq 0x80(%rsp), %rdi
callq 0x2220
movq %r14, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x36491(%rip), %rdi     # 0x2486e2
leaq 0x333b1(%rip), %rsi     # 0x245609
leaq 0x360e3(%rip), %rcx     # 0x248342
movl $0x2790, %edx           # imm = 0x2790
callq 0x2110