// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
movq 0x18(%rdx), %rcx
movq 0x38(%rdx), %r10
movl (%rcx), %r8d
movl %r8d, %ecx
shrl %ecx
movb 0x4(%rax,%rcx,8), %r9b
testq %r10, %r10
je 0xe14b1
movq 0x48(%rdx), %r11
xorl %ebx, %ebx
movl (%r11,%rbx,4), %r14d
shrl %r14d
movb $0x1, 0x4(%rax,%r14,8)
incq %rbx
movl %ebx, %r14d
cmpq %r14, %r10
ja 0xe1499
movq (%rdx), %r10
movl %r8d, %r11d
leaq (%r11,%r11,8), %rbx
leaq (%rbx,%rbx,2), %rbx
addq %r11, %rbx
movl (%r10,%rbx), %r11d
shrl %r8d
movb $0x2, 0x4(%rax,%r8,8)
movl %r11d, %r8d
cmpl %esi, %r11d
jne 0xe14b4
movb %r9b, 0x4(%rax,%rcx,8)
cmpq $0x0, 0x50(%rdx)
je 0xe158f
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x60(%rdx), %r8
movl (%r8,%rax,4), %r8d
leaq (%r8,%r8,2), %r8
movq 0x8(%rdi), %r9
movq 0x20(%rdi), %r10
leaq (%r9,%r8,8), %r9
movq (%r9), %r8
movq %r8, %r11
shrq $0x21, %r11
cmpb $0x1, 0x4(%r10,%r11,8)
jne 0xe1527
movl 0x8(%r9), %r11d
shrl %r11d
cmpb $0x1, 0x4(%r10,%r11,8)
je 0xe1583
orb $-0x80, 0xf(%r9)
movl 0x8(%r9), %r9d
xorq $0x1, %r9
movq 0x28(%rdi), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %rcx, %r9
je 0xe1583
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 0xe157a
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xe1567
incl %eax
cmpq %rax, 0x50(%rdx)
ja 0xe14ee
movq 0x18(%rdx), %rax
movl (%rax), %r8d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq (%rdx), %rcx
movl %r8d, %r9d
leaq (%r9,%r9,8), %r10
leaq (%r10,%r10,2), %r10
addq %r9, %r10
movl (%rcx,%r10), %ecx
movq 0x20(%rdi), %r9
movl %r8d, %r10d
shrl %r10d
andl $0x7fffffff, (%r9,%r10,8) # imm = 0x7FFFFFFF
movq 0x28(%rdi), %r9
movq (%r9,%rcx,8), %r10
movq %r10, %r9
andq %rax, %r9
je 0xe1607
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
cmpl %r8d, -0x4(%r10,%r11)
jne 0xe15fe
andb $0x7f, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xe15f1
xorl $0x1, %r8d
movq 0x28(%rdi), %r9
movq (%r9,%r8,8), %r9
movq %r9, %r8
andq %rax, %r8
je 0xe1657
movq 0x8(%rdi), %r10
shrq $0x20, %r9
shlq $0x3, %r8
leaq (%r8,%r8,2), %r8
leaq (%r9,%r9,2), %r9
leaq (%r10,%r9,8), %r9
addq $0xc, %r9
xorl %r10d, %r10d
movl -0x4(%r9,%r10), %r11d
xorl %ecx, %r11d
cmpl $0x1, %r11d
jne 0xe164e
andb $0x7f, 0x3(%r9,%r10)
addq $0x18, %r10
cmpq %r10, %r8
jne 0xe163a
movl %ecx, %r8d
cmpl %esi, %ecx
jne 0xe159b
popq %rbx
popq %r14
retq