use crate::sexprec::{SEXP, SEXPTYPE};
use ::libc;
extern "C" {
#[no_mangle]
fn R_alloc(_: size_t, _: libc::c_int) -> *mut libc::c_char;
#[no_mangle]
fn dhyper(
_: libc::c_double,
_: libc::c_double,
_: libc::c_double,
_: libc::c_double,
_: libc::c_int,
) -> libc::c_double;
#[no_mangle]
fn imax2(_: libc::c_int, _: libc::c_int) -> libc::c_int;
#[no_mangle]
fn imin2(_: libc::c_int, _: libc::c_int) -> libc::c_int;
#[no_mangle]
fn REAL(x: SEXP) -> *mut libc::c_double;
#[no_mangle]
fn coerceVector(_: SEXP, _: SEXPTYPE) -> SEXP;
#[no_mangle]
fn asInteger(x: SEXP) -> libc::c_int;
#[no_mangle]
fn allocVector(_: SEXPTYPE, _: R_xlen_t) -> SEXP;
#[no_mangle]
fn protect(_: SEXP) -> SEXP;
#[no_mangle]
fn unprotect(_: libc::c_int);
}
pub type size_t = libc::c_ulong;
pub type C2RustUnnamed = libc::c_uint;
pub const TRUE: C2RustUnnamed = 1;
pub const FALSE: C2RustUnnamed = 0;
pub type ptrdiff_t = libc::c_long;
pub type R_xlen_t = ptrdiff_t;
unsafe extern "C" fn int_d2x2xk(
mut K: libc::c_int,
mut m: *mut libc::c_double,
mut n: *mut libc::c_double,
mut t: *mut libc::c_double,
mut d: *mut libc::c_double,
) {
let mut i: libc::c_int = 0;
let mut j: libc::c_int = 0;
let mut l: libc::c_int = 0;
let mut w: libc::c_int = 0;
let mut y: libc::c_int = 0;
let mut z: libc::c_int = 0;
let mut u: libc::c_double = 0.;
let mut c: *mut *mut libc::c_double = 0 as *mut *mut libc::c_double;
c = R_alloc(
(K + 1 as libc::c_int) as size_t,
::std::mem::size_of::<*mut libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut *mut libc::c_double;
z = 0 as libc::c_int;
y = z;
l = y;
let ref mut fresh0 = *c.offset(0 as libc::c_int as isize);
*fresh0 = R_alloc(
1 as libc::c_int as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
*(*c.offset(0 as libc::c_int as isize)).offset(0 as libc::c_int as isize) =
1 as libc::c_int as libc::c_double;
i = 0 as libc::c_int;
while i < K {
y = imax2(0 as libc::c_int, (*t - *n) as libc::c_int);
z = imin2(*m as libc::c_int, *t as libc::c_int);
let ref mut fresh1 = *c.offset((i + 1 as libc::c_int) as isize);
*fresh1 = R_alloc(
(l + z - y + 1 as libc::c_int) as size_t,
::std::mem::size_of::<libc::c_double>() as libc::c_ulong as libc::c_int,
) as *mut libc::c_double;
j = 0 as libc::c_int;
while j <= l + z - y {
*(*c.offset((i + 1 as libc::c_int) as isize)).offset(j as isize) =
0 as libc::c_int as libc::c_double;
j += 1
}
j = 0 as libc::c_int;
while j <= z - y {
u = dhyper((j + y) as libc::c_double, *m, *n, *t, FALSE as libc::c_int);
w = 0 as libc::c_int;
while w <= l {
*(*c.offset((i + 1 as libc::c_int) as isize)).offset((w + j) as isize) +=
*(*c.offset(i as isize)).offset(w as isize) * u;
w += 1
}
j += 1
}
l = l + z - y;
m = m.offset(1);
n = n.offset(1);
t = t.offset(1);
i += 1
}
u = 0 as libc::c_int as libc::c_double;
j = 0 as libc::c_int;
while j <= l {
u += *(*c.offset(K as isize)).offset(j as isize);
j += 1
}
j = 0 as libc::c_int;
while j <= l {
*d.offset(j as isize) = *(*c.offset(K as isize)).offset(j as isize) / u;
j += 1
}
}
#[no_mangle]
pub unsafe extern "C" fn d2x2xk(
mut sK: SEXP,
mut m: SEXP,
mut n: SEXP,
mut t: SEXP,
mut srn: SEXP,
) -> SEXP {
let mut K: libc::c_int = asInteger(sK);
let mut rn: libc::c_int = asInteger(srn);
m = protect(coerceVector(m, 14 as libc::c_int as SEXPTYPE));
n = protect(coerceVector(n, 14 as libc::c_int as SEXPTYPE));
t = protect(coerceVector(t, 14 as libc::c_int as SEXPTYPE));
let mut ans: SEXP = protect(allocVector(14 as libc::c_int as SEXPTYPE, rn as R_xlen_t));
int_d2x2xk(K, REAL(m), REAL(n), REAL(t), REAL(ans));
unprotect(4 as libc::c_int);
return ans;
}