// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x110, %rsp            # imm = 0x110
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x58(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x11c7d0
movq -0x40(%rbp), %rdi
callq 0xce020
leaq -0xd8(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl -0x58(%rbp), %eax
movl %eax, %edi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x11c80d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x11c821
movq -0x40(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0xfde50
movq %rax, -0x50(%rbp)
movq $0x0, -0xe0(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0x50(%rbp)
jbe 0x11cc33
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x11ca68
movq -0xe0(%rbp), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x11c878
jmp 0x11ca5a
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x54(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
cmpl $0x2, -0x60(%rbp)
jb 0x11c8e9
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x11c8e9
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x11c8ee
jmp 0x11ca5a
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x11c933
movq -0x70(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x11c926
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x11c928
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x11c8fc
cmpl $0x2, -0x5c(%rbp)
jae 0x11c93e
jmp 0x11ca5a
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x54(%rbp), %edx
movq -0x50(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xd8(%rbp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
callq 0xfe040
cmpq $0x0, %rax
je 0x11ca58
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpq -0xa0(%rbp), %rax
jae 0x11ca31
movq -0x90(%rbp), %rax
movl -0x64(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x54(%rbp), %eax
je 0x11c9ef
movq -0x90(%rbp), %rax
movl -0x64(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0xc0(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x11c9f1
jmp 0x11ca26
movq -0xe0(%rbp), %rax
movq -0x90(%rbp), %rcx
movl -0x64(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0xe0(%rbp), %rax
movq -0x90(%rbp), %rcx
movl -0x64(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x11c9b9
movq -0xe0(%rbp), %rax
movl -0x54(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq -0xe0(%rbp), %rax
movq -0xc0(%rbp), %rcx
movl (%rcx), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x3, (%rax,%rcx)
jmp 0x11ca5a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x11c854
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x11cc31
movq -0xe0(%rbp), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
je 0x11ca93
jmp 0x11cc23
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x54(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
cmpl $0x2, -0x60(%rbp)
jb 0x11cb04
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x11cb04
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x11cb09
jmp 0x11cc23
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x11cb4e
movq -0x70(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x11cb41
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x11cb43
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x11cb17
cmpl $0x1, -0x5c(%rbp)
jbe 0x11cc21
cmpq $0x0, 0x18(%rbp)
je 0x11cbab
movq -0x18(%rbp), %rdi
movl -0x54(%rbp), %edx
movq -0x50(%rbp), %rcx
movl -0x24(%rbp), %r8d
movl -0x28(%rbp), %r9d
movq -0x30(%rbp), %rbx
movq -0x38(%rbp), %r11
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
leaq -0xd8(%rbp), %rsi
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x13ee50
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x11cc1f
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x54(%rbp), %edx
movq -0x50(%rbp), %rcx
movl -0x24(%rbp), %r9d
movl -0x28(%rbp), %ebx
movq -0x30(%rbp), %r11
movl 0x10(%rbp), %r10d
movq -0x38(%rbp), %rax
leaq -0xd8(%rbp), %r8
xorl %r14d, %r14d
xorl %r14d, %r14d
movl %ebx, (%rsp)
movl $0x1, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movl %r10d, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0xfe040
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x11cc21
jmp 0x11cc23
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x11ca6f
jmp 0x11cc33
movq -0xd8(%rbp), %rdi
callq 0x2210
movq -0xc0(%rbp), %rdi
callq 0x2210
movq -0xa8(%rbp), %rdi
callq 0x2210
movq -0x90(%rbp), %rdi
callq 0x2210
movq -0x78(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0x48(%rbp)
je 0x11cc7c
movq -0x40(%rbp), %rdi
callq 0xcdd10
movq -0xe0(%rbp), %rdi
callq 0x2210
movq -0x48(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax,%rax)