// File: /workspace/github_cpp_makefile_star2_O2/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 %rdx, %r14
movq (%rdi), %rax
movl %esi, %r12d
shrl %r12d
imulq $0x78, %r12, %rdx
cmpb $0x0, 0x70(%rax,%rdx)
je 0x1c7e15
xorl %ecx, %ecx
jmp 0x1c81b6
movl %esi, %ebp
movq %rcx, (%rsp)
movq %rdi, 0x8(%rsp)
xorl %edx, %edx
callq 0x1df024
xorl %ecx, %ecx
cmpl $0x2, %eax
jl 0x1c81b6
movq (%rsp), %r8
andq $0x0, 0x50(%r8)
andq $0x0, 0x38(%r8)
andq $0x0, 0x20(%r8)
andq $0x0, 0x8(%r8)
movq (%r8), %rax
movl %ebp, %ecx
imulq $0x1c, %rcx, %rcx
xorps %xmm0, %xmm0
andl $0x0, 0x14(%rax,%rcx)
movups %xmm0, 0x4(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%r8)
je 0x1c7e70
movq 0x18(%r8), %rax
xorl %ecx, %ecx
jmp 0x1c7e90
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
movq $0x0, 0x20(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x28(%rsp)
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movq %r14, 0x50(%rsp)
movl %ebp, 0x18(%rsp)
movq %r12, 0x48(%rsp)
movq (%r8), %rax
movq 0x18(%r8), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%r8)
movl -0x4(%rdx,%rcx,4), %edx
imulq $0x1c, %rdx, %rcx
movl 0x4(%rax,%rcx), %esi
movl %esi, 0x44(%rsp)
movl 0x8(%rax,%rcx), %eax
movl %eax, 0x2c(%rsp)
movl %edx, %eax
shrl %eax
movq %rdx, 0x38(%rsp)
movl %edx, %ecx
andl $0x1, %ecx
imulq $0x78, %rax, %rax
addq (%rdi), %rax
imulq $0x18, %rcx, %rcx
movl 0x10(%rcx,%rax), %edx
movq 0x20(%rcx,%rax), %rax
shlq $0x5, %rdx
movq %rdx, 0x60(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x14(%rsp)
movq %rax, 0x58(%rsp)
cmpq %rcx, 0x60(%rsp)
je 0x1c812f
cmpl $0x0, 0xc(%rax,%rcx)
js 0x1c8126
movl %r15d, 0x1c(%rsp)
xorl %r15d, %r15d
cmpl %ebp, 0x38(%rsp)
je 0x1c7f45
movl (%rax,%rcx), %r15d
movl 0x8(%rax,%rcx), %r13d
movl %r13d, %ebx
shrl %ebx
cmpl %r12d, %ebx
je 0x1c8183
addl 0x44(%rsp), %r15d
cmpq %r14, %r15
ja 0x1c8183
movq %rcx, 0x30(%rsp)
imulq $0x1c, %r13, %r12
addq (%r8), %r12
movl %r13d, %esi
xorl $0x1, %esi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
movq %r8, %rbp
callq 0x1df024
movq 0x8(%rsp), %rdi
movl %eax, %r14d
movq (%rdi), %rax
imulq $0x78, %rbx, %rcx
movl 0x4(%rax,%rcx), %ebx
movl 0x18(%r12), %eax
testl %eax, %eax
js 0x1c8027
movq 0x38(%rbp), %rcx
movq 0x48(%rbp), %rax
cmpq 0x40(%rbp), %rcx
movq %rbp, %r8
jne 0x1c7fe7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq (%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x38(%r8), %rcx
movl 0x18(%rsp), %ebp
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r8)
movl %r13d, (%rax,%rcx,4)
movq 0x38(%rsp), %rax
movl %eax, (%r12)
movl %r15d, 0x4(%r12)
addl 0x2c(%rsp), %ebx
movl %ebx, 0x8(%r12)
orl $0x80000000, %r14d      # imm = 0x80000000
movl 0x1c(%rsp), %r15d
incl %r15d
movl %r14d, %eax
movq 0x50(%rsp), %r14
jmp 0x1c8069
addl 0x2c(%rsp), %ebx
cmpl 0x8(%r12), %ebx
jae 0x1c8042
movq 0x38(%rsp), %rcx
movl %ecx, (%r12)
movl %ebx, 0x8(%r12)
jmp 0x1c804b
jne 0x1c804b
cmpl 0x4(%r12), %r15d
ja 0x1c8032
cmpl 0x4(%r12), %r15d
movq (%rsp), %r8
movq 0x50(%rsp), %r14
movl 0x18(%rsp), %ebp
jae 0x1c8064
movl %r15d, 0x4(%r12)
movl 0x1c(%rsp), %r15d
addl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%r12)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x1c8096
movq 0x48(%rsp), %r12
movq 0x58(%rsp), %rax
movq 0x30(%rsp), %rcx
jmp 0x1c8126
movl %r13d, %esi
xorl %edx, %edx
callq 0x1df024
testl %eax, %eax
movq 0x48(%rsp), %r12
je 0x1c80fd
movq (%rsp), %r8
movq 0x8(%r8), %rcx
movq 0x18(%r8), %rax
cmpq 0x10(%r8), %rcx
jne 0x1c80e5
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
movq 0x8(%rsp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %r13d, (%rax,%rcx,4)
movq 0x30(%rsp), %rcx
jmp 0x1c811e
movl %r13d, 0x28(%rsp)
cmpl $0x0, 0x14(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movq (%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rcx
jne 0x1c8183
decl %r15d
movq 0x58(%rsp), %rax
addq $0x20, %rcx
jmp 0x1c7f1d
cmpl $0x1, 0x14(%rsp)
je 0x1c815c
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x1c8183
cmpq $0x1, %rcx
jne 0x1c7ebc
testl %r15d, %r15d
jne 0x1c7ebc
pushq $0x1
popq %rax
movq %rax, 0x20(%rsp)
jmp 0x1c8183
movq $0x0, 0x20(%rsp)
movl 0x28(%rsp), %ebx
cmpl $-0x1, %ebx
je 0x1c8183
testl %r15d, %r15d
jne 0x1c8183
cmpq $0x0, 0x8(%r8)
je 0x1c81c8
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
xorps %xmm0, %xmm0
movl %eax, %eax
cmpq %rax, 0x38(%r8)
jbe 0x1c81b1
movq (%r8), %rcx
movq 0x48(%r8), %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 0x1c8188
movq 0x20(%rsp), %rcx
movq %rcx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x10(%r8)
movq 0x18(%r8), %rax
je 0x1c81d7
xorl %ecx, %ecx
jmp 0x1c81f6
movq $0x2, 0x10(%r8)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %ebx, (%rax,%rcx,4)
jmp 0x1c8152