use crate::sexprec::{SEXP, SEXPTYPE};
use ::libc;
extern "C" {
#[no_mangle]
fn memset(_: *mut libc::c_void, _: libc::c_int, _: libc::c_ulong) -> *mut libc::c_void;
#[no_mangle]
fn R_alloc(_: size_t, _: libc::c_int) -> *mut libc::c_char;
#[no_mangle]
fn gamma(_: libc::c_double) -> libc::c_double;
#[no_mangle]
fn LENGTH(x: SEXP) -> 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 ptrdiff_t = libc::c_long;
pub type R_xlen_t = ptrdiff_t;
unsafe extern "C" fn ckendall(
mut k: libc::c_int,
mut n: libc::c_int,
mut w: *mut *mut libc::c_double,
) -> libc::c_double {
let mut i: libc::c_int = 0;
let mut u: libc::c_int = 0;
let mut s: libc::c_double = 0.;
u = n * (n - 1 as libc::c_int) / 2 as libc::c_int;
if k < 0 as libc::c_int || k > u {
return 0 as libc::c_int as libc::c_double;
}
if (*w.offset(n as isize)).is_null() {
let ref mut fresh0 = *w.offset(n as isize);
*fresh0 = R_alloc(
(u + 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;
memset(
*w.offset(n as isize) as *mut libc::c_void,
'\u{0}' as i32,
(::std::mem::size_of::<libc::c_double>() as libc::c_ulong)
.wrapping_mul((u + 1 as libc::c_int) as libc::c_ulong),
);
i = 0 as libc::c_int;
while i <= u {
*(*w.offset(n as isize)).offset(i as isize) = -(1 as libc::c_int) as libc::c_double;
i += 1
}
}
if *(*w.offset(n as isize)).offset(k as isize) < 0 as libc::c_int as libc::c_double {
if n == 1 as libc::c_int {
*(*w.offset(n as isize)).offset(k as isize) =
(k == 0 as libc::c_int) as libc::c_int as libc::c_double
} else {
s = 0 as libc::c_int as libc::c_double;
i = 0 as libc::c_int;
while i < n {
s += ckendall(k - i, n - 1 as libc::c_int, w);
i += 1
}
*(*w.offset(n as isize)).offset(k as isize) = s
}
}
return *(*w.offset(n as isize)).offset(k as isize);
}
unsafe extern "C" fn pkendall(
mut len: libc::c_int,
mut Q: *mut libc::c_double,
mut P: *mut libc::c_double,
mut n: libc::c_int,
) {
let mut i: libc::c_int = 0;
let mut j: libc::c_int = 0;
let mut p: libc::c_double = 0.;
let mut q: libc::c_double = 0.;
let mut w: *mut *mut libc::c_double = 0 as *mut *mut libc::c_double;
w = R_alloc(
(n + 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;
memset(
w as *mut libc::c_void,
'\u{0}' as i32,
(::std::mem::size_of::<*mut libc::c_double>() as libc::c_ulong)
.wrapping_mul((n + 1 as libc::c_int) as libc::c_ulong),
);
i = 0 as libc::c_int;
while i < len {
q = (*Q.offset(i as isize) + 1e-7f64).floor();
if q < 0 as libc::c_int as libc::c_double {
*P.offset(i as isize) = 0 as libc::c_int as libc::c_double
} else if q > (n * (n - 1 as libc::c_int) / 2 as libc::c_int) as libc::c_double {
*P.offset(i as isize) = 1 as libc::c_int as libc::c_double
} else {
p = 0 as libc::c_int as libc::c_double;
j = 0 as libc::c_int;
while j as libc::c_double <= q {
p += ckendall(j, n, w);
j += 1
}
*P.offset(i as isize) = p / gamma((n + 1 as libc::c_int) as libc::c_double)
}
i += 1
}
}
#[no_mangle]
pub unsafe extern "C" fn pKendall(mut q: SEXP, mut sn: SEXP) -> SEXP {
q = protect(coerceVector(q, 14 as libc::c_int as SEXPTYPE));
let mut len: libc::c_int = LENGTH(q);
let mut n: libc::c_int = asInteger(sn);
let mut p: SEXP = protect(allocVector(14 as libc::c_int as SEXPTYPE, len as R_xlen_t));
pkendall(len, REAL(q), REAL(p), n);
unprotect(2 as libc::c_int);
return p;
}