// File: /workspace/github_cpp_makefile_star2_O3/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
movq 0x20(%rdi), %rax
movl %esi, %r11d
shrl %r11d
cmpl $0x0, (%rax,%r11,8)
js 0x11230a
cmpb $0x1, 0x4(%rax,%r11,8)
je 0x11230a
movq %rcx, %rbx
movl %edx, %r10d
movl %esi, %ebp
movq 0x28(%rdi), %rcx
movl %esi, %eax
movq (%rcx,%rax,8), %rcx
xorl %r15d, %r15d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rcx, %rdx
je 0x11230d
movq 0x8(%rdi), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %r15d
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x1122ed
cmpl $0x2, %r15d
jge 0x11231f
xorl %r15d, %r15d
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
movq %rcx, 0x50(%rbx)
movq %rcx, 0x38(%rbx)
movq %rcx, 0x20(%rbx)
movq %rcx, 0x8(%rbx)
movq (%rbx), %rdx
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rdx,%rsi)
movl $0x0, 0x14(%rdx,%rsi)
cmpq %rcx, 0x10(%rbx)
movq %rdi, 0x20(%rsp)
movl %r10d, 0x1c(%rsp)
movq %r11, 0x30(%rsp)
je 0x11236c
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x112399
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x30(%rsp), %r11
movl 0x1c(%rsp), %r10d
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
xorl %r15d, %r15d
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movl %ebp, 0x2c(%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %esi
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rsi, %rdx
movq 0x28(%rdi), %rcx
movq %rsi, 0x40(%rsp)
movq (%rcx,%rsi,8), %rcx
movl 0x8(%rax,%rdx), %esi
movl 0xc(%rax,%rdx), %r9d
movq %rsi, %r8
shlq $0x20, %r8
movabsq $0x7fffffff00000000, %r14 # imm = 0x7FFFFFFF00000000
andq %r14, %r8
orq %r9, %r8
movq %r8, %r9
negq %r9
testl %esi, %esi
cmovsq %r8, %r9
movq %r9, 0x48(%rsp)
testl %ecx, %ecx
je 0x1127ca
addq %rdx, %rax
movq %rcx, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %r9
shlq $0x3, %r9
addq 0x8(%rdi), %r9
movl 0x4(%rax), %r8d
incl %r8d
movl %ecx, %eax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x18(%rsp)
movq %r9, 0x38(%rsp)
movl %r8d, 0xc(%rsp)
leaq (%r12,%r12,2), %rcx
movslq 0xc(%r9,%rcx,8), %rax
testq %rax, %rax
js 0x1127b1
leaq (%r9,%rcx,8), %rcx
movl 0x8(%rcx), %r14d
movl %r14d, %edx
shrl %edx
cmpl %r11d, %edx
je 0x112869
movl %r13d, 0x10(%rsp)
leaq (%r14,%r14,8), %rdx
leaq (%rdx,%rdx,2), %rbp
addq %r14, %rbp
addq (%rbx), %rbp
movq (%rcx), %rcx
movq %rcx, %rdx
shlq $0x20, %rdx
movabsq $0x7fffffff00000000, %rsi # imm = 0x7FFFFFFF00000000
andq %rsi, %rdx
orq %rax, %rdx
movq %rdx, %r13
negq %r13
testl %ecx, %ecx
cmovsq %rdx, %r13
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
jne 0x1124fe
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %r8d
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %r11
movl 0x1c(%rsp), %r10d
movq 0x20(%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%rdi), %rdx
movq 0x40(%rsp), %rsi
movl 0x4(%rdx,%rsi,8), %edx
addl %r12d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
cmpl %r10d, %r8d
ja 0x1127ec
movl 0x18(%rbp), %eax
testl %eax, %eax
js 0x112634
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x11257c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %r8d
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %r11
movl 0x1c(%rsp), %r10d
movq 0x20(%rsp), %rdi
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r14d, (%rax,%rcx,4)
movq 0x40(%rsp), %rax
movl %eax, (%rbp)
orb $-0x80, 0x1b(%rbp)
movl %r8d, 0x4(%rbp)
addq 0x48(%rsp), %r13
movq %r13, %rax
negq %rax
cmovsq %r13, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %r13
orq %rax, %r13
movl %eax, 0xc(%rbp)
shrq $0x20, %r13
addl $0x80000000, %r13d      # imm = 0x80000000
movl %r13d, 0x8(%rbp)
movq %r14, %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 0x1126b1
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
movl 0x10(%rsp), %r13d
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x11260e
orl $0x80000000, %eax       # imm = 0x80000000
movl 0xc(%rsp), %r8d
jmp 0x1126bb
addq 0x48(%rsp), %r13
movl 0x8(%rbp), %ecx
movl 0xc(%rbp), %edx
movq %rcx, %rsi
shlq $0x20, %rsi
movabsq $0x7fffffff00000000, %r8 # imm = 0x7FFFFFFF00000000
andq %r8, %rsi
orq %rdx, %rsi
movq %rsi, %rdx
negq %rdx
testl %ecx, %ecx
cmovsq %rsi, %rdx
cmpq %rdx, %r13
jle 0x11269b
movq 0x40(%rsp), %rcx
movl %ecx, (%rbp)
movq %r13, %rcx
negq %rcx
cmovsq %r13, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
andq %rdx, %r13
orq %rcx, %r13
movl %ecx, 0xc(%rbp)
shrq $0x20, %r13
addl $0x80000000, %r13d      # imm = 0x80000000
movl %r13d, 0x8(%rbp)
movl 0xc(%rsp), %r8d
cmpl 0x4(%rbp), %r8d
jae 0x1126aa
movl %r8d, 0x4(%rbp)
movl 0x10(%rsp), %r13d
jmp 0x1126be
movl $0x80000000, %eax       # imm = 0x80000000
movl 0x10(%rsp), %r13d
incl %r13d
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%rbp)
testl %ecx, %ecx
jne 0x1127b1
movq 0x28(%rdi), %rax
movq (%rax,%r14,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0x112796
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 0x112712
testl %esi, %esi
movl 0xc(%rsp), %r8d
je 0x112796
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x112781
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %r8d
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %r11
movl 0x1c(%rsp), %r10d
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %r14d, (%rax,%rcx,4)
movl 0x14(%rsp), %eax
movl %eax, %r14d
jmp 0x1127a9
cmpl $0x0, 0x18(%rsp)
movl $0x1, 0x18(%rsp)
jne 0x112869
decl %r13d
movl %r14d, 0x14(%rsp)
incq %r12
cmpq 0x50(%rsp), %r12
jne 0x11244e
cmpl $0x1, 0x18(%rsp)
movl 0x2c(%rsp), %ebp
je 0x1127fc
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x112869
cmpq $0x1, %rcx
jne 0x1123b1
testl %r13d, %r13d
jne 0x1123b1
jmp 0x112859
cmpl $0x1, 0x18(%rsp)
movl 0x2c(%rsp), %ebp
movl 0x10(%rsp), %r13d
jne 0x112814
xorl %r15d, %r15d
movl 0x14(%rsp), %esi
cmpl $-0x1, %esi
je 0x112869
testl %r13d, %r13d
jne 0x112869
cmpq $0x0, 0x8(%rbx)
je 0x112819
xorl %r15d, %r15d
jmp 0x112869
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0x112828
xorl %ecx, %ecx
jmp 0x11284e
movq $0x2, 0x10(%rbx)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2370
movl 0x14(%rsp), %esi
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %esi, (%rax,%rcx,4)
movl %ebp, %esi
movq %rbx, %rdx
callq 0xe27f3
movl $0x1, %r15d
cmpq $0x0, 0x38(%rbx)
je 0x11230d
movl $0x1, %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
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
movups %xmm0, 0x4(%rdx,%rsi)
movl $0x0, 0x14(%rdx,%rsi)
andb $0x7f, 0x1b(%rdx,%rsi)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%rbx)
ja 0x11287e
jmp 0x11230d