// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_bub_pop1_label(asg_t*, unsigned int, unsigned long, buf_s_t*)
uint64_t asg_bub_pop1_label(asg_t *g, uint32_t v0, uint64_t max_dist, buf_s_t *b)
{   
	uint32_t i, n_pending = 0, /**is_first = 1,**/ n_tips, tip_end;
	uint64_t n_pop = 0;
	if (g->seq[v0>>1].del) return 0; // already deleted
    if(get_real_length(g, v0, NULL)<2) return 0;

	///S saves nodes with all incoming edges visited
	b->S.n = b->b.n = b->e.n = 0;
	///for each node, b->a saves all related information
	b->a[v0].d = 0;
	///b->S is the nodes with all incoming edges visited
	kv_push(uint32_t, b->S, v0); n_tips = 0; tip_end = (uint32_t)-1;

	do {
		///v is a node that all incoming edges have been visited
		///d is the distance from v0 to v
		uint32_t v = kv_pop(b->S), d = b->a[v].d;
		uint32_t nv = asg_arc_n(g, v);
		asg_arc_t *av = asg_arc_a(g, v);
		///why we have this assert?
		///assert(nv > 0);
		///all out-edges of v
		for (i = 0; i < nv; ++i) { // loop through v's neighbors
            ///if this edge has been deleted
			if (av[i].del) continue;

			uint32_t w = av[i].v, l = (uint32_t)av[i].ul; // v->w with length l
			binfo_s_t *t = &b->a[w];
			///that means there is a circle, directly terminate the whole bubble poping
			///if (w == v0) goto pop_reset;
            if ((w>>1) == (v0>>1)) goto pop_reset;
            /****************************may have bugs********************************/
            ///important when poping at long untig graph
            // if(is_first) l = 0;
            /****************************may have bugs********************************/
			///find a too far path? directly terminate the whole bubble poping
			if ((uint64_t)d + (uint64_t)l > max_dist) break; // too far

            ///if this node
			if (t->s == 0) { // this vertex has never been visited
				kv_push(uint32_t, b->b, w); // save it for revert
				///t->p is the parent node of 
				///t->s = 1 means w has been visited
				///d is len(v0->v), l is len(v->w), so t->d is len(v0->w)
				t->p = v, t->s = 1, t->d = d + l;
				///incoming edges of w
				///t->r = count_out(g, w^1);
                t->r = get_real_length(g, w^1, NULL);
				++n_pending;
			} else { // visited before
                ///it is the shortest edge
				if (d + l < t->d) t->d = d + l, t->p = v; // update dist
			}
			///assert(t->r > 0);
			//if all incoming edges of w have visited
			//push it to b->S
			if (--(t->r) == 0) {
                uint32_t x = get_real_length(g, w, NULL);
                /****************************may have bugs for bubble********************************/
                if(x > 0)
                {
                    kv_push(uint32_t, b->S, w);
                }
                else
                {
                    ///at most one tip
                    if(n_tips != 0) goto pop_reset;
                    n_tips++;
                    tip_end = w;
                }
                /****************************may have bugs for bubble********************************/
				--n_pending;
			}
		}
        // is_first = 0;
        //if found a tip
        /****************************may have bugs for bubble********************************/
        if(n_tips == 1)
        {
            if(tip_end != (uint32_t)-1 && n_pending == 0 && b->S.n == 0)
            {
                kv_push(uint32_t, b->S, tip_end);
                break;
            }
            else
            {
                goto pop_reset;
            }
        }
        /****************************may have bugs for bubble********************************/
		///if i < nv, that means (d + l > max_dist)
		if (i < nv || b->S.n == 0) goto pop_reset;
	} while (b->S.n > 1 || n_pending);

    n_pop = 1;
pop_reset:
	for (i = 0; i < b->b.n; ++i) { // clear the states of visited vertices
		binfo_s_t *t = &b->a[b->b.a[i]];
		t->s = t->d = 0;
	}
	return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, 0x30(%rsp)
movq 0x20(%rdi), %rax
movl %esi, %ecx
shrl %ecx
movq %rcx, 0x38(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0xa84b6
movl %esi, %ebp
movq %rdi, %r15
xorl %r14d, %r14d
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0xa84b9
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movq (%rbx), %rax
movl %ebp, %ecx
imulq $0xc, %rcx, %rcx
andl $0x0, 0x4(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
je 0xa84cb
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0xa84e7
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
xorl %edi, %edi
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %edx
imulq $0xc, %rdx, %rcx
movl 0x4(%rax,%rcx), %eax
movq %rax, 0x18(%rsp)
movq 0x8(%r15), %rax
movq 0x28(%r15), %rcx
movq %rdx, 0x28(%rsp)
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %rdx
movq %rdx, 0x40(%rsp)
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %rsi
addq $0xc, %rsi
xorl %ebp, %ebp
movl $0x0, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
cmpq %rbp, 0x40(%rsp)
je 0xa86f3
cmpl $0x0, (%rsi,%rbp)
js 0xa86ea
movl -0x4(%rsi,%rbp), %r13d
movl %r13d, %eax
shrl %eax
cmpl 0x38(%rsp), %eax
je 0xa8738
movq %r15, %r14
movq -0xc(%rsi,%rbp), %r15
movl %r15d, %eax
addq 0x18(%rsp), %rax
cmpq 0x30(%rsp), %rax
ja 0xa871a
imulq $0xc, %r13, %r12
addq (%rbx), %r12
movl 0x8(%r12), %ecx
testl %ecx, %ecx
js 0xa8639
movl %edi, 0x24(%rsp)
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0xa85e1
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x28(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movl %r13d, (%rax,%rcx,4)
movq 0x28(%rsp), %rax
movl %eax, (%r12)
orb $-0x80, 0xb(%r12)
addl 0x18(%rsp), %r15d
movl %r15d, 0x4(%r12)
movl %r13d, %esi
xorl $0x1, %esi
movq %r14, %r15
movq %r14, %rdi
xorl %edx, %edx
callq 0xc2e7a
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x8(%r12), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl 0x24(%rsp), %edi
incl %edi
movq 0x10(%rsp), %rsi
jmp 0xa8656
addl 0x18(%rsp), %r15d
cmpl 0x4(%r12), %r15d
jae 0xa8653
movl %r15d, 0x4(%r12)
movq 0x28(%rsp), %rax
movl %eax, (%r12)
movq %r14, %r15
leal -0x1(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %eax, %ecx
movl %ecx, 0x8(%r12)
testl %eax, %eax
jne 0xa86ea
movl %edi, %r14d
movq %r15, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
je 0xa86cd
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0xa86b7
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
movl %r14d, %edi
movq 0x10(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %r13d, (%rax,%rcx,4)
jmp 0xa86e8
movl %r13d, 0xc(%rsp)
cmpl $0x0, 0x8(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x8(%rsp)
movl %r14d, %edi
movq 0x10(%rsp), %rsi
jne 0xa8738
decl %edi
addq $0x18, %rbp
jmp 0xa8551
cmpl $0x1, 0x8(%rsp)
je 0xa8721
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0xa8738
cmpq $0x1, %rcx
jne 0xa84f6
testl %edi, %edi
jne 0xa84f6
jmp 0xa879a
cmpl $0x1, 0x8(%rsp)
jne 0xa8738
xorl %r14d, %r14d
movl 0xc(%rsp), %ebp
cmpl $-0x1, %ebp
je 0xa873b
testl %edi, %edi
jne 0xa873b
cmpq $0x0, 0x8(%rbx)
je 0xa8765
xorl %r14d, %r14d
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x20(%rbx)
jbe 0xa84b9
movq (%rbx), %rcx
movq 0x30(%rbx), %rdx
movl (%rdx,%rax,4), %edx
imulq $0xc, %rdx, %rdx
andl $0x0, 0x4(%rcx,%rdx)
andb $0x7f, 0xb(%rcx,%rdx)
incl %eax
jmp 0xa873d
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0xa8774
xorl %ecx, %ecx
jmp 0xa878f
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 %ebp, (%rax,%rcx,4)
pushq $0x1
popq %r14
jmp 0xa873b