// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, 0x38(%rsp)
movq %rsi, 0x18(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movq %rdx, 0x10(%rsp)
movq 0x18(%rdx), %r14
movl 0x14(%r14), %ebx
movq %rbx, %rdi
andq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
andq $0x7fffffff, %rbx       # imm = 0x7FFFFFFF
je 0x161f1e
leaq 0x1343e3(%rip), %rcx    # 0x296230
movq 0x28(%rcx), %r15
movq 0x20(%r14), %rcx
xorl %edx, %edx
movb $0x0, (%rax,%rdx)
cmpl $0x0, (%rcx,%rdx,8)
js 0x161f12
movq 0x8(%rsp), %rdi
movq (%rdi), %rsi
movq 0x80(%rdi), %rdi
movl (%rsi,%rdx,4), %esi
incq %rdi
cmpq %rsi, %rdi
jb 0x161f12
movq 0x10(%rsp), %rsi
movq 0x10(%rsi), %rsi
leaq (%rdx,%rdx,4), %rdi
cmpl $0x0, 0xc(%rsi,%rdi,8)
je 0x161f12
leaq (%rsi,%rdi,8), %rsi
movl 0x10(%rsi), %r9d
testq %r9, %r9
je 0x161f12
movq 0x18(%rsi), %r10
xorl %r11d, %r11d
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movq (%r10,%r11,8), %r14
shrq $0x21, %r14
movzbl (%r15,%r14), %ebp
cmpl $0x2, %ebp
je 0x161ece
cmpl $0x1, %ebp
jne 0x161ed3
incq %r8
jmp 0x161ed6
incq %rsi
jmp 0x161ed6
incq %rdi
incq %r11
cmpq %r11, %r9
jne 0x161eb2
cmpq %rsi, %r8
setb %r9b
cmpq %rdi, %r8
setb %r10b
orb %r9b, %r10b
movb $0x1, %r9b
cmpb $0x1, %r10b
jne 0x161f0e
cmpq %r8, %rsi
setb %r8b
cmpq %rdi, %rsi
setb %sil
orb %r8b, %sil
movb $-0x1, %r9b
jne 0x161f12
movb %r9b, (%rax,%rdx)
incq %rdx
cmpq %rbx, %rdx
jb 0x161e57
movq %rax, 0x70(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %r13
testq %r13, %r13
je 0x16206b
leaq -0x1(%r13), %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, %rbx
shrq $0x10, %rbx
orq %rax, %rbx
incq %rbx
movq %rbx, 0x48(%rsp)
movq %rbx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdi
callq 0x2280
movq %rbx, %r11
movq 0x18(%rsp), %r10
movq %rax, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
xorl %ebx, %ebx
movl $0x2, %r14d
xorl %r12d, %r12d
xorl %r15d, %r15d
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r9
movq (%r9), %r8
movq 0x80(%r9), %rdx
movq 0x10(%r10), %rcx
movl 0x8(%rcx,%rbx), %edi
movl (%r8,%rdi,4), %edi
incq %rdx
cmpq %rdi, %rdx
jb 0x162059
movl 0x14(%rcx,%rbx), %edi
movl (%r8,%rdi,4), %edi
cmpq %rdi, %rdx
jb 0x162059
cmpb $0x0, 0x28(%rcx,%rbx)
js 0x162059
cmpq %r11, %r15
jne 0x162028
leaq (%r15,%r15), %r13
testq %r15, %r15
cmoveq %r14, %r13
movq %r13, 0x48(%rsp)
movq %r13, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %r13, %r11
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %rsi
movq %rax, 0x50(%rsp)
movq 0x10(%r10), %rcx
leaq (%r15,%r15,2), %rdx
incq %r15
movq %r15, 0x40(%rsp)
shlq $0x4, %rdx
movupd (%rcx,%rbx), %xmm0
movups 0x10(%rcx,%rbx), %xmm1
movups 0x20(%rcx,%rbx), %xmm2
movups %xmm2, 0x20(%rax,%rdx)
movups %xmm1, 0x10(%rax,%rdx)
movupd %xmm0, (%rax,%rdx)
movq (%r10), %r13
incq %r12
addq $0x30, %rbx
cmpq %r13, %r12
jb 0x161faf
jmp 0x162086
movq $0x0, 0x40(%rsp)
xorl %eax, %eax
xorl %r11d, %r11d
xorl %r15d, %r15d
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r9
xorpd %xmm0, %xmm0
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x16232f
movq 0x18(%rsi), %rcx
movl 0x14(%rcx), %ecx
addl %ecx, %ecx
je 0x16232f
movapd %xmm1, %xmm0
movslq %ecx, %r10
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
movapd 0xd910e(%rip), %xmm3    # 0x23b1d0
movq %r10, 0x20(%rsp)
movq 0x18(%rsi), %rcx
movq 0x20(%rcx), %rdx
movq %r12, %r13
shrq %r13
cmpl $0x0, (%rdx,%r13,8)
js 0x16231d
movq (%r9), %rdx
movq 0x80(%r9), %rdi
movl (%rdx,%r13,4), %edx
incq %rdi
cmpq %rdx, %rdi
jb 0x16231d
movq 0x28(%rcx), %rdx
movq (%rdx,%r12,8), %rdx
movq %rdx, %rdi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
andq %r8, %rdi
je 0x16231d
movq 0x8(%rcx), %rcx
shrq $0x20, %rdx
movl %r13d, %r8d
movq %r8, 0x78(%rsp)
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rbp
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rbx
addq $0xc, %rbx
xorl %r14d, %r14d
movl (%rbx,%r14), %ecx
testl %ecx, %ecx
js 0x1621f0
movl -0x4(%rbx,%r14), %edx
shrl %edx
cmpq %rdx, %r13
je 0x1621f0
movq 0x18(%rsi), %rdi
movq 0x20(%rdi), %rdi
cmpl $0x0, (%rdi,%rdx,8)
js 0x1621f0
movq (%r9), %r8
movq 0x80(%r9), %rdi
movl (%r8,%rdx,4), %edx
incq %rdi
cmpq %rdx, %rdi
jb 0x1621f0
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm1
jae 0x1621f0
cmpq %r11, %r15
jne 0x162208
leaq (%r11,%r11), %rdx
testq %r11, %r11
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movq %rdx, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
movapd %xmm4, 0x80(%rsp)
callq 0x2370
movapd 0x80(%rsp), %xmm4
movapd 0xd8ff4(%rip), %xmm3    # 0x23b1d0
xorps %xmm2, %xmm2
xorpd %xmm1, %xmm1
movsd 0x30(%rsp), %xmm0
movq %rax, 0x50(%rsp)
jmp 0x16220d
movq %r15, %rcx
addq $0x18, %r14
movq %rcx, %r15
cmpq %r14, %rbp
jne 0x16213b
jmp 0x162320
movq %r11, 0x28(%rsp)
leaq 0x1(%r15), %rcx
movq %rcx, 0x40(%rsp)
leaq (%r15,%r15,2), %r9
shlq $0x4, %r9
leaq (%rax,%r9), %rdx
movups %xmm2, 0x20(%rax,%r9)
movups %xmm2, 0x10(%rax,%r9)
movups %xmm2, (%rax,%r9)
movb $0x3, 0x28(%rax,%r9)
xorpd %xmm3, %xmm4
movlpd %xmm4, 0x20(%rax,%r9)
movl -0x4(%rbx,%r14), %esi
xorl %r12d, %esi
shlb $0x6, %sil
andb $0x40, %sil
orb $0x3, %sil
movb %sil, 0x28(%rax,%r9)
movl %r13d, 0x8(%rax,%r9)
movl -0x4(%rbx,%r14), %esi
movl %esi, %edi
shrl %edi
movl %edi, 0x14(%rax,%r9)
movq 0x38(%rsp), %r8
movq 0x20(%r8), %r8
movq 0x78(%rsp), %r11
movl (%r8,%r11,8), %r10d
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andl %r15d, %r10d
movl %r10d, 0x4(%rax,%r9)
movl (%rbx,%r14), %r9d
andl %r15d, %r9d
movl (%r8,%r11,8), %r11d
andl %r15d, %r11d
cmpl %r11d, %r9d
jae 0x1622b5
testb $0x1, %r12b
jne 0x1622b1
subl %r9d, %r10d
movl %r10d, (%rdx)
jmp 0x1622b5
movl %r9d, 0x4(%rdx)
movl $0x0, 0xc(%rdx)
movl (%r8,%rdi,8), %r9d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %r9d
movl %r9d, 0x10(%rdx)
movl (%rbx,%r14), %r10d
andl %r11d, %r10d
movl (%r8,%rdi,8), %edi
andl %r11d, %edi
cmpl %edi, %r10d
jae 0x1622f6
testb $0x1, %sil
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %r11
jne 0x162307
movl %r10d, 0x10(%rdx)
jmp 0x16230e
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r9
movq 0x28(%rsp), %r11
jmp 0x162313
subl %r10d, %r9d
movl %r9d, 0xc(%rdx)
movq 0x8(%rsp), %r9
movq 0x20(%rsp), %r10
jmp 0x1621f3
movq %r15, %rcx
incq %r12
movq %rcx, %r15
cmpq %r10, %r12
jne 0x1620c7
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq 0x38(%rsp), %r15
movq %r15, %rdx
movq %rsi, %rbx
movq %r9, %r14
callq 0xc49f6
subq $0x8, %rsp
xorl %eax, %eax
movsd 0xcf258(%rip), %xmm0    # 0x2315b0
xorl %edi, %edi
xorl %esi, %esi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r15, %r8
xorl %r9d, %r9d
pushq %rax
pushq %rax
pushq 0x30(%rsp)
pushq %r14
pushq $0x1
movq 0xa0(%rsp), %r14
pushq %r14
pushq %rax
callq 0x1e07b
addq $0x40, %rsp
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x15c5b0
movq %r14, %rdi
callq 0x2220
movq 0x10(%rbp), %rdi
callq 0x2220
movq 0x28(%rbp), %rdi
callq 0x2220
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq