// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/binomlite.c
// Function: bpser
static double bpser(double a, double b, double x, double eps, int log_p)
{
/* -----------------------------------------------------------------------
 * Power SERies expansion for evaluating I_x(a,b) when
 *           b <= 1 or b*x <= 0.7.   eps is the tolerance used.
 * NB: if log_p is 1, also use it if   (b < 40  & lambda > 650)
 * ----------------------------------------------------------------------- */

    int i, m;
    double ans, c, t, u, z, a0, b0, apb;

    if (x == 0.) {
        return R_D__0;
    }
/* ----------------------------------------------------------------------- */
/*          compute the factor  x^a/(a*Beta(a,b)) */
/* ----------------------------------------------------------------------- */
    a0 = fmin2(a,b);
    if (a0 >= 1.) { /*         ------     1 <= a0 <= b0  ------ */
        z = a * log(x) - betaln(a, b);
        ans = log_p ? z - log(a) : exp(z) / a;
    } else {
        b0 = fmax2(a,b);

    if (b0 < 8.) {

        if (b0 <= 1.) { /*     ------     a0 < 1     and  b0 <= 1  ------ */
            
            if(log_p) {
                ans = a * log(x);
            } else {
                ans = pow(x, a);
                if (ans == 0.) /* once underflow, always underflow .. */
                    return ans;
            }
            apb = a + b;
            if (apb > 1.) {
                u = a + b - 1.;
                z = (gam1(u) + 1.) / apb;
            } else {
                z = gam1(apb) + 1.;
            }
            c = (gam1(a) + 1.) * (gam1(b) + 1.) / z;

            if(log_p) /* FIXME ? -- improve quite a bit for c ~= 1 */
                ans += log(c * (b / apb));
            else
                ans *=  c * (b / apb);

            } else { /*     ------    a0 < 1 < b0 < 8     ------ */

                u = gamln1(a0);
                m = (int)(b0 - 1.);
                if (m >= 1) {
                    c = 1.;
                    for (i = 1; i <= m; ++i) {
                        b0 += -1.;
                        c *= b0 / (a0 + b0);
                    }
                    u += log(c);
                }

                z = a * log(x) - u;
                b0 += -1.; // => b0 in (0, 7)
                apb = a0 + b0;
                if (apb > 1.) {
                    u = a0 + b0 - 1.;
                    t = (gam1(u) + 1.) / apb;
                } else {
                    t = gam1(apb) + 1.;
                }

                if(log_p) /* FIXME? potential for improving log(t) */
                    ans = z + log(a0 / a) + log1p(gam1(b0)) - log(t);
                else
                    ans = exp(z) * (a0 / a) * (gam1(b0) + 1.) / t;
            }

        } else { /*         ------  a0 < 1 < 8 <= b0  ------ */

            u = gamln1(a0) + algdiv(a0, b0);
            z = a * log(x) - u;

            if(log_p)
                ans = z + log(a0 / a);
            else
                ans = a0 / a * exp(z);
        }
    }
    if (ans == R_D__0 || (!log_p && a <= eps * 0.1)) {
        return ans;
    }

/* ----------------------------------------------------------------------- */
/*               COMPUTE THE SERIES */
/* ----------------------------------------------------------------------- */
    double tol = eps / a,
    n = 0.,
    sum = 0., w;
    c = 1.;
    do { // sum is alternating as long as n < b (<==> 1 - b/n < 0)
        n += 1.;
        c *= (0.5 - b / n + 0.5) * x;
        w = c / (a + n);
        sum += w;
    } while (n < 1e7 && fabs(w) > tol);
    if(log_p) {
        if (a*sum > -1.)
            ans += log1p(a * sum);
        else
            ans = ML_NEGINF;
    } else if (a*sum > -1.) {
        ans *= (a * sum + 1.);
    } else { // underflow to
        ans = 0.;
    }
    return ans;
}

// Assembly:
pushq %rbx
subq $0x70, %rsp
movl %edi, %ebx
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm2
jne 0x1a674
jp 0x1a674
testl %ebx, %ebx
je 0x1abd1
movsd 0x9241(%rip), %xmm4     # 0x238b0
jmp 0x1abd1
movapd %xmm0, %xmm3
cmplesd %xmm1, %xmm0
movapd %xmm0, %xmm4
andpd %xmm3, %xmm4
andnpd %xmm1, %xmm0
orpd %xmm4, %xmm0
ucomisd 0x7fcb(%rip), %xmm0     # 0x22660
movapd %xmm3, 0x30(%rsp)
movsd %xmm2, 0x58(%rsp)
movapd %xmm1, 0x60(%rsp)
jae 0x1a780
movapd %xmm1, %xmm5
cmplesd %xmm3, %xmm5
movapd %xmm5, %xmm4
andpd %xmm3, %xmm4
andnpd %xmm1, %xmm5
orpd %xmm4, %xmm5
movsd 0x94aa(%rip), %xmm4     # 0x23b78
ucomisd %xmm5, %xmm4
jbe 0x1a7d7
movsd 0x7f80(%rip), %xmm4     # 0x22660
ucomisd %xmm5, %xmm4
jae 0x1a86a
movapd %xmm0, 0x20(%rsp)
movapd %xmm5, (%rsp)
callq 0x1cdd4
movapd (%rsp), %xmm5
movsd 0x9261(%rip), %xmm1     # 0x23968
addsd %xmm5, %xmm1
cvttsd2si %xmm1, %eax
testl %eax, %eax
movsd %xmm0, 0x50(%rsp)
jle 0x1a962
movsd 0x9243(%rip), %xmm4     # 0x23968
movapd 0x20(%rsp), %xmm3
movsd 0x7f2d(%rip), %xmm0     # 0x22660
addsd %xmm4, %xmm5
movapd %xmm3, %xmm1
addsd %xmm5, %xmm1
movapd %xmm5, %xmm2
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
decl %eax
jne 0x1a733
movapd %xmm5, (%rsp)
callq 0x2240
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movapd (%rsp), %xmm0
addsd 0x91f2(%rip), %xmm0     # 0x23968
movsd %xmm0, (%rsp)
jmp 0x1a967
movapd %xmm2, %xmm0
callq 0x2240
movsd %xmm0, (%rsp)
movapd 0x30(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
callq 0x1c8e0
movsd (%rsp), %xmm2
movapd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
testl %ebx, %ebx
je 0x1a850
movapd %xmm1, %xmm0
movsd %xmm2, (%rsp)
callq 0x2240
movsd (%rsp), %xmm13
subsd %xmm0, %xmm13
jmp 0x1aaa7
movapd %xmm0, 0x20(%rsp)
movapd %xmm5, (%rsp)
callq 0x1cdd4
movsd %xmm0, 0x18(%rsp)
movapd 0x20(%rsp), %xmm0
movaps (%rsp), %xmm1
callq 0x1cebd
addsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x2240
movapd 0x20(%rsp), %xmm2
movapd %xmm0, %xmm1
movapd 0x30(%rsp), %xmm0
mulsd %xmm0, %xmm1
subsd (%rsp), %xmm1
divsd %xmm0, %xmm2
testl %ebx, %ebx
je 0x1a8c4
movapd %xmm2, %xmm0
movsd %xmm1, (%rsp)
callq 0x2240
movsd (%rsp), %xmm13
jmp 0x1a958
movapd %xmm2, %xmm0
callq 0x2550
movapd %xmm0, %xmm13
divsd 0x30(%rsp), %xmm13
jmp 0x1aaa7
movapd %xmm2, %xmm0
testl %ebx, %ebx
je 0x1aa37
callq 0x2240
movapd 0x30(%rsp), %xmm1
movapd %xmm0, %xmm4
mulsd %xmm1, %xmm4
movsd %xmm4, 0x20(%rsp)
addsd 0x60(%rsp), %xmm1
ucomisd 0x7dc3(%rip), %xmm1     # 0x22660
movapd %xmm1, (%rsp)
jbe 0x1a8df
movsd 0x90bc(%rip), %xmm0     # 0x23968
addsd %xmm1, %xmm0
callq 0x1d093
addsd 0x7da3(%rip), %xmm0     # 0x22660
divsd (%rsp), %xmm0
jmp 0x1a8f0
movapd %xmm1, %xmm0
movapd %xmm2, 0x20(%rsp)
callq 0x2550
movapd 0x20(%rsp), %xmm13
jmp 0x1aaa2
movapd %xmm1, %xmm0
callq 0x1d093
addsd 0x7d70(%rip), %xmm0     # 0x22660
movsd %xmm0, 0x18(%rsp)
movaps 0x30(%rsp), %xmm0
callq 0x1d093
movsd %xmm0, 0x50(%rsp)
movapd 0x60(%rsp), %xmm0
callq 0x1d093
movapd %xmm0, %xmm1
movsd 0x7d43(%rip), %xmm0     # 0x22660
movsd 0x50(%rsp), %xmm2
addsd %xmm0, %xmm2
addsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
divsd 0x18(%rsp), %xmm1
movapd 0x60(%rsp), %xmm0
divsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
testl %ebx, %ebx
je 0x1aa9b
callq 0x2240
movsd 0x20(%rsp), %xmm13
addsd %xmm0, %xmm13
jmp 0x1aaa7
movsd %xmm1, (%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x2240
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movapd 0x20(%rsp), %xmm1
addsd (%rsp), %xmm1
ucomisd 0x7ccf(%rip), %xmm1     # 0x22660
jbe 0x1a9ba
movsd 0x8fcd(%rip), %xmm0     # 0x23968
addsd %xmm1, %xmm0
movsd %xmm1, 0x48(%rsp)
callq 0x1d093
addsd 0x7cae(%rip), %xmm0     # 0x22660
divsd 0x48(%rsp), %xmm0
jmp 0x1a9cb
movapd %xmm1, %xmm0
callq 0x1d093
addsd 0x7c95(%rip), %xmm0     # 0x22660
movsd %xmm0, 0x48(%rsp)
movsd 0x18(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd (%rsp), %xmm0
callq 0x1d093
movsd %xmm0, (%rsp)
testl %ebx, %ebx
je 0x1aa63
movapd 0x20(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
callq 0x2240
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movsd (%rsp), %xmm0
callq 0x2220
addsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x48(%rsp), %xmm0
jmp 0x1a7c2
movapd %xmm3, %xmm1
callq 0x2140
movapd 0x30(%rsp), %xmm1
movapd %xmm0, %xmm4
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm4
jne 0x1a889
jnp 0x1abd1
jmp 0x1a889
movsd 0x18(%rsp), %xmm0
callq 0x2550
movapd 0x20(%rsp), %xmm1
divsd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd (%rsp), %xmm13
addsd 0x7bd3(%rip), %xmm13    # 0x22660
mulsd %xmm1, %xmm13
divsd 0x48(%rsp), %xmm13
jmp 0x1aaa7
movsd 0x20(%rsp), %xmm13
mulsd %xmm0, %xmm13
testl %ebx, %ebx
sete %al
movsd 0x58(%rsp), %xmm11
jne 0x1aabb
xorpd %xmm0, %xmm0
jmp 0x1aac3
movsd 0x8ded(%rip), %xmm0     # 0x238b0
movapd 0x30(%rsp), %xmm10
movapd 0x60(%rsp), %xmm12
movsd 0x90a7(%rip), %xmm1     # 0x23b80
ucomisd %xmm10, %xmm1
setae %cl
testb %al, %cl
jne 0x1abcc
ucomisd %xmm0, %xmm13
jne 0x1aaf6
jnp 0x1abcc
movsd 0x8dd2(%rip), %xmm1     # 0x238d0
divsd %xmm10, %xmm1
xorpd %xmm2, %xmm2
movsd 0x7b51(%rip), %xmm3     # 0x22660
movsd 0x7701(%rip), %xmm4     # 0x22218
movsd 0x9069(%rip), %xmm5     # 0x23b88
movapd 0x92b9(%rip), %xmm6     # 0x23de0
movapd %xmm3, %xmm7
xorpd %xmm0, %xmm0
movapd %xmm7, %xmm8
addsd %xmm3, %xmm2
movapd %xmm12, %xmm9
divsd %xmm2, %xmm9
movapd %xmm4, %xmm7
subsd %xmm9, %xmm7
addsd %xmm4, %xmm7
mulsd %xmm11, %xmm7
mulsd %xmm8, %xmm7
movapd %xmm2, %xmm9
addsd %xmm10, %xmm9
movapd %xmm7, %xmm8
divsd %xmm9, %xmm8
addsd %xmm8, %xmm0
ucomisd %xmm2, %xmm5
jbe 0x1ab84
andpd %xmm6, %xmm8
ucomisd %xmm1, %xmm8
ja 0x1ab2f
mulsd %xmm10, %xmm0
testl %ebx, %ebx
je 0x1abb1
ucomisd 0x8dd3(%rip), %xmm0     # 0x23968
jbe 0x1a667
movsd %xmm13, (%rsp)
callq 0x2220
movsd (%rsp), %xmm4
addsd %xmm0, %xmm4
jmp 0x1abd1
xorpd %xmm4, %xmm4
ucomisd 0x8dab(%rip), %xmm0     # 0x23968
jbe 0x1abd1
addsd 0x7a99(%rip), %xmm0     # 0x22660
mulsd %xmm0, %xmm13
movapd %xmm13, %xmm4
movapd %xmm4, %xmm0
addq $0x70, %rsp
popq %rbx
retq