use crate::sexprec::{SEXP, SEXPTYPE};
use ::libc;
extern "C" {
#[no_mangle]
static mut R_NaInt: libc::c_int;
#[no_mangle]
fn XLENGTH(x: SEXP) -> R_xlen_t;
#[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 asReal(x: SEXP) -> libc::c_double;
#[no_mangle]
fn error(_: *const libc::c_char, _: ...) -> !;
#[no_mangle]
fn allocVector(_: SEXPTYPE, _: R_xlen_t) -> SEXP;
#[no_mangle]
fn protect(_: SEXP) -> SEXP;
#[no_mangle]
fn unprotect(_: libc::c_int);
}
pub type ptrdiff_t = libc::c_long;
pub type R_xlen_t = ptrdiff_t;
#[no_mangle]
pub unsafe extern "C" fn BinDist(
mut sx: SEXP,
mut sw: SEXP,
mut slo: SEXP,
mut shi: SEXP,
mut sn: SEXP,
) -> SEXP {
sx = coerceVector(sx, 14 as libc::c_int as SEXPTYPE);
protect(sx);
sw = coerceVector(sw, 14 as libc::c_int as SEXPTYPE);
protect(sw);
let mut n: libc::c_int = asInteger(sn);
if n == R_NaInt || n <= 0 as libc::c_int {
error(
b"invalid \'%s\' argument\x00" as *const u8 as *const libc::c_char,
b"n\x00" as *const u8 as *const libc::c_char,
);
}
let mut ans: SEXP = allocVector(
14 as libc::c_int as SEXPTYPE,
(2 as libc::c_int * n) as R_xlen_t,
);
protect(ans);
let mut xlo: libc::c_double = asReal(slo);
let mut xhi: libc::c_double = asReal(shi);
let mut x: *mut libc::c_double = REAL(sx);
let mut w: *mut libc::c_double = REAL(sw);
let mut y: *mut libc::c_double = REAL(ans);
let mut ixmin: libc::c_int = 0 as libc::c_int;
let mut ixmax: libc::c_int = n - 2 as libc::c_int;
let mut xdelta: libc::c_double = (xhi - xlo) / (n - 1 as libc::c_int) as libc::c_double;
let mut i: libc::c_int = 0 as libc::c_int;
while i < 2 as libc::c_int * n {
*y.offset(i as isize) = 0 as libc::c_int as libc::c_double;
i += 1
}
let mut i_0: R_xlen_t = 0 as libc::c_int as R_xlen_t;
while i_0 < XLENGTH(sx) {
if (*x.offset(i_0 as isize)).is_finite() as i32 != 0 {
let mut xpos: libc::c_double = (*x.offset(i_0 as isize) - xlo) / xdelta;
let mut ix: libc::c_int = xpos.floor() as libc::c_int;
let mut fx: libc::c_double = xpos - ix as libc::c_double;
let mut wi: libc::c_double = *w.offset(i_0 as isize);
if ixmin <= ix && ix <= ixmax {
*y.offset(ix as isize) += (1 as libc::c_int as libc::c_double - fx) * wi;
*y.offset((ix + 1 as libc::c_int) as isize) += fx * wi
} else if ix == -(1 as libc::c_int) {
*y.offset(0 as libc::c_int as isize) += fx * wi
} else if ix == ixmax + 1 as libc::c_int {
*y.offset(ix as isize) += (1 as libc::c_int as libc::c_double - fx) * wi
}
}
i_0 += 1
}
unprotect(3 as libc::c_int);
return ans;
}