// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rcx, %rbx
movl %edx, 0x3c(%rsp)
movq 0x20(%rdi), %rax
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xa751c
movl %esi, %r12d
movq 0x28(%rdi), %rcx
movl %esi, %eax
testl $0xfffffffe, (%rcx,%rax,8) # imm = 0xFFFFFFFE
je 0xa751c
xorl %ecx, %ecx
movq %rcx, 0x50(%rbx)
movq %rcx, 0x38(%rbx)
movq %rcx, 0x20(%rbx)
movq %rcx, 0x8(%rbx)
movq (%rbx), %rdx
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movq %rcx, 0x4(%rdx,%rsi)
cmpq %rcx, 0x10(%rbx)
movq %rdi, 0x10(%rsp)
je 0xa7523
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0xa7546
xorl %eax, %eax
jmp 0xa7895
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %r12d, (%rax,%rcx,4)
incq %rcx
movq $0x0, 0x28(%rsp)
xorl %r15d, %r15d
movl %r12d, 0x1c(%rsp)
movq %rcx, %rax
movq (%rbx), %rdx
movq 0x18(%rbx), %rsi
decq %rcx
movq %rcx, 0x8(%rbx)
movl -0x4(%rsi,%rax,4), %r9d
movq 0x28(%rdi), %rax
movq (%rax,%r9,8), %rax
testl %eax, %eax
je 0xa7833
leaq (%r9,%r9,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r9, %rcx
leaq (%rdx,%rcx), %rsi
movl 0x4(%rdx,%rcx), %ecx
movl %ecx, 0x38(%rsp)
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl 0x8(%rsi), %r10d
incl %r10d
movl %eax, %eax
movq %rax, 0x40(%rsp)
leaq (%rdx,%rdx,2), %rax
leaq (%rcx,%rax,8), %r11
addq $0x8, %r11
xorl %ebp, %ebp
movq %r9, 0x20(%rsp)
movl %r10d, 0xc(%rsp)
movl (%r11), %r14d
cmpl %r12d, %r14d
je 0xa7855
movl -0x8(%r11), %r13d
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %r12
addq %r14, %r12
addq (%rbx), %r12
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
jne 0xa7645
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r14, 0x30(%rsp)
movq %rbp, %r14
movq %r11, %rbp
callq 0x2370
movq %rbp, %r11
movq %r14, %rbp
movq 0x30(%rsp), %r14
movl 0xc(%rsp), %r10d
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%rdi), %rdx
movl 0x4(%rdx,%r9,8), %edx
addl %ebp, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
addl 0x38(%rsp), %r13d
cmpl 0x3c(%rsp), %r13d
ja 0xa7855
movl 0x18(%r12), %eax
testl %eax, %eax
js 0xa770d
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0xa76d3
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r14, 0x30(%rsp)
movq %rbp, %r14
movq %r11, %rbp
callq 0x2370
movq %rbp, %r11
movq %r14, %rbp
movq 0x30(%rsp), %r14
movl 0xc(%rsp), %r10d
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r14d, (%rax,%rcx,4)
movl %r9d, (%r12)
orb $-0x80, 0x1b(%r12)
movl %r13d, 0x4(%r12)
movl %r10d, 0x8(%r12)
movq %r14, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %rcx
movl (%rcx,%rax,8), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
incl %r15d
jmp 0xa7738
cmpl 0x8(%r12), %r10d
jbe 0xa771f
movl %r9d, (%r12)
movl %r10d, 0x8(%r12)
jmp 0xa772c
jne 0xa772c
cmpl 0x4(%r12), %r13d
jbe 0xa772c
movl %r9d, (%r12)
cmpl 0x4(%r12), %r13d
jae 0xa7738
movl %r13d, 0x4(%r12)
addl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%r12)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xa775b
movl 0x1c(%rsp), %r12d
jmp 0xa781d
movq 0x28(%rdi), %rax
cmpl $0x0, (%rax,%r14,8)
movl 0x1c(%rsp), %r12d
je 0xa77c0
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0xa77b6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r11, %r13
callq 0x2370
movq %r13, %r11
movl 0xc(%rsp), %r10d
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
jmp 0xa7813
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0xa780b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x28(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r11, %r13
callq 0x2370
movq %r13, %r11
movl 0xc(%rsp), %r10d
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %rdi
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rax,%rcx,4), %rax
movl %r14d, (%rax)
decl %r15d
incq %rbp
addq $0x18, %r11
cmpq %rbp, 0x40(%rsp)
jne 0xa75cc
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0xa7855
cmpq $0x1, %rcx
jne 0xa755e
testl %r15d, %r15d
jne 0xa755e
movl $0x1, %eax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x38(%rbx)
je 0xa7890
movl $0x1, %eax
xorl %ecx, %ecx
movq (%rbx), %rdx
movq 0x48(%rbx), %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(%rbx)
ja 0xa7863
movq 0x28(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq