// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/binomlite.c
// Function: qbinom
double qbinom(double p, double n, double pr, int lower_tail, int log_p)
{
    double q, mu, sigma, gamma, z, y;

    if (pr == 0. || n == 0)
        return 0.;

    q = 1 - pr;
    if(q == 0.)
        return n; /* covers the full range of the distribution */
    mu = n * pr;
    sigma = sqrt(n * pr * q);
    gamma = (q - pr) / sigma;

    if(!lower_tail || log_p) {
        p = R_DT_qIv(p); /* need check again (cancellation!): */
        if (p == 0.) return 0.;
        if (p == 1.) return n;
    }
    /* temporary hack --- FIXME --- */
    if (p + 1.01 * DBL_EPSILON >= 1.)
        return n;

    /* y := approx.value (Cornish-Fisher expansion) :  */
    z = qnorm(p, 0., 1., /*lower_tail*/1, /*log_p*/0);
    y = floor(mu + sigma * (z + gamma * (z * z - 1) / 6) + 0.5);

    if (y > n) /* way off */
        y = n;

    z = pbinom(y, n, pr, /*lower_tail*/1, /*log_p*/0);

    /* fuzz to ensure left continuity: */
    p *= 1 - 64 * DBL_EPSILON;

    if (n < 1e5)
        return do_search(y, &z, p, n, pr, 1);
    /* Otherwise be a bit cleverer in the search */
    {
        double incr = floor(n * 0.001), oldincr;
        do {
            oldincr = incr;
            y = do_search(y, &z, p, n, pr, incr);
            incr = fmax2(1, floor(incr / 100));
        } while (oldincr > 1 && incr > n * 1e-15);
        return y;
    }
}

// Assembly:
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x1bae8
jnp 0x1bb20
movapd %xmm0, %xmm4
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x1baf8
jnp 0x1bb20
pushq %rbp
pushq %rbx
subq $0x48, %rsp
movapd %xmm1, %xmm3
movsd 0x6b56(%rip), %xmm6     # 0x22660
subsd %xmm2, %xmm6
ucomisd %xmm0, %xmm6
jne 0x1bb25
jp 0x1bb25
movapd %xmm3, %xmm5
addq $0x48, %rsp
popq %rbx
popq %rbp
movapd %xmm5, %xmm0
retq
movl %esi, %ebp
movl %edi, %ebx
movapd %xmm3, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x10(%rsp)
mulsd %xmm6, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm3, 0x20(%rsp)
movsd %xmm2, 0x18(%rsp)
jb 0x1bb57
sqrtsd %xmm0, %xmm7
jmp 0x1bb84
movapd %xmm4, 0x30(%rsp)
movsd %xmm6, 0x8(%rsp)
callq 0x24f0
movsd 0x8(%rsp), %xmm6
movapd 0x30(%rsp), %xmm4
movsd 0x18(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
movapd %xmm0, %xmm7
testl %ebx, %ebx
setne %al
testl %ebp, %ebp
sete %cl
testb %cl, %al
jne 0x1bc20
testl %ebp, %ebp
je 0x1bbb9
movsd %xmm7, 0x28(%rsp)
movsd %xmm6, 0x8(%rsp)
movapd %xmm4, %xmm0
testl %ebx, %ebx
je 0x1bbd7
callq 0x2550
movapd %xmm0, %xmm4
jmp 0x1bbe8
testl %ebx, %ebx
jne 0x1bc00
movsd 0x6653(%rip), %xmm0     # 0x22218
movapd %xmm0, %xmm1
subsd %xmm4, %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm4
jmp 0x1bc00
callq 0x22f0
movapd %xmm0, %xmm4
xorpd 0x8208(%rip), %xmm4     # 0x23df0
movsd 0x20(%rsp), %xmm3
movsd 0x18(%rsp), %xmm2
movsd 0x8(%rsp), %xmm6
movsd 0x28(%rsp), %xmm7
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm4
jne 0x1bc10
jnp 0x1bb1a
ucomisd 0x6a48(%rip), %xmm4     # 0x22660
jne 0x1bc20
jnp 0x1bb16
movsd 0x8038(%rip), %xmm0     # 0x23c60
addsd %xmm4, %xmm0
ucomisd 0x6a2c(%rip), %xmm0     # 0x22660
jae 0x1bb16
subsd %xmm2, %xmm6
divsd %xmm7, %xmm6
movsd %xmm6, 0x8(%rsp)
movsd 0x6a10(%rip), %xmm2     # 0x22660
xorpd %xmm1, %xmm1
movapd %xmm4, 0x30(%rsp)
movapd %xmm4, %xmm0
movl $0x1, %edi
xorl %esi, %esi
movsd %xmm7, 0x28(%rsp)
callq 0x1b530
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd 0x7ce8(%rip), %xmm1     # 0x23968
mulsd 0x8(%rsp), %xmm1
divsd 0x7d7a(%rip), %xmm1     # 0x23a08
addsd %xmm1, %xmm0
mulsd 0x28(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
addsd 0x6572(%rip), %xmm0     # 0x22218
callq 0x2470
movsd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm2
minsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movapd %xmm1, %xmm0
callq 0x2090
xorpd %xmm4, %xmm4
movsd 0x10(%rsp), %xmm3
ucomisd %xmm3, %xmm4
ja 0x1bd56
movapd %xmm0, %xmm1
movsd 0x7f74(%rip), %xmm0     # 0x23c58
addsd %xmm3, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x2470
movsd 0x8(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
ucomisd %xmm2, %xmm0
movsd 0x6955(%rip), %xmm4     # 0x22660
jae 0x1bd56
movsd 0x694b(%rip), %xmm1     # 0x22660
xorpd %xmm4, %xmm4
movsd 0x18(%rsp), %xmm5
ucomisd %xmm5, %xmm4
jae 0x1bd52
ucomisd 0x6933(%rip), %xmm5     # 0x22660
jae 0x1bd56
addsd %xmm0, %xmm1
subsd %xmm0, %xmm2
movsd 0x18(%rsp), %xmm0
xorl %edi, %edi
xorl %esi, %esi
callq 0x1b899
movsd 0x10(%rsp), %xmm3
movapd %xmm0, %xmm4
jmp 0x1bd56
movapd %xmm1, %xmm4
movapd 0x30(%rsp), %xmm1
mulsd 0x7f04(%rip), %xmm1     # 0x23c68
movsd %xmm4, 0x40(%rsp)
movsd 0x7efe(%rip), %xmm0     # 0x23c70
movsd 0x20(%rsp), %xmm2
ucomisd %xmm2, %xmm0
jbe 0x1bda3
leaq 0x40(%rsp), %rdi
movsd 0x68d5(%rip), %xmm4     # 0x22660
movapd %xmm3, %xmm0
movsd 0x18(%rsp), %xmm3
callq 0x1be60
movapd %xmm0, %xmm5
jmp 0x1bb1a
movsd 0x7ecd(%rip), %xmm0     # 0x23c78
mulsd %xmm2, %xmm0
movapd %xmm1, 0x30(%rsp)
callq 0x2470
movsd 0x10(%rsp), %xmm5
movapd 0x30(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movapd %xmm0, %xmm4
movsd 0x7af8(%rip), %xmm0     # 0x238d0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x40(%rsp), %rbx
movsd 0x18(%rsp), %xmm3
movapd %xmm5, %xmm0
movq %rbx, %rdi
movsd %xmm4, 0x8(%rsp)
callq 0x1be60
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
divsd 0x7b0d(%rip), %xmm0     # 0x23920
callq 0x2470
movsd 0x6840(%rip), %xmm4     # 0x22660
movsd 0x8(%rsp), %xmm1
ucomisd %xmm4, %xmm1
jbe 0x1be55
maxsd %xmm0, %xmm4
ucomisd 0x28(%rsp), %xmm4
movsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movapd 0x30(%rsp), %xmm1
movsd 0x10(%rsp), %xmm5
ja 0x1bded
jmp 0x1bb1a
movsd 0x10(%rsp), %xmm5
jmp 0x1bb1a