// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x180, %rsp            # imm = 0x180
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movl %ecx, -0x3c(%rbp)
movl %r8d, -0x40(%rbp)
movl %r9d, -0x44(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x50(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x9c(%rbp)
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xe0(%rbp)
movq -0x50(%rbp), %rax
movl 0x14(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x34af45
movq -0x50(%rbp), %rdi
callq 0xce020
leaq -0x148(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl -0x9c(%rbp), %eax
movl %eax, %edi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0x148(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x34af88
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
jmp 0x34af9f
movq -0x50(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0xfde50
movq %rax, -0xd8(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x34afb3
movq 0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
jmp 0x34afc0
leaq -0x68(%rbp), %rax
movq %rax, -0x150(%rbp)
jmp 0x34afc0
movq -0x150(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, 0x18(%rbp)
je 0x34afe2
movq 0x18(%rbp), %rax
movq %rax, -0x158(%rbp)
jmp 0x34afef
leaq -0x80(%rbp), %rax
movq %rax, -0x158(%rbp)
jmp 0x34afef
movq -0x158(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
cmpq $0x0, -0xd8(%rbp)
jbe 0x34b4a4
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0x34b4a2
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa4(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x94(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
cmpl $0x2, -0xa4(%rbp)
jb 0x34b0ad
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x94(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34b0b2
jmp 0x34b48e
movl $0x0, -0xa0(%rbp)
movl $0x0, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0x34b115
movq -0xe0(%rbp), %rax
movl -0xa8(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x34b102
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x34b104
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x34b0c6
cmpl $0x2, -0xa0(%rbp)
jae 0x34b123
jmp 0x34b48e
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x94(%rbp), %edx
movq -0xd8(%rbp), %rcx
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %r15d
movl -0x44(%rbp), %r14d
movq -0x88(%rbp), %rbx
movq -0x90(%rbp), %r11
leaq -0xb0(%rbp), %r10
leaq -0xb8(%rbp), %rax
leaq -0x148(%rbp), %r8
xorl %r12d, %r12d
movl %r15d, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl %r14d, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x34a180
cmpq $0x0, %rax
je 0x34b48c
movq -0x130(%rbp), %rax
movl (%rax), %eax
xorl $0x1, %eax
movl %eax, -0x98(%rbp)
movl $0xffffffff, -0xc4(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xc0(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x94(%rbp), %edx
movq -0xd8(%rbp), %rcx
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %r15d
movl -0x44(%rbp), %r14d
movq -0x88(%rbp), %rbx
movq -0x90(%rbp), %r11
leaq -0xb0(%rbp), %r10
leaq -0xb8(%rbp), %rax
leaq -0x148(%rbp), %r8
xorl %r12d, %r12d
movl %r15d, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl %r14d, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x34a180
cmpq $0x0, %rax
je 0x34b2b1
movl -0xb0(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0xc4(%rbp)
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x98(%rbp), %edx
movq -0xd8(%rbp), %rcx
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %r15d
movl -0x44(%rbp), %r14d
movq -0x88(%rbp), %rbx
movq -0x90(%rbp), %r11
leaq -0xb0(%rbp), %r10
addq $0x4, %r10
leaq -0xb8(%rbp), %rax
addq $0x4, %rax
leaq -0x148(%rbp), %r8
xorl %r12d, %r12d
movl %r15d, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl %r14d, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x34a180
cmpq $0x0, %rax
je 0x34b3a4
movl -0xac(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jae 0x34b3a2
movl -0xac(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jne 0x34b3a2
movl -0xb4(%rbp), %eax
cmpl -0xc0(%rbp), %eax
jbe 0x34b3a2
movl -0xac(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0xc4(%rbp)
jmp 0x34b3a4
cmpl $-0x1, -0xc4(%rbp)
je 0x34b48a
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0xc4(%rbp), %edx
movq -0xd8(%rbp), %rcx
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %r15d
movl -0x44(%rbp), %r14d
movq -0x88(%rbp), %rbx
movq -0x90(%rbp), %r11
leaq -0xb0(%rbp), %r10
leaq -0xb8(%rbp), %rax
leaq -0x148(%rbp), %r8
xorl %r12d, %r12d
movl %r15d, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movl %r14d, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x34a180
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
je 0x34b457
jmp 0x34b476
leaq 0x4df74(%rip), %rdi     # 0x3993d2
leaq 0x4ad54(%rip), %rsi     # 0x3961b9
movl $0x2790, %edx           # imm = 0x2790
leaq 0x4dbc1(%rip), %rcx     # 0x399032
callq 0x2130
movl -0x98(%rbp), %eax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x34b48c
jmp 0x34b48e
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x34b031
jmp 0x34b4a4
movq -0x148(%rbp), %rdi
callq 0x2210
movq -0x130(%rbp), %rdi
callq 0x2210
movq -0x118(%rbp), %rdi
callq 0x2210
movq -0x100(%rbp), %rdi
callq 0x2210
movq -0xe8(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0xd0(%rbp)
je 0x34b4f3
movq -0x50(%rbp), %rdi
callq 0xcdd10
cmpq $0x0, 0x10(%rbp)
jne 0x34b503
movq -0x58(%rbp), %rdi
callq 0x2210
cmpq $0x0, 0x18(%rbp)
jne 0x34b513
movq -0x70(%rbp), %rdi
callq 0x2210
movq -0xd0(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)