// File: /workspace/github_c_makefile_star2_O1/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 0x1ace6
jnp 0x1ad1e
movapd %xmm0, %xmm4
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x1acf6
jnp 0x1ad1e
pushq %rbp
pushq %rbx
subq $0x48, %rsp
movapd %xmm1, %xmm3
movsd 0x6928(%rip), %xmm6     # 0x21630
subsd %xmm2, %xmm6
ucomisd %xmm0, %xmm6
jne 0x1ad23
jp 0x1ad23
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, 0x8(%rsp)
mulsd %xmm6, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm3, 0x18(%rsp)
movsd %xmm2, 0x10(%rsp)
jb 0x1ad55
sqrtsd %xmm0, %xmm7
jmp 0x1ad80
movapd %xmm4, 0x30(%rsp)
movsd %xmm6, (%rsp)
callq 0x24f0
movsd (%rsp), %xmm6
movapd 0x30(%rsp), %xmm4
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
movapd %xmm0, %xmm7
testl %ebx, %ebx
setne %al
testl %ebp, %ebp
sete %cl
testb %cl, %al
jne 0x1ae1a
testl %ebp, %ebp
je 0x1adb4
movsd %xmm7, 0x20(%rsp)
movsd %xmm6, (%rsp)
movapd %xmm4, %xmm0
testl %ebx, %ebx
je 0x1add2
callq 0x2550
movapd %xmm0, %xmm4
jmp 0x1ade3
testl %ebx, %ebx
jne 0x1adfa
movsd 0x6438(%rip), %xmm0     # 0x211f8
movapd %xmm0, %xmm1
subsd %xmm4, %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm4
jmp 0x1adfa
callq 0x22f0
movapd %xmm0, %xmm4
xorpd 0x81fd(%rip), %xmm4     # 0x22fe0
movsd 0x18(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movsd (%rsp), %xmm6
movsd 0x20(%rsp), %xmm7
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm4
jne 0x1ae0a
jnp 0x1ad18
ucomisd 0x681e(%rip), %xmm4     # 0x21630
jne 0x1ae1a
jnp 0x1ad14
movsd 0x7f26(%rip), %xmm0     # 0x22d48
addsd %xmm4, %xmm0
ucomisd 0x6802(%rip), %xmm0     # 0x21630
jae 0x1ad14
subsd %xmm2, %xmm6
divsd %xmm7, %xmm6
movsd %xmm6, (%rsp)
movsd 0x67e7(%rip), %xmm2     # 0x21630
xorpd %xmm1, %xmm1
movapd %xmm4, 0x30(%rsp)
movapd %xmm4, %xmm0
movl $0x1, %edi
xorl %esi, %esi
movsd %xmm7, 0x20(%rsp)
callq 0x1a693
movsd %xmm0, 0x28(%rsp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd 0x7ad1(%rip), %xmm1     # 0x22950
mulsd (%rsp), %xmm1
divsd 0x7b74(%rip), %xmm1     # 0x22a00
addsd %xmm1, %xmm0
mulsd 0x20(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
addsd 0x6354(%rip), %xmm0     # 0x211f8
callq 0x2470
movsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm2
minsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movapd %xmm1, %xmm0
callq 0x2090
xorpd %xmm4, %xmm4
movsd 0x8(%rsp), %xmm3
ucomisd %xmm3, %xmm4
ja 0x1af52
movapd %xmm0, %xmm1
movsd 0x7e5e(%rip), %xmm0     # 0x22d40
addsd %xmm3, %xmm0
movsd %xmm1, (%rsp)
callq 0x2470
movsd (%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
ucomisd %xmm2, %xmm0
movsd 0x6729(%rip), %xmm4     # 0x21630
jae 0x1af52
movsd 0x671f(%rip), %xmm1     # 0x21630
xorpd %xmm4, %xmm4
movsd 0x10(%rsp), %xmm5
ucomisd %xmm5, %xmm4
jae 0x1af4e
ucomisd 0x6707(%rip), %xmm5     # 0x21630
jae 0x1af52
addsd %xmm0, %xmm1
subsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm0
xorl %edi, %edi
xorl %esi, %esi
callq 0x1aa97
movsd 0x8(%rsp), %xmm3
movapd %xmm0, %xmm4
jmp 0x1af52
movapd %xmm1, %xmm4
movapd 0x30(%rsp), %xmm1
mulsd 0x7df0(%rip), %xmm1     # 0x22d50
movsd %xmm4, 0x28(%rsp)
movsd 0x7dea(%rip), %xmm0     # 0x22d58
movsd 0x18(%rsp), %xmm2
ucomisd %xmm2, %xmm0
jbe 0x1af9f
leaq 0x28(%rsp), %rdi
movsd 0x66a9(%rip), %xmm4     # 0x21630
movapd %xmm3, %xmm0
movsd 0x10(%rsp), %xmm3
callq 0x1b059
movapd %xmm0, %xmm5
jmp 0x1ad18
movsd 0x7db9(%rip), %xmm0     # 0x22d60
mulsd %xmm2, %xmm0
movapd %xmm1, 0x30(%rsp)
callq 0x2470
movsd 0x8(%rsp), %xmm5
movapd 0x30(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movapd %xmm0, %xmm4
movsd 0x78cc(%rip), %xmm0     # 0x228a0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x28(%rsp), %rbx
movsd 0x10(%rsp), %xmm3
movapd %xmm5, %xmm0
movq %rbx, %rdi
movsd %xmm4, (%rsp)
callq 0x1b059
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
divsd 0x78e3(%rip), %xmm0     # 0x228f0
callq 0x2470
movsd 0x6616(%rip), %xmm4     # 0x21630
movsd (%rsp), %xmm1
ucomisd %xmm4, %xmm1
jbe 0x1b04e
maxsd %xmm0, %xmm4
ucomisd 0x20(%rsp), %xmm4
movsd 0x18(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
movapd 0x30(%rsp), %xmm1
movsd 0x8(%rsp), %xmm5
ja 0x1afe9
jmp 0x1ad18
movsd 0x8(%rsp), %xmm5
jmp 0x1ad18