#include <math.h>
#include <ch.h>

EXPORTCH double erf_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    double x;
    double retval;

    Ch_VaStart(interp, ap, varg);
    x = Ch_VaArg(interp, ap, double);
    retval = erf(x);
    Ch_VaEnd(interp, ap);
    return retval;
}

EXPORTCH double hypot_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    double x;
    double y;
    double retval;

    Ch_VaStart(interp, ap, varg);
    x = Ch_VaArg(interp, ap, double);
    y = Ch_VaArg(interp, ap, double);
    retval = hypot(x, y);
    Ch_VaEnd(interp, ap);
    return retval;
}

EXPORTCH void func3_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    int n;
    struct tag *retval;

    Ch_VaStart(interp, ap, varg);
    retval = Ch_VaArg(interp, ap, struct tag *);
    n = Ch_VaArg(interp, ap, int);
    *retval = func3(n);
    Ch_VaEnd(interp, ap);
}

EXPORTCH void func4_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    int n;
    structdef_t *retval;

    Ch_VaStart(interp, ap, varg);
    retval = Ch_VaArg(interp, ap, structdef_t *);
    n = Ch_VaArg(interp, ap, int);
    *retval = func4(n);
    Ch_VaEnd(interp, ap);
}

EXPORTCH int func5_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    int n;
    ChVaList_t ap_ch;
    void *ap_c;
    void *memhandle;
    int retval;

    Ch_VaStart(interp, ap, varg);
    n = Ch_VaArg(interp, ap, int);
    ap_ch = Ch_VaArg(interp, ap, ChVaList_t);
    ap_c = Ch_VaVarArgsCreate(interp, ap_ch, &memhandle);
    retval = vfunc5(n, ap_c);
    Ch_VaVarArgsDelete(interp, memhandle);
    Ch_VaEnd(interp, ap);
    return retval;
}

EXPORTCH int vfunc5_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    int n;
    ChVaList_t ap_;
    void *ap_c;
    void *memhandle;
    int retval;

    Ch_VaStart(interp, ap, varg);
    n = Ch_VaArg(interp, ap, int);
    ap_ = Ch_VaArg(interp, ap, ChVaList_t);
    ap_c = Ch_VaVarArgsCreate(interp, ap_, &memhandle);
    retval = vfunc5(n, ap_c);
    Ch_VaVarArgsDelete(interp, memhandle);
    Ch_VaEnd(interp, ap);
    return retval;
}
