// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rcx, %r8
movq (%rdi), %rcx
movl %esi, %r13d
shrl %r13d
imulq $0x78, %r13, %rax
cmpb $0x0, 0x70(%rcx,%rax)
je 0x215d36
xorl %r14d, %r14d
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r9
movl %esi, %r10d
movl %esi, %edx
andl $0x1, %edx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %eax
xorl %r14d, %r14d
testq %rax, %rax
je 0x215d24
movq 0x20(%rcx,%rdx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movl 0xc(%rcx,%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %r14d
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x215d5f
cmpl $0x2, %r14d
jl 0x215d21
xorl %eax, %eax
movq %rax, 0x50(%r8)
movq %rax, 0x38(%r8)
movq %rax, 0x20(%r8)
movq %rax, 0x8(%r8)
movq (%r8), %rcx
movl %r10d, %edx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rcx,%rsi)
movl $0x0, 0x14(%rcx,%rsi)
cmpq %rax, 0x10(%r8)
movq %r8, 0x20(%rsp)
movq %r9, 0x40(%rsp)
movl %r10d, 0xc(%rsp)
movq %rdi, 0x38(%rsp)
je 0x215dcf
movq 0x18(%r8), %rax
xorl %ecx, %ecx
jmp 0x215e01
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x38(%rsp), %rdi
movl 0xc(%rsp), %r10d
movq 0x40(%rsp), %r9
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
movl %r10d, (%rax,%rcx,4)
incq %rcx
xorl %r14d, %r14d
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq %r13, 0x48(%rsp)
movq (%r8), %rax
movq 0x18(%r8), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%r8)
movl -0x4(%rdx,%rcx,4), %r11d
movl %r11d, %ecx
shrl %ecx
movl %r11d, %edx
imulq $0x78, %rcx, %rcx
addq (%rdi), %rcx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %esi
movq %rsi, 0x70(%rsp)
testq %rsi, %rsi
je 0x21610b
leaq (%r11,%r11,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %r11, %rsi
movl 0x4(%rax,%rsi), %ebp
movl %ebp, 0x54(%rsp)
movl 0x8(%rax,%rsi), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rcx,%rdx,8), %r15
xorl %r12d, %r12d
movl $0x0, 0x14(%rsp)
movq %r11, 0x28(%rsp)
movq %r15, 0x58(%rsp)
movq %r12, %rax
shlq $0x5, %rax
cmpl $0x0, 0xc(%r15,%rax)
js 0x2160f6
movl %ebx, 0x18(%rsp)
addq %r15, %rax
xorl %ebx, %ebx
cmpl %r10d, %r11d
je 0x215eae
movl (%rax), %ebx
movl 0x8(%rax), %edx
movl %edx, %ecx
shrl %ecx
cmpl %r13d, %ecx
je 0x216150
addl 0x54(%rsp), %ebx
cmpq %r9, %rbx
ja 0x216150
movq (%rdi), %rax
movq %rdx, 0x30(%rsp)
movl %edx, %esi
andl $0x1, %esi
movq %rsi, 0x60(%rsp)
xorq $0x1, %rsi
imulq $0x78, %rcx, %r13
leaq (%rax,%r13), %rdx
leaq (%rsi,%rsi,2), %rsi
movl 0x10(%rdx,%rsi,8), %ecx
testq %rcx, %rcx
je 0x215f2a
movq 0x20(%rdx,%rsi,8), %rdx
shlq $0x5, %rcx
xorl %esi, %esi
xorl %r15d, %r15d
movl 0xc(%rdx,%rsi), %r11d
notl %r11d
shrl $0x1f, %r11d
addl %r11d, %r15d
addq $0x20, %rsi
cmpq %rsi, %rcx
jne 0x215f04
orl $0x80000000, %r15d      # imm = 0x80000000
movq 0x28(%rsp), %r11
jmp 0x215f30
movl $0x80000000, %r15d      # imm = 0x80000000
movq 0x30(%rsp), %rsi
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rbp
addq %rsi, %rbp
addq (%r8), %rbp
movq %r13, 0x68(%rsp)
movl 0x4(%rax,%r13), %r13d
movl 0x18(%rbp), %eax
testl %eax, %eax
js 0x215fd0
movq 0x38(%r8), %rcx
movq 0x48(%r8), %rax
cmpq 0x40(%r8), %rcx
jne 0x215fa8
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %r11
movq 0x38(%rsp), %rdi
movl 0xc(%rsp), %r10d
movq 0x40(%rsp), %r9
movq 0x20(%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x38(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r8)
movl %esi, (%rax,%rcx,4)
movl %r11d, (%rbp)
movl %ebx, 0x4(%rbp)
addl 0x1c(%rsp), %r13d
movl %r13d, 0x8(%rbp)
movl 0x18(%rsp), %ebx
incl %ebx
movq 0x48(%rsp), %r13
jmp 0x216000
addl 0x1c(%rsp), %r13d
cmpl 0x8(%rbp), %r13d
jae 0x215fe5
movl %r11d, (%rbp)
movl %r13d, 0x8(%rbp)
jmp 0x215fec
jne 0x215fec
cmpl 0x4(%rbp), %ebx
ja 0x215fdb
cmpl 0x4(%rbp), %ebx
jae 0x215ff4
movl %ebx, 0x4(%rbp)
movl %eax, %r15d
movq 0x48(%rsp), %r13
movl 0x18(%rsp), %ebx
leal -0x1(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %r15d      # imm = 0x80000000
orl %eax, %r15d
movl %r15d, 0x18(%rbp)
testl %eax, %eax
je 0x216025
movq 0x58(%rsp), %r15
jmp 0x2160f6
movq 0x68(%rsp), %rdx
addq (%rdi), %rdx
movl 0x60(%rsp), %eax
leaq (%rax,%rax,2), %rcx
movl 0x10(%rdx,%rcx,8), %eax
testq %rax, %rax
movq 0x58(%rsp), %r15
je 0x2160e1
movq 0x20(%rdx,%rcx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%rcx,%rdx), %r11d
notl %r11d
shrl $0x1f, %r11d
addl %r11d, %esi
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x216054
testl %esi, %esi
movq 0x28(%rsp), %r11
movq 0x30(%rsp), %rsi
je 0x2160e1
movq 0x8(%r8), %rcx
movq 0x18(%r8), %rax
cmpq 0x10(%r8), %rcx
jne 0x2160ce
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %r11
movq 0x38(%rsp), %rdi
movl 0xc(%rsp), %r10d
movq 0x40(%rsp), %r9
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %esi, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
movl %eax, %esi
jmp 0x2160f0
cmpl $0x0, 0x14(%rsp)
movl $0x1, 0x14(%rsp)
jne 0x216150
decl %ebx
movl %esi, 0x10(%rsp)
incq %r12
cmpq 0x70(%rsp), %r12
jne 0x215e8b
cmpl $0x1, 0x14(%rsp)
je 0x21612e
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x216150
cmpq $0x1, %rcx
jne 0x215e1a
testl %ebx, %ebx
jne 0x215e1a
movl $0x1, %r14d
jmp 0x216150
movl %ebx, %ecx
movl 0x10(%rsp), %ebx
cmpl $-0x1, %ebx
setne %al
testl %ecx, %ecx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x21614d
cmpq $0x0, 0x8(%r8)
je 0x21619b
xorl %r14d, %r14d
cmpq $0x0, 0x38(%r8)
je 0x215d24
movl $0x1, %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movq (%r8), %rdx
movq 0x48(%r8), %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(%r8)
ja 0x216165
jmp 0x215d24
cmpq $0x0, 0x10(%r8)
movq 0x18(%r8), %rax
je 0x2161aa
xorl %ecx, %ecx
jmp 0x2161cc
movq $0x2, 0x10(%r8)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %ebx, (%rax,%rcx,4)
jmp 0x216126