// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: asg_bub_pop1_purge_graph(asg_t*, unsigned int, int, buf_t*)
uint64_t asg_bub_pop1_purge_graph(asg_t *g, uint32_t v0, int max_dist, buf_t *b)
{   
	uint32_t i, n_pending = 0, n_tips, tip_end;
	uint64_t n_pop = 0;
	///if this node has been deleted
	if (g->seq[v0>>1].del || g->seq[v0>>1].c == ALTER_LABLE) return 0; // already deleted
	///if ((uint32_t)g->idx[v0] < 2) return 0; // no bubbles
    if(get_real_length(g, v0, NULL)<2) return 0;
	///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 = b->a[v0].m = b->a[v0].nc = b->a[v0].np = 0;
	///b->S is the nodes with all incoming edges visited
	kv_push(uint32_t, b->S, v0);
    n_tips = 0;
    tip_end = (uint32_t)-1;

	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;
		uint32_t nv = asg_arc_n(g, v);
		asg_arc_t *av = asg_arc_a(g, v);
        long long t_s = decode_score(b->a[v].c, b->a[v].m), c_s;
		///why we have this assert?
		///assert(nv > 0);
		///all out-edges of v
		for (i = 0; i < nv; ++i) { // loop through v's neighbors
            ///if this edge has been deleted
			if (av[i].del) continue;
			uint32_t w = av[i].v; // 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) goto pop_reset;
            if ((w>>1) == (v0>>1)) goto pop_reset;
            c_s = decode_score((uint32_t)av[i].ul, av[i].ol);
			///push the edge
            ///high 32-bit of g->idx[v] is the start point of v's edges
            //so here is the point of this specfic 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 + 1 > (uint32_t)max_dist) break; // too far

            ///if this node
			if (t->s == 0) { // this vertex has never been visited
				kv_push(uint32_t, b->b, w); // save it for revert
				///t->p is the parent node of 
				///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 + 1;
                encode_score(t_s + c_s, &(t->c), &(t->m));
				///incoming edges of w
				///t->r = count_out(g, w^1);
                t->r = get_real_length(g, w^1, NULL);
				++n_pending;
			} else { // visited before
                if((t_s + c_s)> decode_score(t->c, t->m))
                {
                    t->p = v;
                    encode_score(t_s + c_s, &(t->c), &(t->m));
                }
                ///it is the shortest edge
				if (d + 1 < t->d) t->d = d + 1; // update dist
			}
			///assert(t->r > 0);
			//if all incoming edges of w have visited
			//push it to b->S
			if (--(t->r) == 0) {
                uint32_t x = get_real_length(g, w, NULL);
                /****************************may have bugs for bubble********************************/
                if(x > 0)
                {
                    kv_push(uint32_t, b->S, w);
                }
                else
                {
                    ///at most one tip
                    if(n_tips != 0) goto pop_reset;
                    n_tips++;
                    tip_end = w;
                }
                /****************************may have bugs for bubble********************************/
				--n_pending;
			}
		}
        //if found a tip
        /****************************may have bugs for bubble********************************/
        if(n_tips == 1)
        {
            if(tip_end != (uint32_t)-1 && n_pending == 0 && b->S.n == 0)
            {
                kv_push(uint32_t, b->S, tip_end);
                break;
            }
            else
            {
                goto pop_reset;
            }
        }
        /****************************may have bugs for bubble********************************/
		///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);

	asg_bub_backtrack_primary(g, v0, b);

    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 = t->m = t->nc = t->np = 0;
	}
	return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, 0x4c(%rsp)
movq 0x20(%rdi), %rax
movl %esi, 0x1c(%rsp)
movl %esi, %r11d
shrl %r11d
cmpl $0x0, (%rax,%r11,8)
js 0x10d28d
cmpb $0x1, 0x4(%rax,%r11,8)
je 0x10d28d
movq %rcx, %r9
movq 0x28(%rdi), %rax
movl 0x1c(%rsp), %ecx
movq (%rax,%rcx,8), %rdx
xorl %eax, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rdx, %rsi
je 0x10d28f
movq 0x8(%rdi), %r8
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%r8,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r10d, %r10d
movl (%rdx,%r10), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %r10
cmpq %r10, %rsi
jne 0x10d271
cmpl $0x2, %eax
jge 0x10d29e
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
movq %rsi, 0x50(%r9)
movq %rsi, 0x38(%r9)
movq %rsi, 0x20(%r9)
movq %rsi, 0x8(%r9)
movq (%r9), %rax
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax,%rdx)
movl $0x0, 0x14(%rax,%rdx)
movq $0x0, 0x10(%rsp)
cmpq %rsi, 0x10(%r9)
movq %r9, 0x20(%rsp)
movq %rdi, 0x38(%rsp)
movq %r11, 0x30(%rsp)
jne 0x10d315
movq $0x2, 0x10(%r9)
movq 0x18(%r9), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl 0x1c(%rsp), %edx
movl %edx, (%rcx,%rax,4)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x0, 0x4(%rsp)
movq 0x18(%r9), %rcx
movq (%r9), %rax
movq 0x8(%r9), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, 0x8(%r9)
movl -0x4(%rcx,%rdx,4), %r14d
leaq (%r14,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r14, %rcx
movq 0x28(%rdi), %rdx
movl 0x8(%rax,%rcx), %esi
movl 0xc(%rax,%rcx), %r10d
movq %rsi, %r8
shlq $0x20, %r8
movabsq $0x7fffffff00000000, %rbx # imm = 0x7FFFFFFF00000000
andq %rbx, %r8
orq %r10, %r8
movq %r8, %rbx
negq %rbx
testl %esi, %esi
cmovsq %r8, %rbx
movq %rbx, 0x50(%rsp)
movq (%rdx,%r14,8), %rdx
movq %rdx, %rsi
shlq $0x20, %rsi
setne %r15b
je 0x10d79c
movq %r14, %r10
addq %rcx, %rax
movq %rdx, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rbp
shlq $0x3, %rbp
addq 0x8(%rdi), %rbp
movl 0x4(%rax), %r8d
incl %r8d
movl %edx, %ebx
xorl %r12d, %r12d
movl %r8d, 0x8(%rsp)
movq %rbp, 0x40(%rsp)
movq %rbx, 0x28(%rsp)
leaq (%r12,%r12,2), %rcx
movslq 0xc(%rbp,%rcx,8), %rax
testq %rax, %rax
js 0x10d780
leaq (,%rcx,8), %rcx
addq %rbp, %rcx
movl 0x8(%rcx), %r13d
movl %r13d, %edx
shrl %edx
cmpl %r11d, %edx
jne 0x10d413
movl $0x9, %r14d
jmp 0x10d77b
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rdx,%rdx,2), %rbp
addq %r13, %rbp
addq (%r9), %rbp
movq (%rcx), %rcx
movq %rcx, %rdx
shlq $0x20, %rdx
movabsq $0x7fffffff00000000, %rsi # imm = 0x7FFFFFFF00000000
andq %rsi, %rdx
orq %rax, %rdx
movq %rdx, %rbx
negq %rbx
testl %ecx, %ecx
cmovsq %rdx, %rbx
movq 0x58(%r9), %rax
cmpq %rax, 0x50(%r9)
jne 0x10d497
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x58(%r9)
movq 0x60(%r9), %rdi
shlq $0x2, %rsi
movq %r10, %r14
callq 0x2370
movl 0x8(%rsp), %r8d
movq %r14, %r10
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %r9
movq %rax, 0x60(%r9)
movq 0x28(%rdi), %rax
movl 0x4(%rax,%r10,8), %eax
addl %r12d, %eax
movq 0x50(%r9), %rcx
movq 0x60(%r9), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%r9)
movl %eax, (%rdx,%rcx,4)
movl $0x6, %r14d
cmpl 0x4c(%rsp), %r8d
jbe 0x10d4d2
movq 0x40(%rsp), %rbp
movq 0x28(%rsp), %rbx
jmp 0x10d77b
cmpl $0x0, 0x18(%rbp)
js 0x10d5dc
movq 0x40(%r9), %rax
cmpq %rax, 0x38(%r9)
jne 0x10d525
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x40(%r9)
movq 0x48(%r9), %rdi
shlq $0x2, %rsi
movq %r10, %r14
callq 0x2370
movl 0x8(%rsp), %r8d
movq %r14, %r10
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %r9
movq %rax, 0x48(%r9)
movq 0x38(%r9), %rax
movq 0x48(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x38(%r9)
movl %r13d, (%rcx,%rax,4)
movl %r10d, (%rbp)
orb $-0x80, 0x1b(%rbp)
movl %r8d, 0x4(%rbp)
addq 0x50(%rsp), %rbx
movq %rbx, %rax
negq %rax
cmovsq %rbx, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %rbx
orq %rax, %rbx
movl %eax, 0xc(%rbp)
shrq $0x20, %rbx
addl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, 0x8(%rbp)
movq %r13, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rax
je 0x10d64e
movq 0x8(%rdi), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
xorl %eax, %eax
movq 0x28(%rsp), %rbx
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x10d5b9
orl $0x80000000, %eax       # imm = 0x80000000
movl 0x8(%rsp), %r8d
jmp 0x10d658
addq 0x50(%rsp), %rbx
movl 0x8(%rbp), %eax
movl 0xc(%rbp), %ecx
movq %rax, %rdx
shlq $0x20, %rdx
movabsq $0x7fffffff00000000, %rsi # imm = 0x7FFFFFFF00000000
andq %rsi, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
negq %rcx
testl %eax, %eax
cmovsq %rdx, %rcx
cmpq %rcx, %rbx
jle 0x10d63d
movl %r10d, (%rbp)
movq %rbx, %rax
negq %rax
cmovsq %rbx, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %rbx
orq %rax, %rbx
movl %eax, 0xc(%rbp)
shrq $0x20, %rbx
addl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, 0x8(%rbp)
cmpl 0x4(%rbp), %r8d
jae 0x10d647
movl %r8d, 0x4(%rbp)
movq 0x28(%rsp), %rbx
jmp 0x10d65f
movl $0x80000000, %eax       # imm = 0x80000000
movq 0x28(%rsp), %rbx
movl %eax, 0x18(%rbp)
incl 0x4(%rsp)
movl 0x18(%rbp), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%rbp)
xorl %r14d, %r14d
testl %ecx, %ecx
je 0x10d686
movq 0x40(%rsp), %rbp
jmp 0x10d77b
movq 0x28(%rdi), %rax
movq (%rax,%r13,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
movq 0x40(%rsp), %rbp
je 0x10d754
movq 0x8(%rdi), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x10d6c4
testl %esi, %esi
movl 0x8(%rsp), %r8d
je 0x10d754
movq 0x10(%r9), %rax
cmpq %rax, 0x8(%r9)
jne 0x10d732
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r9)
movq 0x18(%r9), %rdi
shlq $0x2, %rsi
movq %r10, %rbx
callq 0x2370
movl 0x8(%rsp), %r8d
movq %rbx, %r10
movq 0x28(%rsp), %rbx
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl %r13d, (%rcx,%rax,4)
movl 0xc(%rsp), %eax
movl %eax, %r13d
movq 0x10(%rsp), %rax
jmp 0x10d76d
movl $0x1, %eax
cmpl $0x0, 0x10(%rsp)
je 0x10d76d
movl $0x9, %r14d
movl $0x1, %eax
jmp 0x10d776
decl 0x4(%rsp)
movl %r13d, 0xc(%rsp)
movq %rax, 0x10(%rsp)
testl %r14d, %r14d
jne 0x10d792
incq %r12
cmpq %rbx, %r12
setb %r15b
jne 0x10d3dd
jmp 0x10d79c
cmpl $0x6, %r14d
jne 0x10d84e
cmpl $0x1, 0x10(%rsp)
jne 0x10d82b
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movl $0x9, %r14d
cmpl $-0x1, 0xc(%rsp)
je 0x10d84e
cmpl $0x0, 0x4(%rsp)
jne 0x10d84e
cmpq $0x0, 0x8(%r9)
jne 0x10d84e
cmpq $0x0, 0x10(%r9)
jne 0x10d804
movq $0x2, 0x10(%r9)
movq 0x18(%r9), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %r9
movq %rax, 0x18(%r9)
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movl 0xc(%rsp), %edx
movl %edx, (%rcx,%rax,4)
movl $0x0, 0x4(%rsp)
movl $0x4, %r14d
jmp 0x10d84e
movq $0x0, 0x10(%rsp)
movl $0x9, %r14d
testb $0x1, %r15b
jne 0x10d84e
xorl %eax, %eax
cmpq $0x0, 0x8(%r9)
sete %al
leal (%rax,%rax,8), %r14d
testl %r14d, %r14d
jne 0x10d86b
cmpq $0x1, 0x8(%r9)
ja 0x10d33c
cmpl $0x0, 0x4(%rsp)
jne 0x10d33c
jmp 0x10d871
cmpl $0x4, %r14d
jne 0x10d889
movl 0x1c(%rsp), %esi
movq %r9, %rdx
callq 0xe1472
movq 0x20(%rsp), %r9
movl $0x1, %eax
jmp 0x10d88b
xorl %eax, %eax
cmpq $0x0, 0x38(%r9)
je 0x10d28f
movl $0x1, %ecx
xorl %edx, %edx
xorps %xmm0, %xmm0
movq (%r9), %rsi
movq 0x48(%r9), %rdi
movl (%rdi,%rdx,4), %edx
leaq (%rdx,%rdx,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rdx, %rdi
movups %xmm0, 0x4(%rsi,%rdi)
movl $0x0, 0x14(%rsi,%rdi)
andb $0x7f, 0x1b(%rsi,%rdi)
movl %ecx, %edx
incl %ecx
cmpq %rdx, 0x38(%r9)
ja 0x10d8a0
jmp 0x10d28f