// File: /workspace/github_cpp_makefile_star2_O2/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, 0x34(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x20(%rdi), %rax
movl %esi, %r15d
shrl %r15d
cmpl $0x0, (%rax,%r15,8)
js 0xce764
cmpb $0x1, 0x4(%rax,%r15,8)
jne 0xce779
xorl %r14d, %r14d
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %rbx
movl %esi, %ebp
xorl %r14d, %r14d
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0xce767
andq $0x0, 0x50(%rbx)
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movq (%rbx), %rax
movl %ebp, %ecx
imulq $0x1c, %rcx, %rcx
xorps %xmm0, %xmm0
andl $0x0, 0x14(%rax,%rcx)
movups %xmm0, 0x4(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
je 0xce7cc
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0xce7e8
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %ebp, 0x30(%rsp)
movl %ebp, (%rax,%rcx,4)
incq %rcx
pushq $-0x1
popq %rax
movl %eax, 0x1c(%rsp)
xorl %ebp, %ebp
movq %r15, 0x38(%rsp)
movq %rbx, 0x48(%rsp)
movq (%rbx), %rdx
movq 0x18(%rbx), %rax
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rax,%rcx,4), %edi
imulq $0x1c, %rdi, %rcx
movq 0x10(%rsp), %rsi
movq 0x8(%rsi), %rax
movq 0x28(%rsi), %rsi
movq %rdi, 0x28(%rsp)
movq (%rsi,%rdi,8), %rsi
movl %esi, %r10d
shrq $0x20, %rsi
movl 0x4(%rdx,%rcx), %r9d
movl 0x8(%rdx,%rcx), %edi
movl 0xc(%rdx,%rcx), %ecx
movq %rdi, %rdx
shlq $0x20, %rdx
movabsq $0x7fffffff00000000, %r8 # imm = 0x7FFFFFFF00000000
andq %r8, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
negq %rcx
testl %edi, %edi
cmovsq %rdx, %rcx
movq %rcx, 0x40(%rsp)
incl %r9d
movl %r9d, 0xc(%rsp)
imulq $0x18, %rsi, %rcx
leaq (%rax,%rcx), %r12
addq $0xc, %r12
xorl %r14d, %r14d
movl $0x0, 0x4(%rsp)
movq %r10, 0x20(%rsp)
cmpq %r14, %r10
je 0xceb52
movslq (%r12), %rax
testq %rax, %rax
js 0xceb46
movl -0x4(%r12), %r13d
movl %r13d, %ecx
shrl %ecx
cmpl %r15d, %ecx
je 0xceb9d
movl %ebp, 0x8(%rsp)
imulq $0x1c, %r13, %r15
addq (%rbx), %r15
movq -0xc(%r12), %rcx
movq %rcx, %rdx
shlq $0x20, %rdx
movabsq $0x7fffffff00000000, %rsi # imm = 0x7FFFFFFF00000000
andq %rsi, %rdx
orq %rax, %rdx
movq %rdx, %rbp
negq %rbp
testl %ecx, %ecx
cmovsq %rdx, %rbp
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
jne 0xce939
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r13, %rbx
movq %r12, %r13
movq %r10, %r12
callq 0x2370
movq %r12, %r10
movq %r13, %r12
movq %rbx, %r13
movq 0x48(%rsp), %rbx
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x10(%rsp), %rdx
movq 0x28(%rdx), %rdx
movq 0x28(%rsp), %rsi
movl 0x4(%rdx,%rsi,8), %edx
addl %r14d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
movl 0xc(%rsp), %esi
cmpl 0x34(%rsp), %esi
ja 0xceb79
movl 0x18(%r15), %ecx
testl %ecx, %ecx
js 0xcea33
movq %r14, 0x50(%rsp)
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0xce9b0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %esi
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r13d, (%rax,%rcx,4)
movq 0x28(%rsp), %rax
movl %eax, (%r15)
orb $-0x80, 0x1b(%r15)
movl %esi, 0x4(%r15)
addq 0x40(%rsp), %rbp
movq %rbp, %rax
negq %rax
cmovsq %rbp, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %rbp
orq %rax, %rbp
movl %eax, 0xc(%r15)
shrq $0x20, %rbp
movl $0x80000000, %r14d      # imm = 0x80000000
xorl %r14d, %ebp
movl %ebp, 0x8(%r15)
movl %r13d, %esi
xorl $0x1, %esi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0xc2e7a
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x18(%r15), %ecx
andl %r14d, %ecx
orl %eax, %ecx
movl 0x8(%rsp), %ebp
incl %ebp
movq 0x20(%rsp), %r10
movq 0x50(%rsp), %r14
jmp 0xceaae
addq 0x40(%rsp), %rbp
movl 0x8(%r15), %eax
movl 0xc(%r15), %edx
movq %rax, %rsi
shlq $0x20, %rsi
movabsq $0x7fffffff00000000, %rdi # imm = 0x7FFFFFFF00000000
andq %rdi, %rsi
orq %rdx, %rsi
movq %rsi, %rdx
negq %rdx
testl %eax, %eax
cmovsq %rsi, %rdx
cmpq %rdx, %rbp
jle 0xcea9c
movq 0x28(%rsp), %rax
movl %eax, (%r15)
movq %rbp, %rax
negq %rax
cmovsq %rbp, %rax
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
andq %rdx, %rbp
orq %rax, %rbp
movl %eax, 0xc(%r15)
shrq $0x20, %rbp
addl $0x80000000, %ebp       # imm = 0x80000000
movl %ebp, 0x8(%r15)
movl 0xc(%rsp), %eax
cmpl 0x4(%r15), %eax
jae 0xceaaa
movl %eax, 0x4(%r15)
movl 0x8(%rsp), %ebp
leal -0x1(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %eax, %ecx
movl %ecx, 0x18(%r15)
testl %eax, %eax
je 0xceacd
movq 0x38(%rsp), %r15
jmp 0xceb46
movq 0x10(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
movq 0x38(%rsp), %r15
je 0xceb2c
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0xceb19
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
movq 0x20(%rsp), %r10
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %r13d, (%rax,%rcx,4)
jmp 0xceb44
movl %r13d, 0x1c(%rsp)
cmpl $0x0, 0x4(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movq 0x20(%rsp), %r10
jne 0xceb9d
decl %ebp
incq %r14
addq $0x18, %r12
jmp 0xce892
cmpl $0x1, 0x4(%rsp)
je 0xceb84
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0xceb9d
cmpq $0x1, %rcx
jne 0xce805
testl %ebp, %ebp
jne 0xce805
jmp 0xcec07
cmpl $0x1, 0x4(%rsp)
movl 0x8(%rsp), %ebp
jne 0xceb9d
movl %ebp, %eax
xorl %r14d, %r14d
movl 0x1c(%rsp), %ebp
cmpl $-0x1, %ebp
je 0xceba0
testl %eax, %eax
jne 0xceba0
cmpq $0x0, 0x8(%rbx)
je 0xcebd2
xorl %r14d, %r14d
xorl %eax, %eax
xorps %xmm0, %xmm0
movl %eax, %eax
cmpq %rax, 0x38(%rbx)
jbe 0xce767
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
movl (%rdx,%rax,4), %edx
imulq $0x1c, %rdx, %rdx
movups %xmm0, 0x4(%rcx,%rdx)
andl $0x0, 0x14(%rcx,%rdx)
andb $0x7f, 0x1b(%rcx,%rdx)
incl %eax
jmp 0xceba5
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0xcebe1
xorl %ecx, %ecx
jmp 0xcebfc
movq $0x2, 0x10(%rbx)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %ebp, (%rax,%rcx,4)
movq 0x10(%rsp), %rdi
movl 0x30(%rsp), %esi
movq %rbx, %rdx
callq 0xa55fa
pushq $0x1
popq %r14
jmp 0xceba0