// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/tovlp.cpp
// Function: asg_pop_bubble_primary_trio(asg_t*, ma_ug_t*, unsigned char*, buf_t*, unsigned long, unsigned int, unsigned int, utg_trans_t*)
int asg_pop_bubble_primary_trio(asg_t *g, ma_ug_t *ug, uint8_t *bs_flag, buf_t *b,
uint64_t max_dist, uint32_t positive_flag, uint32_t negative_flag, utg_trans_t *o)
{
	uint32_t v, n_vtx = g->n_seq * 2, n_arc, nv, i;
	uint64_t n_pop = 0;
    asg_arc_t *av = NULL;

    if(max_dist > 0)
    {
        for (v = 0; v < n_vtx; ++v) 
        {
            if(bs_flag[v] != 0) continue;
            nv = asg_arc_n(g, v);
            av = asg_arc_a(g, v);
            ///some node could be deleted
            if (nv < 2 || g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
            ///some edges could be deleted
            for (i = n_arc = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
                if (!av[i].del) ++n_arc;
            if (n_arc < 2) continue;
            if(asg_bub_pop1_primary_trio(ug->g, NULL, v, max_dist, b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL))
            {
                //beg is v, end is b.S.a[0]
                //note b.b include end, does not include beg
                for (i = 0; i < b->b.n; i++)
                {
                    if(b->b.a[i]==v || b->b.a[i]==b->S.a[0]) continue;
                    bs_flag[b->b.a[i]] = bs_flag[b->b.a[i]^1] = 1;
                }
                bs_flag[v] = 2; bs_flag[b->S.a[0]^1] = 3;
            }
        }

        //traverse all node with two directions 
        for (v = 0; v < n_vtx; ++v) {
            if(bs_flag[v] !=2) continue;
            nv = asg_arc_n(g, v);
            av = asg_arc_a(g, v);
            ///some node could be deleted
            if (nv < 2 || g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
            ///some edges could be deleted
            for (i = n_arc = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
                if (!av[i].del) ++n_arc;
            if (n_arc > 1)
                n_pop += asg_bub_pop1_primary_trio(ug->g, ug, v, max_dist, b, positive_flag, negative_flag, 1, NULL, NULL, NULL, 0, 0, o);
        }
        
        if(VERBOSE >= 1)
        {
            fprintf(stderr, "[M::%s] popped %lu bubbles\n", __func__, (unsigned long)n_pop);
        }
    }
    
    if (n_pop) asg_cleanup(g);
    return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
xorl %ebp, %ebp
testq %r8, %r8
je 0x19a025
movq %rdi, %rbx
movl 0x14(%rdi), %eax
addl %eax, %eax
je 0x19a025
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r10
movl %r9d, 0x4c(%rsp)
movl %eax, 0x40(%rsp)
movl %eax, %r14d
xorl %ebp, %ebp
movq %r8, 0x50(%rsp)
cmpb $0x0, (%r12,%rbp)
je 0x199e14
incq %rbp
cmpq %r14, %rbp
jne 0x199e00
jmp 0x199f1e
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x199e07
movq 0x20(%rbx), %rcx
movl %ebp, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x199e07
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x199e07
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0xc, %rdx
xorl %ecx, %ecx
movl (%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %ecx
addq $0x18, %rdx
decq %rax
jne 0x199e53
cmpl $0x2, %ecx
jb 0x199e07
movq 0x18(%r10), %rdi
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %ebp, %edx
movq %r8, %rcx
movq %r15, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq %r10, %r13
callq 0xc02d6
movb $0x1, %r9b
movq %r13, %r10
movq 0x50(%rsp), %r8
testq %rax, %rax
je 0x199e07
movq 0x38(%r15), %rcx
testq %rcx, %rcx
je 0x199f05
movl $0x1, %eax
xorl %edx, %edx
movq 0x48(%r15), %rsi
movl (%rsi,%rdx,4), %esi
cmpq %rsi, %rbp
je 0x199efc
movq 0x18(%r15), %rdi
cmpl (%rdi), %esi
je 0x199efc
xorq $0x1, %rsi
movb %r9b, (%r12,%rsi)
movq 0x48(%r15), %rcx
movl (%rcx,%rdx,4), %ecx
movb %r9b, (%r12,%rcx)
movq 0x38(%r15), %rcx
movl %eax, %edx
incl %eax
cmpq %rdx, %rcx
ja 0x199ed1
movb $0x2, (%r12,%rbp)
movq 0x18(%r15), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r12,%rax)
jmp 0x199e07
cmpl $0x0, 0x40(%rsp)
je 0x19a023
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpb $0x2, (%r12,%r13)
jne 0x19a008
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x19a008
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x19a008
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x19a008
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0xc, %rdx
xorl %ecx, %ecx
movl (%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %ecx
addq $0x18, %rdx
decq %rax
jne 0x199f85
cmpl $0x2, %ecx
jb 0x19a008
movq 0x18(%r10), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl 0x90(%rsp), %eax
movl %eax, (%rsp)
movq $0x0, 0x20(%rsp)
movl $0x1, 0x8(%rsp)
movq %r10, %rsi
movl %r13d, %edx
movq %r8, %rcx
movq %r15, %r8
movl 0x4c(%rsp), %r9d
movq %rbp, 0x40(%rsp)
movq %r10, %rbp
callq 0xc02d6
movq %rbp, %r10
movq 0x40(%rsp), %rbp
movq 0x50(%rsp), %r8
addq %rax, %rbp
incq %r13
cmpq %r14, %r13
jne 0x199f2e
testq %rbp, %rbp
je 0x19a023
movq %rbx, %rdi
callq 0xa0a7d
jmp 0x19a025
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq