// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/fet.c
// Function: kt_fisher_exact
double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two)
{
	int i, j, max, min;
	double p, q, left, right;
	hgacc_t aux;
	int n1_, n_1, n;

	n1_ = n11 + n12; n_1 = n11 + n21; n = n11 + n12 + n21 + n22; // calculate n1_, n_1 and n
	max = (n_1 < n1_) ? n_1 : n1_; // max n11, for right tail
	min = n1_ + n_1 - n;
	if (min < 0) min = 0; // min n11, for left tail
	*two = *_left = *_right = 1.;
	if (min == max) return 1.; // no need to do test
	q = hypergeo_acc(n11, n1_, n_1, n, &aux); // the probability of the current table
	// left tail
	p = hypergeo_acc(min, 0, 0, 0, &aux);
	for (left = 0., i = min + 1; p < 0.99999999 * q; ++i) // loop until underflow
		left += p, p = hypergeo_acc(i, 0, 0, 0, &aux);
	--i;
	if (p < 1.00000001 * q) left += p;
	else --i;
	// right tail
	p = hypergeo_acc(max, 0, 0, 0, &aux);
	for (right = 0., j = max - 1; p < 0.99999999 * q; --j) // loop until underflow
		right += p, p = hypergeo_acc(j, 0, 0, 0, &aux);
	++j;
	if (p < 1.00000001 * q) right += p;
	else ++j;
	// two-tail
	*two = left + right;
	if (*two > 1.) *two = 1.;
	// adjust left and right
	if (abs(i - n11) < abs(j - n11)) right = 1. - left + q;
	else left = 1.0 - right + q;
	*_left = left; *_right = right;
	return q;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %eax
movl %edi, %r15d
addl %edi, %esi
leal (%rax,%r15), %edx
addl %eax, %ecx
addl %esi, %ecx
cmpl %esi, %edx
movl %esi, %ebp
cmovll %edx, %ebp
movq 0x80(%rsp), %rdi
leal (%rdx,%rsi), %eax
subl %ecx, %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovgl %eax, %r12d
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r9)
movq %rax, (%r8)
movq %rax, (%rdi)
cmpl %ebp, %r12d
jne 0x3d60f
movsd 0x33ae(%rip), %xmm0     # 0x409b8
jmp 0x3d7a1
movq %r8, %r14
movq %r9, 0x18(%rsp)
leaq 0x30(%rsp), %r13
movl %r15d, %edi
movq %r13, %r8
callq 0x3d7b0
movsd %xmm0, 0x8(%rsp)
movl %r12d, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r13, %r8
callq 0x3d7b0
movsd 0x789a(%rip), %xmm2     # 0x44ee0
mulsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
movsd %xmm2, 0x10(%rsp)
ucomisd %xmm0, %xmm2
jbe 0x3d68f
leaq 0x30(%rsp), %r13
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incl %r12d
movl %r12d, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r13, %r8
callq 0x3d7b0
movsd (%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
ucomisd %xmm0, %xmm2
ja 0x3d661
movsd 0x7851(%rip), %xmm2     # 0x44ee8
mulsd 0x8(%rsp), %xmm2
xorl %ebx, %ebx
movsd %xmm2, 0x20(%rsp)
ucomisd %xmm0, %xmm2
setbe %al
jbe 0x3d6b2
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movb %al, %bl
leaq 0x30(%rsp), %r8
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3d7b0
xorpd %xmm5, %xmm5
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3d70e
leaq 0x30(%rsp), %r13
addsd %xmm0, %xmm5
movsd %xmm5, 0x28(%rsp)
decl %ebp
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r13, %r8
callq 0x3d7b0
movsd 0x28(%rsp), %xmm5
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3d6e0
negl %ebx
xorl %eax, %eax
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %cl
jbe 0x3d725
addsd %xmm0, %xmm5
movq 0x18(%rsp), %rsi
movq 0x80(%rsp), %rdx
movsd (%rsp), %xmm4
movb %cl, %al
movapd %xmm4, %xmm1
addsd %xmm5, %xmm1
movsd 0x326f(%rip), %xmm3     # 0x409b8
movapd %xmm3, %xmm2
minsd %xmm1, %xmm2
subl %r15d, %ebx
addl %r12d, %ebx
movl %ebx, %ecx
negl %ecx
cmovsl %ebx, %ecx
movsd %xmm2, (%rdx)
subl %r15d, %eax
addl %ebp, %eax
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
cmpl %edx, %ecx
jb 0x3d786
subsd %xmm5, %xmm3
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm3
movapd %xmm3, %xmm4
jmp 0x3d798
subsd %xmm4, %xmm3
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm3
movapd %xmm3, %xmm5
movsd %xmm4, (%r14)
movsd %xmm5, (%rsi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq