// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_bub_end_finder_with_del_advance(asg_t*, unsigned int*, unsigned int, int, buf_t*, unsigned int, unsigned int, unsigned int*)
static uint64_t asg_bub_end_finder_with_del_advance(asg_t *g, uint32_t* v_Ns, uint32_t occ, 
int max_dist, buf_t *b, uint32_t exculde_init, uint32_t exclude_node, uint32_t* sink)
{
	uint32_t i, j, n_pending = 0;
	uint64_t n_pop = 0;
    ///S saves nodes with all incoming edges visited
    b->S.n = b->T.n = b->b.n = b->e.n = 0;
    for (j = 0; j < occ; j++)
    {
        ///if this node has been deleted
        if (g->seq[v_Ns[j]>>1].del) return 0; // already deleted
        ///for each node, b->a saves all related information
        b->a[v_Ns[j]].c = b->a[v_Ns[j]].d = 0;
        ///b->S is the nodes with all incoming edges visited
        kv_push(uint32_t, b->S, (v_Ns[j]<<1)|exculde_init);
    }
    

	
	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), f = v & (uint32_t)1;
        v = v >> 1;
        uint32_t 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);
		
		///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];

            for (j = 0; j < occ; j++)
            {
                if(w == v_Ns[j]) goto pop_reset;
            }
            


            if(f && (exclude_node) == (w)) continue;

            ///if (av[i].del) continue;

			///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)
            {
                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);
                ///t->r = count_out_without_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);
                if (x) kv_push(uint32_t, b->S, w<<1);
				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)
        {
            goto pop_reset;
        } 
        
	} while (b->S.n > 1 || n_pending);

    (*sink) = b->S.a[0]>>1;
	///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;
	}
	return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %r13d
movq %r8, %rbx
movl %ecx, 0x44(%rsp)
movq %rsi, %r15
movq %rdi, %r12
andq $0x0, 0x50(%r8)
andq $0x0, 0x38(%r8)
andq $0x0, 0x20(%r8)
andq $0x0, 0x8(%r8)
movl %edx, %ebp
xorl %ecx, %ecx
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x6d7bc
movq 0x20(%r12), %rdx
movl (%r15,%r14,4), %eax
movl %eax, %esi
shrl %esi
cmpl $0x0, (%rdx,%rsi,8)
js 0x6db3f
movq (%rbx), %rdx
imulq $0x1c, %rax, %rax
andl $0x0, 0x4(%rdx,%rax)
movl (%r15,%r14,4), %eax
imulq $0x1c, %rax, %rax
andl $0x0, 0x8(%rdx,%rax)
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x6d79d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl (%r15,%r14,4), %edx
addl %edx, %edx
orl %r13d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl %edx, (%rax,%rcx,4)
incq %r14
movq %rsi, %rcx
jmp 0x6d738
movl $0x80000000, %edi       # imm = 0x80000000
xorl %r8d, %r8d
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %r9d
movl %r9d, %r10d
shrl %r10d
imulq $0x1c, %r10, %rcx
movl 0x4(%rax,%rcx), %r11d
movl 0x8(%rax,%rcx), %esi
movq 0x28(%r12), %rax
movq (%rax,%r10,8), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rax, %r14
addq 0x8(%r12), %r14
incl %esi
xorl %eax, %eax
movq %r14, 0x20(%rsp)
cmpq %rdx, %rax
je 0x6dae2
movq %rax, 0x30(%rsp)
imulq $0x18, %rax, %rax
movl (%r14,%rax), %ecx
movl %ecx, 0x14(%rsp)
movl 0x8(%r14,%rax), %r13d
imulq $0x1c, %r13, %r14
addq (%rbx), %r14
xorl %eax, %eax
cmpq %rax, %rbp
je 0x6d84b
leaq 0x1(%rax), %rcx
cmpl (%r15,%rax,4), %r13d
movq %rcx, %rax
jne 0x6d834
jmp 0x6db15
testb $0x1, %r9b
je 0x6d85f
cmpl 0x80(%rsp), %r13d
je 0x6d9e6
movl %esi, 0x10(%rsp)
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
movl %r9d, 0x18(%rsp)
movq %r10, 0x28(%rsp)
movq %rdx, 0x38(%rsp)
jne 0x6d8c9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, 0xc(%rsp)
movl %r11d, 0x1c(%rsp)
callq 0x2370
movl 0x1c(%rsp), %r11d
movq 0x28(%rsp), %r10
movl 0x18(%rsp), %r9d
movl 0xc(%rsp), %r8d
movl $0x80000000, %edi       # imm = 0x80000000
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%r12), %rdx
movl 0x4(%rdx,%r10,8), %edx
addl 0x30(%rsp), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
movl 0x14(%rsp), %eax
addl %r11d, %eax
cmpl 0x44(%rsp), %eax
ja 0x6db15
movl %eax, 0x14(%rsp)
movl 0x18(%r14), %eax
testl %eax, %eax
js 0x6d999
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x6d95a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, 0xc(%rsp)
movl %r11d, 0x1c(%rsp)
callq 0x2370
movl 0x1c(%rsp), %r11d
movq 0x28(%rsp), %r10
movl 0x18(%rsp), %r9d
movl 0xc(%rsp), %r8d
movl $0x80000000, %edi       # imm = 0x80000000
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
movl 0x10(%rsp), %esi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r13d, (%rax,%rcx,4)
movl %r10d, (%r14)
orb $-0x80, 0x1b(%r14)
movl 0x14(%rsp), %eax
movl %eax, 0x4(%r14)
movl %esi, 0x8(%r14)
movq %r13, %rax
xorq $0x1, %rax
movq 0x28(%r12), %rcx
movl (%rcx,%rax,8), %eax
orl %edi, %eax
incl %r8d
movq 0x38(%rsp), %rdx
jmp 0x6d9ce
movl 0x10(%rsp), %esi
cmpl 0x8(%r14), %esi
movq 0x38(%rsp), %rdx
jbe 0x6d9b1
movl %r10d, (%r14)
movl %esi, 0x8(%r14)
jmp 0x6d9c0
jne 0x6d9c0
movl 0x14(%rsp), %ecx
cmpl 0x4(%r14), %ecx
jbe 0x6d9c0
movl %r10d, (%r14)
movl 0x14(%rsp), %ecx
cmpl 0x4(%r14), %ecx
jae 0x6d9ce
movl %ecx, 0x4(%r14)
addl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%r14)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x6d9f8
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %rax
incq %rax
jmp 0x6d80c
movq 0x28(%r12), %rax
cmpl $0x0, (%rax,%r13,8)
je 0x6da6d
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x6da5b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, 0xc(%rsp)
movl %r11d, %r14d
callq 0x2370
movl 0x10(%rsp), %esi
movl %r14d, %r11d
movq 0x28(%rsp), %r10
movl 0x18(%rsp), %r9d
movl 0xc(%rsp), %r8d
movl $0x80000000, %edi       # imm = 0x80000000
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movq 0x20(%rsp), %r14
addl %r13d, %r13d
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
jmp 0x6dad1
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0x6dac4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x28(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, 0xc(%rsp)
movl %r11d, %r14d
callq 0x2370
movl 0x10(%rsp), %esi
movl %r14d, %r11d
movq 0x28(%rsp), %r10
movl 0x18(%rsp), %r9d
movl 0xc(%rsp), %r8d
movl $0x80000000, %edi       # imm = 0x80000000
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
movq 0x20(%rsp), %r14
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movl %r13d, (%rax,%rcx,4)
decl %r8d
movq 0x38(%rsp), %rdx
jmp 0x6d9eb
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x6db15
cmpq $0x1, %rcx
jne 0x6d7c4
testl %r8d, %r8d
jne 0x6d7c4
movq 0x18(%rbx), %rax
movl (%rax), %eax
shrl %eax
movq 0x88(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x1
popq %rax
jmp 0x6db17
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %esi
cmpq %rsi, 0x38(%rbx)
jbe 0x6db41
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 0x6db19
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq