// File: /workspace/github_c_makefile_star2_O2/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 0x1684e
jp 0x1684e
testl %ebx, %ebx
je 0x16db8
movsd 0x8f2f(%rip), %xmm4     # 0x1f778
jmp 0x16db8
movapd %xmm0, %xmm3
cmplesd %xmm1, %xmm0
movapd %xmm0, %xmm4
andpd %xmm3, %xmm4
andnpd %xmm1, %xmm0
orpd %xmm4, %xmm0
ucomisd 0x7da9(%rip), %xmm0     # 0x1e618
movapd %xmm3, 0x30(%rsp)
movsd %xmm2, 0x58(%rsp)
movapd %xmm1, 0x60(%rsp)
jae 0x16930
movapd %xmm1, %xmm5
cmplesd %xmm3, %xmm5
movapd %xmm5, %xmm4
andpd %xmm3, %xmm4
andnpd %xmm1, %xmm5
orpd %xmm4, %xmm5
movsd 0x9008(%rip), %xmm4     # 0x1f8b0
ucomisd %xmm5, %xmm4
jbe 0x16987
movsd 0x7d5e(%rip), %xmm4     # 0x1e618
ucomisd %xmm5, %xmm4
jae 0x16a1a
movapd %xmm0, 0x20(%rsp)
movapd %xmm5, (%rsp)
callq 0x1842e
movapd (%rsp), %xmm5
movsd 0x8ecf(%rip), %xmm1     # 0x1f7b0
addsd %xmm5, %xmm1
cvttsd2si %xmm1, %eax
testl %eax, %eax
movsd %xmm0, 0x50(%rsp)
jle 0x16b49
movsd 0x8eb1(%rip), %xmm4     # 0x1f7b0
movapd 0x20(%rsp), %xmm3
movsd 0x7d0b(%rip), %xmm0     # 0x1e618
subl $0x1, %eax
jb 0x16a8f
addsd %xmm4, %xmm5
movapd %xmm3, %xmm1
addsd %xmm5, %xmm1
movapd %xmm5, %xmm2
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
jmp 0x1690d
movapd %xmm2, %xmm0
callq 0x2240
movsd %xmm0, (%rsp)
movapd 0x30(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
callq 0x17f4b
movsd (%rsp), %xmm2
movapd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
testl %ebx, %ebx
je 0x16a00
movapd %xmm1, %xmm0
movsd %xmm2, (%rsp)
callq 0x2240
movsd (%rsp), %xmm13
subsd %xmm0, %xmm13
jmp 0x16c8e
movapd %xmm0, 0x20(%rsp)
movapd %xmm5, (%rsp)
callq 0x1842e
movsd %xmm0, 0x18(%rsp)
movapd 0x20(%rsp), %xmm0
movaps (%rsp), %xmm1
callq 0x18517
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 0x16a74
movapd %xmm2, %xmm0
movsd %xmm1, (%rsp)
callq 0x2240
movsd (%rsp), %xmm13
jmp 0x16b3f
movapd %xmm2, %xmm0
callq 0x2550
movapd %xmm0, %xmm13
divsd 0x30(%rsp), %xmm13
jmp 0x16c8e
movapd %xmm2, %xmm0
testl %ebx, %ebx
je 0x16c1e
callq 0x2240
movapd 0x30(%rsp), %xmm1
movapd %xmm0, %xmm4
mulsd %xmm1, %xmm4
movsd %xmm4, 0x20(%rsp)
addsd 0x60(%rsp), %xmm1
ucomisd 0x7bcb(%rip), %xmm1     # 0x1e618
movapd %xmm1, (%rsp)
jbe 0x16ac6
movsd 0x8d54(%rip), %xmm0     # 0x1f7b0
addsd %xmm1, %xmm0
callq 0x186ed
addsd 0x7bab(%rip), %xmm0     # 0x1e618
divsd (%rsp), %xmm0
jmp 0x16ad7
movapd %xmm1, %xmm0
movapd %xmm2, 0x20(%rsp)
callq 0x2550
movapd 0x20(%rsp), %xmm13
jmp 0x16c89
movapd %xmm5, (%rsp)
callq 0x2240
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movapd (%rsp), %xmm0
addsd 0x8cfa(%rip), %xmm0     # 0x1f7b0
movsd %xmm0, (%rsp)
movsd 0x58(%rsp), %xmm0
jmp 0x16b54
movapd %xmm1, %xmm0
callq 0x186ed
addsd 0x7b41(%rip), %xmm0     # 0x1e618
movsd %xmm0, 0x18(%rsp)
movaps 0x30(%rsp), %xmm0
callq 0x186ed
movsd %xmm0, 0x50(%rsp)
movapd 0x60(%rsp), %xmm0
callq 0x186ed
movapd %xmm0, %xmm1
movsd 0x7b14(%rip), %xmm0     # 0x1e618
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 0x16c82
callq 0x2240
movsd 0x20(%rsp), %xmm13
addsd %xmm0, %xmm13
jmp 0x16c8e
movsd 0x58(%rsp), %xmm0
movsd %xmm1, (%rsp)
callq 0x2240
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movapd 0x20(%rsp), %xmm1
addsd (%rsp), %xmm1
ucomisd 0x7aa0(%rip), %xmm1     # 0x1e618
jbe 0x16ba1
movsd 0x8c2e(%rip), %xmm0     # 0x1f7b0
addsd %xmm1, %xmm0
movsd %xmm1, 0x48(%rsp)
callq 0x186ed
addsd 0x7a7f(%rip), %xmm0     # 0x1e618
divsd 0x48(%rsp), %xmm0
jmp 0x16bb2
movapd %xmm1, %xmm0
callq 0x186ed
addsd 0x7a66(%rip), %xmm0     # 0x1e618
movsd %xmm0, 0x48(%rsp)
movsd 0x18(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd (%rsp), %xmm0
callq 0x186ed
movsd %xmm0, (%rsp)
testl %ebx, %ebx
je 0x16c4a
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 0x16972
movapd %xmm3, %xmm1
callq 0x2140
movapd 0x30(%rsp), %xmm1
movapd %xmm0, %xmm4
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm4
jne 0x16a39
jnp 0x16db8
jmp 0x16a39
movsd 0x18(%rsp), %xmm0
callq 0x2550
movapd 0x20(%rsp), %xmm1
divsd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd (%rsp), %xmm13
addsd 0x79a4(%rip), %xmm13    # 0x1e618
mulsd %xmm1, %xmm13
divsd 0x48(%rsp), %xmm13
jmp 0x16c8e
movsd 0x20(%rsp), %xmm13
mulsd %xmm0, %xmm13
testl %ebx, %ebx
sete %al
movsd 0x58(%rsp), %xmm11
jne 0x16ca2
xorpd %xmm0, %xmm0
jmp 0x16caa
movsd 0x8ace(%rip), %xmm0     # 0x1f778
movapd 0x30(%rsp), %xmm10
movapd 0x60(%rsp), %xmm12
movsd 0x8dc0(%rip), %xmm1     # 0x1fa80
ucomisd %xmm10, %xmm1
setae %cl
testb %al, %cl
jne 0x16db3
ucomisd %xmm0, %xmm13
jne 0x16cdd
jnp 0x16db3
movsd 0x8ab3(%rip), %xmm1     # 0x1f798
divsd %xmm10, %xmm1
xorpd %xmm2, %xmm2
movsd 0x7922(%rip), %xmm3     # 0x1e618
movsd 0x74e2(%rip), %xmm4     # 0x1e1e0
movsd 0x8d82(%rip), %xmm5     # 0x1fa88
movapd 0x8fb2(%rip), %xmm6     # 0x1fcc0
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 0x16d6b
andpd %xmm6, %xmm8
ucomisd %xmm1, %xmm8
ja 0x16d16
mulsd %xmm10, %xmm0
testl %ebx, %ebx
je 0x16d98
ucomisd 0x8a34(%rip), %xmm0     # 0x1f7b0
jbe 0x16841
movsd %xmm13, (%rsp)
callq 0x2220
movsd (%rsp), %xmm4
addsd %xmm0, %xmm4
jmp 0x16db8
xorpd %xmm4, %xmm4
ucomisd 0x8a0c(%rip), %xmm0     # 0x1f7b0
jbe 0x16db8
addsd 0x786a(%rip), %xmm0     # 0x1e618
mulsd %xmm0, %xmm13
movapd %xmm13, %xmm4
movapd %xmm4, %xmm0
addq $0x70, %rsp
popq %rbx
retq