// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_bub_finder_with_del_advance(asg_t*, unsigned int, int, buf_t*)
static uint64_t asg_bub_finder_with_del_advance(asg_t *g, uint32_t v0, int max_dist, buf_t *b)
{
	uint32_t i, n_pending = 0;
	uint64_t n_pop = 0;
	///if this node has been deleted
	if (g->seq[v0>>1].del) return 0; // already deleted
	///asg_arc_n(n0)
	if ((uint32_t)g->idx[v0] < 2) return 0; // no bubbles
	///S saves nodes with all incoming edges visited
	b->S.n = b->T.n = b->b.n = b->e.n = 0;
	///for each node, b->a saves all related information
	b->a[v0].c = b->a[v0].d = 0;
	///b->S is the nodes with all incoming edges visited
	kv_push(uint32_t, b->S, v0);

	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, c = b->a[v].c;
		uint32_t nv = asg_arc_n(g, v);
		asg_arc_t *av = asg_arc_a(g, v);
		///why we have this assert?
        /****************************may have bugs********************************/
		///assert(nv > 0);
        /****************************may have bugs********************************/

		///all out-edges of v
		for (i = 0; i < nv; ++i) { // loop through v's neighbors
			/**
			p->ul: |____________31__________|__________1___________|______________32_____________|
								qn            direction of overlap       length of this node (not overlap length)
												(in the view of query)
			p->v : |___________31___________|__________1___________|
								tn             reverse direction of overlap
											(in the view of target)
			p->ol: overlap length
			**/
			uint32_t w = av[i].v, l = (uint32_t)av[i].ul; // v->w with length l
			binfo_t *t = &b->a[w];
			///that means there is a circle, directly terminate the whole bubble poping
			if (w == v0)
            {
                //fprintf(stderr, "n_pop error1\n");
                goto pop_reset;
            } 
            
			///if this edge has been deleted
            /****************************may have bugs********************************/
			///if (av[i].del) continue;
            /****************************may have bugs********************************/

			///push the edge
			kv_push(uint32_t, b->e, (g->idx[v]>>32) + i);

			///find a too far path? directly terminate the whole bubble poping
			if (d + l > (uint32_t)max_dist)
            {
                //fprintf(stderr, "n_pop error2\n");
                break; // too far
            } 
            


			if (t->s == 0) { // this vertex has never been visited
				kv_push(uint32_t, b->b, w); // save it for revert
				///t->p means the in-node of w is v
				///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, t->c = c + 1;
				///incoming edges of w
				t->r = count_out_with_del(g, w^1);
				++n_pending;
			} else { // visited before
				///c seems the max weight of node
				if (c + 1 > t->c || (c + 1 == t->c && d + l > t->d)) t->p = v;
				if (c + 1 > t->c) t->c = c + 1;
				///update len(v0->w)
				if (d + l < t->d) t->d = d + l; // update dist
			}
            /****************************may have bugs********************************/
			///assert(t->r > 0);
            /****************************may have bugs********************************/
			//if all incoming edges of w have visited
			//push it to b->S
			if (--(t->r) == 0) {
				uint32_t x = asg_arc_n(g, w);
				if (x) kv_push(uint32_t, b->S, w);
				else kv_push(uint32_t, b->T, w); // a tip
				--n_pending;
			}
		}
		///if i < nv, that means (d + l > max_dist)
		if (i < nv || b->S.n == 0)
        {
            ///fprintf(stderr, "n_pop error3\n");
            goto pop_reset;
        } 
        
	} while (b->S.n > 1 || n_pending);
	///asg_bub_backtrack(g, v0, b);
	///n_pop = 1 | (uint64_t)b->T.n<<32;
    n_pop = 1;
pop_reset:
	for (i = 0; i < b->b.n; ++i) { // clear the states of visited vertices
		binfo_t *t = &b->a[b->b.a[i]];
		t->s = t->c = t->d = 0;
	}
    ///fprintf(stderr, "n_pop: %d\n", n_pop);
	return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, %r8
movl %edx, 0x40(%rsp)
movq 0x20(%rdi), %rax
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xa6878
movq 0x28(%rdi), %rcx
movl %esi, %eax
testl $0xfffffffe, (%rcx,%rax,8) # imm = 0xFFFFFFFE
je 0xa6878
xorl %r9d, %r9d
movq %r9, 0x50(%r8)
movq %r9, 0x38(%r8)
movq %r9, 0x20(%r8)
movq %r9, 0x8(%r8)
movq (%r8), %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movq %r9, 0x4(%rcx,%rdx)
movq $0x0, 0x28(%rsp)
cmpq %r9, 0x10(%r8)
movq %r8, 0x20(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, 0x18(%rsp)
jne 0xa64bc
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
leaq 0x20(%r8), %rax
movq %rax, 0x58(%rsp)
leaq 0x8(%r8), %rax
movq %rax, 0x30(%rsp)
leaq 0x10(%r8), %rax
movq %rax, 0x68(%rsp)
leaq 0x18(%r8), %rax
movq %rax, 0x60(%rsp)
movq 0x8(%r8), %rax
movq 0x18(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r8)
movl %esi, (%rcx,%rax,4)
leaq 0x28(%r8), %rax
movq %rax, 0x50(%rsp)
leaq 0x30(%r8), %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x18(%r8), %rax
movq (%r8), %rcx
movq 0x8(%r8), %rdx
leaq -0x1(%rdx), %r9
movq %r9, 0x8(%r8)
movl -0x4(%rax,%rdx,4), %r11d
movq 0x28(%rdi), %rax
movq (%rax,%r11,8), %rax
movq %rax, %rdx
shlq $0x20, %rdx
setne %r10b
je 0xa67ef
leaq (%r11,%r11,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r11, %rdx
leaq (%rcx,%rdx), %r9
movl 0x4(%rcx,%rdx), %ecx
movl %ecx, 0x44(%rsp)
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl 0x8(%r9), %r9d
incl %r9d
movl %r9d, 0x10(%rsp)
movl %eax, %eax
movq %rax, 0x80(%rsp)
leaq (%rdx,%rdx,2), %rax
leaq (%rcx,%rax,8), %r12
addq $0x8, %r12
xorl %r15d, %r15d
movq %r11, 0x38(%rsp)
movl (%r12), %ebp
movl $0x9, %r13d
cmpl %esi, %ebp
je 0xa67c3
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %r14
addq %rbp, %r14
addq (%r8), %r14
movl -0x8(%r12), %ebx
movq 0x58(%r8), %rax
cmpq %rax, 0x50(%r8)
jne 0xa65fe
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x58(%r8)
movq 0x60(%r8), %rdi
shlq $0x2, %rsi
movl %r10d, %r13d
callq 0x2370
movq 0x38(%rsp), %r11
movl %r13d, %r10d
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
movq %rax, 0x60(%r8)
movq 0x28(%rdi), %rax
movl 0x4(%rax,%r11,8), %eax
addl %r15d, %eax
movq 0x50(%r8), %rcx
movq 0x60(%r8), %rdx
leaq 0x1(%rcx), %r9
movq %r9, 0x50(%r8)
movl %eax, (%rdx,%rcx,4)
addl 0x44(%rsp), %ebx
movl $0x6, %r13d
cmpl 0x40(%rsp), %ebx
ja 0xa67c3
cmpl $0x0, 0x18(%r14)
js 0xa66ca
movq 0x40(%r8), %rax
cmpq %rax, 0x38(%r8)
jne 0xa6684
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x40(%r8)
movq 0x48(%r8), %rdi
shlq $0x2, %rsi
movl %r10d, %r13d
callq 0x2370
movq 0x38(%rsp), %r11
movl %r13d, %r10d
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x38(%r8), %rax
movq 0x48(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x38(%r8)
movl %ebp, (%rcx,%rax,4)
movl %r11d, (%r14)
orb $-0x80, 0x1b(%r14)
movl %ebx, 0x4(%r14)
movl 0x10(%rsp), %eax
movl %eax, 0x8(%r14)
movq %rbp, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %rcx
movl (%rcx,%rax,8), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%r14)
incl 0x14(%rsp)
jmp 0xa66f9
movl 0x8(%r14), %eax
cmpl %eax, 0x10(%rsp)
jbe 0xa66d9
movl %r11d, (%r14)
jmp 0xa66e1
jne 0xa66e1
cmpl 0x4(%r14), %ebx
ja 0xa66d4
cmpl %eax, 0x10(%rsp)
jbe 0xa66ef
movl 0x10(%rsp), %eax
movl %eax, 0x8(%r14)
cmpl 0x4(%r14), %ebx
jae 0xa66f9
movl %ebx, 0x4(%r14)
movl 0x18(%r14), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%r14)
xorl %r13d, %r13d
testl %ecx, %ecx
jne 0xa67c3
movq 0x28(%rdi), %rax
cmpl $0x0, (%rax,%rbp,8)
je 0xa6740
movq 0x68(%rsp), %rcx
movq (%rcx), %rax
movq 0x60(%rsp), %rdx
movq %rdx, %r14
movq %rdx, %r9
movq 0x30(%rsp), %rdx
jmp 0xa6758
movq 0x50(%rsp), %rcx
movq (%rcx), %rax
movq 0x48(%rsp), %rdx
movq %rdx, %r14
movq %rdx, %r9
movq 0x58(%rsp), %rdx
movq %rdx, %rbx
cmpq %rax, (%rdx)
jne 0xa67af
movq %r14, 0x70(%rsp)
movq %r9, 0x78(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, (%rcx)
movq (%r14), %rdi
shlq $0x2, %rsi
movl %r10d, %r14d
callq 0x2370
movq 0x78(%rsp), %r9
movq 0x38(%rsp), %r11
movl %r14d, %r10d
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
movq 0x70(%rsp), %rcx
movq %rax, (%rcx)
movq (%r9), %rax
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movl %ebp, (%rax,%rcx,4)
decl 0x14(%rsp)
testl %r13d, %r13d
jne 0xa67e9
incq %r15
movq 0x80(%rsp), %rax
cmpq %rax, %r15
setb %r10b
addq $0x18, %r12
cmpq %r15, %rax
jne 0xa658a
jmp 0xa67ef
cmpl $0x6, %r13d
jne 0xa680d
movl $0x9, %r13d
testb $0x1, %r10b
jne 0xa680d
xorl %eax, %eax
movq 0x30(%rsp), %rcx
cmpq $0x0, (%rcx)
sete %al
leal (%rax,%rax,8), %r13d
testl %r13d, %r13d
jne 0xa6836
movq 0x30(%rsp), %rax
cmpq $0x1, (%rax)
ja 0xa650d
cmpl $0x0, 0x14(%rsp)
jne 0xa650d
movl $0x1, %eax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x38(%r8)
je 0xa6871
movl $0x1, %eax
xorl %ecx, %ecx
movq (%r8), %rdx
movq 0x48(%r8), %rsi
movl (%rsi,%rcx,4), %ecx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movq $0x0, 0x4(%rdx,%rsi)
andb $0x7f, 0x1b(%rdx,%rsi)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r8)
ja 0xa6844
movq 0x28(%rsp), %rax
jmp 0xa687a
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq