// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_bub_pop1(usg_t*, unsigned int, unsigned long, buf_t*)
uint64_t usg_bub_pop1(usg_t *g, uint32_t v0, uint64_t max_dist, buf_t *x)
{
    uint32_t v, w, i, nv, kw, cnt = 0, fail_b = 0, n_tips = 0, tip_end = (uint32_t)-1;
    uint32_t l, d, c, n_pending = 0, z, to_replace, wc; usg_arc_t *av; binfo_t *t;
    if(g->a[v0>>1].del || undel_arcs(g, v0, NULL) < 2) return 0; // already deleted

    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; 
        nv = usg_arc_n(g, v); av = usg_arc_a(g, v); 
        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 = undel_arcs(g, w^1, NULL); wc = g->a[w>>1].occ;
            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->r = kw;
				++n_pending;
            } else {
                to_replace = 0;
                if((c + wc) < t->c) {
                    to_replace = 1;
                } else if(((c + wc) == t->c) && (d + l > t->d)) {
                    to_replace = 1;
                }
                if(to_replace) {
                    t->p = v; t->c = c + wc; 
                }
                if (d + l < t->d) t->d = d + l; // update dist
            }

            if (--(t->r) == 0) {
                z = undel_arcs(g, w, NULL);
                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; 
    }
    for (i = 0; i < x->b.n; ++i) { // clear the states of visited vertices
		t = &x->a[x->b.a[i]];
		t->s = t->c = t->d = t->m = t->nc = t->np = 0;
	}
    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r9
movq (%rdi), %rcx
movl %esi, %ebp
shrl %ebp
imulq $0x78, %rbp, %rax
cmpb $0x0, 0x70(%rcx,%rax)
je 0x20a9af
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r10
movl %esi, %edx
andl $0x1, %edx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %eax
xorl %ebx, %ebx
testq %rax, %rax
je 0x20a99d
movq %rdi, %r14
movq 0x20(%rcx,%rdx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movl 0xc(%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %ebx
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x20a9d7
cmpl $0x2, %ebx
jl 0x20a99b
xorl %ebx, %ebx
movq %rbx, 0x50(%r9)
movq %rbx, 0x38(%r9)
movq %rbx, 0x20(%r9)
movq %rbx, 0x8(%r9)
movq (%r9), %rax
movl %esi, %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax,%rdx)
movl $0x0, 0x14(%rax,%rdx)
cmpq %rbx, 0x10(%r9)
movq %r9, 0x18(%rsp)
movq %r10, 0x30(%rsp)
movl %esi, 0xc(%rsp)
jne 0x20aa5e
movq $0x2, 0x10(%r9)
movq 0x18(%r9), %rdi
movl $0x8, %esi
callq 0x2370
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %r10
movq 0x18(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl %esi, (%rcx,%rax,4)
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
xorl %r11d, %r11d
movq %rbp, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movq 0x18(%r9), %rcx
movq (%r9), %rax
movq 0x8(%r9), %rdx
leaq -0x1(%rdx), %rdi
movq %rdi, 0x8(%r9)
movl -0x4(%rcx,%rdx,4), %r8d
movl %r8d, %ecx
shrl %ecx
movl %r8d, %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq (%r14), %rcx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %r15d
xorl %edi, %edi
movq %r15, 0x60(%rsp)
testq %r15, %r15
setne %r12b
je 0x20ad9f
movq $0x0, 0x20(%rsp)
leaq (%r8,%r8,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r8, %rdi
movl 0x4(%rax,%rdi), %ebx
movl %ebx, 0x3c(%rsp)
movl 0x8(%rax,%rdi), %eax
movl %eax, 0x14(%rsp)
movq 0x20(%rcx,%rdx,8), %rcx
xorl %r15d, %r15d
movq %r8, 0x28(%rsp)
movq %rcx, 0x50(%rsp)
movq %r15, %rax
shlq $0x5, %rax
cmpl $0x0, 0xc(%rcx,%rax)
js 0x20ad7b
movb %r12b, 0xb(%rsp)
addq %rcx, %rax
xorl %r12d, %r12d
cmpl %esi, %r8d
je 0x20ab28
movl (%rax), %r12d
movl 0x8(%rax), %r13d
movl %r13d, %eax
shrl %eax
cmpl %ebp, %eax
je 0x20ad8f
addl 0x3c(%rsp), %r12d
cmpq %r10, %r12
ja 0x20ad8f
movq (%r14), %rcx
movl %r13d, %edx
shrl %edx
movl %r13d, %edi
notl %edi
andl $0x1, %edi
imulq $0x78, %rdx, %r8
addq %rcx, %r8
leaq (%rdi,%rdi,2), %rdi
movl 0x10(%r8,%rdi,8), %edx
testq %rdx, %rdx
je 0x20aba1
movl %r11d, %ebx
movq 0x20(%r8,%rdi,8), %r11
shlq $0x5, %rdx
xorl %edi, %edi
xorl %r14d, %r14d
movl 0xc(%r11,%rdi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %r14d
addq $0x20, %rdi
cmpq %rdi, %rdx
jne 0x20ab7d
orl $0x80000000, %r14d      # imm = 0x80000000
movl %ebx, %r11d
jmp 0x20aba7
movl $0x80000000, %r14d      # imm = 0x80000000
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rdx,%rdx,2), %rbp
addq %r13, %rbp
addq (%r9), %rbp
imulq $0x78, %rax, %rdi
movl 0x4(%rcx,%rdi), %ebx
cmpl $0x0, 0x18(%rbp)
movq 0x28(%rsp), %r8
js 0x20ac5a
movq 0x40(%r9), %rax
cmpq %rax, 0x38(%r9)
jne 0x20ac29
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x40(%r9)
movq %rdi, 0x58(%rsp)
movq 0x48(%r9), %rdi
shlq $0x2, %rsi
movl %r11d, 0x38(%rsp)
callq 0x2370
movq 0x58(%rsp), %rdi
movq 0x28(%rsp), %r8
movl 0x38(%rsp), %r11d
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %r10
movq 0x18(%rsp), %r9
movq %rax, 0x48(%r9)
movq 0x38(%r9), %rax
movq 0x48(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x38(%r9)
movl %r13d, (%rcx,%rax,4)
movl %r8d, (%rbp)
movl %r12d, 0x4(%rbp)
addl 0x14(%rsp), %ebx
movl %ebx, 0x8(%rbp)
movl %r14d, 0x18(%rbp)
incl %r11d
movq 0x48(%rsp), %r14
jmp 0x20ac83
addl 0x14(%rsp), %ebx
cmpl 0x8(%rbp), %ebx
jae 0x20ac6c
movl %r8d, (%rbp)
movl %ebx, 0x8(%rbp)
jmp 0x20ac74
jne 0x20ac74
cmpl 0x4(%rbp), %r12d
ja 0x20ac63
cmpl 0x4(%rbp), %r12d
movq 0x48(%rsp), %r14
jae 0x20ac83
movl %r12d, 0x4(%rbp)
movl 0x18(%rbp), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%rbp)
testl %ecx, %ecx
je 0x20aca7
movq 0x40(%rsp), %rbp
jmp 0x20ad76
movl %r13d, %eax
addq (%r14), %rdi
andl $0x1, %eax
leaq (%rax,%rax,2), %rcx
movl 0x10(%rdi,%rcx,8), %eax
testq %rax, %rax
movq 0x40(%rsp), %rbp
je 0x20ad5d
movq 0x20(%rdi,%rcx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
xorl %r8d, %r8d
movl 0xc(%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %r8d
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x20acd4
testl %r8d, %r8d
movq 0x28(%rsp), %r8
je 0x20ad5d
movq 0x10(%r9), %rax
cmpq %rax, 0x8(%r9)
jne 0x20ad3b
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r9)
movq 0x18(%r9), %rdi
shlq $0x2, %rsi
movl %r11d, %ebx
callq 0x2370
movq 0x28(%rsp), %r8
movl %ebx, %r11d
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %r10
movq 0x18(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl %r13d, (%rcx,%rax,4)
movl 0x10(%rsp), %eax
movl %eax, %r13d
movq 0x20(%rsp), %rax
jmp 0x20ad69
movl $0x1, %eax
cmpl $0x0, 0x20(%rsp)
jne 0x20ad8f
decl %r11d
movl %r13d, 0x10(%rsp)
movq %rax, 0x20(%rsp)
movq 0x50(%rsp), %rcx
incq %r15
cmpq 0x60(%rsp), %r15
setb %r12b
jne 0x20ab03
jmp 0x20ad94
movb 0xb(%rsp), %r12b
cmpl $0x1, 0x20(%rsp)
sete %dil
xorl %ebx, %ebx
testb $0x1, %r12b
jne 0x20ade6
testb %dil, %dil
jne 0x20adcd
movq 0x8(%r9), %rax
testq %rax, %rax
je 0x20ade6
cmpq $0x1, %rax
jne 0x20aa86
testl %r11d, %r11d
jne 0x20aa86
movl $0x1, %ebx
jmp 0x20ade6
xorl %ebx, %ebx
movl 0x10(%rsp), %ebp
cmpl $-0x1, %ebp
je 0x20ade6
testl %r11d, %r11d
jne 0x20ade6
cmpq $0x0, 0x8(%r9)
je 0x20ae31
xorl %ebx, %ebx
cmpq $0x0, 0x38(%r9)
je 0x20a99d
movl $0x1, %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movq (%r9), %rdx
movq 0x48(%r9), %rsi
movl (%rsi,%rcx,4), %ecx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movups %xmm0, 0x4(%rdx,%rsi)
movl $0x0, 0x14(%rdx,%rsi)
andb $0x7f, 0x1b(%rdx,%rsi)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r9)
ja 0x20adfb
jmp 0x20a99d
cmpq $0x0, 0x10(%r9)
jne 0x20ae57
movq $0x2, 0x10(%r9)
movq 0x18(%r9), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x18(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl %ebp, (%rcx,%rax,4)
jmp 0x20adc6