use crate::sexprec::{SEXP, SEXPREC, SEXPTYPE};
use ::libc;
extern "C" {
#[no_mangle]
static mut R_PosInf: libc::c_double;
#[no_mangle]
fn LENGTH(x: SEXP) -> libc::c_int;
#[no_mangle]
fn INTEGER(x: SEXP) -> *mut libc::c_int;
#[no_mangle]
fn REAL(x: SEXP) -> *mut libc::c_double;
#[no_mangle]
fn SET_VECTOR_ELT(x: SEXP, i: R_xlen_t, v: SEXP) -> SEXP;
#[no_mangle]
fn lengthgets(_: SEXP, _: R_len_t) -> SEXP;
#[no_mangle]
fn allocVector(_: SEXPTYPE, _: R_xlen_t) -> SEXP;
#[no_mangle]
fn mkNamed(_: SEXPTYPE, _: *mut *const libc::c_char) -> SEXP;
#[no_mangle]
fn protect(_: SEXP) -> SEXP;
#[no_mangle]
fn unprotect(_: libc::c_int);
}
pub type ptrdiff_t = libc::c_long;
pub type R_len_t = libc::c_int;
pub type R_xlen_t = ptrdiff_t;
#[no_mangle]
pub unsafe extern "C" fn isoreg(mut y: SEXP) -> SEXP {
let mut n: libc::c_int = LENGTH(y);
let mut i: libc::c_int = 0;
let mut ip: libc::c_int = 0;
let mut known: libc::c_int = 0;
let mut n_ip: libc::c_int = 0;
let mut tmp: libc::c_double = 0.;
let mut slope: libc::c_double = 0.;
let mut yc: SEXP = 0 as *mut SEXPREC;
let mut yf: SEXP = 0 as *mut SEXPREC;
let mut iKnots: SEXP = 0 as *mut SEXPREC;
let mut ans: SEXP = 0 as *mut SEXPREC;
let mut anms: [*const libc::c_char; 5] = [
b"y\x00" as *const u8 as *const libc::c_char,
b"yc\x00" as *const u8 as *const libc::c_char,
b"yf\x00" as *const u8 as *const libc::c_char,
b"iKnots\x00" as *const u8 as *const libc::c_char,
b"\x00" as *const u8 as *const libc::c_char,
];
ans = mkNamed(19 as libc::c_int as SEXPTYPE, anms.as_mut_ptr());
protect(ans);
SET_VECTOR_ELT(ans, 0 as libc::c_int as R_xlen_t, y);
yc = allocVector(
14 as libc::c_int as SEXPTYPE,
(n + 1 as libc::c_int) as R_xlen_t,
);
SET_VECTOR_ELT(ans, 1 as libc::c_int as R_xlen_t, yc);
yf = allocVector(14 as libc::c_int as SEXPTYPE, n as R_xlen_t);
SET_VECTOR_ELT(ans, 2 as libc::c_int as R_xlen_t, yf);
iKnots = allocVector(13 as libc::c_int as SEXPTYPE, n as R_xlen_t);
SET_VECTOR_ELT(ans, 3 as libc::c_int as R_xlen_t, iKnots);
if n == 0 as libc::c_int {
return ans;
}
*REAL(yc).offset(0 as libc::c_int as isize) = 0.0f64;
tmp = 0.0f64;
i = 0 as libc::c_int;
while i < n {
tmp += *REAL(y).offset(i as isize);
*REAL(yc).offset((i + 1 as libc::c_int) as isize) = tmp;
i += 1
}
known = 0 as libc::c_int;
ip = 0 as libc::c_int;
n_ip = 0 as libc::c_int;
loop {
slope = R_PosInf;
i = known + 1 as libc::c_int;
while i <= n {
tmp = (*REAL(yc).offset(i as isize) - *REAL(yc).offset(known as isize))
/ (i - known) as libc::c_double;
if tmp < slope {
slope = tmp;
ip = i
}
i += 1
}
let fresh0 = n_ip;
n_ip = n_ip + 1;
*INTEGER(iKnots).offset(fresh0 as isize) = ip;
i = known;
while i < ip {
*REAL(yf).offset(i as isize) = (*REAL(yc).offset(ip as isize)
- *REAL(yc).offset(known as isize))
/ (ip - known) as libc::c_double;
i += 1
}
known = ip;
if !(known < n) {
break;
}
}
if n_ip < n {
SET_VECTOR_ELT(ans, 3 as libc::c_int as R_xlen_t, lengthgets(iKnots, n_ip));
}
unprotect(1 as libc::c_int);
return ans;
}