// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movl %ecx, 0x44(%rsp)
movl %edx, 0x40(%rsp)
movq %rsi, %r14
movq %rdi, 0x48(%rsp)
movq 0x18(%rdi), %rbx
movl 0x14(%rbx), %eax
leal (%rax,%rax), %ebp
testl %eax, %eax
js 0x971ad
movq %rbx, %rdi
callq 0x69638
xorps %xmm0, %xmm0
movups %xmm0, 0xb8(%rsp)
movups %xmm0, 0xa8(%rsp)
movups %xmm0, 0x98(%rsp)
movups %xmm0, 0x88(%rsp)
movups %xmm0, 0x78(%rsp)
movups %xmm0, 0x68(%rsp)
movl %ebp, %r15d
pushq $0x1c
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x60(%rsp)
testq %r14, %r14
je 0x971f7
movq (%r14), %rbp
jmp 0x97207
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x84975
movq %rax, %rbp
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, %r14
testq %rbp, %rbp
je 0x97487
movq 0x108(%rsp), %r9
xorl %r12d, %r12d
movb $0x1, %r13b
cmpq %r15, %r12
je 0x97349
cmpb $0x0, (%r14,%r12)
je 0x97241
incq %r12
jmp 0x9722c
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x9723c
movq 0x20(%rbx), %rcx
movl %r12d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x9723c
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x9723c
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x9729b
movl (%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
jmp 0x97286
cmpl $0x2, %esi
jb 0x9723c
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
xorl %esi, %esi
movl %r12d, %edx
movq %rbp, %rcx
leaq 0x60(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
movq 0x108(%rsp), %r9
testq %rax, %rax
je 0x9723c
movq 0x78(%rsp), %rax
movq 0x98(%rsp), %rcx
movq 0xa8(%rsp), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0x97334
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r12
je 0x97330
cmpl (%rax), %edi
je 0x97330
movq %rdi, %r8
xorq $0x1, %r8
movb %r13b, (%r14,%r8)
movb %r13b, (%r14,%rdi)
incl %esi
jmp 0x9730e
movb $0x2, (%r14,%r12)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%r14,%rax)
jmp 0x9723c
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq %r15, %r13
je 0x9748a
cmpb $0x2, (%r14,%r13)
jne 0x9747f
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x9747f
movq 0x20(%rbx), %rcx
movl %r13d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x9747f
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x9747f
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x973c9
movl (%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
jmp 0x973b4
cmpl $0x2, %esi
jb 0x9747f
testq %r9, %r9
je 0x9741b
movq %r9, 0x18(%rsp)
movl 0x100(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movl %r13d, %edx
movq %rbp, %rcx
movl 0x40(%rsp), %r8d
movl 0x44(%rsp), %r9d
callq 0xaa45d
jmp 0x97474
movq 0x48(%rsp), %rsi
movq 0x18(%rsi), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x100(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x30(%rsp)
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
movl $0x1, 0x8(%rsp)
movl %r13d, %edx
movq %rbp, %rcx
leaq 0x60(%rsp), %r8
movl 0x40(%rsp), %r9d
callq 0x84a86
movq 0x108(%rsp), %r9
addq %rax, %r12
incq %r13
jmp 0x9734f
xorl %r12d, %r12d
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %rdi
callq 0x2220
testq %r12, %r12
je 0x974d2
movq %rbx, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x2220
movl %r12d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq