// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: bp_solve(ug_opt_t*, kv_u_trans_t*, ma_ug_t*, asg_t*, bubble_type*, double)
void bp_solve(ug_opt_t *opt, kv_u_trans_t *ref, ma_ug_t *ug, asg_t *sg, bubble_type *bub, double cis_rate)
{
    uint64_t k, i, v, nvx, n[3], nv; u_trans_t *z;
    kv_u_trans_t in; memset(&in, 0, sizeof(in));
    ma_utg_t *u = NULL; asg_arc_t *av; double w;
    ps_t *s = init_ps_t(11, ug->g->n_seq);

    for (i = 0; i < ug->g->n_seq; i++) {
        s->s[i] = 0;
        if((ug->g->seq[i].del) || (IF_HOM(i, (*bub)))) {
            continue;
        }

        u = &ug->u.a[i];
        if(u->m == 0 || u->n == 0) continue;
        for (k = n[0] = n[1] = n[2] = 0; k < u->n; k++) {
            if(R_INF.trio_flag[u->a[k]>>33] == FATHER) n[1]++;
            else if(R_INF.trio_flag[u->a[k]>>33] == MOTHER) n[2]++;
            else n[0]++;
        }
        if(n[1] >= n[2] && n[1] >= n[0]) s->s[i] = 1;
        else if(n[2] >= n[1] && n[2] >= n[0]) s->s[i] = -1;
    }

    kv_resize(u_trans_t, in, ref->n);
    for (k = in.n = 0; k < ref->n; k++) {
        if((IF_HOM(ref->a[k].qn, (*bub))) || (IF_HOM(ref->a[k].tn, (*bub))) || ref->a[k].del) {
            continue;
        }
        kv_push(u_trans_t, in, ref->a[k]);
    }

    if(cis_rate > 0) {
        nvx = ug->g->n_seq<<1;
        for (v = 0; v < nvx; v++) {
            if((ug->g->seq[v>>1].del) || (IF_HOM((v>>1), (*bub)))) continue;
            av = asg_arc_a(ug->g, v); nv = asg_arc_n(ug->g, v);
            for (i = 0; i < nv; i++) {
                if((av[i].del) || (av[i].v>>1) == (v>>1) || 
                                            (ug->g->seq[av[i].v>>1].del) || (IF_HOM((av[i].v>>1), (*bub)))) {
                    continue;
                }
                w = ((double)av[i].ol)*cis_rate;
                if(w <= 0) continue;
                kv_pushp(u_trans_t, in, &z); 
                memset(z, 0, sizeof((*z)));
                z->f = RC_3; z->nw =-w; z->rev = (v^av[i].v)&1;
                z->qn = v>>1; z->tn = av[i].v>>1;
    
                z->qs = 0; z->qe = sg->seq[z->qn].len;
                if(av[i].ol < sg->seq[z->qn].len) {
                    if(v&1) {
                        z->qe = z->qs + av[i].ol;
                    } else {
                        z->qs = z->qe - av[i].ol;
                    }
                }

                z->ts = 0; z->te = sg->seq[z->tn].len;
                if(av[i].ol < sg->seq[z->tn].len) {
                    if(av[i].v&1) {
                        z->ts = z->te - av[i].ol;
                    } else {
                        z->te = z->ts + av[i].ol;
                    }
                }
            }

        }
    }
    // fprintf(stderr, "[M::%s::] ==> nup::%lu\n", __func__, nup);
    clean_u_trans_t_idx_adv(&in, ug, sg);

    mc_solve(NULL, NULL, &in, ug, sg, 0.8, NULL, 0, s->s, 1, bub, ref, 0, 0);
    label_unitigs_sm(s->s, NULL, ug);
    destory_ps_t(&s); kv_destroy(in); kv_destroy(in.idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r8, %rbx
movq %rcx, 0x58(%rsp)
movq %rdx, %r15
movq %rsi, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x78(%rsp)
andq $0x0, 0x88(%rsp)
movupd %xmm0, 0x68(%rsp)
movq 0x18(%rdx), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
pushq $0xb
popq %rdi
callq 0x1193f3
movq %rbx, %r9
movq %rax, 0xb8(%rsp)
xorl %r14d, %r14d
leaq 0x137c26(%rip), %rcx    # 0x254230
movq %rbx, 0x38(%rsp)
movq 0x18(%r15), %rdx
movl 0x14(%rdx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %r14
jae 0x11c6fb
movq (%rax), %rdx
movb $0x0, (%rdx,%r14)
movq 0x18(%r15), %rdx
movq 0x20(%rdx), %rdx
cmpl $0x0, (%rdx,%r14,8)
js 0x11c6f3
movq (%r9), %rdx
movq 0x80(%r9), %rsi
movl (%rdx,%r14,4), %edx
incq %rsi
cmpq %rdx, %rsi
jb 0x11c6f3
movq 0x10(%r15), %rdx
imulq $0x28, %r14, %rsi
cmpl $0x0, 0xc(%rdx,%rsi)
je 0x11c6f3
addq %rsi, %rdx
movl 0x10(%rdx), %r8d
testq %r8, %r8
je 0x11c6f3
movq 0x28(%rcx), %r9
movq 0x18(%rdx), %r10
xorl %esi, %esi
xorl %edx, %edx
xorl %edi, %edi
xorl %r11d, %r11d
cmpq %r11, %r8
je 0x11c6b8
movq (%r10,%r11,8), %rbx
shrq $0x21, %rbx
movzbl (%r9,%rbx), %ebx
cmpl $0x2, %ebx
je 0x11c6ab
cmpl $0x1, %ebx
jne 0x11c6b0
incq %rsi
jmp 0x11c6b3
incq %rdi
jmp 0x11c6b3
incq %rdx
incq %r11
jmp 0x11c68a
cmpq %rdi, %rsi
setb %r8b
cmpq %rdx, %rsi
setb %r9b
orb %r8b, %r9b
movb $0x1, %r8b
cmpb $0x1, %r9b
movq 0x38(%rsp), %r9
jne 0x11c6ec
cmpq %rsi, %rdi
setb %sil
cmpq %rdx, %rdi
setb %dl
orb %sil, %dl
movb $-0x1, %r8b
jne 0x11c6f3
movq (%rax), %rdx
movb %r8b, (%rdx,%r14)
incq %r14
jmp 0x11c60f
movq (%rbp), %rbx
testq %rbx, %rbx
movq %rax, 0x98(%rsp)
je 0x11c761
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r14
shrq $0x10, %r14
orq %rax, %r14
incq %r14
movq %r14, 0x68(%rsp)
imulq $0x30, %r14, %rdi
callq 0x2280
movq %r14, %r8
movq 0x38(%rsp), %r9
movq %rax, 0x70(%rsp)
jmp 0x11c766
xorl %eax, %eax
xorl %r8d, %r8d
movsd 0x40(%rsp), %xmm3
andq $0x0, 0x60(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq %rbx, %r13
jae 0x11c835
movq (%r9), %rsi
movq 0x80(%r9), %rdx
movq 0x10(%rbp), %rcx
movl 0x8(%rcx,%r12), %edi
movl (%rsi,%rdi,4), %edi
incq %rdx
cmpq %rdi, %rdx
jb 0x11c829
movl 0x14(%rcx,%r12), %edi
movl (%rsi,%rdi,4), %esi
cmpq %rsi, %rdx
jb 0x11c829
cmpb $0x0, 0x28(%rcx,%r12)
js 0x11c829
cmpq %r8, %r14
jne 0x11c7f6
leaq (%r8,%r8), %rbx
testq %r8, %r8
pushq $0x2
popq %rcx
cmoveq %rcx, %rbx
movq %rbx, 0x68(%rsp)
imulq $0x30, %rbx, %rsi
movq %rax, %rdi
callq 0x2370
movsd 0x40(%rsp), %xmm3
movq 0x38(%rsp), %r9
movq %rax, 0x70(%rsp)
movq 0x10(%rbp), %rcx
movq %rbx, %r8
imulq $0x30, %r14, %rdx
incq %r14
movq %r14, 0x60(%rsp)
movupd (%rcx,%r12), %xmm0
movups 0x10(%rcx,%r12), %xmm1
movupd 0x20(%rcx,%r12), %xmm2
movupd %xmm2, 0x20(%rax,%rdx)
movups %xmm1, 0x10(%rax,%rdx)
movupd %xmm0, (%rax,%rdx)
movq (%rbp), %rbx
incq %r13
addq $0x30, %r12
jmp 0x11c77b
movq %rbp, 0x90(%rsp)
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jbe 0x11cad3
movq 0x18(%r15), %rcx
movl 0x14(%rcx), %ecx
addl %ecx, %ecx
movslq %ecx, %rdi
xorl %r11d, %r11d
xorps %xmm1, %xmm1
movapd 0xd085b(%rip), %xmm2    # 0x1ed0c0
movq %r15, 0xa0(%rsp)
movq %rdi, 0xa8(%rsp)
cmpq %rdi, %r11
je 0x11cad3
movq 0x18(%r15), %rcx
movq 0x20(%rcx), %rdx
movq %r11, %r12
shrq %r12
cmpl $0x0, (%rdx,%r12,8)
js 0x11cacb
movq (%r9), %rdx
movq 0x80(%r9), %rsi
movl (%rdx,%r12,4), %edx
incq %rsi
cmpq %rdx, %rsi
jb 0x11cacb
movq 0x8(%rcx), %rdx
movq 0x28(%rcx), %rcx
movq (%rcx,%r11,8), %rcx
movl %ecx, %esi
shrq $0x20, %rcx
movl %r12d, %edi
movq %rdi, 0xb0(%rsp)
imulq $0x18, %rsi, %rbp
imulq $0x18, %rcx, %rcx
leaq (%rdx,%rcx), %rbx
addq $0xc, %rbx
xorl %r13d, %r13d
movq %r11, 0x48(%rsp)
cmpq %r13, %rbp
je 0x11cac3
movl (%rbx,%r13), %ecx
testl %ecx, %ecx
js 0x11c9a4
movl -0x4(%rbx,%r13), %edx
shrl %edx
cmpq %rdx, %r12
je 0x11c9a4
movq 0x18(%r15), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0x11c9a4
movq (%r9), %rsi
movq 0x80(%r9), %rdi
movl (%rsi,%rdx,4), %edx
incq %rdi
cmpq %rdx, %rdi
jb 0x11c9a4
cvtsi2sd %ecx, %xmm4
mulsd %xmm3, %xmm4
ucomisd %xmm4, %xmm0
jae 0x11c9a4
cmpq %r8, %r14
jne 0x11c9b3
leaq (%r8,%r8), %r15
testq %r8, %r8
pushq $0x2
popq %rcx
cmoveq %rcx, %r15
movq %r15, 0x68(%rsp)
imulq $0x30, %r15, %rsi
movq %rax, %rdi
movapd %xmm4, 0xc0(%rsp)
callq 0x2370
movapd 0xc0(%rsp), %xmm4
movapd 0xd0742(%rip), %xmm2    # 0x1ed0c0
xorps %xmm1, %xmm1
movq 0x48(%rsp), %r11
xorpd %xmm0, %xmm0
movsd 0x40(%rsp), %xmm3
movq %rax, 0x70(%rsp)
movq %r15, 0x50(%rsp)
movq 0xa0(%rsp), %r15
jmp 0x11c9b8
movq %r14, %rcx
addq $0x18, %r13
movq %rcx, %r14
jmp 0x11c8e6
movq %r8, 0x50(%rsp)
leaq 0x1(%r14), %rcx
movq %rcx, 0x60(%rsp)
imulq $0x30, %r14, %r10
leaq (%rax,%r10), %rdx
movups %xmm1, 0x20(%rax,%r10)
movups %xmm1, 0x10(%rax,%r10)
movups %xmm1, (%rax,%r10)
movb $0x3, 0x28(%rax,%r10)
xorpd %xmm2, %xmm4
movlpd %xmm4, 0x20(%rax,%r10)
movl -0x4(%rbx,%r13), %esi
xorl %r11d, %esi
shlb $0x6, %sil
andb $0x40, %sil
orb $0x3, %sil
movb %sil, 0x28(%rax,%r10)
movl %r12d, 0x8(%rax,%r10)
movl -0x4(%rbx,%r13), %esi
movl %esi, %edi
shrl %edi
movl %edi, 0x14(%rax,%r10)
movq 0x58(%rsp), %r8
movq 0x20(%r8), %r8
movq 0xb0(%rsp), %r11
movl (%r8,%r11,8), %r9d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andl %r14d, %r9d
movl %r9d, 0x4(%rax,%r10)
movl (%rbx,%r13), %r10d
andl %r14d, %r10d
movl (%r8,%r11,8), %r11d
andl %r14d, %r11d
cmpl %r11d, %r10d
jae 0x11ca60
testb $0x1, 0x48(%rsp)
jne 0x11ca5c
subl %r10d, %r9d
movl %r9d, (%rdx)
jmp 0x11ca60
movl %r10d, 0x4(%rdx)
andl $0x0, 0xc(%rdx)
movl (%r8,%rdi,8), %r9d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r9d
movl %r9d, 0x10(%rdx)
movl (%rbx,%r13), %r10d
andl %r11d, %r10d
movl (%r8,%rdi,8), %edi
andl %r11d, %edi
cmpl %edi, %r10d
jae 0x11ca9e
testb $0x1, %sil
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r11
jne 0x11cab2
movl %r10d, 0x10(%rdx)
jmp 0x11cab9
movq 0x38(%rsp), %r9
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r11
jmp 0x11c9a7
subl %r10d, %r9d
movl %r9d, 0xc(%rdx)
movq 0x38(%rsp), %r9
jmp 0x11c9a7
movq 0xa8(%rsp), %rdi
incq %r11
jmp 0x11c875
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq 0x58(%rsp), %rbx
movq %rbx, %rdx
movq %r9, %r14
callq 0x885ac
movq 0x98(%rsp), %r12
movq (%r12), %rax
movq 0x90(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %r14, 0x18(%rsp)
andl $0x0, 0x30(%rsp)
movq %rax, 0x8(%rsp)
andl $0x0, 0x28(%rsp)
movl $0x1, 0x10(%rsp)
andl $0x0, (%rsp)
movsd 0xc69d1(%rip), %xmm0    # 0x1e3500
xorl %edi, %edi
xorl %esi, %esi
movq %rbp, %rdx
movq %r15, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x12b808
movq (%r12), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x116189
leaq 0xb8(%rsp), %rdi
callq 0x11943a
movq 0x10(%rbp), %rdi
callq 0x2220
movq 0x28(%rbp), %rdi
callq 0x2220
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq