// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r8, %r14
movq %rcx, 0x48(%rsp)
movq %rsi, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq 0x18(%rdx), %rbx
movl 0x14(%rbx), %r15d
movl %r15d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
testl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
je 0x15b51f
leaq 0x12fde6(%rip), %rcx    # 0x28b230
movq 0x28(%rcx), %r15
xorl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movq 0x20(%rbx), %rdx
cmpl $0x0, (%rdx,%rcx,8)
js 0x15b50a
movq (%r14), %rdx
movq 0x80(%r14), %rsi
movl (%rdx,%rcx,4), %edx
incq %rsi
cmpq %rdx, %rsi
jb 0x15b50a
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rsi
cmpl $0x0, 0xc(%rdx,%rsi,8)
je 0x15b50a
leaq (%rdx,%rsi,8), %rdx
movl 0x10(%rdx), %r8d
testq %r8, %r8
je 0x15b50a
movq 0x18(%rdx), %r9
xorl %r10d, %r10d
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
movq (%r9,%r10,8), %r11
shrq $0x21, %r11
movzbl (%r15,%r11), %r11d
cmpl $0x2, %r11d
je 0x15b4c7
cmpl $0x1, %r11d
jne 0x15b4cc
incq %rdi
jmp 0x15b4cf
incq %rdx
jmp 0x15b4cf
incq %rsi
incq %r10
cmpq %r10, %r8
jne 0x15b4a9
cmpq %rdx, %rdi
setb %r8b
cmpq %rsi, %rdi
setb %r9b
orb %r8b, %r9b
movb $0x1, %r8b
cmpb $0x1, %r9b
jne 0x15b506
cmpq %rdi, %rdx
setb %dil
cmpq %rsi, %rdx
setb %dl
orb %dil, %dl
movb $-0x1, %r8b
jne 0x15b50a
movb %r8b, (%rax,%rcx)
incq %rcx
movl 0x14(%rbx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jb 0x15b450
movq %rax, 0x70(%rsp)
movq (%r13), %rax
cmpq %rax, 0x18(%rsp)
jae 0x15b582
decq %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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
movq 0x20(%rsp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x0, (%r13)
movq 0x8(%rsp), %rsi
je 0x15b660
xorl %ebx, %ebx
movl $0x2, %r15d
xorl %r12d, %r12d
movq (%r14), %rdx
movq 0x80(%r14), %rcx
movq 0x10(%r13), %rax
movl 0x8(%rax,%rbx), %edi
movl (%rdx,%rdi,4), %edi
incq %rcx
cmpq %rdi, %rcx
jb 0x15b64f
movl 0x14(%rax,%rbx), %edi
movl (%rdx,%rdi,4), %edx
cmpq %rdx, %rcx
jb 0x15b64f
cmpb $0x0, 0x28(%rax,%rbx)
js 0x15b64f
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jne 0x15b612
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r15, %rcx
movq %rcx, 0x18(%rsp)
movq 0x20(%rsp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x8(%rsp), %rsi
movq %rax, 0x20(%rsp)
movq 0x10(%r13), %rax
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rdx
leaq 0x1(%rcx), %rdi
movq %rdi, 0x10(%rsp)
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movupd (%rax,%rbx), %xmm0
movups 0x10(%rax,%rbx), %xmm1
movups 0x20(%rax,%rbx), %xmm2
movups %xmm2, 0x20(%rdx,%rcx)
movups %xmm1, 0x10(%rdx,%rcx)
movupd %xmm0, (%rdx,%rcx)
incq %r12
addq $0x30, %rbx
cmpq (%r13), %r12
jb 0x15b5a6
movq %r13, 0x68(%rsp)
xorpd %xmm0, %xmm0
movsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x15b8ef
movq 0x18(%rsi), %rax
movl 0x14(%rax), %eax
addl %eax, %eax
je 0x15b8ef
movapd %xmm1, %xmm0
cltq
movq %rax, 0x80(%rsp)
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
movapd 0xd3919(%rip), %xmm3    # 0x22efc0
movq 0x18(%rsi), %rax
movq 0x20(%rax), %rcx
movq %rbp, %r13
shrq %r13
cmpl $0x0, (%rcx,%r13,8)
js 0x15b8de
movq (%r14), %rcx
movq 0x80(%r14), %rdx
movl (%rcx,%r13,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jb 0x15b8de
movq 0x28(%rax), %rcx
movq (%rcx,%rbp,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x15b8de
movq 0x8(%rax), %rax
shrq $0x20, %rcx
movl %r13d, %edi
movq %rdi, 0x78(%rsp)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r15
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbx
addq $0xc, %rbx
xorl %r12d, %r12d
movl (%rbx,%r12), %eax
testl %eax, %eax
js 0x15b8d1
movl -0x4(%rbx,%r12), %ecx
shrl %ecx
cmpq %rcx, %r13
je 0x15b8d1
movq 0x18(%rsi), %rdx
movq 0x20(%rdx), %rdx
cmpl $0x0, (%rdx,%rcx,8)
js 0x15b8d1
movq (%r14), %rdx
movq 0x80(%r14), %rdi
movl (%rdx,%rcx,4), %ecx
incq %rdi
cmpq %rcx, %rdi
jb 0x15b8d1
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm1
jae 0x15b8d1
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jne 0x15b7d4
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq 0x20(%rsp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movapd %xmm4, 0x50(%rsp)
callq 0x2370
movapd 0x50(%rsp), %xmm4
movapd 0xd3803(%rip), %xmm3    # 0x22efc0
xorps %xmm2, %xmm2
xorpd %xmm1, %xmm1
movsd 0x40(%rsp), %xmm0
movq 0x8(%rsp), %rsi
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rdx
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%rsp)
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
leaq (%rdx,%rdi), %rax
movups %xmm2, 0x20(%rdx,%rdi)
movups %xmm2, 0x10(%rdx,%rdi)
movups %xmm2, (%rdx,%rdi)
movb 0x28(%rdx,%rdi), %cl
andb $-0x40, %cl
orb $0x3, %cl
movb %cl, 0x28(%rdx,%rdi)
xorpd %xmm3, %xmm4
movlpd %xmm4, 0x20(%rdx,%rdi)
movl -0x4(%rbx,%r12), %r8d
xorl %ebp, %r8d
shlb $0x6, %r8b
andb $0x40, %r8b
andb $-0x7d, %cl
orb %r8b, %cl
movb %cl, 0x28(%rdx,%rdi)
movl %r13d, 0x8(%rdx,%rdi)
movl -0x4(%rbx,%r12), %ecx
movl %ecx, 0x50(%rsp)
movl %ecx, %r10d
shrl %r10d
movl %r10d, 0x14(%rdx,%rdi)
movl $0x0, (%rdx,%rdi)
movq 0x48(%rsp), %r8
movq 0x20(%r8), %r8
movq 0x78(%rsp), %rcx
movl (%r8,%rcx,8), %r9d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r9d
movl %r9d, 0x4(%rdx,%rdi)
movl (%rbx,%r12), %edx
andl %r11d, %edx
movl (%r8,%rcx,8), %edi
andl %r11d, %edi
cmpl %edi, %edx
jae 0x15b896
testb $0x1, %bpl
jne 0x15b893
subl %edx, %r9d
movl %r9d, (%rax)
jmp 0x15b896
movl %edx, 0x4(%rax)
movl $0x0, 0xc(%rax)
movl (%r8,%r10,8), %edx
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edx
movl %edx, 0x10(%rax)
movl (%rbx,%r12), %edi
andl %r9d, %edi
movl (%r8,%r10,8), %r8d
andl %r9d, %r8d
cmpl %r8d, %edi
jae 0x15b8d1
testb $0x1, 0x50(%rsp)
jne 0x15b8cc
movl %edi, 0x10(%rax)
jmp 0x15b8d1
subl %edi, %edx
movl %edx, 0xc(%rax)
addq $0x18, %r12
cmpq %r12, %r15
jne 0x15b71a
incq %rbp
cmpq 0x80(%rsp), %rbp
jne 0x15b6a7
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq 0x48(%rsp), %r15
movq %r15, %rdx
movq %rsi, %rbx
callq 0xc3b24
subq $0x8, %rsp
xorl %eax, %eax
movsd 0xc9b9b(%rip), %xmm0    # 0x2254b0
xorl %edi, %edi
xorl %esi, %esi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r15, %r8
xorl %r9d, %r9d
pushq %rax
pushq %rax
pushq 0x80(%rsp)
pushq %r14
pushq $0x1
movq 0xa0(%rsp), %r14
pushq %r14
pushq %rax
callq 0x1edea
addq $0x40, %rsp
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x155c54
movq %r14, %rdi
callq 0x2220
movq 0x10(%rbp), %rdi
callq 0x2220
movq 0x28(%rbp), %rdi
callq 0x2220
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq