// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl %r9d, %r14d
movl %ecx, 0x54(%rsp)
movl %edx, %r9d
movq %rsi, %r12
xorl %eax, %eax
movq %rax, 0x50(%r8)
movq %rax, 0x38(%r8)
movq %rax, 0x20(%r8)
movq $0x0, 0x30(%rsp)
movq %rax, 0x8(%r8)
movl %edx, %ebp
testl %edx, %edx
movq %r8, 0x20(%rsp)
movl %edx, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
je 0xa4e06
movl $0x2, %ebx
xorl %r15d, %r15d
movq 0x20(%rdi), %rcx
movl (%r12,%r15,4), %eax
movl %eax, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xa5241
movq (%r8), %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
xorl %esi, %esi
movl %esi, 0x4(%rcx,%rdx)
movl (%r12,%r15,4), %eax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movq $0x0, 0x30(%rsp)
movl %esi, 0x8(%rcx,%rdx)
movq 0x10(%r8), %rax
cmpq %rax, 0x8(%r8)
jne 0xa4dde
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbx, %rsi
movq %rsi, 0x10(%r8)
movq 0x18(%r8), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x18(%rsp), %rdi
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movl (%r12,%r15,4), %eax
addl %eax, %eax
orl %r14d, %eax
movq 0x8(%r8), %rcx
movq 0x18(%r8), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%r8)
movl %eax, (%rdx,%rcx,4)
incq %r15
cmpl %r15d, %ebp
jne 0xa4d5f
movl 0xb0(%rsp), %r10d
leaq 0x20(%r8), %rax
movq %rax, 0x68(%rsp)
leaq 0x8(%r8), %rax
movq %rax, 0x38(%rsp)
leaq 0x18(%r8), %rax
movq %rax, 0x70(%rsp)
leaq 0x30(%r8), %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x18(%r8), %rax
movq (%r8), %rcx
movq 0x8(%r8), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, 0x8(%r8)
movl -0x4(%rax,%rdx,4), %eax
movl %eax, 0x5c(%rsp)
movl %eax, %esi
shrl %esi
movq 0x28(%rdi), %rax
movq (%rax,%rsi,8), %rax
movq %rax, %rdx
shlq $0x20, %rdx
setne %r11b
je 0xa51a7
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq %rsi, 0x48(%rsp)
addq %rsi, %rdx
leaq (%rcx,%rdx), %rsi
movl 0x4(%rcx,%rdx), %ecx
movl %ecx, 0x58(%rsp)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x8(%rdi), %rcx
movl 0x8(%rsi), %edx
incl %edx
movl %edx, 0xc(%rsp)
movl %eax, %edx
xorl %r15d, %r15d
movq %rcx, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
leaq (%r15,%r15,2), %rax
movl (%rcx,%rax,8), %esi
movl 0x8(%rcx,%rax,8), %r13d
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %r14
addq %r13, %r14
addq (%r8), %r14
testl %r9d, %r9d
je 0xa4ef2
xorl %eax, %eax
cmpl (%r12,%rax,4), %r13d
je 0xa5041
incq %rax
cmpl %eax, %ebp
jne 0xa4ee1
testb $0x1, 0x5c(%rsp)
je 0xa4f07
movl $0xb, %ebx
cmpl %r10d, %r13d
je 0xa5187
movl %esi, 0x14(%rsp)
movq 0x58(%r8), %rax
cmpq %rax, 0x50(%r8)
jne 0xa4f57
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 %r11d, %ebx
callq 0x2370
movl %ebx, %r11d
movl 0xb0(%rsp), %r10d
movq 0x18(%rsp), %rdi
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %r8
movq %rax, 0x60(%r8)
movq 0x28(%rdi), %rax
movq 0x48(%rsp), %rcx
movl 0x4(%rax,%rcx,8), %eax
addl %r15d, %eax
movq 0x50(%r8), %rcx
movq 0x60(%r8), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%r8)
movl %eax, (%rdx,%rcx,4)
movl 0x14(%rsp), %esi
addl 0x58(%rsp), %esi
movl $0x9, %ebx
cmpl 0x54(%rsp), %esi
ja 0xa517d
cmpl $0x0, 0x18(%r14)
js 0xa504b
movq 0x40(%r8), %rax
cmpq %rax, 0x38(%r8)
jne 0xa4ff0
movl %esi, 0x14(%rsp)
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 %r11d, %ebx
callq 0x2370
movl 0x14(%rsp), %esi
movl %ebx, %r11d
movl 0xb0(%rsp), %r10d
movq 0x18(%rsp), %rdi
movl 0x8(%rsp), %r9d
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 %r13d, (%rcx,%rax,4)
movq 0x48(%rsp), %rax
movl %eax, (%r14)
orb $-0x80, 0x1b(%r14)
movl %esi, 0x4(%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
movq %r13, %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 0x10(%rsp)
movq 0x28(%rsp), %rdx
jmp 0xa5084
movl $0xf, %ebx
jmp 0xa5187
movl 0x8(%r14), %eax
cmpl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rdx
jbe 0xa5064
movq 0x48(%rsp), %rcx
movl %ecx, (%r14)
jmp 0xa506c
jne 0xa506c
cmpl 0x4(%r14), %esi
ja 0xa505a
cmpl %eax, 0xc(%rsp)
jbe 0xa507a
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
cmpl 0x4(%r14), %esi
jae 0xa5084
movl %esi, 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 %ebx, %ebx
testl %ecx, %ecx
je 0xa50ac
movq 0x40(%rsp), %rcx
jmp 0xa5187
movq 0x28(%rdi), %rax
cmpl $0x0, (%rax,%r13,8)
je 0xa5112
movq 0x10(%r8), %rax
cmpq %rax, 0x8(%r8)
jne 0xa5103
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r8)
movq 0x18(%r8), %rdi
shlq $0x2, %rsi
movl %r11d, %r14d
callq 0x2370
movl %r14d, %r11d
movl 0xb0(%rsp), %r10d
movq 0x18(%rsp), %rdi
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
addl %r13d, %r13d
movq 0x70(%rsp), %rax
movq 0x38(%rsp), %rcx
jmp 0xa5168
movq 0x28(%r8), %rax
cmpq %rax, 0x20(%r8)
jne 0xa515e
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x28(%r8)
movq 0x30(%r8), %rdi
shlq $0x2, %rsi
movl %r11d, %r14d
callq 0x2370
movl %r14d, %r11d
movl 0xb0(%rsp), %r10d
movq 0x18(%rsp), %rdi
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %r8
movq %rax, 0x30(%r8)
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
movq (%rax), %rax
movq (%rcx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rcx)
movl %r13d, (%rax,%rdx,4)
decl 0x10(%rsp)
movq 0x40(%rsp), %rcx
movq 0x28(%rsp), %rdx
testl %ebx, %ebx
je 0xa5190
cmpl $0xb, %ebx
jne 0xa51a2
incq %r15
cmpq %rdx, %r15
setb %r11b
jne 0xa4eb9
jmp 0xa51a7
cmpl $0x9, %ebx
jne 0xa51c8
movl $0xf, %ebx
testb $0x1, %r11b
jne 0xa51c8
movq 0x38(%rsp), %rax
cmpq $0x0, (%rax)
movl $0x0, %ebx
movl $0xf, %eax
cmovel %eax, %ebx
testl %ebx, %ebx
jne 0xa5206
movq 0x38(%rsp), %rax
cmpq $0x1, (%rax)
ja 0xa4e3a
cmpl $0x0, 0x10(%rsp)
jne 0xa4e3a
movq 0x70(%rsp), %rax
movq (%rax), %rax
movl (%rax), %eax
shrl %eax
movq 0xb8(%rsp), %rcx
movl %eax, (%rcx)
movl $0x1, %eax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x38(%r8)
je 0xa5241
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 0xa5214
movq 0x30(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq