// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: refine_bubble_popping(ma_ug_t*, buf_t*, unsigned int, unsigned long, unsigned int, unsigned int, hap_cov_t*, utg_trans_t*, unsigned int, rd_hamming_fly_simp_t*)
uint64_t refine_bubble_popping(ma_ug_t *ug, buf_t *b, uint32_t v0, uint64_t 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 *pf)
{
    // fprintf(stderr, "[M::%s]\n", __func__);
    if(!asg_bub_pop1_primary_trio(ug->g, ug, v0, max_dist, b, positive_flag, negative_flag, 0, NULL, NULL, NULL, 0, 0, NULL)) return 0;
    // if(((v0>>1) == 7536) && ((b->S.a[0]>>1) == 99223)) {
    //     fprintf(stderr, "[M::%s] uga_v::%u\n", __func__, v0); dbg_asys_gfa(ug->g);
    //     print_simple_dbg_gfa(ug->g, "uga");
    // }
    uint32_t non_positive_flag = (uint32_t)-1, v, u, k, rId, pn, npn;
    if(positive_flag == FATHER) non_positive_flag = MOTHER;
    if(positive_flag == MOTHER) non_positive_flag = FATHER;
    ma_utg_t* p = NULL;
    ///b->S.a[0] is the sink of this bubble
    ///v is the sink of this bubble
    v = b->S.a[0]; pn = npn = 0;
    ///scan node
    do {
        u = b->a[v].p; // u->v
        if(v != b->S.a[0]) {
            p = &(ug->u.a[v>>1]);
            for (k = 0; k < p->n; k++) {
                rId = p->a[k]>>33;
                if(R_INF.trio_flag[rId] == positive_flag) pn++;
                if(R_INF.trio_flag[rId] == non_positive_flag) npn++;
            }
        }
        v = u;
    } while (v != v0);
    // fprintf(stderr, "[M::%s] pn::%u, npn::%u\n", __func__, pn, npn);
    ///debug
    if((npn <= 0) || ((npn <= ((npn+pn)*0.05)) && (npn <= 64))) {///phasing is ok
        return asg_bub_pop1_primary_trio(ug->g, ug, v0, max_dist, b, positive_flag, negative_flag, 1, NULL, NULL, cov, is_update_chain, 0, o);
    }
    return renew_phase_bubble(pf, v0, b, ug, max_dist, positive_flag, negative_flag, cov, o, is_update_chain);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0xa0, %rsp
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movl %edx, -0x2c(%rbp)
movq %rcx, -0x38(%rbp)
movl %r8d, -0x3c(%rbp)
movl %r9d, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %eax
xorl %r10d, %r10d
xorl %r10d, %r10d
movl %eax, (%rsp)
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
jne 0x13eeff
movq $0x0, -0x18(%rbp)
jmp 0x13f0f4
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x3c(%rbp)
jne 0x13ef13
movl $0x2, -0x44(%rbp)
cmpl $0x2, -0x3c(%rbp)
jne 0x13ef20
movl $0x1, -0x44(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl (%rax), %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
je 0x13effe
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x68(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x13effc
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x54(%rbp)
leaq 0x28631a(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x3c(%rbp), %eax
jne 0x13efcf
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
leaq 0x2862fa(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x54(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x44(%rbp), %eax
jne 0x13efef
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x13eff1
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x13ef8d
jmp 0x13effe
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x13ef43
cmpl $0x0, -0x5c(%rbp)
jbe 0x13f044
movl -0x5c(%rbp), %eax
movl %eax, %ecx
movl %ecx, %eax
cvtsi2sd %rcx, %xmm1
movl -0x58(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm0
movsd 0x24320c(%rip), %xmm2   # 0x382240
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x13f0b3
cmpl $0x40, -0x5c(%rbp)
ja 0x13f0b3
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %ebx
movq 0x10(%rbp), %r11
movl 0x20(%rbp), %r10d
movq 0x18(%rbp), %rax
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
movq %rax, -0x18(%rbp)
jmp 0x13f0f4
movq 0x28(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl -0x40(%rbp), %ebx
movq 0x10(%rbp), %r11
movq 0x18(%rbp), %r10
movl 0x20(%rbp), %eax
movl %ebx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x13d970
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0xa0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)