// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, %rbx
movl %edx, 0x2c(%rsp)
movq 0x20(%rdi), %rax
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x6e7ec
movl %esi, %r15d
movq 0x28(%rdi), %rcx
movl %esi, %eax
testl $0xfffffffe, (%rcx,%rax,8) # imm = 0xFFFFFFFE
je 0x6e7ec
andq $0x0, 0x50(%rbx)
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movq (%rbx), %rcx
imulq $0x1c, %rax, %rax
andq $0x0, 0x4(%rcx,%rax)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
movq %rdi, 0x8(%rsp)
je 0x6e7fd
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x6e81e
xorl %eax, %eax
addq $0x38, %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 0x8(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %r15d, (%rax,%rcx,4)
incq %rcx
xorl %r14d, %r14d
movl %r15d, 0x14(%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %edx
imulq $0x1c, %rdx, %rcx
movl 0x4(%rax,%rcx), %esi
movl %esi, 0x28(%rsp)
movl 0x8(%rax,%rcx), %r9d
movq 0x8(%rdi), %rax
movq 0x28(%rdi), %rcx
movq %rdx, 0x18(%rsp)
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq %rdx, 0x30(%rsp)
shrq $0x20, %rcx
incl %r9d
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r10
addq $0x8, %r10
xorl %ebp, %ebp
movl %r9d, 0x4(%rsp)
cmpq %rbp, 0x30(%rsp)
je 0x6eace
movl (%r10), %r12d
cmpl %r15d, %r12d
je 0x6eaef
movl -0x8(%r10), %r13d
imulq $0x1c, %r12, %r15
addq (%rbx), %r15
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
jne 0x6e8f9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r12, 0x20(%rsp)
movq %rbp, %r12
movq %r10, %rbp
callq 0x2370
movq %rbp, %r10
movq %r12, %rbp
movq 0x20(%rsp), %r12
movl 0x4(%rsp), %r9d
movq 0x8(%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%rdi), %rdx
movq 0x18(%rsp), %rsi
movl 0x4(%rdx,%rsi,8), %edx
addl %ebp, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
addl 0x28(%rsp), %r13d
cmpl 0x2c(%rsp), %r13d
ja 0x6eaef
movl 0x18(%r15), %eax
testl %eax, %eax
js 0x6e9be
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x6e983
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r12, 0x20(%rsp)
movq %rbp, %r12
movq %r10, %rbp
callq 0x2370
movq %rbp, %r10
movq %r12, %rbp
movq 0x20(%rsp), %r12
movl 0x4(%rsp), %r9d
movq 0x8(%rsp), %rdi
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r12d, (%rax,%rcx,4)
movq 0x18(%rsp), %rax
movl %eax, (%r15)
orb $-0x80, 0x1b(%r15)
movl %r13d, 0x4(%r15)
movl %r9d, 0x8(%r15)
movq %r12, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %rcx
movl (%rcx,%rax,8), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
incl %r14d
jmp 0x6e9ec
cmpl 0x8(%r15), %r9d
jbe 0x6e9d2
movq 0x18(%rsp), %rcx
movl %ecx, (%r15)
movl %r9d, 0x8(%r15)
jmp 0x6e9e2
jne 0x6e9e2
cmpl 0x4(%r15), %r13d
jbe 0x6e9e2
movq 0x18(%rsp), %rcx
movl %ecx, (%r15)
cmpl 0x4(%r15), %r13d
jae 0x6e9ec
movl %r13d, 0x4(%r15)
addl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%r15)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x6ea0e
movl 0x14(%rsp), %r15d
jmp 0x6eac2
movq 0x28(%rdi), %rax
cmpl $0x0, (%rax,%r12,8)
movl 0x14(%rsp), %r15d
je 0x6ea6c
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x6ea62
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r10, %r13
callq 0x2370
movq %r13, %r10
movl 0x4(%rsp), %r9d
movq 0x8(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
jmp 0x6eab8
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0x6eab0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x28(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r10, %r13
callq 0x2370
movq %r13, %r10
movl 0x4(%rsp), %r9d
movq 0x8(%rsp), %rdi
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rax,%rcx,4), %rax
movl %r12d, (%rax)
decl %r14d
incq %rbp
addq $0x18, %r10
jmp 0x6e883
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x6eaef
cmpq $0x1, %rcx
jne 0x6e82d
testl %r14d, %r14d
jne 0x6e82d
pushq $0x1
popq %rax
jmp 0x6eaf1
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %esi
cmpq %rsi, 0x38(%rbx)
jbe 0x6e7ee
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
movl (%rdx,%rsi,4), %edx
imulq $0x1c, %rdx, %rdx
andq $0x0, 0x4(%rcx,%rdx)
andb $0x7f, 0x1b(%rcx,%rdx)
leal 0x1(%rsi), %ecx
jmp 0x6eaf3