// File: /workspace/github_c_makefile_star2_O1/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 0x19847
jp 0x19847
testl %ebx, %ebx
je 0x19d9f
movsd 0x903e(%rip), %xmm4     # 0x22880
jmp 0x19d9f
movapd %xmm0, %xmm3
cmplesd %xmm1, %xmm0
movapd %xmm0, %xmm4
andpd %xmm3, %xmm4
andnpd %xmm1, %xmm0
orpd %xmm4, %xmm0
ucomisd 0x7dc8(%rip), %xmm0     # 0x21630
movapd %xmm3, 0x30(%rsp)
movsd %xmm2, 0x58(%rsp)
movapd %xmm1, 0x60(%rsp)
jae 0x199a3
movapd %xmm1, %xmm5
cmplesd %xmm3, %xmm5
movapd %xmm5, %xmm4
andpd %xmm3, %xmm4
andnpd %xmm1, %xmm5
orpd %xmm4, %xmm5
movsd 0x92cf(%rip), %xmm4     # 0x22b70
ucomisd %xmm5, %xmm4
jbe 0x199fa
movsd 0x7d7d(%rip), %xmm4     # 0x21630
ucomisd %xmm5, %xmm4
jae 0x19a8d
movapd %xmm0, 0x20(%rsp)
movapd %xmm5, (%rsp)
callq 0x1bfbb
movapd (%rsp), %xmm4
movsd 0x9076(%rip), %xmm1     # 0x22950
addsd %xmm4, %xmm1
cvttsd2si %xmm1, %eax
testl %eax, %eax
movsd %xmm0, 0x50(%rsp)
jle 0x19938
movsd 0x905c(%rip), %xmm5     # 0x22950
movapd 0x20(%rsp), %xmm3
movsd 0x7d2e(%rip), %xmm0     # 0x21630
addsd %xmm5, %xmm4
movapd %xmm3, %xmm1
addsd %xmm4, %xmm1
movapd %xmm4, %xmm2
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
decl %eax
jne 0x19902
movapd %xmm4, (%rsp)
callq 0x2240
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x2240
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movapd (%rsp), %xmm1
addsd 0x8ff4(%rip), %xmm1     # 0x22950
movapd 0x20(%rsp), %xmm2
movapd %xmm1, (%rsp)
addsd %xmm1, %xmm2
ucomisd 0x7cbd(%rip), %xmm2     # 0x21630
jbe 0x19b09
movsd 0x8fcf(%rip), %xmm0     # 0x22950
addsd %xmm2, %xmm0
movsd %xmm2, 0x48(%rsp)
callq 0x1c2db
addsd 0x7c98(%rip), %xmm0     # 0x21630
divsd 0x48(%rsp), %xmm0
jmp 0x19b1a
movapd %xmm2, %xmm0
callq 0x2240
movsd %xmm0, (%rsp)
movapd 0x30(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
callq 0x1bac7
movsd (%rsp), %xmm2
movapd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
testl %ebx, %ebx
je 0x19a73
movapd %xmm1, %xmm0
movsd %xmm2, (%rsp)
callq 0x2240
movsd (%rsp), %xmm13
subsd %xmm0, %xmm13
jmp 0x19c75
movapd %xmm0, 0x20(%rsp)
movapd %xmm5, (%rsp)
callq 0x1bfbb
movsd %xmm0, 0x18(%rsp)
movapd 0x20(%rsp), %xmm0
movaps (%rsp), %xmm1
callq 0x1c11f
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 0x19aee
movapd %xmm2, %xmm0
movsd %xmm1, (%rsp)
callq 0x2240
movsd (%rsp), %xmm13
jmp 0x19bfe
movapd %xmm2, %xmm0
callq 0x2550
movapd %xmm0, %xmm13
divsd 0x30(%rsp), %xmm13
jmp 0x19c75
movapd %xmm2, %xmm0
testl %ebx, %ebx
je 0x19c05
callq 0x2240
movapd 0x30(%rsp), %xmm1
movapd %xmm0, %xmm4
mulsd %xmm1, %xmm4
movsd %xmm4, 0x20(%rsp)
addsd 0x60(%rsp), %xmm1
ucomisd 0x7b70(%rip), %xmm1     # 0x21630
movapd %xmm1, (%rsp)
jbe 0x19b89
movsd 0x8e7d(%rip), %xmm0     # 0x22950
addsd %xmm1, %xmm0
callq 0x1c2db
addsd 0x7b4c(%rip), %xmm0     # 0x21630
divsd (%rsp), %xmm0
jmp 0x19b9a
movapd %xmm1, %xmm0
movapd %xmm2, 0x20(%rsp)
callq 0x2550
movapd 0x20(%rsp), %xmm13
jmp 0x19c70
movapd %xmm2, %xmm0
callq 0x1c2db
addsd 0x7b16(%rip), %xmm0     # 0x21630
movsd %xmm0, 0x48(%rsp)
movsd 0x18(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movaps (%rsp), %xmm0
callq 0x1c2db
movsd %xmm0, (%rsp)
testl %ebx, %ebx
je 0x19c31
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 0x199e5
movapd %xmm1, %xmm0
callq 0x1c2db
addsd 0x7a96(%rip), %xmm0     # 0x21630
movsd %xmm0, 0x18(%rsp)
movaps 0x30(%rsp), %xmm0
callq 0x1c2db
movsd %xmm0, 0x50(%rsp)
movapd 0x60(%rsp), %xmm0
callq 0x1c2db
movapd %xmm0, %xmm1
movsd 0x7a69(%rip), %xmm0     # 0x21630
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 0x19c69
callq 0x2240
movsd 0x20(%rsp), %xmm13
addsd %xmm0, %xmm13
jmp 0x19c75
movapd %xmm3, %xmm1
callq 0x2140
movapd 0x30(%rsp), %xmm1
movapd %xmm0, %xmm4
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm4
jne 0x19aac
jnp 0x19d9f
jmp 0x19aac
movsd 0x18(%rsp), %xmm0
callq 0x2550
movapd 0x20(%rsp), %xmm1
divsd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd (%rsp), %xmm13
addsd 0x79d5(%rip), %xmm13    # 0x21630
mulsd %xmm1, %xmm13
divsd 0x48(%rsp), %xmm13
jmp 0x19c75
movsd 0x20(%rsp), %xmm13
mulsd %xmm0, %xmm13
testl %ebx, %ebx
sete %al
movsd 0x58(%rsp), %xmm11
jne 0x19c89
xorpd %xmm0, %xmm0
jmp 0x19c91
movsd 0x8bef(%rip), %xmm0     # 0x22880
movapd 0x30(%rsp), %xmm10
movapd 0x60(%rsp), %xmm12
movsd 0x8ed1(%rip), %xmm1     # 0x22b78
ucomisd %xmm10, %xmm1
setae %cl
testb %al, %cl
jne 0x19d9a
ucomisd %xmm0, %xmm13
jne 0x19cc4
jnp 0x19d9a
movsd 0x8bd4(%rip), %xmm1     # 0x228a0
divsd %xmm10, %xmm1
xorpd %xmm2, %xmm2
movsd 0x7953(%rip), %xmm3     # 0x21630
movsd 0x7513(%rip), %xmm4     # 0x211f8
movsd 0x8e93(%rip), %xmm5     # 0x22b80
movapd 0x92db(%rip), %xmm6     # 0x22fd0
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 0x19d52
andpd %xmm6, %xmm8
ucomisd %xmm1, %xmm8
ja 0x19cfd
mulsd %xmm10, %xmm0
testl %ebx, %ebx
je 0x19d7f
ucomisd 0x8bed(%rip), %xmm0     # 0x22950
jbe 0x1983a
movsd %xmm13, (%rsp)
callq 0x2220
movsd (%rsp), %xmm4
addsd %xmm0, %xmm4
jmp 0x19d9f
xorpd %xmm4, %xmm4
ucomisd 0x8bc5(%rip), %xmm0     # 0x22950
jbe 0x19d9f
addsd 0x789b(%rip), %xmm0     # 0x21630
mulsd %xmm0, %xmm13
movapd %xmm13, %xmm4
movapd %xmm4, %xmm0
addq $0x70, %rsp
popq %rbx
retq