// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x70(%rax)
jne 0x351ced
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x37d7e0
cmpl $0x2, %eax
jge 0x351cfa
movq $0x0, -0x8(%rbp)
jmp 0x352431
movq -0x28(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x10(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x14(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0xc(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x351e0b
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x351dd0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0x80(%rbp)
jmp 0x351ddb
movl $0x2, %eax
movq %rax, -0x80(%rbp)
jmp 0x351ddb
movq -0x80(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x14(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x351e2e
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $-0x1, %rsi
movq %rsi, 0x8(%rdx)
movl -0x4(%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x2c(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x2c(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x35226d
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x351f0d
jmp 0x35225f
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x351f48
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0x351f5f
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x30(%rbp), %eax
shrl %eax
movl -0x14(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x351f82
movl $0x1, -0x44(%rbp)
jmp 0x35226d
movl -0x54(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, %eax
cmpq -0x20(%rbp), %rax
jbe 0x351f9c
movl $0x1, -0x44(%rbp)
jmp 0x35226d
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x37d7e0
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x3520d7
jmp 0x351fe2
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x352054
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x352013
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x352021
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x352021
movq -0x90(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x30(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x38(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x38(%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, (%rax)
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%rax)
movl -0x54(%rbp), %ecx
addl -0x50(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x58(%rbp), %ecx
addl -0x68(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x3c(%rbp), %edx
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x352157
movl $0x0, -0x64(%rbp)
movl -0x58(%rbp), %eax
addl -0x68(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x3520f6
movl $0x1, -0x64(%rbp)
jmp 0x35211d
movl -0x58(%rbp), %eax
addl -0x68(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x35211b
movl -0x54(%rbp), %eax
addl -0x50(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0x35211b
movl $0x1, -0x64(%rbp)
jmp 0x35211d
cmpl $0x0, -0x64(%rbp)
je 0x352139
movl -0x2c(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, (%rax)
movl -0x58(%rbp), %ecx
addl -0x68(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x54(%rbp), %eax
addl -0x50(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x352155
movl -0x54(%rbp), %ecx
addl -0x50(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x352157
movq -0x78(%rbp), %rcx
movl 0x18(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl $-0x1, %eax
movl 0x18(%rcx), %edx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %eax, %edx
movl %edx, 0x18(%rcx)
cmpl $0x0, %eax
jne 0x35225d
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x37d7e0
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jbe 0x352236
jmp 0x3521a1
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x352213
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x3521d2
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0x3521e0
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0x3521e0
movq -0x98(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x30(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x352254
cmpl $0x0, -0x48(%rbp)
je 0x352245
movl $0x1, -0x44(%rbp)
jmp 0x35226d
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x35225f
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x351ee3
cmpl $0x0, -0x44(%rbp)
je 0x352278
jmp 0x352391
cmpl $0x1, -0x48(%rbp)
jne 0x352345
cmpl $-0x1, -0x4c(%rbp)
je 0x35233c
cmpl $0x0, -0x5c(%rbp)
jne 0x35233c
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x35233c
jmp 0x3522a7
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x352319
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x3522d8
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x3522e6
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x3522e6
movq -0xa0(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x4c(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x352391
movl $0x1, -0x44(%rbp)
jmp 0x352391
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jb 0x352358
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x352361
movl $0x1, -0x44(%rbp)
jmp 0x352391
jmp 0x352363
movq -0x28(%rbp), %rcx
movb $0x1, %al
cmpq $0x1, 0x8(%rcx)
movb %al, -0xa1(%rbp)
ja 0x352383
cmpl $0x0, -0x5c(%rbp)
setne %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x351e2e
cmpl $0x0, -0x44(%rbp)
jne 0x35239e
movl $0x1, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x35242a
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3523a5
movl -0x40(%rbp), %eax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nop