// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_bub_backtrack_primary(asg_t*, unsigned int, buf_t*)
void asg_bub_backtrack_primary(asg_t *g, uint32_t v0, buf_t *b)
{
	uint32_t i, v, qn, tn;
    ///b->S.a[0] is the sink of this bubble
    uint32_t tmp_c = g->seq[b->S.a[0]>>1].c;

	///assert(b->S.n == 1);
	///first remove all nodes in this bubble
	for (i = 0; i < b->b.n; ++i)
    {
        g->seq[b->b.a[i]>>1].c = ALTER_LABLE;
    }

    ///v is the sink of this bubble
	v = b->S.a[0];
	///recover node
	do {
		uint32_t u = b->a[v].p; // u->v
        /****************************may have hap bugs********************************/
		////g->seq[v>>1].c = PRIMARY_LABLE;
        g->seq[v>>1].c = HAP_LABLE;
        /****************************may have hap bugs********************************/
		v = u;
	} while (v != v0);
    ///especially for unitig graph, don't label beg and sink node of a bubble as HAP_LABLE
    ///since in unitig graph, a node may consist of a lot of reads
    g->seq[b->S.a[0]>>1].c = tmp_c;

    ///remove all edges (self/reverse for each edge) in this bubble
	for (i = 0; i < b->e.n; ++i) {
		asg_arc_t *a = &g->arc[b->e.a[i]];
        qn = a->ul>>33;
        tn = a->v>>1;
        if(g->seq[qn].c == ALTER_LABLE && g->seq[tn].c == ALTER_LABLE)
        {
            continue;
        }
		///remove this edge self
		a->del = 1;
		///remove the reverse direction
		asg_arc_del(g, a->v^1, a->ul>>32^1, 1);
	}

    ///v is the sink of this bubble
	v = b->S.a[0];
	///recover node
	do {
		uint32_t u = b->a[v].p; // u->v
		g->seq[v>>1].del = 0;
		asg_arc_del(g, u, v, 0);
		asg_arc_del(g, v^1, u^1, 0);
		v = u;
	} while (v != v0);
}

// Assembly:
pushq %r14
pushq %rbx
movq 0x20(%rdi), %rcx
movq 0x18(%rdx), %rax
movq 0x38(%rdx), %r10
movl (%rax), %eax
movl %eax, %r8d
shrl %r8d
movb 0x4(%rcx,%r8,8), %r9b
testq %r10, %r10
je 0xe2832
movq 0x48(%rdx), %r11
xorl %ebx, %ebx
movl (%r11,%rbx,4), %r14d
shrl %r14d
movb $0x1, 0x4(%rcx,%r14,8)
incq %rbx
movl %ebx, %r14d
cmpq %r14, %r10
ja 0xe281a
movq (%rdx), %r10
movl %eax, %r11d
movl %r11d, %ebx
leaq (%rbx,%rbx,8), %r14
leaq (%r14,%r14,2), %r14
addq %rbx, %r14
movl (%r10,%r14), %ebx
shrl %r11d
movb $0x2, 0x4(%rcx,%r11,8)
movl %ebx, %r11d
cmpl %esi, %ebx
jne 0xe2838
movb %r9b, 0x4(%rcx,%r8,8)
cmpq $0x0, 0x50(%rdx)
je 0xe2919
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x60(%rdx), %r8
movl (%r8,%rax,4), %r8d
leaq (%r8,%r8,2), %r9
movq 0x8(%rdi), %r11
movq 0x20(%rdi), %rbx
movq (%r11,%r9,8), %r8
movq %r8, %r10
shrq $0x21, %r10
cmpb $0x1, 0x4(%rbx,%r10,8)
movl 0x8(%r11,%r9,8), %r10d
jne 0xe28ab
movl %r10d, %r14d
shrl %r14d
cmpb $0x1, 0x4(%rbx,%r14,8)
je 0xe2907
leaq (%r11,%r9,8), %r9
orb $-0x80, 0xf(%r9)
xorl $0x1, %r10d
movq 0x28(%rdi), %r9
movq (%r9,%r10,8), %r10
movq %r10, %r9
andq %rcx, %r9
je 0xe2907
shrq $0x20, %r8
movq 0x8(%rdi), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %ebx
xorl %r8d, %ebx
cmpl $0x1, %ebx
jne 0xe28fe
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xe28eb
incl %eax
cmpq %rax, 0x50(%rdx)
ja 0xe2871
movq 0x18(%rdx), %rax
movl (%rax), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq (%rdx), %r8
movl %eax, %r9d
leaq (%r9,%r9,8), %r10
leaq (%r10,%r10,2), %r10
addq %r9, %r10
movl (%r8,%r10), %r8d
movq 0x20(%rdi), %r9
movl %eax, %r10d
shrl %r10d
andl $0x7fffffff, (%r9,%r10,8) # imm = 0x7FFFFFFF
movq 0x8(%rdi), %r9
movq 0x28(%rdi), %r10
movq (%r10,%r8,8), %r11
movq %r11, %rbx
andq %rcx, %rbx
je 0xe2992
shrq $0x20, %r11
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%r9,%r11,8), %r9
addq $0xc, %r9
xorl %r11d, %r11d
cmpl %eax, -0x4(%r9,%r11)
jne 0xe2981
andb $0x7f, 0x3(%r9,%r11)
addq $0x18, %r11
cmpq %r11, %r10
jne 0xe2974
movq 0x8(%rdi), %r9
movq 0x28(%rdi), %r10
xorl $0x1, %eax
movq (%r10,%rax,8), %r10
movq %r10, %rax
andq %rcx, %rax
je 0xe29d9
shrq $0x20, %r10
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
leaq (%r10,%r10,2), %r10
leaq (%r9,%r10,8), %r9
addq $0xc, %r9
xorl %r10d, %r10d
movl -0x4(%r9,%r10), %r11d
xorl %r8d, %r11d
cmpl $0x1, %r11d
jne 0xe29d0
andb $0x7f, 0x3(%r9,%r10)
addq $0x18, %r10
cmpq %r10, %rax
jne 0xe29bc
movl %r8d, %eax
cmpl %esi, %r8d
jne 0xe291e
popq %rbx
popq %r14
retq