// File: /workspace/github_cpp_makefile_star2_O3/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
movq %r8, %rbx
movl %ecx, 0x30(%rsp)
movq %rsi, %r12
xorl %r14d, %r14d
movq %r14, 0x50(%r8)
movq %r14, 0x38(%r8)
movq %r14, 0x20(%r8)
movq %r14, 0x8(%r8)
movl %edx, %r15d
movl $0x0, %ecx
movl %edx, 0x34(%rsp)
testl %edx, %edx
movq %rdi, 0x18(%rsp)
je 0xa5f3a
movl %r9d, %ebp
xorl %edx, %edx
xorl %r13d, %r13d
movq 0x20(%rdi), %rcx
movl (%r12,%r13,4), %eax
movl %eax, %esi
shrl %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0xa62dc
movq (%rbx), %rcx
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movl %r14d, 0x4(%rcx,%rsi)
movl (%r12,%r13,4), %eax
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movl %r14d, 0x8(%rcx,%rsi)
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rdx
jne 0xa5f18
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rdx
movl (%r12,%r13,4), %esi
addl %esi, %esi
orl %ebp, %esi
leaq 0x1(%rdx), %rcx
movq %rcx, 0x8(%rbx)
movl %esi, (%rax,%rdx,4)
incq %r13
movq %rcx, %rdx
cmpq %r13, %r15
jne 0xa5ea4
movl 0x80(%rsp), %r8d
movl $0x2, %r9d
xorl %r10d, %r10d
movq %rcx, %rax
movq (%rbx), %rdx
movq 0x18(%rbx), %rsi
decq %rcx
movq %rcx, 0x8(%rbx)
movl -0x4(%rsi,%rax,4), %r11d
movl %r11d, %esi
shrl %esi
movq 0x28(%rdi), %rax
movq (%rax,%rsi,8), %rax
testl %eax, %eax
je 0xa6271
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rsi, %rcx
movq %rsi, 0x8(%rsp)
leaq (%rdx,%rcx), %rsi
movl 0x4(%rdx,%rcx), %ecx
movl %ecx, 0x2c(%rsp)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x8(%rdi), %rcx
movl 0x8(%rsi), %edx
movq 0x8(%rsp), %rsi
incl %edx
movl %eax, %eax
movq %rax, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
leaq (%rax,%rax,2), %rax
movl (%rcx,%rax,8), %ebp
movl %ebp, 0x14(%rsp)
movl 0x8(%rcx,%rax,8), %r13d
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rbp
addq %r13, %rbp
addq (%rbx), %rbp
cmpl $0x0, 0x34(%rsp)
je 0xa5ffd
xorl %eax, %eax
cmpl (%r12,%rax,4), %r13d
je 0xa62a1
incq %rax
cmpq %rax, %r15
jne 0xa5feb
testb $0x1, %r11b
je 0xa600c
cmpl %r8d, %r13d
je 0xa625a
movl %edx, 0x10(%rsp)
movq %rcx, 0x38(%rsp)
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
movl %r11d, 0x4(%rsp)
jne 0xa6070
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r9, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r10d, (%rsp)
callq 0x2370
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %r11d
movl (%rsp), %r10d
movl $0x2, %r9d
movl 0x80(%rsp), %r8d
movq 0x18(%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%rdi), %rdx
movl 0x4(%rdx,%rsi,8), %edx
addl 0x20(%rsp), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
movl 0x14(%rsp), %ecx
addl 0x2c(%rsp), %ecx
cmpl 0x30(%rsp), %ecx
ja 0xa62a1
movl 0x18(%rbp), %eax
testl %eax, %eax
js 0xa6139
movl %ecx, 0x14(%rsp)
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0xa60f9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r9, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r10d, (%rsp)
callq 0x2370
movl 0x4(%rsp), %r11d
movl (%rsp), %r10d
movl $0x2, %r9d
movl 0x80(%rsp), %r8d
movq 0x18(%rsp), %rdi
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
movq 0x8(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r13d, (%rax,%rcx,4)
movl %esi, (%rbp)
orb $-0x80, 0x1b(%rbp)
movl 0x14(%rsp), %eax
movl %eax, 0x4(%rbp)
movl 0x10(%rsp), %edx
movl %edx, 0x8(%rbp)
movq %r13, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %rcx
movl (%rcx,%rax,8), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
incl %r10d
jmp 0xa6161
movl 0x10(%rsp), %edx
cmpl 0x8(%rbp), %edx
movq 0x8(%rsp), %rsi
jbe 0xa614f
movl %esi, (%rbp)
movl %edx, 0x8(%rbp)
jmp 0xa6159
jne 0xa6159
cmpl 0x4(%rbp), %ecx
jbe 0xa6159
movl %esi, (%rbp)
cmpl 0x4(%rbp), %ecx
jae 0xa6161
movl %ecx, 0x4(%rbp)
addl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%rbp)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xa6182
movq 0x38(%rsp), %rcx
jmp 0xa625a
movq 0x28(%rdi), %rax
cmpl $0x0, (%rax,%r13,8)
je 0xa61ee
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0xa61e1
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r9, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, %ebp
movl %r10d, (%rsp)
callq 0x2370
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %r11d
movl (%rsp), %r10d
movl $0x2, %r9d
movl %ebp, %r8d
movq 0x18(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
addl %r13d, %r13d
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
jmp 0xa624a
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0xa6242
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r9, %rsi
movq %rsi, 0x28(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %r8d, %ebp
movl %r10d, (%rsp)
callq 0x2370
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %r11d
movl (%rsp), %r10d
movl $0x2, %r9d
movl %ebp, %r8d
movq 0x18(%rsp), %rdi
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movl %r13d, (%rax,%rcx,4)
decl %r10d
movq 0x38(%rsp), %rcx
movl 0x10(%rsp), %edx
movq 0x20(%rsp), %rax
incq %rax
cmpq 0x40(%rsp), %rax
jne 0xa5fb8
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0xa62a1
cmpq $0x1, %rcx
jne 0xa5f4b
testl %r10d, %r10d
jne 0xa5f4b
movq 0x18(%rbx), %rax
movl (%rax), %eax
shrl %eax
movq 0x88(%rsp), %rcx
movl %eax, (%rcx)
movl $0x1, %r14d
cmpq $0x0, 0x38(%rbx)
je 0xa62dc
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 0xa62af
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq