// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]yahs/binomlite.c
// Function: bgrat
static void bgrat(double a, double b, double x, double y, double *w,
          double eps, int *ierr, int log_w)
{
/* -----------------------------------------------------------------------
*     Asymptotic Expansion for I_x(a,b)  when a is larger than b.
*     Compute   w := w + I_x(a,b)
*     It is assumed a >= 15 and b <= 1.
*     eps is the tolerance used.
*     ierr is a variable that reports the status of the results.
*
* if(log_w),  *w  itself must be in log-space;
*     compute   w := w + I_x(a,b)  but return *w = log(w):
*          *w := log(exp(*w) + I_x(a,b)) = logspace_add(*w, log( I_x(a,b) ))
* ----------------------------------------------------------------------- */

#define n_terms_bgrat 30
    double c[n_terms_bgrat], d[n_terms_bgrat];
    double bm1 = b - 0.5 - 0.5,
    nu = a + bm1 * 0.5, /* nu = a + (b-1)/2 =: T, in (9.1) of
                 * Didonato & Morris(1992), p.362 */
    lnx = (y > 0.375) ? log(x) : alnrel(-y),
    z = -nu * lnx; // z =: u in (9.1) of D.&M.(1992)

    if (b * z == 0.) { // should not happen, but does, e.g.,
    // for  pbeta(1e-320, 1e-5, 0.5)  i.e., _subnormal_ x,
    // Warning ... bgrat(a=20.5, b=1e-05, x=1, y=9.99989e-321): ..
    /* L_Error:    THE EXPANSION CANNOT BE COMPUTED */
     *ierr = 1; return;
    }

/*                 COMPUTATION OF THE EXPANSION */
    double
    /* r1 = b * (gam1(b) + 1.) * exp(b * log(z)),// = b/gamma(b+1) z^b = z^b / gamma(b)
     * set r := exp(-z) * z^b / gamma(b) ;
     *          gam1(b) = 1/gamma(b+1) - 1 , b in [-1/2, 3/2] */
    // exp(a*lnx) underflows for large (a * lnx); e.g. large a ==> using log_r := log(r):
    // r = r1 * exp(a * lnx) * exp(bm1 * 0.5 * lnx);
    // log(r)=log(b) + log1p(gam1(b)) + b * log(z) + (a * lnx) + (bm1 * 0.5 * lnx),
    log_r = log(b) + log1p(gam1(b)) + b * log(z) + nu * lnx,
    // FIXME work with  log_u = log(u)  also when log_p=0  (??)
    // u is 'factored out' from the expansion {and multiplied back, at the end}:
    log_u = log_r - (algdiv(b, a) + b * log(nu)),// algdiv(b,a) = log(gamma(a)/gamma(a+b))
    /* u = (log_p) ? log_r - u : exp(log_r-u); // =: M  in (9.2) of {reference above} */
    /* u = algdiv(b, a) + b * log(nu);// algdiv(b,a) = log(gamma(a)/gamma(a+b)) */
    // u = (log_p) ? log_u : exp(log_u); // =: M  in (9.2) of {reference above}
    u = exp(log_u);

    if (log_u == ML_NEGINF) {
    /* L_Error:    THE EXPANSION CANNOT BE COMPUTED */ *ierr = 2; return;
    }

    int u_0 = (u == 0.); // underflow --> do work with log(u) == log_u !
    double l = // := *w/u .. but with care: such that it also works when u underflows to 0:
    log_w
    ? ((*w == ML_NEGINF) ? 0. : exp(  *w    - log_u))
    : ((*w == 0.)        ? 0. : exp(log(*w) - log_u));

    double
    q_r = grat_r(b, z, log_r, eps), // = q/r of former grat1(b,z, r, &p, &q)
    v = 0.25 / (nu * nu),
    t2 = lnx * 0.25 * lnx,
    j = q_r,
    sum = j,
    t = 1., cn = 1., n2 = 0.;
    int n;
    for (n = 1; n <= n_terms_bgrat; ++n) {
    double bp2n = b + n2;
    j = (bp2n * (bp2n + 1.) * j + (z + bp2n + 1.) * t) * v;
    n2 += 2.;
    t *= t2;
    cn /= n2 * (n2 + 1.);
    int nm1 = n - 1;
    c[nm1] = cn;
    double s = 0.;
    if (n > 1) {
        double coef = b - n;
        int i;
        for (i = 1; i <= nm1; ++i) {
        s += coef * c[i - 1] * d[nm1 - i];
        coef += b;
        }
    }
    d[nm1] = bm1 * cn + s / n;
    double dj = d[nm1] * j;
    sum += dj;
    if (sum <= 0.) {
        /* L_Error:    THE EXPANSION CANNOT BE COMPUTED */ *ierr = 3; return;
    }
    if (fabs(dj) <= eps * (sum + l)) {
        *ierr = 0;
        break;
    } else if(n == n_terms_bgrat) { // never? ; please notify R-core if seen:
        *ierr = 4;
    }
    } // for(n .. n_terms..)

/*                    ADD THE RESULTS TO W */

    if(log_w) // *w is in log space already:
    *w = logspace_add(*w, log_u + log(sum));
    else
    *w += (u_0 ? exp(log_u + log(sum)) : u * sum);
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movsd %xmm0, -0x8(%rbp)
movsd %xmm1, -0x10(%rbp)
movsd %xmm2, -0x18(%rbp)
movsd %xmm3, -0x20(%rbp)
movq %rdi, -0x28(%rbp)
movsd %xmm4, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movl %edx, -0x3c(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0xf874(%rip), %xmm1     # 0x31600
subsd %xmm1, %xmm0
movsd 0xf868(%rip), %xmm1     # 0x31600
subsd %xmm1, %xmm0
movsd %xmm0, -0x228(%rbp)
movsd -0x8(%rbp), %xmm1
movsd -0x228(%rbp), %xmm0
movsd 0xf847(%rip), %xmm2     # 0x31600
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x230(%rbp)
movsd -0x20(%rbp), %xmm0
movsd 0x10a5a(%rip), %xmm1    # 0x32830
ucomisd %xmm1, %xmm0
jbe 0x21df0
movsd -0x18(%rbp), %xmm0
callq 0x2250
movsd %xmm0, -0x2e8(%rbp)
jmp 0x21e19
movsd -0x20(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x25fc0
movsd %xmm0, -0x2e8(%rbp)
movsd -0x2e8(%rbp), %xmm0
movsd %xmm0, -0x238(%rbp)
movsd -0x230(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd -0x238(%rbp), %xmm0
movsd %xmm0, -0x240(%rbp)
movsd -0x10(%rbp), %xmm0
mulsd -0x240(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x21e7f
jp 0x21e7f
movq -0x38(%rbp), %rax
movl $0x1, (%rax)
jmp 0x224df
movsd -0x10(%rbp), %xmm0
callq 0x2250
movsd %xmm0, -0x318(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x26e70
callq 0x2230
movaps %xmm0, %xmm1
movsd -0x318(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x308(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x310(%rbp)
movsd -0x240(%rbp), %xmm0
callq 0x2250
movsd -0x310(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x308(%rbp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x230(%rbp), %xmm0
movsd -0x238(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x248(%rbp)
movsd -0x248(%rbp), %xmm0
movsd %xmm0, -0x2f0(%rbp)
movsd -0x10(%rbp), %xmm0
movsd -0x8(%rbp), %xmm1
callq 0x26b40
movsd %xmm0, -0x2f8(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x300(%rbp)
movsd -0x230(%rbp), %xmm0
callq 0x2250
movsd -0x300(%rbp), %xmm1
movsd -0x2f8(%rbp), %xmm2
movaps %xmm0, %xmm3
movsd -0x2f0(%rbp), %xmm0
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x250(%rbp)
movsd -0x250(%rbp), %xmm0
callq 0x2560
movsd %xmm0, -0x258(%rbp)
movsd -0x250(%rbp), %xmm0
movsd 0x107df(%rip), %xmm1    # 0x32780
ucomisd %xmm1, %xmm0
jne 0x21fb8
jp 0x21fb8
movq -0x38(%rbp), %rax
movl $0x2, (%rax)
jmp 0x224df
movsd -0x258(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
sete %al
setnp %cl
andb %cl, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x25c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x22034
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd 0x10790(%rip), %xmm1    # 0x32780
ucomisd %xmm1, %xmm0
jne 0x22005
jp 0x22005
xorps %xmm0, %xmm0
movsd %xmm0, -0x320(%rbp)
jmp 0x22022
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
subsd -0x250(%rbp), %xmm0
callq 0x2560
movsd %xmm0, -0x320(%rbp)
movsd -0x320(%rbp), %xmm0
movsd %xmm0, -0x328(%rbp)
jmp 0x22086
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x22054
jp 0x22054
xorps %xmm0, %xmm0
movsd %xmm0, -0x330(%rbp)
jmp 0x22076
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
callq 0x2250
subsd -0x250(%rbp), %xmm0
callq 0x2560
movsd %xmm0, -0x330(%rbp)
movsd -0x330(%rbp), %xmm0
movsd %xmm0, -0x328(%rbp)
movsd -0x328(%rbp), %xmm0
movsd %xmm0, -0x268(%rbp)
movsd -0x10(%rbp), %xmm0
movsd -0x240(%rbp), %xmm1
movsd -0x248(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
callq 0x27a30
movsd %xmm0, -0x270(%rbp)
movsd -0x230(%rbp), %xmm1
mulsd -0x230(%rbp), %xmm1
movsd 0x10763(%rip), %xmm0    # 0x32838
divsd %xmm1, %xmm0
movsd %xmm0, -0x278(%rbp)
movsd 0x1074f(%rip), %xmm0    # 0x32838
mulsd -0x238(%rbp), %xmm0
mulsd -0x238(%rbp), %xmm0
movsd %xmm0, -0x280(%rbp)
movsd -0x270(%rbp), %xmm0
movsd %xmm0, -0x288(%rbp)
movsd -0x288(%rbp), %xmm0
movsd %xmm0, -0x290(%rbp)
movsd 0xf4cf(%rip), %xmm0     # 0x315f8
movsd %xmm0, -0x298(%rbp)
movsd 0xf4bf(%rip), %xmm0     # 0x315f8
movsd %xmm0, -0x2a0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2a8(%rbp)
movl $0x1, -0x2ac(%rbp)
cmpl $0x1e, -0x2ac(%rbp)
jg 0x22414
movsd -0x10(%rbp), %xmm0
addsd -0x2a8(%rbp), %xmm0
movsd %xmm0, -0x2b8(%rbp)
movsd -0x2b8(%rbp), %xmm0
movsd 0xf470(%rip), %xmm1     # 0x315f8
addsd -0x2b8(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x288(%rbp), %xmm2
movsd -0x240(%rbp), %xmm1
addsd -0x2b8(%rbp), %xmm1
movsd 0xf444(%rip), %xmm3     # 0x315f8
addsd %xmm3, %xmm1
mulsd -0x298(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
mulsd -0x278(%rbp), %xmm0
movsd %xmm0, -0x288(%rbp)
movsd 0xefe0(%rip), %xmm0     # 0x311c0
addsd -0x2a8(%rbp), %xmm0
movsd %xmm0, -0x2a8(%rbp)
movsd -0x280(%rbp), %xmm0
mulsd -0x298(%rbp), %xmm0
movsd %xmm0, -0x298(%rbp)
movsd -0x2a8(%rbp), %xmm1
movsd 0xf3e0(%rip), %xmm0     # 0x315f8
addsd -0x2a8(%rbp), %xmm0
mulsd %xmm0, %xmm1
movsd -0x2a0(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x2a0(%rbp)
movl -0x2ac(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2bc(%rbp)
movsd -0x2a0(%rbp), %xmm0
movslq -0x2bc(%rbp), %rax
movsd %xmm0, -0x130(%rbp,%rax,8)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2c8(%rbp)
cmpl $0x1, -0x2ac(%rbp)
jle 0x2231b
movsd -0x10(%rbp), %xmm0
cvtsi2sdl -0x2ac(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x2d0(%rbp)
movl $0x1, -0x2d4(%rbp)
movl -0x2d4(%rbp), %eax
cmpl -0x2bc(%rbp), %eax
jg 0x22319
movsd -0x2d0(%rbp), %xmm0
movl -0x2d4(%rbp), %eax
subl $0x1, %eax
cltq
mulsd -0x130(%rbp,%rax,8), %xmm0
movl -0x2bc(%rbp), %eax
subl -0x2d4(%rbp), %eax
cltq
movsd -0x220(%rbp,%rax,8), %xmm2
movsd -0x2c8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x2c8(%rbp)
movsd -0x10(%rbp), %xmm0
addsd -0x2d0(%rbp), %xmm0
movsd %xmm0, -0x2d0(%rbp)
movl -0x2d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d4(%rbp)
jmp 0x2229a
jmp 0x2231b
movsd -0x228(%rbp), %xmm0
movsd -0x2a0(%rbp), %xmm2
movsd -0x2c8(%rbp), %xmm1
cvtsi2sdl -0x2ac(%rbp), %xmm3
divsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x2bc(%rbp), %rax
movsd %xmm0, -0x220(%rbp,%rax,8)
movslq -0x2bc(%rbp), %rax
movsd -0x220(%rbp,%rax,8), %xmm0
mulsd -0x288(%rbp), %xmm0
movsd %xmm0, -0x2e0(%rbp)
movsd -0x2e0(%rbp), %xmm0
addsd -0x290(%rbp), %xmm0
movsd %xmm0, -0x290(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x290(%rbp), %xmm0
jb 0x223ab
movq -0x38(%rbp), %rax
movl $0x3, (%rax)
jmp 0x224df
movsd -0x2e0(%rbp), %xmm1
movaps 0x10736(%rip), %xmm0    # 0x32af0
pand %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
movsd -0x290(%rbp), %xmm2
addsd -0x268(%rbp), %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x223e9
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
jmp 0x22414
cmpl $0x1e, -0x2ac(%rbp)
jne 0x223fc
movq -0x38(%rbp), %rax
movl $0x4, (%rax)
jmp 0x223fe
jmp 0x22400
movl -0x2ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ac(%rbp)
jmp 0x22156
cmpl $0x0, -0x3c(%rbp)
je 0x2246d
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x338(%rbp)
movsd -0x250(%rbp), %xmm0
movsd %xmm0, -0x340(%rbp)
movsd -0x290(%rbp), %xmm0
callq 0x2250
movsd -0x340(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x338(%rbp), %xmm0
addsd %xmm2, %xmm1
callq 0x280a0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x224dd
cmpl $0x0, -0x25c(%rbp)
je 0x224b1
movsd -0x250(%rbp), %xmm0
movsd %xmm0, -0x350(%rbp)
movsd -0x290(%rbp), %xmm0
callq 0x2250
movaps %xmm0, %xmm1
movsd -0x350(%rbp), %xmm0
addsd %xmm1, %xmm0
callq 0x2560
movsd %xmm0, -0x348(%rbp)
jmp 0x224c9
movsd -0x258(%rbp), %xmm0
mulsd -0x290(%rbp), %xmm0
movsd %xmm0, -0x348(%rbp)
movsd -0x348(%rbp), %xmm0
movq -0x28(%rbp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x224df
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
nopl (%rax,%rax)