// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/errmod.c
// Function: errmod_cal
int errmod_cal(const errmod_t *em, int n, int m, uint16_t *bases, float *q)
{
    // Aux
    // aux.c is total count of each base observed (ignoring strand)
    call_aux_t aux;
    // Loop variables
    int i, j, k;
    // The total count of each base observed per strand
    int w[32];

    memset(q, 0, m * m * sizeof(float)); // initialise q to 0
    if (n == 0) return 0;
    // This section randomly downsamples to 255 depth so as not to go beyond our precalculated matrix
    if (n > 255) { // if we exceed 255 bases observed then shuffle them to sample and only keep the first 255
        ks_shuffle(uint16_t, n, bases);
        n = 255;
    }
    ks_introsort(uint16_t, n, bases);
    /* zero out w and aux */
    memset(w, 0, 32 * sizeof(int));
    memset(&aux, 0, sizeof(call_aux_t));

    for (j = n - 1; j >= 0; --j) { // calculate esum and fsum
        uint16_t b = bases[j];
        /* extract quality and cap at 63 */
        int qual = b>>5 < 4? 4 : b>>5;
        if (qual > 63) qual = 63;
        /* extract base ORed with strand */
        int basestrand = b&0x1f;
        /* extract base */
        int base = b&0xf;
        aux.fsum[base] += em->fk[w[basestrand]];
        aux.bsum[base] += em->fk[w[basestrand]] * em->beta[qual<<16|n<<8|aux.c[base]];
        ++aux.c[base];
        ++w[basestrand];
    }

    // generate likelihood
    for (j = 0; j < m; ++j) {
        float tmp1, tmp3;
        int tmp2;
        // homozygous
        for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k < m; ++k) {
            if (k == j) continue;
            tmp1 += aux.bsum[k]; tmp2 += aux.c[k]; tmp3 += aux.fsum[k];
        }
        if (tmp2) {
            q[j*m+j] = tmp1;
        }
        // heterozygous
        for (k = j + 1; k < m; ++k) {
            int cjk = aux.c[j] + aux.c[k];
            for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i < m; ++i) {
                if (i == j || i == k) continue;
                tmp1 += aux.bsum[i]; tmp2 += aux.c[i]; tmp3 += aux.fsum[i];
            }
            if (tmp2) {
                q[j*m+k] = q[k*m+j] = -4.343 * em->lhet[cjk<<8|aux.c[k]] + tmp1;
            } else q[j*m+k] = q[k*m+j] = -4.343 * em->lhet[cjk<<8|aux.c[k]]; // all the bases are either j or k
        }
        /* clamp to greater than 0 */
        for (k = 0; k < m; ++k) if (q[j*m+k] < 0.0) q[j*m+k] = 0.0;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rcx, %r12
movl %esi, %ebp
movq %rdi, 0x28(%rsp)
movq %rdx, 0x8(%rsp)
imull %edx, %edx
shlq $0x2, %rdx
movq %r8, 0x18(%rsp)
movq %r8, %rdi
xorl %esi, %esi
callq 0x14240
testl %ebp, %ebp
je 0x21cf0
cmpl $0x100, %ebp            # imm = 0x100
jl 0x216e3
movl %ebp, %ebx
incl %ebp
callq 0x15780
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cltq
movzwl (%r12,%rax,2), %ecx
movzwl -0x2(%r12,%rbx,2), %edx
movw %dx, (%r12,%rax,2)
movw %cx, -0x2(%r12,%rbx,2)
decq %rbx
decl %ebp
cmpl $0x2, %ebp
jg 0x2169e
movl $0xff, %ebp
movl $0xff, %ebx
jmp 0x21782
cmpl $0x2, %ebp
je 0x21709
testl %ebp, %ebp
jne 0x2177f
leaq 0xb8(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x14240
jmp 0x21b7a
movzwl (%r12), %eax
movzwl 0x2(%r12), %ecx
cmpw %ax, %cx
jae 0x21724
movw %cx, (%r12)
movw %ax, 0x2(%r12)
pxor %xmm0, %xmm0
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
leaq 0xb8(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x14240
movl $0x2, %ebp
jmp 0x21ac1
movslq %ebp, %rbx
movl $0x2, %ecx
movl $0xf0, %edi
movl $0x2, %r15d
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addq $0xc0, %rdi
addl $0x2, %r15d
cmpq %rbx, %rax
jb 0x21792
callq 0x14810
movq %rax, %rdx
leaq (%r12,%rbx,2), %rax
movq %rax, (%rsp)
leaq (%r12,%rbx,2), %rax
addq $-0x2, %rax
movq 0xa9953(%rip), %xmm0    # 0xcb120
movapd 0xa995b(%rip), %xmm1    # 0xcb130
movsd 0xa991b(%rip), %xmm2    # 0xcb0f8
movsd 0xa991b(%rip), %xmm3    # 0xcb100
movq %rdx, 0x10(%rsp)
movq %r12, %rsi
leaq 0x2(%rsi), %rdi
movq %rsi, %r9
negq %r9
cmpq %rax, %rsi
jae 0x219de
decl %r15d
je 0x218f0
movq %rax, %r8
subq %rsi, %r8
sarq $0x2, %r8
movzwl 0x2(%rsi,%r8,2), %r11d
movzwl (%rsi), %r10d
movzwl (%rax), %ecx
cmpw %r10w, %r11w
jae 0x21838
cmpw %cx, %r11w
cmovbel %ecx, %r11d
leaq (%rdi,%r8,2), %r8
cmovbq %rax, %r8
jmp 0x2184a
cmpw %r10w, %cx
cmoval %ecx, %r10d
movq %rax, %r8
cmovbq %rsi, %r8
movl %r10d, %r11d
cmpq %rax, %r8
je 0x21857
movw %cx, (%r8)
movw %r11w, (%rax)
movq %rsi, %r10
movq %rax, %rbx
movq %rax, %r8
subq %r10, %r8
leaq (%r9,%r10), %r14
movzwl 0x2(%r10), %ecx
addq $0x2, %r10
addq $-0x2, %r8
addq $0x2, %r14
cmpw %r11w, %cx
jb 0x21867
addq $-0x2, %rbx
movq %rbx, %r13
movq %r13, %rbx
cmpq %r13, %r10
ja 0x21897
leaq -0x2(%rbx), %r13
cmpw (%rbx), %r11w
jb 0x21885
cmpq %r10, %rbx
jbe 0x218a9
movzwl (%rbx), %r8d
movw %r8w, (%r10)
movw %cx, (%rbx)
jmp 0x2185d
movzwl (%rax), %r11d
movw %r11w, (%r10)
movw %cx, (%rax)
sarq %r14
sarq %r8
leaq -0x2(%r10), %rcx
cmpq %r8, %r14
jg 0x219fa
cmpq $0x11, %r8
jl 0x218e0
addq $0x2, %r10
movq %r10, (%rdx)
movq %rax, 0x8(%rdx)
movl %r15d, 0x10(%rdx)
addq $0x18, %rdx
cmpq $0x11, %r14
cmovlq %rsi, %rcx
movq %rcx, %rax
jmp 0x217f7
subq %rsi, %rax
sarq %rax
leaq 0x1(%rax), %r10
leaq (%rsi,%rax,2), %r9
addq $0x2, %r9
cmpq $0x3, %r10
jb 0x21951
movq %r10, %xmm4
punpckldq %xmm0, %xmm4    # xmm4 = xmm4[0],xmm0[0],xmm4[1],xmm0[1]
subpd %xmm1, %xmm4
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
divsd %xmm2, %xmm5
cvttsd2si %xmm5, %rcx
movq %rcx, %r8
sarq $0x3f, %r8
subsd %xmm3, %xmm5
cvttsd2si %xmm5, %r10
andq %r8, %r10
orq %rcx, %r10
leaq -0x9(%r10), %rcx
cmpq $0x2, %rcx
movl $0xb, %ecx
cmovbq %rcx, %r10
movq %r9, %r8
subq %r10, %r8
subq %r10, %r8
xorl %ecx, %ecx
cmpq %rsi, %r8
jbe 0x2198f
movq %rsi, %r11
movzwl (%r11,%r10,2), %r14d
movzwl (%r11), %r15d
cmpw %r15w, %r14w
jae 0x21981
movw %r14w, (%r11)
movw %r15w, (%r11,%r10,2)
movl $0x1, %ecx
addq $0x2, %r11
cmpq %r8, %r11
jb 0x21964
testl %ecx, %ecx
setne %cl
cmpq $0x3, %r10
setae %r8b
orb %cl, %r8b
jne 0x21902
testq %rax, %rax
jle 0x219de
cmpq $0x1, %r10
je 0x219de
cmpq %rsi, %rdi
jbe 0x219d5
movzwl (%rdi), %ecx
movq %rdi, %r8
movzwl -0x2(%r8), %eax
cmpw %ax, %cx
jae 0x219d5
movw %ax, (%r8)
movw %cx, -0x2(%r8)
leaq -0x2(%r8), %rax
movq %rax, %r8
cmpq %rsi, %rax
ja 0x219b6
addq $0x2, %rdi
cmpq %r9, %rdi
jb 0x219ab
cmpq 0x10(%rsp), %rdx
je 0x21a23
movq -0x18(%rdx), %rsi
movq -0x10(%rdx), %rax
movl -0x8(%rdx), %r15d
addq $-0x18, %rdx
jmp 0x217ed
cmpq $0x11, %r14
jl 0x21a0f
movq %rsi, (%rdx)
movq %rcx, 0x8(%rdx)
movl %r15d, 0x10(%rdx)
addq $0x18, %rdx
addq $0x2, %r10
cmpq $0x11, %r8
cmovlq %rax, %r10
movq %r10, %rsi
jmp 0x217ed
movq 0x10(%rsp), %rdi
callq 0x14dd0
leaq 0x2(%r12), %rax
movq (%rsp), %rdi
cmpq %rdi, %rax
jae 0x21a68
cmpq %r12, %rax
jbe 0x21a62
movzwl (%rax), %ecx
movq %rax, %rdx
movzwl -0x2(%rdx), %esi
cmpw %si, %cx
jae 0x21a62
movw %si, (%rdx)
movw %cx, -0x2(%rdx)
leaq -0x2(%rdx), %rsi
movq %rsi, %rdx
cmpq %r12, %rsi
ja 0x21a46
addq $0x2, %rax
jmp 0x21a36
pxor %xmm0, %xmm0
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
leaq 0xb8(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x14240
testl %ebp, %ebp
jle 0x21b7a
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x10(%rcx), %rcx
movl %ebp, %edx
shll $0x8, %ebp
incq %rdx
movl $0x4, %esi
movl $0x3f, %edi
movzwl -0x4(%r12,%rdx,2), %r8d
movl %r8d, %r10d
shrl $0x5, %r10d
cmpl $0x5, %r10d
cmovbl %esi, %r10d
cmpl $0x3f, %r10d
cmovael %edi, %r10d
movl %r8d, %r9d
andl $0x1f, %r9d
andl $0xf, %r8d
movslq 0x30(%rsp,%r9,4), %r11
movsd (%rax,%r11,8), %xmm0
movsd 0xb8(%rsp,%r8,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0xb8(%rsp,%r8,8)
shll $0x10, %r10d
movl 0x1b8(%rsp,%r8,4), %ebx
movl %ebp, %r14d
orl %ebx, %r14d
orl %r10d, %r14d
mulsd (%rcx,%r14,8), %xmm0
addsd 0x138(%rsp,%r8,8), %xmm0
movsd %xmm0, 0x138(%rsp,%r8,8)
incl %ebx
movl %ebx, 0x1b8(%rsp,%r8,4)
leal 0x1(%r11), %r8d
movl %r8d, 0x30(%rsp,%r9,4)
decq %rdx
cmpq $0x1, %rdx
ja 0x21ae0
cmpl $0x0, 0x8(%rsp)
jle 0x21cf0
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x24(%rsp)
movl %eax, %ecx
movl $0x1, %eax
movq %rax, (%rsp)
xorl %esi, %esi
movsd 0xa9562(%rip), %xmm0    # 0xcb108
xorpd %xmm1, %xmm1
xorl %r8d, %r8d
movl %esi, %eax
movq 0x18(%rsp), %rdx
leaq (%rdx,%rax,4), %rdi
xorl %r9d, %r9d
xorl %eax, %eax
xorpd %xmm2, %xmm2
cmpq %r9, %r8
je 0x21be0
cvtss2sd %xmm2, %xmm2
addsd 0x138(%rsp,%r9,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1b8(%rsp,%r9,4), %eax
incq %r9
cmpq %r9, %rcx
jne 0x21bc1
testl %eax, %eax
je 0x21c00
movl 0x24(%rsp), %eax
imull %r8d, %eax
cltq
movq 0x18(%rsp), %rdx
movss %xmm2, (%rdx,%rax,4)
leaq 0x1(%r8), %rax
movq %rax, 0x10(%rsp)
cmpq %rcx, %rax
jae 0x21cc3
movl 0x1b8(%rsp,%r8,4), %r10d
movl %r8d, %eax
imull 0x8(%rsp), %eax
movq 0x28(%rsp), %rdx
movq 0x18(%rdx), %r11
movq 0x18(%rsp), %rdx
leaq (%rdx,%r8,4), %r15
leaq (%rdx,%rax,4), %r12
movq (%rsp), %r13
movl 0x1b8(%rsp,%r13,4), %r14d
leal (%r14,%r10), %ebx
xorps %xmm2, %xmm2
xorl %eax, %eax
xorl %ebp, %ebp
cmpq %rax, %r8
sete %r9b
cmpq %rax, %r13
sete %dl
orb %r9b, %dl
jne 0x21c79
cvtss2sd %xmm2, %xmm2
addsd 0x138(%rsp,%rax,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1b8(%rsp,%rax,4), %ebp
incq %rax
cmpq %rax, %rcx
jne 0x21c4f
shll $0x8, %ebx
orl %r14d, %ebx
movsd (%r11,%rbx,8), %xmm3
mulsd %xmm0, %xmm3
testl %ebp, %ebp
je 0x21c9d
cvtss2sd %xmm2, %xmm2
addsd %xmm2, %xmm3
xorps %xmm2, %xmm2
cvtsd2ss %xmm3, %xmm2
movq %r13, %rax
imulq %rcx, %rax
movss %xmm2, (%r15,%rax,4)
movss %xmm2, (%r12,%r13,4)
incq %r13
cmpq %rcx, %r13
jne 0x21c3c
xorl %eax, %eax
movq 0x10(%rsp), %r8
ucomiss (%rdi,%rax,4), %xmm1
jbe 0x21cd7
movl $0x0, (%rdi,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x21cca
incq (%rsp)
addl 0x8(%rsp), %esi
cmpq %rcx, %r8
jne 0x21bad
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq