// File: /workspace/github_cpp_makefile_star2_O1/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
movl %r9d, 0x44(%rsp)
movq %rcx, %r15
movq %rsi, 0x48(%rsp)
movq %rdi, %rbx
movl 0x14(%rdi), %ebp
addl %ebp, %ebp
movq %r8, 0x50(%rsp)
testq %r8, %r8
sete %al
testl %ebp, %ebp
sete %cl
xorl %r8d, %r8d
orb %al, %cl
jne 0x191268
movq %rdx, %r12
movl %ebp, %r14d
xorl %r13d, %r13d
cmpb $0x0, (%r12,%r13)
je 0x19106d
incq %r13
cmpq %r14, %r13
jne 0x191059
jmp 0x191174
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x191060
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x191060
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x191060
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 0x1910ad
cmpl $0x2, %ecx
jb 0x191060
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r8, 0x38(%rsp)
movl %r8d, 0x30(%rsp)
movl %r8d, 0x28(%rsp)
movq %r8, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl %r8d, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %r13d, %edx
movq 0x50(%rsp), %rcx
movq %r15, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
movb $0x1, %dil
xorl %r8d, %r8d
testq %rax, %rax
je 0x191060
cmpq $0x0, 0x38(%r15)
je 0x19115b
movl $0x1, %eax
xorl %ecx, %ecx
movq 0x48(%r15), %rdx
movl (%rdx,%rcx,4), %edx
cmpq %rdx, %r13
je 0x191151
movq 0x18(%r15), %rsi
cmpl (%rsi), %edx
je 0x191151
xorq $0x1, %rdx
movb %dil, (%r12,%rdx)
movq 0x48(%r15), %rdx
movl (%rdx,%rcx,4), %ecx
movb %dil, (%r12,%rcx)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r15)
ja 0x19112a
movb $0x2, (%r12,%r13)
movq 0x18(%r15), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r12,%rax)
jmp 0x191060
xorl %r9d, %r9d
testl %ebp, %ebp
je 0x191265
xorl %r13d, %r13d
xorl %r8d, %r8d
cmpb $0x2, (%r12,%r13)
jne 0x191257
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x191257
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x191257
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x191257
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 0x1911dc
cmpl $0x2, %ecx
jb 0x191257
movq 0x48(%rsp), %rsi
movq 0x18(%rsi), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x38(%rsp)
movl %r9d, 0x30(%rsp)
movl %r9d, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl 0x90(%rsp), %eax
movl %eax, (%rsp)
movq $0x0, 0x20(%rsp)
movl $0x1, 0x8(%rsp)
movl %r13d, %edx
movq 0x50(%rsp), %rcx
movq %r8, %rbp
movq %r15, %r8
movl 0x44(%rsp), %r9d
callq 0xbf39f
xorl %r9d, %r9d
movq %rbp, %r8
addq %rax, %r8
incq %r13
cmpq %r14, %r13
jne 0x191185
jmp 0x191268
xorl %r8d, %r8d
testq %r8, %r8
je 0x19127b
movq %rbx, %rdi
movq %r8, %rbx
callq 0xa02fa
movq %rbx, %r8
movl %r8d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq