// File: /workspace/github_c_makefile_star2_O1/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 $0x1e8, %rsp            # imm = 0x1E8
movq %rcx, %r12
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %r15
imull %edx, %edx
shlq $0x2, %rdx
movq %r8, (%rsp)
movq %r8, %rdi
xorl %esi, %esi
callq 0x3080
testl %ebp, %ebp
je 0x163f0
cmpl $0x100, %ebp            # imm = 0x100
jl 0x16144
movl %ebp, %edi
movq %r12, %rsi
callq 0x15d3e
movl $0xff, %ebp
movslq %ebp, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x15a24
xorpd %xmm0, %xmm0
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)
movapd %xmm0, 0x20(%rsp)
leaq 0xa8(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x3080
testl %r13d, %r13d
jle 0x1625f
movq 0x8(%r15), %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 0x20(%rsp,%r9,4), %r11
movsd (%rax,%r11,8), %xmm0
andl $0xf, %r8d
movsd 0xa8(%rsp,%r8,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0xa8(%rsp,%r8,8)
shll $0x10, %r10d
movl 0x1a8(%rsp,%r8,4), %r13d
movl %ebp, %ebx
orl %r13d, %ebx
orl %r10d, %ebx
mulsd (%rcx,%rbx,8), %xmm0
addsd 0x128(%rsp,%r8,8), %xmm0
movsd %xmm0, 0x128(%rsp,%r8,8)
incl %r13d
movl %r13d, 0x1a8(%rsp,%r8,4)
leal 0x1(%r11), %r8d
movl %r8d, 0x20(%rsp,%r9,4)
decq %rdx
cmpq $0x1, %rdx
jg 0x161c6
testl %r14d, %r14d
je 0x163f0
leal 0x1(%r14), %eax
movl %eax, 0xc(%rsp)
movl %r14d, %ecx
movslq %r14d, %rdx
leaq (,%rdx,4), %rax
movq %rax, 0x10(%rsp)
movl $0x1, %edi
xorl %r9d, %r9d
movsd 0x2a7a5(%rip), %xmm0    # 0x40a38
xorpd %xmm1, %xmm1
movq (%rsp), %r8
xorl %esi, %esi
xorl %eax, %eax
xorps %xmm2, %xmm2
cmpq %rsi, %r9
je 0x162bf
cvtss2sd %xmm2, %xmm2
addsd 0x128(%rsp,%rsi,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1a8(%rsp,%rsi,4), %eax
incq %rsi
cmpq %rsi, %rcx
jne 0x162a2
testl %eax, %eax
je 0x162de
movl 0xc(%rsp), %eax
imull %r9d, %eax
cltq
movq (%rsp), %rsi
movss %xmm2, (%rsi,%rax,4)
leaq 0x1(%r9), %rax
movq %rax, 0x18(%rsp)
cmpq %rdx, %rax
jge 0x163c1
movl 0x1a8(%rsp,%r9,4), %r11d
movq %r9, %rax
imulq %rdx, %rax
movq (%rsp), %rsi
leaq (%rsi,%r9,4), %r12
leaq (%rsi,%rax,4), %r13
movq %rdi, %rbp
testl %r14d, %r14d
jle 0x16357
xorps %xmm2, %xmm2
xorl %eax, %eax
xorl %esi, %esi
cmpq %rax, %r9
sete %bl
cmpq %rax, %rbp
sete %r10b
orb %bl, %r10b
jne 0x16344
cvtss2sd %xmm2, %xmm2
addsd 0x128(%rsp,%rax,8), %xmm2
cvtsd2ss %xmm2, %xmm2
addl 0x1a8(%rsp,%rax,4), %esi
incq %rax
cmpq %rax, %rcx
jne 0x1631a
testl %esi, %esi
sete %al
cvtss2sd %xmm2, %xmm2
jmp 0x1635c
movb $0x1, %al
xorps %xmm2, %xmm2
movl 0x1a8(%rsp,%rbp,4), %r10d
leal (%r10,%r11), %esi
shll $0x8, %esi
orl %r10d, %esi
testb %al, %al
je 0x16385
movq 0x8(%r15), %rax
movq 0x10(%rax), %rax
movsd (%rax,%rsi,8), %xmm3
mulsd %xmm0, %xmm3
jmp 0x1639a
movq 0x8(%r15), %rax
movq 0x10(%rax), %rax
movsd (%rax,%rsi,8), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
xorps %xmm2, %xmm2
cvtsd2ss %xmm3, %xmm2
movq %rbp, %rax
imulq %rdx, %rax
movss %xmm2, (%r12,%rax,4)
movss %xmm2, (%r13,%rbp,4)
incq %rbp
cmpq %rcx, %rbp
jne 0x1630e
xorl %eax, %eax
movq 0x18(%rsp), %r9
ucomiss (%r8,%rax,4), %xmm1
jbe 0x163d7
movl $0x0, (%r8,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x163c8
incq %rdi
addq 0x10(%rsp), %r8
cmpq %rcx, %r9
jne 0x1629b
xorl %eax, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq