// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_pop_bubble_primary_trio(ma_ug_t*, unsigned long*, unsigned int, unsigned int, hap_cov_t*, utg_trans_t*, unsigned int, rd_hamming_fly_simp_t*)
int asg_pop_bubble_primary_trio(ma_ug_t *ug, uint64_t* i_max_dist, uint32_t positive_flag, uint32_t negative_flag, hap_cov_t *cov, utg_trans_t *o, uint32_t is_update_chain, rd_hamming_fly_simp_t *p)
{
    asg_t *g = ug->g; uint64_t n_pop = 0, max_dist;
	uint32_t v, n_vtx = g->n_seq * 2, n_arc, nv, i;
    asg_arc_t *av = NULL;
	buf_t b;
	if (!g->is_symm) asg_symm(g);
	memset(&b, 0, sizeof(buf_t));
	///set information for each node
	b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    if(i_max_dist) max_dist = (*i_max_dist);
    else max_dist = get_bub_pop_max_dist_advance(g, &b);
    uint8_t* bs_flag = NULL; CALLOC(bs_flag, n_vtx);

    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) {
                if(p){
                    n_pop += refine_bubble_popping(ug, &b, v, max_dist, positive_flag, negative_flag, cov, o, is_update_chain, p);
                } else {
                    n_pop += asg_bub_pop1_primary_trio(ug->g, ug, v, max_dist, &b, positive_flag, negative_flag, 1, NULL, NULL, cov, is_update_chain, 0, o);
                }
            }
        }
        
        if(VERBOSE >= 1)
        {
            fprintf(stderr, "[M::%s] popped %lu bubbles\n", __func__, (unsigned long)n_pop);
        }
    }
    
    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    if (n_pop) asg_cleanup(g);
    free(bs_flag);
    return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, %r13
movq %r8, 0x68(%rsp)
movl %ecx, 0x4c(%rsp)
movl %edx, 0x48(%rsp)
movq %rsi, %r14
movq %rdi, 0x58(%rsp)
movq 0x18(%rdi), %rbx
movl 0x14(%rbx), %eax
leal (%rax,%rax), %r12d
testl %eax, %eax
js 0xd3891
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0xc8(%rsp)
movups %xmm0, 0xb8(%rsp)
movups %xmm0, 0xa8(%rsp)
movups %xmm0, 0x98(%rsp)
movups %xmm0, 0x88(%rsp)
movups %xmm0, 0x78(%rsp)
movl %r12d, %ebp
movl $0x1c, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x70(%rsp)
testq %r14, %r14
je 0xd38e0
movq (%r14), %r15
jmp 0xd38f0
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
callq 0xc0162
movq %rax, %r15
movl $0x1, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, %r14
testq %r15, %r15
je 0xd3b7b
movq %r15, 0x50(%rsp)
xorl %r15d, %r15d
testl %r12d, %r12d
je 0xd3b7e
movq %r13, 0x60(%rsp)
xorl %r13d, %r13d
cmpb $0x0, (%r14,%r13)
je 0xd3936
incq %r13
cmpq %rbp, %r13
jne 0xd3922
jmp 0xd3a3d
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xd3929
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xd3929
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xd3929
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 0xd3976
cmpl $0x2, %ecx
jb 0xd3929
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r15, 0x38(%rsp)
movl %r15d, 0x30(%rsp)
movl %r15d, 0x28(%rsp)
movq %r15, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl %r15d, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %esi, %esi
movl %r13d, %edx
movq 0x50(%rsp), %rcx
leaq 0x70(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
movb $0x1, %r9b
testq %rax, %rax
je 0xd3929
movq 0x88(%rsp), %rax
movq 0xa8(%rsp), %rcx
testq %rcx, %rcx
je 0xd3a28
movq 0xb8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r13
je 0xd3a1e
cmpl (%rax), %edi
je 0xd3a1e
movq %rdi, %r8
xorq $0x1, %r8
movb %r9b, (%r14,%r8)
movb %r9b, (%r14,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0xd3a03
movb $0x2, (%r14,%r13)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r14,%rax)
jmp 0xd3929
testl %r12d, %r12d
je 0xd3b7b
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x68(%rsp), %r12
cmpb $0x2, (%r14,%r13)
jne 0xd3b6d
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xd3b6d
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xd3b6d
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xd3b6d
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 0xd3aa8
cmpl $0x2, %ecx
jb 0xd3b6d
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0xd3b11
movq %rax, 0x18(%rsp)
movl 0x110(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
movq 0x58(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %r13d, %edx
movq 0x50(%rsp), %rcx
movl 0x48(%rsp), %r8d
movl 0x4c(%rsp), %r9d
callq 0xe8523
jmp 0xd3b6a
movq 0x58(%rsp), %rsi
movq 0x18(%rsi), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x110(%rsp), %eax
movl %eax, 0x28(%rsp)
movq %r12, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, (%rsp)
movl $0x0, 0x30(%rsp)
movl $0x1, 0x8(%rsp)
movl %r13d, %edx
movq 0x50(%rsp), %rcx
leaq 0x70(%rsp), %r8
movl 0x48(%rsp), %r9d
callq 0xc02d6
addq %rax, %r15
incq %r13
cmpq %rbp, %r13
jne 0xd3a51
jmp 0xd3b7e
xorl %r15d, %r15d
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x88(%rsp), %rdi
callq 0x2220
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0xb8(%rsp), %rdi
callq 0x2220
movq 0xd0(%rsp), %rdi
callq 0x2220
testq %r15, %r15
je 0xd3bc9
movq %rbx, %rdi
callq 0xa0a7d
movq %r14, %rdi
callq 0x2220
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq