// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movq 0x20(%rdi), %rax
movq 0x18(%rdx), %rcx
movq 0x38(%rdx), %rdi
movl (%rcx), %edx
movl %edx, %ecx
shrl %ecx
movb 0x4(%rax,%rcx,8), %sil
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %rdi
jbe 0xa5642
movq 0x48(%rbx), %r9
movl (%r9,%r8,4), %r9d
shrl %r9d
movb $0x1, 0x4(%rax,%r9,8)
incl %r8d
jmp 0xa5624
movq (%rbx), %rdi
movl %edx, %r8d
imulq $0x1c, %r8, %r8
movl (%rdi,%r8), %r8d
shrl %edx
movb $0x2, 0x4(%rax,%rdx,8)
movl %r8d, %edx
cmpl %ebp, %r8d
jne 0xa5645
movb %sil, 0x4(%rax,%rcx,8)
xorl %r12d, %r12d
pushq $0x1
popq %r15
movl %r12d, %r12d
cmpq %r12, 0x50(%rbx)
jbe 0xa56cd
movq 0x60(%rbx), %rax
movl (%rax,%r12,4), %eax
imulq $0x18, %rax, %rcx
movq 0x8(%r14), %rax
movq 0x20(%r14), %rdi
movq (%rax,%rcx), %rdx
movq %rdx, %rsi
shrq $0x21, %rsi
cmpb $0x1, 0x4(%rdi,%rsi,8)
movl 0x8(%rax,%rcx), %esi
jne 0xa56ac
movl %esi, %r8d
shrl %r8d
cmpb $0x1, 0x4(%rdi,%r8,8)
je 0xa56c8
addq %rcx, %rax
orb $-0x80, 0xf(%rax)
xorl $0x1, %esi
shrq $0x20, %rdx
xorl $0x1, %edx
movq %r14, %rdi
movl %r15d, %ecx
callq 0x6db50
incl %r12d
jmp 0xa566b
movq 0x18(%rbx), %rax
movl (%rax), %r15d
movq (%rbx), %rax
movl %r15d, %ecx
imulq $0x1c, %rcx, %rcx
movl (%rax,%rcx), %r12d
movq 0x20(%r14), %rax
movl %r15d, %ecx
shrl %ecx
andl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x6db50
xorl $0x1, %r15d
movl %r12d, %edx
xorl $0x1, %edx
movq %r14, %rdi
movl %r15d, %esi
xorl %ecx, %ecx
callq 0x6db50
movl %r12d, %r15d
cmpl %ebp, %r12d
jne 0xa56d4
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq