// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl -0x18(%rbp), %eax
imull -0x18(%rbp), %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x142a0
cmpl $0x0, -0x14(%rbp)
jne 0x26778
movl $0x0, -0x4(%rbp)
jmp 0x26cf3
cmpl $0xff, -0x14(%rbp)
jle 0x26795
movslq -0x14(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x26d00
movl $0xff, -0x14(%rbp)
movslq -0x14(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x26d90
leaq -0x200(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x142a0
leaq -0x168(%rbp), %rdi
xorl %esi, %esi
movl $0x140, %edx            # imm = 0x140
callq 0x142a0
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x170(%rbp)
cmpl $0x0, -0x170(%rbp)
jl 0x2693d
movq -0x20(%rbp), %rax
movslq -0x170(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x202(%rbp)
movzwl -0x202(%rbp), %eax
sarl $0x5, %eax
cmpl $0x4, %eax
jge 0x26813
movl $0x4, %eax
movl %eax, -0x224(%rbp)
jmp 0x26823
movzwl -0x202(%rbp), %eax
sarl $0x5, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
movl %eax, -0x208(%rbp)
cmpl $0x3f, -0x208(%rbp)
jle 0x26842
movl $0x3f, -0x208(%rbp)
movzwl -0x202(%rbp), %eax
andl $0x1f, %eax
movl %eax, -0x20c(%rbp)
movzwl -0x202(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x210(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x20c(%rbp), %rcx
movslq -0x200(%rbp,%rcx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x210(%rbp), %rax
addsd -0x168(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x168(%rbp,%rax,8)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x20c(%rbp), %rcx
movslq -0x200(%rbp,%rcx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x208(%rbp), %ecx
shll $0x10, %ecx
movl -0x14(%rbp), %edx
shll $0x8, %edx
orl %edx, %ecx
movslq -0x210(%rbp), %rdx
orl -0x68(%rbp,%rdx,4), %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm2
movslq -0x210(%rbp), %rax
movsd -0xe8(%rbp,%rax,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp,%rax,8)
movslq -0x210(%rbp), %rax
movl -0x68(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x68(%rbp,%rax,4)
movslq -0x20c(%rbp), %rax
movl -0x200(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x200(%rbp,%rax,4)
movl -0x170(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x170(%rbp)
jmp 0x267d4
movl $0x0, -0x170(%rbp)
movl -0x170(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x26cec
movl $0x0, -0x174(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x218(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
movl $0x0, -0x21c(%rbp)
movl -0x174(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x26a22
movl -0x174(%rbp), %eax
cmpl -0x170(%rbp), %eax
jne 0x2699f
jmp 0x26a0e
movslq -0x174(%rbp), %rax
movsd -0xe8(%rbp,%rax,8), %xmm1
movss -0x214(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
movslq -0x174(%rbp), %rax
movl -0x68(%rbp,%rax,4), %eax
addl -0x21c(%rbp), %eax
movl %eax, -0x21c(%rbp)
movslq -0x174(%rbp), %rax
movsd -0x168(%rbp,%rax,8), %xmm1
movss -0x218(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x218(%rbp)
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x26980
cmpl $0x0, -0x21c(%rbp)
je 0x26a4f
movss -0x214(%rbp), %xmm0
movq -0x28(%rbp), %rax
movl -0x170(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x170(%rbp), %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x170(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
movl -0x174(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x26c66
movslq -0x170(%rbp), %rax
movl -0x68(%rbp,%rax,4), %eax
movslq -0x174(%rbp), %rcx
addl -0x68(%rbp,%rcx,4), %eax
movl %eax, -0x220(%rbp)
movl $0x0, -0x16c(%rbp)
movl $0x0, -0x21c(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x218(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
movl -0x16c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x26b63
movl -0x16c(%rbp), %eax
cmpl -0x170(%rbp), %eax
je 0x26ade
movl -0x16c(%rbp), %eax
cmpl -0x174(%rbp), %eax
jne 0x26ae0
jmp 0x26b4f
movslq -0x16c(%rbp), %rax
movsd -0xe8(%rbp,%rax,8), %xmm1
movss -0x214(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x214(%rbp)
movslq -0x16c(%rbp), %rax
movl -0x68(%rbp,%rax,4), %eax
addl -0x21c(%rbp), %eax
movl %eax, -0x21c(%rbp)
movslq -0x16c(%rbp), %rax
movsd -0x168(%rbp,%rax,8), %xmm1
movss -0x218(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x218(%rbp)
movl -0x16c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x16c(%rbp)
jmp 0x26ab3
cmpl $0x0, -0x21c(%rbp)
je 0x26be9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x220(%rbp), %ecx
shll $0x8, %ecx
movslq -0x174(%rbp), %rdx
orl -0x68(%rbp,%rdx,4), %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movss -0x214(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x10a5ad(%rip), %xmm2   # 0x131150
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x28(%rbp), %rax
movl -0x174(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x170(%rbp), %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movl -0x170(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x174(%rbp), %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x26c50
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x220(%rbp), %ecx
shll $0x8, %ecx
movslq -0x174(%rbp), %rdx
orl -0x68(%rbp,%rdx,4), %ecx
movl %ecx, %ecx
movsd 0x10a541(%rip), %xmm0   # 0x131150
mulsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x28(%rbp), %rax
movl -0x174(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x170(%rbp), %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movl -0x170(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x174(%rbp), %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x26c52
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x26a5e
movl $0x0, -0x174(%rbp)
movl -0x174(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x26cd6
movq -0x28(%rbp), %rax
movl -0x170(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x174(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x26cc3
movq -0x28(%rbp), %rax
movl -0x170(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x174(%rbp), %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
jmp 0x26cc5
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x26c70
jmp 0x26cd8
movl -0x170(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x170(%rbp)
jmp 0x26947
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
nop