// File: /workspace/github_cpp_makefile_star2_O3/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 0x20(%rdi), %rax
movl %esi, %r10d
shrl %r10d
cmpl $0x0, (%rax,%r10,8)
js 0xe5ee6
movq %rcx, %rbx
movq %rdx, %r8
movl %esi, %ebp
movq 0x28(%rdi), %rcx
movl %esi, %eax
movq (%rcx,%rax,8), %rcx
xorl %r14d, %r14d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rcx, %rdx
je 0xe5ee9
movq 0x8(%rdi), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movl (%rcx,%rsi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r14d
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xe5ec9
cmpl $0x2, %r14d
jge 0xe5efb
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
movq %rcx, 0x38(%rbx)
movq %rcx, 0x20(%rbx)
movq %rcx, 0x8(%rbx)
movq (%rbx), %rdx
leaq (%rax,%rax,2), %rax
movl $0x0, 0x4(%rdx,%rax,4)
cmpq %rcx, 0x10(%rbx)
movq %r8, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq %r10, 0x10(%rsp)
je 0xe5f35
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0xe5f62
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
xorl %r14d, %r14d
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %r11d, %r11d
movq %rbx, 0x38(%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %edx
movq 0x28(%rdi), %rcx
movq (%rcx,%rdx,8), %rcx
testl %ecx, %ecx
je 0xe6244
movq %rdx, 0x28(%rsp)
leaq (%rdx,%rdx,2), %rdx
movq %rcx, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %r12
shlq $0x3, %r12
addq 0x8(%rdi), %r12
movl 0x4(%rax,%rdx,4), %esi
movl %ecx, %eax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
movq %rsi, 0x8(%rsp)
movq %r12, 0x30(%rsp)
leaq (,%r13,2), %rax
addq %r13, %rax
cmpl $0x0, 0xc(%r12,%rax,8)
js 0xe622f
leaq (%r12,%rax,8), %rax
movl 0x8(%rax), %ebp
movl %ebp, %ecx
shrl %ecx
cmpl %r10d, %ecx
je 0xe62c5
movq (%rax), %r15
movl %r15d, %eax
addq %rsi, %rax
cmpq %r8, %rax
ja 0xe6262
leaq (,%rbp,2), %r12
addq %rbp, %r12
shlq $0x2, %r12
addq (%rbx), %r12
movl 0x8(%r12), %eax
testl %eax, %eax
js 0xe610b
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0xe608a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x28(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r11d, %ebx
callq 0x2370
movq 0x8(%rsp), %rsi
movl %ebx, %r11d
movq 0x38(%rsp), %rbx
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movl %ebp, (%rax,%rcx,4)
movq 0x28(%rsp), %rax
movl %eax, (%r12)
orb $-0x80, 0xb(%r12)
addl %esi, %r15d
movl %r15d, 0x4(%r12)
movq %rbp, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rax
je 0xe6125
movq 0x8(%rdi), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
xorl %eax, %eax
movl (%rcx,%rsi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %eax
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xe60e8
orl $0x80000000, %eax       # imm = 0x80000000
movq 0x8(%rsp), %rsi
jmp 0xe612a
addl %esi, %r15d
cmpl 0x4(%r12), %r15d
jae 0xe612d
movl %r15d, 0x4(%r12)
movq 0x28(%rsp), %rcx
movl %ecx, (%r12)
jmp 0xe612d
movl $0x80000000, %eax       # imm = 0x80000000
incl %r11d
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x8(%r12)
testl %ecx, %ecx
je 0xe6150
movq 0x30(%rsp), %r12
jmp 0xe622f
movq 0x28(%rdi), %rax
movq (%rax,%rbp,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
movq 0x30(%rsp), %r12
je 0xe6216
movq 0x8(%rdi), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xe618e
testl %esi, %esi
movq 0x8(%rsp), %rsi
je 0xe6216
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0xe6204
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %rbx, %r15
movl %r11d, %ebx
callq 0x2370
movq 0x8(%rsp), %rsi
movl %ebx, %r11d
movq %r15, %rbx
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r15)
movq 0x8(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %ebp, (%rax,%rcx,4)
movl (%rsp), %eax
movl %eax, %ebp
jmp 0xe6229
cmpl $0x0, 0x4(%rsp)
movl $0x1, 0x4(%rsp)
jne 0xe62c5
decl %r11d
movl %ebp, (%rsp)
incq %r13
cmpq 0x40(%rsp), %r13
jne 0xe5fd9
cmpl $0x1, 0x4(%rsp)
je 0xe6269
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0xe62c5
cmpq $0x1, %rcx
jne 0xe5f7a
testl %r11d, %r11d
jne 0xe5f7a
jmp 0xe62bf
cmpl $0x1, 0x4(%rsp)
jne 0xe6280
xorl %r14d, %r14d
movl (%rsp), %esi
cmpl $-0x1, %esi
je 0xe62c5
testl %r11d, %r11d
jne 0xe62c5
cmpq $0x0, 0x8(%rbx)
je 0xe6285
xorl %r14d, %r14d
jmp 0xe62c5
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0xe6294
xorl %ecx, %ecx
jmp 0xe62b4
movq $0x2, 0x10(%rbx)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2370
movl (%rsp), %esi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %esi, (%rax,%rcx,4)
movl $0x1, %r14d
cmpq $0x0, 0x20(%rbx)
je 0xe5ee9
movl $0x1, %eax
xorl %ecx, %ecx
movq (%rbx), %rdx
movq 0x30(%rbx), %rsi
movl (%rsi,%rcx,4), %ecx
leaq (%rcx,%rcx,2), %rcx
movl $0x0, 0x4(%rdx,%rcx,4)
andb $0x7f, 0xb(%rdx,%rcx,4)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x20(%rbx)
ja 0xe62d7
jmp 0xe5ee9