// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_bub_pop1(ul_resolve_t*, ma_ug_t*, unsigned int, unsigned long, buf_t*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, asg64_v*, asg64_v*, unsigned int*, unsigned int*)
uint64_t ulg_bub_pop1(ul_resolve_t *uidx, ma_ug_t *ug, uint32_t v0, uint64_t max_dist, buf_t *x, 
uint32_t max_ext, uint32_t max_ext_hifi, uint32_t check_bubble_only, uint32_t is_pop, uint32_t skip_hom, 
asg64_v *b, asg64_v *ub, uint32_t *r_w_c, uint32_t *r_w_m)
{
    asg_t *g = ug->g; uint32_t pb = b->n; uint64_t w_q, w_t, wc, wm, ww; (*r_w_c) = (*r_w_m) = (uint32_t)-1;
    uint32_t v, w, i, kv, nv, kw, cnt = 0, fail_b = 0, n_tips = 0, tip_end = (uint32_t)-1;
    uint32_t l, d, c, m, n_pending = 0, z, to_replace; asg_arc_t *av, *ve, *we; binfo_t *t;
    if(g->seq[v0>>1].del || get_arcs(g, v0, NULL, 0) < 2) return 0; // already deleted
    // fprintf(stderr, "sbsbsbsbsbsbsb[M::%s::(v0>>1)->%u::(v0&1)->%u] check_bubble_only::%u, is_pop::%u\n", 
    //             __func__, v0>>1, v0&1, check_bubble_only, is_pop);

    x->S.n = x->T.n = x->b.n = x->e.n = 0;
    x->a[v0].c = x->a[v0].d = x->a[v0].m = x->a[v0].nc = x->a[v0].np = 0;
    kv_push(uint32_t, x->S, v0);

    do {
        v = kv_pop(x->S); d = x->a[v].d; c = x->a[v].c; m = x->a[v].m;
        nv = asg_arc_n(g, v); av = asg_arc_a(g, v); kv = get_arcs(g, v, NULL, 0);
        for (i = 0; i < nv; ++i) {
            if (av[i].del) continue;
            w = av[i].v; t = &(x->a[w]); l = ((v == v0)?(0):((uint32_t)av[i].ul));
            if ((w>>1) == (v0>>1)) {
                fail_b = 1;
                break;
            }
            kv_push(uint32_t, x->e, (g->idx[v]>>32) + i); ///for backtracking
            if (d + l > max_dist) {
                fail_b = 1;
                break;
            }
            kw = get_arcs(g, w^1, NULL, 0);
            wc = 0; wm = get_ul_occ(uidx, w>>1);
            if(!check_bubble_only) {
                if(kv > 1) {
                    pb = b->n; ub->n = 0; ve = &(av[i]);
                    // fprintf(stderr, "[M::%s::] ve->v>>1::%lu, ve->v&1::%lu, ve->w>>1::%u, ve->w&1::%u\n", 
                    // __func__, ve->ul>>33, (ve->ul>>32)&1, ve->v>>1, ve->v&1);
                    get_ul_arc_supports(uidx, ve, b, ub, skip_hom, &w_q, &w_t);
                    b->n = pb; ub->n = 0;
                    if(w_q < w_t) {
                        if(w_q == 0) {
                            ww = 10;
                        } else {
                            if(w_t != 0) ww = ((w_t - w_q)*10)/w_t;
                            else ww = 0;
                        } 
                        if(ww > wc) wc = ww;
                    }
                }

                if(kw > 1) {
                    pb = b->n; ub->n = 0; we = get_specfic_edge(g, w^1, v^1); assert(we);
                    // fprintf(stderr, "[M::%s::] we->v>>1::%lu, we->v&1::%lu, we->w>>1::%u, we->w&1::%u\n", 
                    // __func__, we->ul>>33, (we->ul>>32)&1, we->v>>1, we->v&1);
                    get_ul_arc_supports(uidx, we, b, ub, skip_hom, &w_q, &w_t);
                    b->n = pb; ub->n = 0;
                    if(w_q < w_t) {
                        if(w_q == 0) {
                            ww = 10;
                        } else {
                            if(w_t != 0) ww = ((w_t - w_q)*10)/w_t;
                            else ww = 0;
                        } 
                        if(ww > wc) wc = ww;
                    }
                }
            }

            if (t->s == 0) {
                kv_push(uint32_t, x->b, w); 
                t->p = v, t->s = 1, t->d = d + l;
                t->c = c + wc; t->m = m + wm;
                t->r = kw;
				++n_pending;
            } else {
                to_replace = 0;
                if((c + wc) < t->c) {
                    to_replace = 1;
                } else if(((c + wc) == t->c) && (m + wm > t->m)) {
                    to_replace = 1;
                } else if(((c + wc) == t->c) && (m + wm == t->m) && (d + l > t->d)) {
                    to_replace = 1;
                }
                if(to_replace) {
                    t->p = v; t->c = c + wc; t->m = m + wm;
                }
                if (d + l < t->d) t->d = d + l; // update dist
            }

            if (--(t->r) == 0) {
                z = get_arcs(g, w, NULL, 0);
                if(z > 0) {
                    kv_push(uint32_t, x->S, w);
                }
                else {
                    ///at most one tip
                    if(n_tips != 0) {
                        fail_b = 1;
                        break;
                    }
                    n_tips++; tip_end = w;
                }
				--n_pending;
            }
        }
        if(fail_b) break;
        if(n_tips == 1) {
            if(tip_end != (uint32_t)-1 && n_pending == 0 && x->S.n == 0) {
                kv_push(uint32_t, x->S, tip_end);
                break;
            }
            fail_b = 1;
            break;
        }

        if (i < nv || x->S.n == 0) {
            fail_b = 1;
            break;
        }
    } while (x->S.n > 1 || n_pending);

    if(!fail_b) {//there is a bubble
        cnt = 1; (*r_w_c) = x->a[x->S.a[0]].c; (*r_w_m) = x->a[x->S.a[0]].m; 
        if(!check_bubble_only) {
            if(is_pop) {
                cnt = ulg_bub_pop_backtrack(ug, v0, x);
            } else {
                cnt = ulg_bub_pop_cut_aux(uidx, ug, v0, x, max_ext, max_ext_hifi, b, ub);
            }
        }
    }
    for (i = 0; i < x->b.n; ++i) { // clear the states of visited vertices
        // if(v0 == 119) {
        //     fprintf(stderr, "-[M::%s::(v0>>1)->%u::(v0&1)->%u] v[%u]>>1::%u, v[%u]&1::%u, cnt::%u, is_pop::%u\n", 
        //         __func__, v0>>1, v0&1, i, x->b.a[i]>>1, i, x->b.a[i]&1, cnt, is_pop);
        // }
		t = &x->a[x->b.a[i]];
		t->s = t->c = t->d = t->m = t->nc = t->np = 0;
	}
    if(!cnt) (*r_w_c) = (*r_w_m) = (uint32_t)-1;
    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, 0xc0(%rsp)
movq %rdi, 0x58(%rsp)
movq 0x148(%rsp), %rcx
movq 0x140(%rsp), %rax
movq 0x18(%rsi), %r15
orl $-0x1, (%rcx)
orl $-0x1, (%rax)
movq 0x20(%r15), %rax
movl %edx, %ecx
shrl %ecx
movq %rcx, 0xc8(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x1c3fa7
movq %r8, %rbx
movl %edx, %r13d
movq %rsi, 0xb8(%rsp)
movl %r9d, 0x48(%rsp)
xorl %r14d, %r14d
movq %r15, %rdi
movl %edx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jb 0x1c45cd
movq %r15, 0x40(%rsp)
andq $0x0, 0x50(%rbx)
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movl 0x128(%rsp), %ebp
movq (%rbx), %rax
movl %r13d, %ecx
imulq $0x1c, %rcx, %rcx
xorps %xmm0, %xmm0
andl $0x0, 0x14(%rax,%rcx)
movups %xmm0, 0x4(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
je 0x1c3faf
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x1c3fcb
xorl %r14d, %r14d
jmp 0x1c45cd
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %r13d, (%rax,%rcx,4)
incq %rcx
movl %ebp, %eax
movq %rax, 0xa8(%rsp)
xorl %r12d, %r12d
pushq $-0x1
popq %rax
movl %eax, 0x30(%rsp)
movl %r13d, 0x4c(%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %r15d
imulq $0x1c, %r15, %rcx
movl 0x4(%rax,%rcx), %edx
movl %edx, 0xa4(%rsp)
movl 0x8(%rax,%rcx), %edx
movq %rdx, 0xb0(%rsp)
movl 0xc(%rax,%rcx), %eax
movq %rax, 0x70(%rsp)
movq 0x40(%rsp), %rdi
movq 0x28(%rdi), %rax
movq (%rax,%r15,8), %rbp
movq %rbp, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %r14
addq 0x8(%rdi), %r14
movl %r15d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
movl %eax, 0xa0(%rsp)
movq %r14, %rax
movq 0x130(%rsp), %r14
movq %r15, 0x60(%rsp)
movl %r15d, %ecx
xorl $0x1, %ecx
movl %ecx, 0x9c(%rsp)
movl %ebp, %ecx
movq %rcx, 0xd0(%rsp)
xorl %edi, %edi
movl $0x0, 0x34(%rsp)
cmpq %rdi, 0xd0(%rsp)
je 0x1c44d1
cmpl $0x0, 0xc(%rax)
js 0x1c44c5
movq %r12, 0x78(%rsp)
movl $0x0, %r15d
cmpl %r13d, 0x60(%rsp)
je 0x1c40b0
movl (%rax), %r15d
movl 0x8(%rax), %ebp
movl %ebp, %r12d
shrl %r12d
cmpl 0xc8(%rsp), %r12d
je 0x1c4574
movq $0x0, 0x20(%rsp)
movq %rax, 0x80(%rsp)
imulq $0x1c, %rbp, %r13
addq (%rbx), %r13
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
movq %rdi, 0x28(%rsp)
jne 0x1c411d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x40(%rsp), %rdx
movq 0x28(%rdx), %rdx
movq 0x60(%rsp), %rsi
movl 0x4(%rdx,%rsi,8), %edx
addl %edi, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
addl 0xa4(%rsp), %r15d
cmpq 0xc0(%rsp), %r15
ja 0x1c457d
movq %rbp, 0x38(%rsp)
xorl $0x1, %ebp
movq 0x40(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
movl %eax, %r8d
movq 0x58(%rsp), %rax
movq 0x248(%rax), %rax
movl (%rax,%r12,4), %r14d
movl $0x0, %r12d
cmpl $0x0, 0x118(%rsp)
jne 0x1c431e
cmpl $0x2, 0xa0(%rsp)
movq %r14, 0x50(%rsp)
movl %r8d, 0x1c(%rsp)
jb 0x1c4240
movq 0x130(%rsp), %r14
movl (%r14), %eax
movq %rax, 0x68(%rsp)
movq 0x138(%rsp), %r12
andq $0x0, (%r12)
leaq 0x88(%rsp), %rax
movq %rax, (%rsp)
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
movq 0xa8(%rsp), %r8
leaq 0x90(%rsp), %r9
callq 0x1c2c0a
movq 0x68(%rsp), %rax
movq %rax, (%r14)
andq $0x0, (%r12)
movq 0x90(%rsp), %rdx
movq 0x88(%rsp), %rcx
movq %rcx, %rax
subq %rdx, %rax
jbe 0x1c4245
pushq $0xa
popq %r12
testq %rdx, %rdx
movq 0x50(%rsp), %r14
movl 0x1c(%rsp), %r8d
je 0x1c4252
imulq $0xa, %rax, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r12
jmp 0x1c4252
xorl %r12d, %r12d
jmp 0x1c4252
xorl %r12d, %r12d
movq 0x50(%rsp), %r14
movl 0x1c(%rsp), %r8d
cmpl $0x2, %r8d
jb 0x1c431e
movq 0x130(%rsp), %r14
movl (%r14), %eax
movq %rax, 0x68(%rsp)
movq 0x138(%rsp), %r14
andq $0x0, (%r14)
movq 0x40(%rsp), %rdi
movl %ebp, %esi
movl 0x9c(%rsp), %edx
callq 0x1de62d
testq %rax, %rax
je 0x1c4663
leaq 0x88(%rsp), %rcx
movq %rcx, (%rsp)
movq 0x58(%rsp), %rdi
movq %rax, %rsi
movq 0x130(%rsp), %rbp
movq %rbp, %rdx
movq %r14, %rcx
movq 0xa8(%rsp), %r8
leaq 0x90(%rsp), %r9
callq 0x1c2c0a
movq 0x68(%rsp), %rax
movq %rax, (%rbp)
andq $0x0, (%r14)
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rcx
movq %rcx, %rdx
subq %rsi, %rdx
jbe 0x1c4314
pushq $0xa
popq %rax
testq %rsi, %rsi
movq 0x50(%rsp), %r14
movl 0x1c(%rsp), %r8d
je 0x1c430b
imulq $0xa, %rdx, %rax
xorl %edx, %edx
divq %rcx
cmpq %r12, %rax
cmovaq %rax, %r12
jmp 0x1c431e
movq 0x50(%rsp), %r14
movl 0x1c(%rsp), %r8d
movl 0x18(%r13), %eax
testl %eax, %eax
js 0x1c43b2
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x1c4364
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, %ebp
callq 0x2370
movl %ebp, %r8d
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %esi, (%rax,%rcx,4)
movq 0x60(%rsp), %rax
movl %eax, (%r13)
movl %r15d, 0x4(%r13)
addl 0xb0(%rsp), %r12d
movl %r12d, 0x8(%r13)
movq 0x70(%rsp), %rax
addl %r14d, %eax
movl %eax, 0xc(%r13)
orl $0x80000000, %r8d       # imm = 0x80000000
movq 0x78(%rsp), %r12
incl %r12d
movl %r8d, %eax
jmp 0x1c440f
addq 0xb0(%rsp), %r12
movl 0x8(%r13), %ecx
cmpq %rcx, %r12
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rsi
jae 0x1c43e5
movq 0x60(%rsp), %rcx
movl %ecx, (%r13)
movl %r12d, 0x8(%r13)
addl 0x70(%rsp), %r14d
movl %r14d, 0xc(%r13)
jmp 0x1c4400
jne 0x1c4400
movq 0x70(%rsp), %rcx
addq %r14, %rcx
movl 0xc(%r13), %edx
cmpq %rdx, %rcx
ja 0x1c43cd
jne 0x1c4400
cmpl 0x4(%r13), %r15d
ja 0x1c43cd
cmpl 0x4(%r13), %r15d
jae 0x1c440a
movl %r15d, 0x4(%r13)
movq 0x78(%rsp), %r12
addl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%r13)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x130(%rsp), %r14
je 0x1c4439
movl 0x4c(%rsp), %r13d
jmp 0x1c44bd
movq 0x40(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rsi, %r15
callq 0x1a5422
testl %eax, %eax
movl 0x4c(%rsp), %r13d
je 0x1c449e
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x1c4487
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %esi, (%rax,%rcx,4)
jmp 0x1c44ba
movl %r15d, 0x30(%rsp)
cmpl $0x0, 0x34(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x34(%rsp)
movq 0x28(%rsp), %rdi
jne 0x1c457d
decl %r12d
movq 0x80(%rsp), %rax
incq %rdi
addq $0x18, %rax
jmp 0x1c4083
cmpl $0x1, 0x34(%rsp)
je 0x1c4542
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x1c4574
cmpq $0x1, %rcx
jne 0x1c3feb
testl %r12d, %r12d
jne 0x1c3feb
movq 0x18(%rbx), %rax
movl 0x48(%rsp), %r8d
movq (%rbx), %rcx
movl (%rax), %edx
imulq $0x1c, %rdx, %rdx
movl 0x8(%rcx,%rdx), %edx
movq 0x140(%rsp), %rsi
movl %edx, (%rsi)
movl (%rax), %eax
imulq $0x1c, %rax, %rax
movl 0xc(%rcx,%rax), %eax
movq 0x148(%rsp), %rcx
movl %eax, (%rcx)
cmpl $0x0, 0x118(%rsp)
je 0x1c45e2
pushq $0x1
popq %rax
jmp 0x1c462a
cmpl $-0x1, 0x30(%rsp)
je 0x1c4574
movq $0x0, 0x20(%rsp)
testl %r12d, %r12d
jne 0x1c457d
cmpq $0x0, 0x8(%rbx)
jne 0x1c457d
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0x1c4634
xorl %ecx, %ecx
jmp 0x1c464f
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
xorps %xmm0, %xmm0
movl %eax, %eax
cmpq %rax, 0x38(%rbx)
jbe 0x1c45ab
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
movl (%rdx,%rax,4), %edx
imulq $0x1c, %rdx, %rdx
movups %xmm0, 0x4(%rcx,%rdx)
andl $0x0, 0x14(%rcx,%rdx)
andb $0x7f, 0x1b(%rcx,%rdx)
incl %eax
jmp 0x1c4582
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
jne 0x1c45ca
movq 0x148(%rsp), %rax
orl $-0x1, (%rax)
movq 0x140(%rsp), %rax
orl $-0x1, (%rax)
movl %ecx, %r14d
movq %r14, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x120(%rsp)
je 0x1c4601
movq 0xb8(%rsp), %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x1c3db2
jmp 0x1c462a
movq 0x138(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r14, (%rsp)
movq 0x58(%rsp), %rdi
movl %r13d, %edx
movq %rbx, %rcx
movl 0x110(%rsp), %r9d
callq 0x1c3c93
movq %rax, 0x20(%rsp)
jmp 0x1c457d
movq $0x2, 0x10(%rbx)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl 0x30(%rsp), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x1c44fc
leaq 0x35be6(%rip), %rdi     # 0x1fa250
leaq 0x32e68(%rip), %rsi     # 0x1f74d9
leaq 0x35bdb(%rip), %rcx     # 0x1fa253
movl $0x2708, %edx           # imm = 0x2708
callq 0x2110