// File: /workspace/github_cpp_makefile_star2_O1/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, 0x68(%rsp)
movq %r8, %r13
movl %ecx, 0x4c(%rsp)
movl %edx, 0x48(%rsp)
movq %rsi, %r14
movq %rdi, 0x58(%rsp)
movq 0x18(%rdi), %rbx
movl 0x14(%rbx), %r12d
addl %r12d, %r12d
cmpl $0x0, 0x14(%rbx)
js 0xd2430
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0xd0(%rsp)
movl %r12d, %r15d
movl $0x1c, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x70(%rsp)
testq %r14, %r14
je 0xd248b
movq (%r14), %rbp
jmp 0xd249b
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
callq 0xbf22d
movq %rax, %rbp
movl $0x1, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, %r14
movq %rbp, 0x50(%rsp)
testq %rbp, %rbp
sete %al
testl %r12d, %r12d
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0xd272d
movq %r13, 0x60(%rsp)
xorl %r13d, %r13d
cmpb $0x0, (%r14,%r13)
je 0xd24e2
incq %r13
cmpq %r15, %r13
jne 0xd24ce
jmp 0xd25ee
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xd24d5
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xd24d5
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xd24d5
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 0xd2522
cmpl $0x2, %ecx
jb 0xd24d5
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rdi
movq %rbp, 0x38(%rsp)
movl %ebp, 0x30(%rsp)
movl %ebp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl %ebp, 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 0xbf39f
movb $0x1, %r9b
testq %rax, %rax
je 0xd24d5
movq 0xa8(%rsp), %rax
testq %rax, %rax
je 0xd25d1
movq 0x88(%rsp), %rcx
movq 0xb8(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r13
je 0xd25c7
cmpl (%rcx), %edi
je 0xd25c7
movq %rdi, %r8
xorq $0x1, %r8
movb %r9b, (%r14,%r8)
movb %r9b, (%r14,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0xd25ac
movb $0x2, (%r14,%r13)
movq 0x88(%rsp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r14,%rax)
jmp 0xd24d5
testl %r12d, %r12d
je 0xd272b
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x68(%rsp), %r12
cmpb $0x2, (%r14,%r13)
jne 0xd271d
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xd271d
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xd271d
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xd271d
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 0xd2658
cmpl $0x2, %ecx
jb 0xd271d
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0xd26c1
movq %rax, 0x18(%rsp)
movl 0x110(%rsp), %eax
movl %eax, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq 0x60(%rsp), %rax
movq %rax, (%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 0xe6da0
jmp 0xd271a
movq 0x58(%rsp), %rsi
movq 0x18(%rsi), %rdi
movq %r12, 0x38(%rsp)
movl 0x110(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 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 0xbf39f
addq %rax, %rbp
incq %r13
cmpq %r15, %r13
jne 0xd2601
jmp 0xd272d
xorl %ebp, %ebp
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 %rbp, %rbp
je 0xd2778
movq %rbx, %rdi
callq 0xa02fa
movq %r14, %rdi
callq 0x2220
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq