// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl %esi, %ebp
movq %rdi, 0x20(%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 0x21d18
cmpl $0x100, %ebp            # imm = 0x100
jl 0x21770
movl %ebp, %ebx
incl %ebp
callq 0x15780
xorps %xmm1, %xmm1
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
decl %ebp
cmpl $0x2, %ebp
jg 0x21735
movl $0xff, %ebp
movslq %ebp, %r13
testl %r13d, %r13d
je 0x21a89
cmpl $0x2, %ebp
jne 0x217a1
movzwl (%r15), %eax
movzwl 0x2(%r15), %ecx
cmpw %ax, %cx
jae 0x21a89
movw %cx, (%r15)
movw %ax, 0x2(%r15)
jmp 0x21a89
movl %ebp, 0x4(%rsp)
movl $0x2, %ecx
movl $0xf0, %edi
movl $0x2, %r14d
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addq $0xc0, %rdi
addl $0x2, %r14d
cmpq %r13, %rax
jb 0x217b5
callq 0x14810
movq %rax, %rdx
leaq (%r15,%r13,2), %rax
movq %rax, 0x28(%rsp)
leaq (%r15,%r13,2), %rbp
addq $-0x2, %rbp
movq 0xab8bf(%rip), %xmm0    # 0xcd0b0
movapd 0xab8c7(%rip), %xmm1    # 0xcd0c0
movsd 0xab88f(%rip), %xmm2    # 0xcd090
movsd 0xab88f(%rip), %xmm3    # 0xcd098
movq %rdx, 0x10(%rsp)
movq %r15, %rsi
leaq 0x2(%rsi), %rdi
movq %rsi, %r9
negq %r9
cmpq %rbp, %rsi
jae 0x219fb
decl %r14d
je 0x21912
movq %rbp, %rax
subq %rsi, %rax
sarq $0x2, %rax
leaq (%rdi,%rax,2), %r8
movzwl 0x2(%rsi,%rax,2), %eax
movzwl (%rsi), %ecx
movzwl (%rbp), %r10d
cmpw %cx, %r10w
movq %rbp, %r11
cmovbq %rsi, %r11
cmpw %r10w, %ax
cmovbq %rbp, %r8
cmpw %cx, %ax
cmovaeq %r11, %r8
movzwl (%r8), %ecx
cmpq %rbp, %r8
je 0x21877
movzwl (%rbp), %eax
movw %ax, (%r8)
movw %cx, (%rbp)
movq %rsi, %r10
movq %rbp, %rax
movq %rbp, %r11
subq %r10, %r11
leaq (%r9,%r10), %rbx
movzwl 0x2(%r10), %r12d
addq $0x2, %r10
addq $-0x2, %r11
addq $0x2, %rbx
cmpw %cx, %r12w
jb 0x21887
addq $-0x2, %rax
movq %rax, %r8
movq %r8, %rax
cmpq %r8, %r10
ja 0x218b6
leaq -0x2(%rax), %r8
cmpw (%rax), %cx
jb 0x218a5
cmpq %r10, %rax
jbe 0x218c9
movzwl (%rax), %r8d
movw %r8w, (%r10)
movw %r12w, (%rax)
jmp 0x2187d
movzwl (%rbp), %eax
movw %ax, (%r10)
movw %r12w, (%rbp)
sarq %rbx
sarq %r11
leaq -0x2(%r10), %rcx
cmpq %r11, %rbx
jg 0x21a17
cmpq $0x11, %r11
jl 0x21902
addq $0x2, %r10
movq %r10, (%rdx)
movq %rbp, 0x8(%rdx)
movl %r14d, 0x10(%rdx)
addq $0x18, %rdx
cmpq $0x11, %rbx
cmovlq %rsi, %rcx
movq %rcx, %rbp
jmp 0x2181b
subq %rsi, %rbp
sarq %rbp
leaq 0x1(%rbp), %r10
leaq (%rsi,%rbp,2), %r9
addq $0x2, %r9
cmpq $0x3, %r10
jb 0x21973
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, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd %xmm3, %xmm5
cvttsd2si %xmm5, %r10
andq %rcx, %r10
orq %rax, %r10
leaq -0x9(%r10), %rax
cmpq $0x2, %rax
movl $0xb, %eax
cmovbq %rax, %r10
movq %r9, %r8
subq %r10, %r8
subq %r10, %r8
xorl %ecx, %ecx
cmpq %rsi, %r8
jbe 0x219b1
movq %rsi, %r11
movzwl (%r11,%r10,2), %ebx
movzwl (%r11), %r14d
cmpw %r14w, %bx
jae 0x219a3
movw %bx, (%r11)
movw %r14w, (%r11,%r10,2)
movl $0x1, %ecx
addq $0x2, %r11
cmpq %r8, %r11
jb 0x21986
testl %ecx, %ecx
setne %cl
cmpq $0x3, %r10
setae %al
orb %cl, %al
jne 0x21924
testq %rbp, %rbp
jle 0x219fb
cmpq $0x1, %r10
je 0x219fb
cmpq %rsi, %rdi
jbe 0x219f2
movq %rdi, %rax
movzwl -0x2(%rax), %r8d
movzwl (%rax), %ecx
cmpw %r8w, %cx
jae 0x219f2
movw %r8w, (%rax)
movw %cx, -0x2(%rax)
addq $-0x2, %rax
cmpq %rsi, %rax
ja 0x219d3
addq $0x2, %rdi
cmpq %r9, %rdi
jb 0x219cb
cmpq 0x10(%rsp), %rdx
je 0x21a40
movq -0x18(%rdx), %rsi
movq -0x10(%rdx), %rbp
movl -0x8(%rdx), %r14d
addq $-0x18, %rdx
jmp 0x21811
cmpq $0x11, %rbx
jl 0x21a2c
movq %rsi, (%rdx)
movq %rcx, 0x8(%rdx)
movl %r14d, 0x10(%rdx)
addq $0x18, %rdx
addq $0x2, %r10
cmpq $0x11, %r11
cmovlq %rbp, %r10
movq %r10, %rsi
jmp 0x21811
movq 0x10(%rsp), %rdi
callq 0x14dd0
leaq 0x2(%r15), %rax
movq 0x28(%rsp), %rdi
cmpq %rdi, %rax
movl 0x4(%rsp), %ebp
jae 0x21a89
cmpq %r15, %rax
jbe 0x21a80
movq %rax, %rcx
movzwl -0x2(%rcx), %esi
movzwl (%rcx), %edx
cmpw %si, %dx
jae 0x21a80
movw %si, (%rcx)
movw %dx, -0x2(%rcx)
addq $-0x2, %rcx
cmpq %r15, %rcx
ja 0x21a64
addq $0x2, %rax
cmpq %rdi, %rax
jb 0x21a5c
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
leaq 0xb8(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x14240
testl %ebp, %ebp
jle 0x21b93
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x10(%rcx), %rcx
shll $0x8, %ebp
incq %r13
movl $0x4, %edx
movl $0x3f, %esi
movzwl -0x4(%r15,%r13,2), %edi
movl %edi, %r9d
shrl $0x5, %r9d
cmpl $0x5, %r9d
cmovbl %edx, %r9d
cmpl $0x3f, %r9d
cmovael %esi, %r9d
movl %edi, %r8d
andl $0x1f, %r8d
andl $0xf, %edi
movslq 0x30(%rsp,%r8,4), %r10
movsd (%rax,%r10,8), %xmm0
movsd 0xb8(%rsp,%rdi,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0xb8(%rsp,%rdi,8)
shll $0x10, %r9d
movl 0x1b8(%rsp,%rdi,4), %r11d
movl %ebp, %ebx
orl %r11d, %ebx
orl %r9d, %ebx
mulsd (%rcx,%rbx,8), %xmm0
addsd 0x138(%rsp,%rdi,8), %xmm0
movsd %xmm0, 0x138(%rsp,%rdi,8)
incl %r11d
movl %r11d, 0x1b8(%rsp,%rdi,4)
leal 0x1(%r10), %edi
movl %edi, 0x30(%rsp,%r8,4)
decq %r13
cmpq $0x1, %r13
jg 0x21aff
cmpl $0x0, 0x8(%rsp)
jle 0x21d18
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x4(%rsp)
movl %eax, %ecx
movl $0x1, %edx
xorl %esi, %esi
movsd 0xab4e5(%rip), %xmm0    # 0xcd0a0
xorpd %xmm1, %xmm1
xorl %r8d, %r8d
movl %esi, %eax
movq 0x18(%rsp), %rdi
leaq (%rdi,%rax,4), %rdi
xorl %r9d, %r9d
xorl %eax, %eax
xorpd %xmm2, %xmm2
cmpq %r9, %r8
je 0x21bf5
cvtss2sd %xmm2, %xmm2
addsd 0x138(%rsp,%r9,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1b8(%rsp,%r9,4), %eax
incq %r9
cmpq %r9, %rcx
jne 0x21bd6
testl %eax, %eax
je 0x21c16
movl 0x4(%rsp), %eax
imull %r8d, %eax
cltq
movq 0x18(%rsp), %r9
movss %xmm2, (%r9,%rax,4)
leaq 0x1(%r8), %rax
movq %rax, 0x10(%rsp)
cmpq %rcx, %rax
jae 0x21cec
movl 0x1b8(%rsp,%r8,4), %r10d
movl %r8d, %eax
imull 0x8(%rsp), %eax
movq 0x18(%rsp), %r9
leaq (%r9,%r8,4), %r11
leaq (%r9,%rax,4), %r14
movq %rdx, %r12
movl 0x1b8(%rsp,%r12,4), %r13d
leal (%r10,%r13), %ebx
xorps %xmm2, %xmm2
xorl %eax, %eax
xorl %ebp, %ebp
cmpq %rax, %r8
sete %r15b
cmpq %rax, %r12
sete %r9b
orb %r15b, %r9b
jne 0x21c86
cvtss2sd %xmm2, %xmm2
addsd 0x138(%rsp,%rax,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1b8(%rsp,%rax,4), %ebp
incq %rax
cmpq %rax, %rcx
jne 0x21c5b
shll $0x8, %ebx
orl %r13d, %ebx
testl %ebp, %ebp
je 0x21cb7
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
xorps %xmm3, %xmm3
cvtss2sd %xmm2, %xmm3
movsd (%rax,%rbx,8), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
jmp 0x21cc9
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movsd (%rax,%rbx,8), %xmm2
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movq %r12, %rax
imulq %rcx, %rax
movss %xmm2, (%r11,%rax,4)
movss %xmm2, (%r14,%r12,4)
incq %r12
cmpq %rcx, %r12
jne 0x21c48
xorl %eax, %eax
movq 0x10(%rsp), %r8
ucomiss (%rdi,%rax,4), %xmm1
jbe 0x21d00
movl $0x0, (%rdi,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x21cf3
incq %rdx
addl 0x8(%rsp), %esi
cmpq %rcx, %r8
jne 0x21bc2
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq