// File: /workspace/github_c_makefile_star2_O2/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 $0x218, %rsp            # imm = 0x218
movq %r8, %r12
movq %rcx, %r15
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, 0x38(%rsp)
imull %edx, %edx
shlq $0x2, %rdx
movq %r8, %rdi
xorl %esi, %esi
callq 0x14250
testl %ebp, %ebp
je 0x1fd64
cmpl $0x100, %ebp            # imm = 0x100
movq %r12, 0x30(%rsp)
jl 0x1f7b3
movl %ebp, %ebx
cmpl $0x2, %ebx
jl 0x1f7e3
callq 0x15800
cvtsi2sd %ebx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cltq
movzwl (%r15,%rax,2), %ecx
movzwl -0x2(%r15,%rbx,2), %edx
movw %dx, (%r15,%rax,2)
movw %cx, -0x2(%r15,%rbx,2)
decq %rbx
jmp 0x1f780
testl %ebp, %ebp
je 0x1faec
cmpl $0x2, %ebp
jne 0x1f7f5
pushq $0x2
popq %rbp
movzwl (%r15), %eax
movzwl 0x2(%r15), %ecx
cmpw %ax, %cx
jae 0x1faec
movw %cx, (%r15)
movw %ax, 0x2(%r15)
jmp 0x1faec
movq %r14, 0x28(%rsp)
movl $0xff, %ebp
movl $0xff, %r14d
jmp 0x1f7fd
movq %r14, 0x28(%rsp)
movslq %ebp, %r14
movl %ebp, 0x24(%rsp)
pushq $0x2
popq %rcx
movl $0xf0, %edi
pushq $0x2
popq %rbx
pushq $0x1
popq %rax
movq %rax, %rdx
shlq %cl, %rdx
incq %rcx
addq $0xc0, %rdi
addl $0x2, %ebx
cmpq %r14, %rdx
jb 0x1f80f
callq 0x14840
movq %rax, %rcx
leaq (%r15,%r14,2), %rax
movq %rax, 0x40(%rsp)
leaq (%r15,%r14,2), %rax
addq $-0x2, %rax
movsd 0x82888(%rip), %xmm2    # 0xa20d0
movapd 0x82890(%rip), %xmm3    # 0xa20e0
movsd 0x82868(%rip), %xmm4    # 0xa20c0
movsd 0x82868(%rip), %xmm5    # 0xa20c8
pushq $0xb
popq %r13
pushq $0x1
popq %rbp
pushq $-0x1
popq %r14
movq %rcx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %r15, 0x48(%rsp)
movq %r15, %r12
leaq 0x2(%r12), %rcx
movq %rcx, 0x18(%rsp)
movq %r12, %r15
negq %r15
decl %ebx
cmpq %rax, %r12
jae 0x1fa6b
movq %rax, %rcx
subq %r12, %rcx
testl %ebx, %ebx
jne 0x1f988
sarq %rcx
leaq (%r12,%rcx,2), %rsi
addq $0x2, %rsi
incq %rcx
cmpq $0x3, %rcx
jb 0x1f8fe
movq %rcx, %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm4, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subsd %xmm5, %xmm1
cvttsd2si %xmm1, %rcx
andq %rdx, %rcx
orq %rax, %rcx
leaq -0x9(%rcx), %rax
cmpq $0x2, %rax
cmovbq %r13, %rcx
movq %rsi, %rax
subq %rcx, %rax
subq %rcx, %rax
xorl %edi, %edi
movq %r12, %rdx
cmpq %rax, %rdx
jae 0x1f931
movzwl (%rdx,%rcx,2), %r8d
movzwl (%rdx), %r9d
cmpw %r9w, %r8w
jae 0x1f92b
movw %r8w, (%rdx)
movw %r9w, (%rdx,%rcx,2)
movl %ebp, %edi
addq $0x2, %rdx
jmp 0x1f90c
testl %edi, %edi
setne %al
cmpq $0x3, %rcx
setae %dl
orb %al, %dl
jne 0x1f8b4
movl %r14d, %ebx
movq %r12, %rax
cmpq $0x1, %rcx
je 0x1f88f
movq %r12, %rdi
callq 0x1fd78
movsd 0x82763(%rip), %xmm5    # 0xa20c8
movsd 0x82753(%rip), %xmm4    # 0xa20c0
movapd 0x8276b(%rip), %xmm3    # 0xa20e0
movsd 0x82753(%rip), %xmm2    # 0xa20d0
movl %r14d, %ebx
movq %r12, %rax
jmp 0x1f88f
sarq $0x2, %rcx
movzwl 0x2(%r12,%rcx,2), %edx
movzwl (%r12), %edi
movzwl (%rax), %esi
cmpw %di, %dx
jae 0x1f9b4
cmpw %si, %dx
cmovbel %esi, %edx
movq 0x18(%rsp), %rdi
leaq (%rdi,%rcx,2), %rcx
cmovbq %rax, %rcx
jmp 0x1f9c3
cmpw %di, %si
cmoval %esi, %edi
movq %rax, %rcx
cmovbq %r12, %rcx
movl %edi, %edx
cmpq %rax, %rcx
je 0x1f9ce
movw %si, (%rcx)
movw %dx, (%rax)
movq %r12, %rcx
movq %rax, %r8
movq %rax, %rsi
subq %rcx, %rsi
leaq (%r15,%rcx), %rdi
movzwl 0x2(%rcx), %r9d
addq $0x2, %rcx
addq $-0x2, %rsi
addq $0x2, %rdi
cmpw %dx, %r9w
jb 0x1f9de
addq $-0x2, %r8
movq %r8, %r10
movq %r10, %r8
cmpq %r10, %rcx
ja 0x1fa0e
leaq -0x2(%r8), %r10
cmpw (%r8), %dx
jb 0x1f9fc
cmpq %rcx, %r8
jbe 0x1fa20
movzwl (%r8), %esi
movw %si, (%rcx)
movw %r9w, (%r8)
jmp 0x1f9d4
movzwl (%rax), %edx
movw %dx, (%rcx)
movw %r9w, (%rax)
sarq %rdi
sarq %rsi
leaq -0x2(%rcx), %rdx
cmpq %rsi, %rdi
jg 0x1fa90
cmpq $0x11, %rsi
jl 0x1fa5b
addq $0x2, %rcx
movq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
movq %rax, 0x8(%rsi)
movl %ebx, 0x10(%rsi)
addq $0x18, %rsi
movq %rsi, 0x8(%rsp)
cmpq $0x11, %rdi
cmovlq %r12, %rdx
movq %rdx, %rax
jmp 0x1f88d
movq 0x8(%rsp), %rcx
cmpq 0x10(%rsp), %rcx
je 0x1fac2
movq -0x18(%rcx), %r12
movq -0x10(%rcx), %rax
movl -0x8(%rcx), %ebx
addq $-0x18, %rcx
movq %rcx, 0x8(%rsp)
jmp 0x1f87d
cmpq $0x11, %rdi
jl 0x1faae
movq 0x8(%rsp), %rdi
movq %r12, (%rdi)
movq %rdx, 0x8(%rdi)
movl %ebx, 0x10(%rdi)
addq $0x18, %rdi
movq %rdi, 0x8(%rsp)
addq $0x2, %rcx
cmpq $0x11, %rsi
cmovlq %rax, %rcx
movq %rcx, %r12
jmp 0x1f87d
movq 0x10(%rsp), %rdi
callq 0x14e30
movq 0x48(%rsp), %r15
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x1fd78
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %r14
movl 0x24(%rsp), %ebp
pushq $0x1
popq %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
leaq 0xd8(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x14250
movl %ebp, %eax
shll $0x8, %ebp
pushq $0x4
popq %rcx
pushq $0x3f
popq %rdx
testl %eax, %eax
jle 0x1fbeb
movzwl -0x2(%r15,%rax,2), %esi
decq %rax
movl %esi, %r8d
shrl $0x5, %r8d
cmpl $0x5, %r8d
cmovbl %ecx, %r8d
cmpl $0x3f, %r8d
cmovael %edx, %r8d
movl %esi, %edi
andl $0x1f, %edi
andl $0xf, %esi
movq 0x38(%rsp), %r11
movq 0x8(%r11), %r9
movslq 0x50(%rsp,%rdi,4), %r10
movsd (%r9,%r10,8), %xmm0
movsd 0xd8(%rsp,%rsi,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0xd8(%rsp,%rsi,8)
movq 0x10(%r11), %r9
shll $0x10, %r8d
movl 0x1d8(%rsp,%rsi,4), %r11d
movl %ebp, %ebx
orl %r11d, %ebx
orl %r8d, %ebx
mulsd (%r9,%rbx,8), %xmm0
addsd 0x158(%rsp,%rsi,8), %xmm0
movsd %xmm0, 0x158(%rsp,%rsi,8)
incl %r11d
movl %r11d, 0x1d8(%rsp,%rsi,4)
leal 0x1(%r10), %esi
movl %esi, 0x50(%rsp,%rdi,4)
jmp 0x1fb49
leal 0x1(%r14), %eax
movl %eax, 0x10(%rsp)
xorl %ecx, %ecx
testl %r14d, %r14d
movl $0x0, %edx
cmovgl %r14d, %edx
movslq %r14d, %rsi
movl %r14d, %edi
leaq (,%rdx,4), %r8
leaq (,%rsi,4), %rax
movq %rax, 0x18(%rsp)
movsd 0x82494(%rip), %xmm0    # 0xa20b8
xorps %xmm1, %xmm1
movq %r12, %r10
xorl %r11d, %r11d
cmpq %rdx, %r11
je 0x1fd64
movl %r13d, 0x8(%rsp)
movslq %r13d, %r14
xorl %eax, %eax
xorps %xmm2, %xmm2
xorl %r9d, %r9d
cmpq %rax, %rdi
je 0x1fc6e
cmpq %rax, %r11
je 0x1fc69
cvtss2sd %xmm2, %xmm2
addsd 0x158(%rsp,%rax,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1d8(%rsp,%rax,4), %r9d
incq %rax
jmp 0x1fc46
testl %r9d, %r9d
je 0x1fc83
movl 0x10(%rsp), %eax
imull %r11d, %eax
cltq
movss %xmm2, (%r12,%rax,4)
movq %r11, %rax
imulq %rsi, %rax
leaq (%r12,%r11,4), %r15
leaq (%r12,%rax,4), %r12
cmpq %rsi, %r14
jge 0x1fd2e
movl %r14d, %r9d
shlq $0x2, %r9
movl 0x1d8(%rsp,%r11,4), %r13d
movl 0x1d8(%rsp,%r14,4), %eax
xorl %ebx, %ebx
xorps %xmm2, %xmm2
xorl %ebp, %ebp
cmpq %rbx, %r8
je 0x1fce6
cmpq %rbx, %rcx
je 0x1fce0
cmpq %rbx, %r9
je 0x1fce0
cvtss2sd %xmm2, %xmm2
addsd 0x158(%rsp,%rbx,2), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1d8(%rsp,%rbx), %ebp
addq $0x4, %rbx
jmp 0x1fcb9
addl %eax, %r13d
shll $0x8, %r13d
orl %eax, %r13d
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movsd (%rax,%r13,8), %xmm3
mulsd %xmm0, %xmm3
testl %ebp, %ebp
je 0x1fd0f
cvtss2sd %xmm2, %xmm2
addsd %xmm2, %xmm3
cvtsd2ss %xmm3, %xmm2
movq %r14, %rax
imulq %rsi, %rax
movss %xmm2, (%r15,%rax,4)
movss %xmm2, (%r12,%r14,4)
incq %r14
jmp 0x1fc92
xorl %eax, %eax
movq 0x30(%rsp), %r12
cmpq %rax, %rdx
je 0x1fd4b
ucomiss (%r10,%rax,4), %xmm1
jbe 0x1fd46
andl $0x0, (%r10,%rax,4)
incq %rax
jmp 0x1fd35
incq %r11
movl 0x8(%rsp), %r13d
incl %r13d
addq $0x4, %rcx
addq 0x18(%rsp), %r10
jmp 0x1fc2d
xorl %eax, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq