// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/errmod.c
// Function: errmod_cal
int errmod_cal(const errmod_t *em, int n, int m, uint16_t *bases, float *q)
{
	call_aux_t aux;
	int i, j, k, w[32];

	if (m > m) return -1;
	memset(q, 0, m * m * sizeof(float));
	if (n == 0) return 0;
	// calculate aux.esum and aux.fsum
	if (n > 255) { // then sample 255 bases
		ks_shuffle(uint16_t, n, bases);
		n = 255;
	}
	ks_introsort(uint16_t, n, bases);
	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];
		int q = b>>5 < 4? 4 : b>>5;
		if (q > 63) q = 63;
		k = b&0x1f;
		aux.fsum[k&0xf] += em->coef->fk[w[k]];
		aux.bsum[k&0xf] += em->coef->fk[w[k]] * em->coef->beta[q<<16|n<<8|aux.c[k&0xf]];
		++aux.c[k&0xf];
		++w[k];
	}
	// generate likelihood
	for (j = 0; j != m; ++j) {
		float tmp1, tmp3;
		int tmp2, bar_e;
		// 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) {
			bar_e = (int)(tmp1 / tmp3 + 0.499);
			if (bar_e > 63) bar_e = 63;
			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) {
				bar_e = (int)(tmp1 / tmp3 + 0.499);
				if (bar_e > 63) bar_e = 63;
				q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]] + tmp1;
			} else q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]]; // all the bases are either j or k
		}
		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 %edx, %r14d
movl %esi, %ebp
movq %rdi, 0x8(%rsp)
imull %edx, %edx
shlq $0x2, %rdx
movq %r8, (%rsp)
movq %r8, %rdi
xorl %esi, %esi
callq 0x3080
testl %ebp, %ebp
je 0x166d4
cmpl $0x100, %ebp            # imm = 0x100
jl 0x16411
movl %ebp, %edi
movq %r12, %rsi
callq 0x16000
movl $0xff, %ebp
movslq %ebp, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x15cde
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 0x3080
testl %r13d, %r13d
jle 0x16535
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rcx
movq (%rcx), %rax
movq 0x8(%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
movslq 0x30(%rsp,%r9,4), %r11
movsd (%rax,%r11,8), %xmm0
andl $0xf, %r8d
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, %r13d
orl %ebx, %r13d
orl %r10d, %r13d
mulsd (%rcx,%r13,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 0x1649b
testl %r14d, %r14d
je 0x166d4
leal 0x1(%r14), %eax
movl %eax, 0x14(%rsp)
movl %r14d, %ecx
movslq %r14d, %rdx
leaq (,%rdx,4), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %eax
movq %rax, 0x18(%rsp)
xorl %r9d, %r9d
movsd 0x2b512(%rip), %xmm0    # 0x41a80
xorpd %xmm1, %xmm1
movq (%rsp), %r8
xorl %esi, %esi
xorl %eax, %eax
xorps %xmm2, %xmm2
cmpq %rsi, %r9
je 0x1659a
cvtss2sd %xmm2, %xmm2
addsd 0x138(%rsp,%rsi,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1b8(%rsp,%rsi,4), %eax
incq %rsi
cmpq %rsi, %rcx
jne 0x1657d
testl %eax, %eax
je 0x165b9
movl 0x14(%rsp), %eax
imull %r9d, %eax
cltq
movq (%rsp), %rsi
movss %xmm2, (%rsi,%rax,4)
leaq 0x1(%r9), %rax
movq %rax, 0x28(%rsp)
cmpq %rdx, %rax
jge 0x166a3
movl 0x1b8(%rsp,%r9,4), %r11d
movq %r9, %rax
imulq %rdx, %rax
movq (%rsp), %rsi
leaq (%rsi,%rax,4), %r12
leaq (%rsi,%r9,4), %r13
movq 0x18(%rsp), %rbp
movl 0x1b8(%rsp,%rbp,4), %esi
leal (%rsi,%r11), %eax
testl %r14d, %r14d
jle 0x16664
xorps %xmm2, %xmm2
xorl %ebx, %ebx
xorl %r10d, %r10d
cmpq %rbx, %r9
sete %dil
cmpq %rbx, %rbp
sete %r15b
orb %dil, %r15b
jne 0x1662f
cvtss2sd %xmm2, %xmm2
addsd 0x138(%rsp,%rbx,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1b8(%rsp,%rbx,4), %r10d
incq %rbx
cmpq %rbx, %rcx
jne 0x16603
testl %r10d, %r10d
je 0x16664
xorps %xmm3, %xmm3
cvtss2sd %xmm2, %xmm3
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rdi
movq 0x10(%rdi), %rdi
shll $0x8, %eax
orl %esi, %eax
movsd (%rdi,%rax,8), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
jmp 0x1667f
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rdi
movq 0x10(%rdi), %rdi
shll $0x8, %eax
orl %esi, %eax
movsd (%rdi,%rax,8), %xmm2
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movq %rbp, %rax
imulq %rdx, %rax
movss %xmm2, (%r13,%rax,4)
movss %xmm2, (%r12,%rbp,4)
incq %rbp
cmpq %rcx, %rbp
jne 0x165eb
xorl %eax, %eax
movq 0x28(%rsp), %r9
ucomiss (%r8,%rax,4), %xmm1
jbe 0x166b9
movl $0x0, (%r8,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x166aa
incq 0x18(%rsp)
addq 0x20(%rsp), %r8
cmpq %rcx, %r9
jne 0x16576
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq