// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0xd5f45
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xd5e58
movq $0x0, -0x8(%rbp)
jmp 0xd6517
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0xd5f08
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xd5ec7
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0x88(%rbp)
jmp 0xd5ed5
movl $0x2, %eax
movq %rax, -0x88(%rbp)
jmp 0xd5ed5
movq -0x88(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %edx
shll %edx
orl -0x2c(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0xd5f37
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xd5e1e
jmp 0xd5f47
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $-0x1, %rsi
movq %rsi, 0x8(%rdx)
movl -0x4(%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x44(%rbp), %eax
shrl %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0xd6448
movq -0x60(%rbp), %rax
movl -0x30(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rax
movl -0x30(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x64(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0xd6060
movl -0x64(%rbp), %eax
movq -0x18(%rbp), %rcx
movl -0x34(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
jne 0xd6053
jmp 0xd64a7
jmp 0xd6055
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xd6037
cmpl $0x0, -0x48(%rbp)
je 0xd6073
movl 0x10(%rbp), %eax
cmpl -0x64(%rbp), %eax
jne 0xd6073
jmp 0xd643a
jmp 0xd6075
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jne 0xd60e7
movq -0x28(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0xd60a6
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0xd60b4
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0xd60b4
movq -0x90(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl -0x30(%rbp), %ecx
addq %rcx, %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x50(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x50(%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl -0x68(%rbp), %eax
cmpl -0x20(%rbp), %eax
jbe 0xd6130
jmp 0xd6448
movq -0x70(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xd6245
jmp 0xd6145
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0xd61b7
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0xd6176
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0xd6184
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0xd6184
movq -0x98(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x64(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x38(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x38(%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movq -0x70(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%rax)
movl -0x4c(%rbp), %ecx
addl -0x68(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x50(%rbp), %ecx
addl $0x1, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %esi
xorl $0x1, %esi
callq 0x1663f0
movl %eax, %edx
movq -0x70(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd62b5
movl -0x50(%rbp), %eax
addl $0x1, %eax
movq -0x70(%rbp), %rcx
cmpl 0x8(%rcx), %eax
ja 0xd6272
movl -0x50(%rbp), %eax
addl $0x1, %eax
movq -0x70(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0xd627b
movl -0x4c(%rbp), %eax
addl -0x68(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0xd627b
movl -0x44(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movq -0x70(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jbe 0xd6297
movl -0x50(%rbp), %ecx
addl $0x1, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x4c(%rbp), %eax
addl -0x68(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd62b3
movl -0x4c(%rbp), %ecx
addl -0x68(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xd62b5
movq -0x70(%rbp), %rcx
movl 0x18(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl $-0x1, %eax
movl 0x18(%rcx), %edx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %eax, %edx
movl %edx, 0x18(%rcx)
cmpl $0x0, %eax
jne 0xd6438
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0xd6398
jmp 0xd62fe
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0xd6370
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xd632f
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0xd633d
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0xd633d
movq -0xa0(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x64(%rbp), %edx
shll %edx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0xd642f
jmp 0xd639a
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jne 0xd640c
movq -0x28(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0xd63cb
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0xd63d9
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0xd63d9
movq -0xa8(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movl -0x64(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x20(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x20(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0xd642f
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd643a
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xd5fe7
movl -0x30(%rbp), %eax
cmpl -0x54(%rbp), %eax
jb 0xd645b
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xd645d
jmp 0xd64a7
jmp 0xd645f
movq -0x28(%rbp), %rcx
movb $0x1, %al
cmpq $0x1, 0x8(%rcx)
movb %al, -0xa9(%rbp)
ja 0xd647f
cmpl $0x0, -0x38(%rbp)
setne %al
movb %al, -0xa9(%rbp)
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0xd5f47
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl (%rax), %ecx
shrl %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movq $0x1, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xd650f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x30(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x80(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x80(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xd64ae
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)