// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq 0x20(%rdi), %rax
movl %esi, %r8d
shrl %r8d
cmpl $0x0, (%rax,%r8,8)
js 0xe471d
movq %rcx, %r9
movq %rdx, %r10
movl %esi, %ebp
movq 0x28(%rdi), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rdx
xorl %eax, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rdx, %rsi
je 0xe471f
movq %r8, 0x38(%rsp)
movq 0x8(%rdi), %r8
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%r8,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r11d, %r11d
movl (%rdx,%r11), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %r11
cmpq %r11, %rsi
jne 0xe4701
cmpl $0x2, %eax
jge 0xe472e
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
movq %rdx, 0x38(%r9)
movq %rdx, 0x20(%r9)
movq %rdx, 0x8(%r9)
movq (%r9), %rax
leaq (%rcx,%rcx,2), %rcx
movl $0x0, 0x4(%rax,%rcx,4)
movq $0x0, 0x10(%rsp)
cmpq %rdx, 0x10(%r9)
movq %r9, 0x30(%rsp)
movq %r10, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
jne 0xe4792
movq $0x2, 0x10(%r9)
movq 0x18(%r9), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl %ebp, (%rcx,%rax,4)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq 0x38(%rsp), %r11
movq 0x18(%r9), %rax
movq (%r9), %rcx
movq 0x8(%r9), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, 0x8(%r9)
movl -0x4(%rax,%rdx,4), %esi
movq 0x28(%rdi), %rax
movq (%rax,%rsi,8), %rax
movq %rax, %rdx
shlq $0x20, %rdx
setne %r14b
je 0xe4ab3
leaq (%rsi,%rsi,2), %rdx
movl 0x4(%rcx,%rdx,4), %edx
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r15
shlq $0x3, %r15
addq 0x8(%rdi), %r15
movl %eax, %ebx
xorl %r12d, %r12d
movq %rsi, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rbx, 0x18(%rsp)
leaq (%r12,%r12,2), %rax
cmpl $0x0, 0xc(%r15,%rax,8)
js 0xe4a97
leaq (%r15,%rax,8), %rax
movl 0x8(%rax), %ebp
movl %ebp, %ecx
shrl %ecx
cmpl %r11d, %ecx
jne 0xe483f
movl $0x9, %r13d
jmp 0xe4a92
movq (%rax), %rbx
movl %ebx, %eax
addq %rdx, %rax
movl $0x6, %r13d
cmpq %r10, %rax
ja 0xe4a8d
leaq (,%rbp,2), %r13
addq %rbp, %r13
shlq $0x2, %r13
addq (%r9), %r13
cmpl $0x0, 0x8(%r13)
js 0xe494e
movq 0x28(%r9), %rax
cmpq %rax, 0x20(%r9)
jne 0xe48c5
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x28(%r9)
movq 0x30(%r9), %rdi
shlq $0x2, %rsi
movl %r8d, 0x54(%rsp)
callq 0x2370
movq 0x40(%rsp), %rsi
movl 0x54(%rsp), %r8d
movq 0x38(%rsp), %r11
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r9
movq %rax, 0x30(%r9)
movq 0x20(%r9), %rax
movq 0x30(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%r9)
movl %ebp, (%rcx,%rax,4)
movl %esi, (%r13)
orb $-0x80, 0xb(%r13)
addl 0x48(%rsp), %ebx
movl %ebx, 0x4(%r13)
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 0xe4960
movl %r8d, %ebx
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), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xe4928
orl $0x80000000, %eax       # imm = 0x80000000
movl %ebx, %r8d
movq 0x40(%rsp), %rsi
jmp 0xe4965
addl %edx, %ebx
cmpl 0x4(%r13), %ebx
jae 0xe4971
movl %ebx, 0x4(%r13)
movl %esi, (%r13)
jmp 0xe4971
movl $0x80000000, %eax       # imm = 0x80000000
movl %eax, 0x8(%r13)
incl %r8d
movq 0x48(%rsp), %rdx
movq 0x18(%rsp), %rbx
movl 0x8(%r13), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x8(%r13)
xorl %r13d, %r13d
testl %ecx, %ecx
jne 0xe4a92
movq 0x28(%rdi), %rax
movq (%rax,%rbp,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xe4a63
movl %r8d, %ebx
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), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xe49d5
testl %esi, %esi
movl %ebx, %r8d
movq 0x40(%rsp), %rsi
je 0xe4a63
movq 0x10(%r9), %rax
cmpq %rax, 0x8(%r9)
jne 0xe4a43
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r9)
movq 0x18(%r9), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x40(%rsp), %rsi
movl %ebx, %r8d
movq 0x38(%rsp), %r11
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl %ebp, (%rcx,%rax,4)
movl 0xc(%rsp), %eax
movl %eax, %ebp
movq 0x10(%rsp), %rax
jmp 0xe4a7c
movl $0x1, %eax
cmpl $0x0, 0x10(%rsp)
je 0xe4a7c
movl $0x9, %r13d
movl $0x1, %eax
jmp 0xe4a83
decl %r8d
movl %ebp, 0xc(%rsp)
movq %rax, 0x10(%rsp)
movq 0x48(%rsp), %rdx
movq 0x18(%rsp), %rbx
testl %r13d, %r13d
jne 0xe4aa9
incq %r12
cmpq %rbx, %r12
setb %r14b
jne 0xe4814
jmp 0xe4ab3
cmpl $0x6, %r13d
jne 0xe4b63
cmpl $0x1, 0x10(%rsp)
jne 0xe4b40
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movl $0x9, %r13d
cmpl $-0x1, 0xc(%rsp)
je 0xe4b63
testl %r8d, %r8d
jne 0xe4b63
cmpq $0x0, 0x8(%r9)
jne 0xe4b63
cmpq $0x0, 0x10(%r9)
jne 0xe4b1e
movq $0x2, 0x10(%r9)
movq 0x18(%r9), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x38(%rsp), %r11
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl 0xc(%rsp), %edx
movl %edx, (%rcx,%rax,4)
xorl %r8d, %r8d
movl $0x4, %r13d
jmp 0xe4b63
movq $0x0, 0x10(%rsp)
movl $0x9, %r13d
testb $0x1, %r14b
jne 0xe4b63
xorl %eax, %eax
cmpq $0x0, 0x8(%r9)
sete %al
leal (%rax,%rax,8), %r13d
movl $0x1, %eax
testl %r13d, %r13d
jne 0xe4b83
cmpq $0x1, 0x8(%r9)
ja 0xe47b5
testl %r8d, %r8d
jne 0xe47b5
jmp 0xe4b8b
cmpl $0x4, %r13d
je 0xe4b8b
xorl %eax, %eax
cmpq $0x0, 0x20(%r9)
je 0xe471f
movl $0x1, %ecx
xorl %edx, %edx
movq (%r9), %rsi
movq 0x30(%r9), %rdi
movl (%rdi,%rdx,4), %edx
leaq (%rdx,%rdx,2), %rdx
movl $0x0, 0x4(%rsi,%rdx,4)
andb $0x7f, 0xb(%rsi,%rdx,4)
movl %ecx, %edx
incl %ecx
cmpq %rdx, 0x20(%r9)
ja 0xe4b9d
jmp 0xe471f