#include <time.h>
#include <stdlib.h>
unsigned long int __timeId=0;
char *genRandomChar(void);
void writeHeader(const char *md5, const char *extra) {
sAppend(&sbOut, "#define _getRxSolve_ _rx%s%s%ld__getRxSolve__%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _evalUdf _rx%s%s%ld__evalUdf_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _solveData _rx%s%s%ld__solveData_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _assign_ptr _rx%s%s%ld__assign_ptr_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxRmModelLib _rx%s%s%ld__rxRmModelLib_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxGetModelLib _rx%s%s%ld__rxGetModelLib_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _old_c _rx%s%s%ld__old_c_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _ptrid _rx%s%s%ld__ptrid_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxIsCurrentC _rx%s%s%ld__rxIsCurrentC_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _sumPS _rx%s%s%ld__sumPS_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _prodPS _rx%s%s%ld__prodPS_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _prodType _rx%s%s%ld__prodType_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _sumType _rx%s%s%ld__sumType_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _update_par_ptr _rx%s%s%ld__update_par_ptr_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _getParCov _rx%s%s%ld__getParCov_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _setThreadInd _rx%s%s%ld__setThreadInd_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxPushDose _rx%s%s%ld__rxPushDose_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxode2_rxAssignPtr _rx%s%s%ld__rxode2_rxAssignPtr_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxQr _rx%s%s%ld__rxQr_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _compareFactorVal _rx%s%s%ld__compareFactorVal_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _compareFactorInt _rx%s%s%ld__compareFactorInt_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _sum _rx%s%s%ld__sum_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _udf _rx%s%s%ld__udf_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _sign _rx%s%s%ld__sign_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _prod _rx%s%s%ld__prod_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _max _rx%s%s%ld__max_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _min _rx%s%s%ld__min_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _transit4P _rx%s%s%ld__transit4P_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _transit3P _rx%s%s%ld__transit3P_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxDelay _rx%s%s%ld__rxDelay_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxDelayD _rx%s%s%ld__rxDelayD_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxDelayD2 _rx%s%s%ld__rxDelayD2_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxDelayD3 _rx%s%s%ld__rxDelayD3_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxPast _rx%s%s%ld__rxPast_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _assignFuns0 _rx%s%s%ld__assignFuns0_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _assignFuns _rx%s%s%ld__assignFuns_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define _rxord _rx%s%s%ld__rxord_%s\n", extra, md5, __timeId++, genRandomChar());
sAppend(&sbOut, "#define __assignFuns2 _rx%s%s%ld___assignFuns2_%s\n", extra, md5, __timeId++, genRandomChar());
}
void writeBody0(void) {
sAppendN(&sbOut, "_getRxSolve_t _getRxSolve_;\n", 28);
sAppendN(&sbOut, "_simfun simeps;\n", 16);
sAppendN(&sbOut, "_simfun simeta;\n", 16);
sAppendN(&sbOut, "_udf_type _evalUdf=NULL;\n", 25);
sAppendN(&sbOut, "rx_solve *_solveData=NULL;\n", 27);
sAppendN(&sbOut, "rxode2_assign_ptr _assign_ptr=NULL;\n", 36);
sAppendN(&sbOut, "_rxRmModelLibType _rxRmModelLib=NULL;\n", 38);
sAppendN(&sbOut, "_rxGetModelLibType _rxGetModelLib=NULL;\n", 40);
sAppendN(&sbOut, "rxode2_ode_solver_old_c _old_c=NULL;\n", 37);
sAppendN(&sbOut, "rxode2_fn0i _ptrid=NULL;\n", 25);
sAppendN(&sbOut, "_rxIsCurrentC_type _rxIsCurrentC=NULL;\n", 39);
sAppendN(&sbOut, "_rxSumType _sumPS=NULL;\n", 24);
sAppendN(&sbOut, "_rxProdType _prodPS=NULL;\n", 26);
sAppendN(&sbOut, "rxode2_fn0i _prodType=NULL;\n", 28);
sAppendN(&sbOut, "rxode2_fn0i _sumType=NULL;\n", 27);
sAppendN(&sbOut, "_update_par_ptr_p _update_par_ptr=NULL;\n", 40);
sAppendN(&sbOut, "_getParCov_p _getParCov=NULL;\n", 30);
sAppendN(&sbOut, "_setThreadInd_t _setThreadInd=NULL;\n", 36);
sAppendN(&sbOut, "_rxPushDose_t _rxPushDose=NULL;\n", 32);
sAppendN(&sbOut, "linCmtA_p linCmtA;\n", 19);
sAppendN(&sbOut, "linCmtB_p linCmtB;\n", 19);
sAppendN(&sbOut, "_rx_asgn _rxode2_rxAssignPtr=NULL;\n", 35);
sAppendN(&sbOut, "_rx_asgn _rxQr=NULL;\n", 21);
sAppendN(&sbOut, "rxode2_fn phi;\n", 15);
sAppendN(&sbOut, "rxode2_fn ReLU;\n", 16);
sAppendN(&sbOut, "rxode2_fn dReLU;\n", 17);
sAppendN(&sbOut, "rxode2_fn GELU;\n", 16);
sAppendN(&sbOut, "rxode2_fn dGELU;\n", 17);
sAppendN(&sbOut, "rxode2_fn d2GELU;\n", 18);
sAppendN(&sbOut, "rxode2_fn d3GELU;\n", 18);
sAppendN(&sbOut, "rxode2_fn d4GELU;\n", 18);
sAppendN(&sbOut, "rxode2_fn softplus;\n", 20);
sAppendN(&sbOut, "rxode2_fn dsoftplus;\n", 21);
sAppendN(&sbOut, "rxode2_fn d2softplus;\n", 22);
sAppendN(&sbOut, "rxode2_fn d3softplus;\n", 22);
sAppendN(&sbOut, "rxode2_fn d4softplus;\n", 22);
sAppendN(&sbOut, "rxode2_fn SELU;\n", 16);
sAppendN(&sbOut, "rxode2_fn dSELU;\n", 17);
sAppendN(&sbOut, "rxode2_fn lReLU;\n", 17);
sAppendN(&sbOut, "rxode2_fn dlReLU;\n", 18);
sAppendN(&sbOut, "rxode2_fn Swish;\n", 17);
sAppendN(&sbOut, "rxode2_fn dSwish;\n", 18);
sAppendN(&sbOut, "rxode2_fn2 PReLU;\n", 18);
sAppendN(&sbOut, "rxode2_fn2 dPReLU;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 dPReLUa;\n", 20);
sAppendN(&sbOut, "rxode2_fn2 dPReLUa1;\n", 21);
sAppendN(&sbOut, "rxode2_fn2 ELU;\n", 16);
sAppendN(&sbOut, "rxode2_fn2 dELU;\n", 17);
sAppendN(&sbOut, "rxode2_fn2 d2ELU;\n", 18);
sAppendN(&sbOut, "rxode2_fn2 d2aELU;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 dELUa;\n", 18);
sAppendN(&sbOut, "rxode2_fn2 d2ELUa;\n", 19);
sAppendN(&sbOut, "rxode2_fn3 logit;\n", 18);
sAppendN(&sbOut, "rxode2_fn3 expit;\n", 18);
sAppendN(&sbOut, "rxode2_fn3 probitInv;\n", 22);
sAppendN(&sbOut, "rxode2_fn3 probit;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 gammap;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 gammaq;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 lowergamma;\n", 23);
sAppendN(&sbOut, "rxode2_fn2 uppergamma;\n", 23);
sAppendN(&sbOut, "rxode2_fn2 gammapInv;\n", 22);
sAppendN(&sbOut, "rxode2_fn2 gammapDer;\n", 22);
sAppendN(&sbOut, "rxode2_fn2 gammapInva;\n", 23);
sAppendN(&sbOut, "rxode2_fn2 gammaqInv;\n", 22);
sAppendN(&sbOut, "rxode2_fn2 gammaqInva;\n", 23);
sAppendN(&sbOut, "rxode2_fn2 rxnorm;\n", 19);
sAppendN(&sbOut, "rxode2i_rxbinom rxbinom;\n", 25);
sAppendN(&sbOut, "rxode2i_rxbinom rxnbinom;\n", 26);
sAppendN(&sbOut, "rxode2i_rxbinom rxnbinomMu;\n", 28);
sAppendN(&sbOut, "rxode2_fn2 rxcauchy;\n", 21);
sAppendN(&sbOut, "rxode2_fn rxchisq;\n", 19);
sAppendN(&sbOut, "rxode2_fn rxexp;\n", 17);
sAppendN(&sbOut, "rxode2_fn2 rxf;\n", 16);
sAppendN(&sbOut, "rxode2_ifn rxgeom;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 rxgamma;\n", 20);
sAppendN(&sbOut, "rxode2_fn2 rxbeta;\n", 19);
sAppendN(&sbOut, "rxode2_ifn rxpois;\n", 19);
sAppendN(&sbOut, "rxode2_fn rxt_;\n", 16);
sAppendN(&sbOut, "rxode2_fn2 rxunif;\n", 19);
sAppendN(&sbOut, "rxode2_fn2 rxweibull;\n", 22);
sAppendN(&sbOut, "rxode2i2_fn2 rinorm;\n", 21);
sAppendN(&sbOut, "rxode2i2_ribinom ribinom;\n", 26);
sAppendN(&sbOut, "rxode2i2_ribinom rinbinom;\n", 27);
sAppendN(&sbOut, "rxode2i2_ribinom rinbinomMu;\n", 29);
sAppendN(&sbOut, "rxode2i2_fn2 ricauchy;\n", 23);
sAppendN(&sbOut, "rxode2i2_fn richisq;\n", 21);
sAppendN(&sbOut, "rxode2i2_fn riexp;\n", 19);
sAppendN(&sbOut, "rxode2i2_fn2 rif;\n", 18);
sAppendN(&sbOut, "rxode2i2_ifn rigeom;\n", 21);
sAppendN(&sbOut, "rxode2i2_fn2 rigamma;\n", 22);
sAppendN(&sbOut, "rxode2i2_fn2 ribeta;\n", 21);
sAppendN(&sbOut, "rxode2i2_ifn ripois;\n", 21);
sAppendN(&sbOut, "rxode2i2_fn rit_;\n", 18);
sAppendN(&sbOut, "rxode2i2_fn2 riunif;\n", 21);
sAppendN(&sbOut, "rxode2i2_fn2 riweibull;\n", 24);
sAppendN(&sbOut, "rxode2_llikNormFun _llikNorm;\n", 30);
sAppendN(&sbOut, "rxode2_llikNormFun _llikNormDmean;\n", 35);
sAppendN(&sbOut, "rxode2_llikNormFun _llikNormDsd;\n", 33);
sAppendN(&sbOut, "rxode2_llikPoisFun _llikPois;\n", 30);
sAppendN(&sbOut, "rxode2_llikPoisFun _llikPoisDlambda;\n", 37);
sAppendN(&sbOut, "rxode2_llikBinomFun _llikBinom;\n", 32);
sAppendN(&sbOut, "rxode2_llikBinomFun _llikBinomDprob;\n", 37);
sAppendN(&sbOut, "rxode2_llikBinomFun _llikNbinom;\n", 33);
sAppendN(&sbOut, "rxode2_llikBinomFun _llikNbinomDprob;\n", 38);
sAppendN(&sbOut, "rxode2_llikBinomFun _llikNbinomMu;\n", 35);
sAppendN(&sbOut, "rxode2_llikBinomFun _llikNbinomMuDmu;\n", 38);
sAppendN(&sbOut, "rxode2_llikBetaFun _llikBeta;\n", 30);
sAppendN(&sbOut, "rxode2_llikBetaFun _llikBetaDshape1;\n", 37);
sAppendN(&sbOut, "rxode2_llikBetaFun _llikBetaDshape2;\n", 37);
sAppendN(&sbOut, "rxode2_llikTFun _llikT;\n", 24);
sAppendN(&sbOut, "rxode2_llikTFun _llikTDdf;\n", 27);
sAppendN(&sbOut, "rxode2_llikTFun _llikTDmean;\n", 29);
sAppendN(&sbOut, "rxode2_llikTFun _llikTDsd;\n", 27);
sAppendN(&sbOut, "rxode2_llikChisqFun _llikChisq;\n", 32);
sAppendN(&sbOut, "rxode2_llikChisqFun _llikChisqDdf;\n", 35);
sAppendN(&sbOut, "rxode2_llikExpFun _llikExp;\n", 28);
sAppendN(&sbOut, "rxode2_llikExpFun _llikExpDrate;\n", 33);
sAppendN(&sbOut, "rxode2_llikFFun _llikF;\n", 24);
sAppendN(&sbOut, "rxode2_llikFFun _llikFDdf1;\n", 28);
sAppendN(&sbOut, "rxode2_llikFFun _llikFDdf2;\n", 28);
sAppendN(&sbOut, "rxode2_llikGeomFun _llikGeom;\n", 30);
sAppendN(&sbOut, "rxode2_llikGeomFun _llikGeomDp;\n", 32);
sAppendN(&sbOut, "rxode2_llikUnifFun _llikUnif;\n", 30);
sAppendN(&sbOut, "rxode2_llikUnifFun _llikUnifDalpha;\n", 36);
sAppendN(&sbOut, "rxode2_llikUnifFun _llikUnifDbeta;\n", 35);
sAppendN(&sbOut, "rxode2_llikWeibullFun _llikWeibull;\n", 36);
sAppendN(&sbOut, "rxode2_llikWeibullFun _llikWeibullDshape;\n", 42);
sAppendN(&sbOut, "rxode2_llikWeibullFun _llikWeibullDscale;\n", 42);
sAppendN(&sbOut, "rxode2_llikGammaFun _llikGamma;\n", 32);
sAppendN(&sbOut, "rxode2_llikGammaFun _llikGammaDshape;\n", 38);
sAppendN(&sbOut, "rxode2_llikGammaFun _llikGammaDrate;\n", 37);
sAppendN(&sbOut, "rxode2_llikCauchyFun _llikCauchy;\n", 34);
sAppendN(&sbOut, "rxode2_llikCauchyFun _llikCauchyDlocation;\n", 43);
sAppendN(&sbOut, "rxode2_llikCauchyFun _llikCauchyDscale;\n", 40);
sAppendN(&sbOut, "rxode2_compareFactorVal_fn _compareFactorVal;\n", 46);
sAppendN(&sbOut, "rxode2_compareFactorInt_fn _compareFactorInt;\n", 46);
}
void writeBody1(void) {
sAppendN(&sbOut, "static double _prod(double *input, double *p, int type, int n, ...){\n", 69);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  for (unsigned int i = 0; i < n; i++){\n", 40);
sAppendN(&sbOut, "    input[i] = va_arg(valist, double);\n", 39);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_end(valist);\n", 18);
sAppendN(&sbOut, "  return _prodPS(input, p, n, type);\n", 37);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "static double _udf(const char *funName, double *input, int n, ...) {\n", 69);
sAppendN(&sbOut, "  if (n == -42) (Rf_error)(\"%s\", \"this has a ui user function that cannot be called directly\");\n", 96);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  for (unsigned int i = 0; i < n; i++){\n", 40);
sAppendN(&sbOut, "    input[i] = va_arg(valist, double);\n", 39);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_end(valist);\n", 18);
sAppendN(&sbOut, "  return _evalUdf(funName, n, input);\n", 38);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "static double _sum(double *input, double *pld, int m, int type, int n, ...){\n", 77);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  for (unsigned int i = 0; i < n; i++){\n", 40);
sAppendN(&sbOut, "    input[i] = va_arg(valist, double);\n", 39);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_end(valist);\n", 18);
sAppendN(&sbOut, "  double ret = _sumPS(input, n, pld, m, type);\n", 47);
sAppendN(&sbOut, "  if (type == 2 && m < 0){\n", 27);
sAppendN(&sbOut, "    for (int i = -m; i--;){\n", 28);
sAppendN(&sbOut, "      pld[i] = 0.0;\n", 20);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  return ret;\n", 14);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "static double _sign(unsigned int n, ...) {\n", 43);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  double s = 1;\n", 16);
sAppendN(&sbOut, "  for (unsigned int i = 0; i < n; i++) {\n", 41);
sAppendN(&sbOut, "    s = sign(va_arg(valist, double))*s;\n", 40);
sAppendN(&sbOut, "    if (s == 0){\n", 17);
sAppendN(&sbOut, "      break;\n", 13);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_end(valist);\n", 18);
sAppendN(&sbOut, "  return s;\n", 12);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "static double _mix(int _cSub, unsigned int n,  ...) {\n", 54);
sAppendN(&sbOut, "  rx_solving_options_ind* ind = &(_solveData->subjects[_cSub]);\n", 64);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  double ret = NA_REAL;\n", 24);
sAppendN(&sbOut, "  double p = 0.0;\n", 18);
sAppendN(&sbOut, "  // In the case of simulating, mixunif is sampled once per patient\n", 68);
sAppendN(&sbOut, "  double u = ind->mixunif;\n", 27);
sAppendN(&sbOut, "  if (u >= 1.0) {\n", 18);
sAppendN(&sbOut, "    // In this case, the mixest is assigned\n", 44);
sAppendN(&sbOut, "    ind->mixest = u;\n", 21);
sAppendN(&sbOut, "    u = 0;\n", 11);
sAppendN(&sbOut, "    int found = 0;\n", 19);
sAppendN(&sbOut, "    double v = 0.0;\n", 20);
sAppendN(&sbOut, "    va_start(valist, n);\n", 25);
sAppendN(&sbOut, "    for (unsigned int i = 0; i < n-1; i+= 2) {\n", 47);
sAppendN(&sbOut, "      v = va_arg(valist, double);\n", 34);
sAppendN(&sbOut, "      p += va_arg(valist, double);\n", 35);
sAppendN(&sbOut, "      if (found == 0) {\n", 24);
sAppendN(&sbOut, "        if (trunc(i*0.5 + 1) == trunc(ind->mixest)) {\n", 54);
sAppendN(&sbOut, "          // This is the mixture selected\n", 42);
sAppendN(&sbOut, "          ret = v;\n", 19);
sAppendN(&sbOut, "          found = 1;\n", 21);
sAppendN(&sbOut, "          ind->mixunif = u + (p - u)*0.5;\n", 42);
sAppendN(&sbOut, "        }\n", 10);
sAppendN(&sbOut, "        u = p;\n", 15);
sAppendN(&sbOut, "      }\n", 8);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "    v = va_arg(valist, double); // other possibility\n", 53);
sAppendN(&sbOut, "    if (found == 0) {\n", 22);
sAppendN(&sbOut, "      ind->mixunif = u + (1.0 - u)*0.5;\n", 40);
sAppendN(&sbOut, "      ret = v;\n", 15);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "    va_end(valist);\n", 20);
sAppendN(&sbOut, "    return ret;\n", 16);
sAppendN(&sbOut, "  } else {\n", 11);
sAppendN(&sbOut, "    int found = 0;\n", 19);
sAppendN(&sbOut, "    double v = 0.0;\n", 20);
sAppendN(&sbOut, "    va_start(valist, n);\n", 25);
sAppendN(&sbOut, "    for (unsigned int i = 0; i < n-1; i+= 2) {\n", 47);
sAppendN(&sbOut, "      v = va_arg(valist, double);\n", 34);
sAppendN(&sbOut, "      p += va_arg(valist, double);\n", 35);
sAppendN(&sbOut, "      if (found == 0) {\n", 24);
sAppendN(&sbOut, "        if (u < p) {\n", 21);
sAppendN(&sbOut, "          ret = v;\n", 19);
sAppendN(&sbOut, "          ind->mixest = i*0.5 + 1; // 1-based index\n", 52);
sAppendN(&sbOut, "          found = 1;\n", 21);
sAppendN(&sbOut, "        }\n", 10);
sAppendN(&sbOut, "      }\n", 8);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "    v = va_arg(valist, double); // other possibility\n", 53);
sAppendN(&sbOut, "    if (found == 0) {\n", 22);
sAppendN(&sbOut, "      ind->mixest = _solveData->mixnum;\n", 40);
sAppendN(&sbOut, "      ret = v;\n", 15);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "    va_end(valist);\n", 20);
sAppendN(&sbOut, "    return ret;\n", 16);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "double _rxord(int _cSub, unsigned int n,  ...) {\n", 49);
sAppendN(&sbOut, "  rx_solving_options_ind* ind = &(_solveData->subjects[_cSub]);\n", 64);
sAppendN(&sbOut, "  if (!ind->inLhs) {\n", 21);
sAppendN(&sbOut, "    return 1.0;\n", 16);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  double ret = 1.0;\n", 20);
sAppendN(&sbOut, "  double p = 0.0;\n", 18);
sAppendN(&sbOut, "  double u = rxunif(0.0, 1.0);\n", 31);
sAppendN(&sbOut, "  int found = 0;\n", 17);
sAppendN(&sbOut, "  for (unsigned int i = 0; i < n; i++) {\n", 41);
sAppendN(&sbOut, "    p += va_arg(valist, double);\n", 33);
sAppendN(&sbOut, "    if (!found) {\n", 18);
sAppendN(&sbOut, "      if (u < p) {\n", 19);
sAppendN(&sbOut, "        ret = (double)(i+1);\n", 29);
sAppendN(&sbOut, "        found = 1;\n", 19);
sAppendN(&sbOut, "      }\n", 8);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  if (!found) ret =(double)(n+1);\n", 34);
sAppendN(&sbOut, "  va_end(valist);\n", 18);
sAppendN(&sbOut, "  return ret;\n", 14);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "double _max(unsigned int n, ...) {\n", 35);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  double mx = NA_REAL;\n", 23);
sAppendN(&sbOut, "  double tmp = 0;\n", 18);
sAppendN(&sbOut, "  if (n >= 1){\n", 15);
sAppendN(&sbOut, "    mx = va_arg(valist, double);\n", 33);
sAppendN(&sbOut, "    for (unsigned int i = 1; i < n; i++) {\n", 43);
sAppendN(&sbOut, "      tmp = va_arg(valist, double);\n", 36);
sAppendN(&sbOut, "      if (tmp>mx) mx=tmp;\n", 26);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "    va_end(valist);\n", 20);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  return mx;\n", 13);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "double _min(unsigned int n, ...){\n", 34);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  double mn = NA_REAL;\n", 23);
sAppendN(&sbOut, "  double tmp = 0;\n", 18);
sAppendN(&sbOut, "  if (n >= 1){\n", 15);
sAppendN(&sbOut, "    mn = va_arg(valist, double);\n", 33);
sAppendN(&sbOut, "    for (unsigned int i = 1; i < n; i++){\n", 42);
sAppendN(&sbOut, "      tmp = va_arg(valist, double);\n", 36);
sAppendN(&sbOut, "      if (tmp<mn) mn=tmp;\n", 26);
sAppendN(&sbOut, "    }\n", 6);
sAppendN(&sbOut, "    va_end(valist);\n", 20);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  return mn;\n", 13);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "static void _obs(int _cSub, double _curTime, unsigned int n,  ...) {\n", 69);
sAppendN(&sbOut, "  rx_solving_options_ind* _ind = &(_solveData->subjects[_cSub]);\n", 65);
sAppendN(&sbOut, "  if (_ind->inLhs) {\n", 21);
sAppendN(&sbOut, "    return; // only push observations in ode solving\n", 53);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_list valist;\n", 18);
sAppendN(&sbOut, "  va_start(valist, n);\n", 23);
sAppendN(&sbOut, "  double _t;\n", 13);
sAppendN(&sbOut, "  for (unsigned int i = 0; i < n; i++) {\n", 41);
sAppendN(&sbOut, "    _t = va_arg(valist, double);\n", 33);
sAppendN(&sbOut, "    // push observations after time\n", 36);
sAppendN(&sbOut, "    _rxPushDose(_ind, _curTime,\n", 32);
sAppendN(&sbOut, "                _curTime + _t, 0, 0, 1, 0, 0, 0, 0, 0);\n", 56);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  va_end(valist);\n", 18);
sAppendN(&sbOut, "  return;\n", 10);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "double _transit4P(int cmt, double t, unsigned int id, double n, double mtt, double bio){\n", 89);
sAppendN(&sbOut, "  double nd = (double) n;\n", 26);
sAppendN(&sbOut, "  double ktr = (nd+1)/mtt;\n", 27);
sAppendN(&sbOut, "  double lktr = _safe_log(nd+1)-_safe_log(mtt);\n", 48);
sAppendN(&sbOut, "  double tlast = _solveData->subjects[id].tlastS[cmt];\n", 55);
sAppendN(&sbOut, "  double dose = _solveData->subjects[id].curDoseS[cmt];\n", 56);
sAppendN(&sbOut, "  if (ISNA(dose)) dose = 0.0;\n", 30);
sAppendN(&sbOut, "  if (ISNA(tlast)) tlast = 0.0;\n", 32);
sAppendN(&sbOut, "  double tad = (t-tlast);\n", 26);
sAppendN(&sbOut, "  return exp(_safe_log(bio*dose)+lktr+n*(lktr+_safe_log(tad))-ktr*(tad)-lgamma1p(nd));\n", 87);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "double _transit3P(int cmt, double t, unsigned int id, double n, double mtt){\n", 77);
sAppendN(&sbOut, "  double nd = (double) n;\n", 26);
sAppendN(&sbOut, "  double ktr = (nd+1)/mtt;\n", 27);
sAppendN(&sbOut, "  double lktr = _safe_log(nd+1)-_safe_log(mtt);\n", 48);
sAppendN(&sbOut, "  double tlast = _solveData->subjects[id].tlastS[cmt];\n", 55);
sAppendN(&sbOut, "  if (ISNA(tlast)) tlast = 0.0;\n", 32);
sAppendN(&sbOut, "  double tad = t-tlast;\n", 24);
sAppendN(&sbOut, "  double podo = _solveData->subjects[id].curDoseS[cmt];\n", 56);
sAppendN(&sbOut, "  if (ISNA(podo)) podo = 0.0;\n", 30);
sAppendN(&sbOut, "  return exp(_safe_log(podo)+lktr+n*(lktr+_safe_log(tad))-ktr*(tad)-lgamma1p(nd));\n", 83);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "// delay(state, T): value of ODE state `i` at time (t - T), used for delay\n", 75);
sAppendN(&sbOut, "// differential equations (Monolix delay() semantics).  Before the start of\n", 76);
sAppendN(&sbOut, "// integration the constant initial-condition history is returned.  Otherwise\n", 78);
sAppendN(&sbOut, "// the value is interpolated from the per-subject dense history recorded by the\n", 80);
sAppendN(&sbOut, "// solver, using the same 8th-order Dormand-Prince interpolant as dop853's\n", 75);
sAppendN(&sbOut, "// contd8(), so delayed states are obtained to the full accuracy of the solve.\n", 79);
sAppendN(&sbOut, "// forward declaration of the model's non-constant pre-history (past()); the\n", 77);
sAppendN(&sbOut, "// default (no past()) returns the constant initial condition.\n", 63);
sAppendN(&sbOut, "double _rxPast(int _cSub, int _cmt, double __t, double *__zzStateVar__);\n", 73);
sAppendN(&sbOut, "double _rxDelay(rx_solving_options_ind *_ind, int i, double t, double T) {\n", 75);
sAppendN(&sbOut, "  double td = t - T;\n", 21);
sAppendN(&sbOut, "  // Learn the smallest delay so the solver can cap its step size and never\n", 76);
sAppendN(&sbOut, "  // step over the delay (keeping the lagged time inside recorded history).\n", 76);
sAppendN(&sbOut, "  if (T > 0.0 && T < _ind->delayMinT) _ind->delayMinT = T;\n", 59);
sAppendN(&sbOut, "  if (!_ind->delayHistOn || _ind->delayHistN == 0 || td <= _ind->delayT0) {\n", 76);
sAppendN(&sbOut, "    return _rxPast(_ind->id, i, td, (double*)0);   // pre-history: user past() or constant IC\n", 94);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  int n = _ind->delayHistNeq;\n", 30);
sAppendN(&sbOut, "  int stride = _ind->delayHistStride;\n", 38);
sAppendN(&sbOut, "  double *hist = _ind->delayHist;\n", 34);
sAppendN(&sbOut, "  // History stores only the states delay() looks back on; map this state's ODE\n", 80);
sAppendN(&sbOut, "  // index to its compact history column.\n", 42);
sAppendN(&sbOut, "  int col = _solveData->op->delayCol[i];\n", 41);
sAppendN(&sbOut, "  if (col < 0) {\n", 17);
sAppendN(&sbOut, "    return _rxPast(_ind->id, i, td, (double*)0);   // defensive: past() or constant IC\n", 87);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  // Records are sorted by increasing step start time (xold) and cover\n", 71);
sAppendN(&sbOut, "  // contiguous intervals [xold, xold+h].  Find the largest xold <= td; that\n", 77);
sAppendN(&sbOut, "  // record's dense polynomial interpolates td (extrapolating slightly when td\n", 79);
sAppendN(&sbOut, "  // falls in the in-progress, not-yet-recorded step, i.e. a delay below the\n", 77);
sAppendN(&sbOut, "  // current step size).\n", 25);
sAppendN(&sbOut, "  // t_old, h and the per-record solver type are stored in the last three slots\n", 80);
sAppendN(&sbOut, "  // of every record, regardless of solver, so the bracketing search and the\n", 77);
sAppendN(&sbOut, "  // interpolation dispatch are uniform even when a solve mixes dop853 and ros4\n", 80);
sAppendN(&sbOut, "  // steps (the dense AutoSwitch composite).\n", 45);
sAppendN(&sbOut, "  int lo = 0, hi = _ind->delayHistN - 1;\n", 41);
sAppendN(&sbOut, "  while (lo < hi) {\n", 20);
sAppendN(&sbOut, "    int mid = (lo + hi + 1) / 2;\n", 33);
sAppendN(&sbOut, "    if (hist[(size_t) mid * stride + (stride - 3)] <= td) lo = mid; else hi = mid - 1;\n", 87);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  double *rec = hist + (size_t) lo * stride;\n", 45);
sAppendN(&sbOut, "  double xold = rec[stride - 3];\n", 33);
sAppendN(&sbOut, "  double h    = rec[stride - 2];\n", 33);
sAppendN(&sbOut, "  double s  = (td - xold) / h;\n", 31);
sAppendN(&sbOut, "  if (rec[stride - 1] == 1.0) {\n", 32);
sAppendN(&sbOut, "    // ros4 dense output is a cubic; reconstruct it from the four samples\n", 74);
sAppendN(&sbOut, "    // stored at s = 0, 1/3, 2/3, 1 via Lagrange interpolation.\n", 64);
sAppendN(&sbOut, "    double L0 = -4.5 * (s - 1.0/3.0) * (s - 2.0/3.0) * (s - 1.0);\n", 66);
sAppendN(&sbOut, "    double L1 = 13.5 * s * (s - 2.0/3.0) * (s - 1.0);\n", 54);
sAppendN(&sbOut, "    double L2 = -13.5 * s * (s - 1.0/3.0) * (s - 1.0);\n", 55);
sAppendN(&sbOut, "    double L3 = 4.5 * s * (s - 1.0/3.0) * (s - 2.0/3.0);\n", 57);
sAppendN(&sbOut, "    return L0 * rec[0 * n + col] + L1 * rec[1 * n + col] +\n", 59);
sAppendN(&sbOut, "           L2 * rec[2 * n + col] + L3 * rec[3 * n + col];\n", 58);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  // dop853 8th-order dense interpolant (same polynomial as contd8())\n", 70);
sAppendN(&sbOut, "  double s1 = 1.0 - s;\n", 23);
sAppendN(&sbOut, "  return rec[0 * n + col] + s * (rec[1 * n + col] + s1 * (rec[2 * n + col] +\n", 77);
sAppendN(&sbOut, "         s * (rec[3 * n + col] + s1 * (rec[4 * n + col] + s * (rec[5 * n + col] +\n", 82);
sAppendN(&sbOut, "         s1 * (rec[6 * n + col] + s * rec[7 * n + col]))))));\n", 62);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "// rxDelayD(state, T): time-derivative d/dt'[state(t')] at t' = t - T, i.e. the\n", 80);
sAppendN(&sbOut, "// derivative of the dense history interpolant.  Used by the forward-sensitivity\n", 81);
sAppendN(&sbOut, "// machinery for parameter-dependent delays (delay durations that depend on an\n", 79);
sAppendN(&sbOut, "// estimated parameter).  Before the start of integration the history is the\n", 77);
sAppendN(&sbOut, "// constant initial condition, so the derivative is 0.\n", 55);
sAppendN(&sbOut, "double _rxDelayD(rx_solving_options_ind *_ind, int i, double t, double T) {\n", 76);
sAppendN(&sbOut, "  double td = t - T;\n", 21);
sAppendN(&sbOut, "  if (T > 0.0 && T < _ind->delayMinT) _ind->delayMinT = T;\n", 59);
sAppendN(&sbOut, "  if (!_ind->delayHistOn || _ind->delayHistN == 0 || td <= _ind->delayT0) {\n", 76);
sAppendN(&sbOut, "    return 0.0;   // constant initial history -> zero time-derivative\n", 70);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  int n = _ind->delayHistNeq;\n", 30);
sAppendN(&sbOut, "  int stride = _ind->delayHistStride;\n", 38);
sAppendN(&sbOut, "  double *hist = _ind->delayHist;\n", 34);
sAppendN(&sbOut, "  int col = _solveData->op->delayCol[i];\n", 41);
sAppendN(&sbOut, "  if (col < 0) {\n", 17);
sAppendN(&sbOut, "    return 0.0;   // constant initial history -> zero time-derivative defensive fallback\n", 89);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  int lo = 0, hi = _ind->delayHistN - 1;\n", 41);
sAppendN(&sbOut, "  while (lo < hi) {\n", 20);
sAppendN(&sbOut, "    int mid = (lo + hi + 1) / 2;\n", 33);
sAppendN(&sbOut, "    if (hist[(size_t) mid * stride + (stride - 3)] <= td) lo = mid; else hi = mid - 1;\n", 87);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  double *rec = hist + (size_t) lo * stride;\n", 45);
sAppendN(&sbOut, "  double xold = rec[stride - 3];\n", 33);
sAppendN(&sbOut, "  double h    = rec[stride - 2];\n", 33);
sAppendN(&sbOut, "  double s  = (td - xold) / h;\n", 31);
sAppendN(&sbOut, "  if (rec[stride - 1] == 1.0) {\n", 32);
sAppendN(&sbOut, "    // derivative of the ros4 cubic Lagrange interpolant (nodes 0,1/3,2/3,1)\n", 77);
sAppendN(&sbOut, "    double a = s - 1.0/3.0, b = s - 2.0/3.0, c = s - 1.0;\n", 58);
sAppendN(&sbOut, "    double L0d = -4.5 * (b*c + a*c + a*b);\n", 43);
sAppendN(&sbOut, "    double L1d = 13.5 * (b*c + s*c + s*b);\n", 43);
sAppendN(&sbOut, "    double L2d = -13.5 * (a*c + s*c + s*a);\n", 44);
sAppendN(&sbOut, "    double L3d = 4.5 * (a*b + s*b + s*a);\n", 42);
sAppendN(&sbOut, "    return (L0d * rec[0 * n + col] + L1d * rec[1 * n + col] +\n", 62);
sAppendN(&sbOut, "            L2d * rec[2 * n + col] + L3d * rec[3 * n + col]) / h;\n", 66);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  // derivative of the dop853 8th-order interpolant: dy/dt' = (1/h) dy/ds,\n", 75);
sAppendN(&sbOut, "  // computed from the same nested (Horner) form as _rxDelay by carrying the\n", 77);
sAppendN(&sbOut, "  // value and its s-derivative together through each level (s1 = 1 - s).\n", 74);
sAppendN(&sbOut, "  double s1 = 1.0 - s;\n", 23);
sAppendN(&sbOut, "  double c0 = rec[0*n+col], c1 = rec[1*n+col], c2 = rec[2*n+col], c3 = rec[3*n+col];\n", 85);
sAppendN(&sbOut, "  double c4 = rec[4*n+col], c5 = rec[5*n+col], c6 = rec[6*n+col], c7 = rec[7*n+col];\n", 85);
sAppendN(&sbOut, "  double a6 = c6 + s * c7,    a6d = c7;\n", 40);
sAppendN(&sbOut, "  double a5 = c5 + s1 * a6,   a5d = -a6 + s1 * a6d;\n", 52);
sAppendN(&sbOut, "  double a4 = c4 + s * a5,    a4d = a5 + s * a5d;\n", 50);
sAppendN(&sbOut, "  double a3 = c3 + s1 * a4,   a3d = -a4 + s1 * a4d;\n", 52);
sAppendN(&sbOut, "  double a2 = c2 + s * a3,    a2d = a3 + s * a3d;\n", 50);
sAppendN(&sbOut, "  double a1 = c1 + s1 * a2,   a1d = -a2 + s1 * a2d;\n", 52);
sAppendN(&sbOut, "  double yd = a1 + s * a1d;   // d/ds of (c0 + s*a1)\n", 53);
sAppendN(&sbOut, "  return yd / h;\n", 17);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "// rxDelayD2(state, T): second time-derivative d^2/dt'^2[state(t')] at t' = t - T,\n", 83);
sAppendN(&sbOut, "// i.e. the second derivative of the dense history interpolant.  Used by the\n", 77);
sAppendN(&sbOut, "// second-order forward sensitivities of parameter-dependent delays.  Constant\n", 79);
sAppendN(&sbOut, "// initial history -> 0.\n", 25);
sAppendN(&sbOut, "double _rxDelayD2(rx_solving_options_ind *_ind, int i, double t, double T) {\n", 77);
sAppendN(&sbOut, "  double td = t - T;\n", 21);
sAppendN(&sbOut, "  if (T > 0.0 && T < _ind->delayMinT) _ind->delayMinT = T;\n", 59);
sAppendN(&sbOut, "  if (!_ind->delayHistOn || _ind->delayHistN == 0 || td <= _ind->delayT0) {\n", 76);
sAppendN(&sbOut, "    return 0.0;\n", 16);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  int n = _ind->delayHistNeq;\n", 30);
sAppendN(&sbOut, "  int stride = _ind->delayHistStride;\n", 38);
sAppendN(&sbOut, "  double *hist = _ind->delayHist;\n", 34);
sAppendN(&sbOut, "  int col = _solveData->op->delayCol[i];\n", 41);
sAppendN(&sbOut, "  if (col < 0) return 0.0;\n", 27);
sAppendN(&sbOut, "  int lo = 0, hi = _ind->delayHistN - 1;\n", 41);
sAppendN(&sbOut, "  while (lo < hi) {\n", 20);
sAppendN(&sbOut, "    int mid = (lo + hi + 1) / 2;\n", 33);
sAppendN(&sbOut, "    if (hist[(size_t) mid * stride + (stride - 3)] <= td) lo = mid; else hi = mid - 1;\n", 87);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  double *rec = hist + (size_t) lo * stride;\n", 45);
sAppendN(&sbOut, "  double xold = rec[stride - 3];\n", 33);
sAppendN(&sbOut, "  double h    = rec[stride - 2];\n", 33);
sAppendN(&sbOut, "  double s  = (td - xold) / h;\n", 31);
sAppendN(&sbOut, "  if (rec[stride - 1] == 1.0) {\n", 32);
sAppendN(&sbOut, "    // second derivative of the ros4 cubic Lagrange interpolant (linear in s):\n", 79);
sAppendN(&sbOut, "    // for a cubic (s-a)(s-b)(s-c), d^2/ds^2 = 6s - 2(a+b+c).\n", 62);
sAppendN(&sbOut, "    double L0dd = -4.5 * (6.0 * s - 4.0);            // nodes 1/3,2/3,1 -> sum 2\n", 81);
sAppendN(&sbOut, "    double L1dd = 13.5 * (6.0 * s - 10.0 / 3.0);     // nodes 0,2/3,1   -> sum 5/3\n", 83);
sAppendN(&sbOut, "    double L2dd = -13.5 * (6.0 * s - 8.0 / 3.0);     // nodes 0,1/3,1   -> sum 4/3\n", 83);
sAppendN(&sbOut, "    double L3dd = 4.5 * (6.0 * s - 2.0);             // nodes 0,1/3,2/3 -> sum 1\n", 81);
sAppendN(&sbOut, "    return (L0dd * rec[0 * n + col] + L1dd * rec[1 * n + col] +\n", 64);
sAppendN(&sbOut, "            L2dd * rec[2 * n + col] + L3dd * rec[3 * n + col]) / (h * h);\n", 74);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  // second derivative of the dop853 8th-order interpolant: d^2y/dt'^2 =\n", 73);
sAppendN(&sbOut, "  // (1/h^2) d^2y/ds^2, carrying value, first and second s-derivatives through\n", 79);
sAppendN(&sbOut, "  // the same nested Horner form (d^2/ds^2 of c+s*A = 2A'+s*A''; of c+s1*A =\n", 77);
sAppendN(&sbOut, "  // -2A'+s1*A'').\n", 19);
sAppendN(&sbOut, "  double s1 = 1.0 - s;\n", 23);
sAppendN(&sbOut, "  double c0 = rec[0*n+col], c1 = rec[1*n+col], c2 = rec[2*n+col], c3 = rec[3*n+col];\n", 85);
sAppendN(&sbOut, "  double c4 = rec[4*n+col], c5 = rec[5*n+col], c6 = rec[6*n+col], c7 = rec[7*n+col];\n", 85);
sAppendN(&sbOut, "  double a6 = c6 + s * c7,    a6d = c7,            a6dd = 0.0;\n", 63);
sAppendN(&sbOut, "  double a5 = c5 + s1 * a6,   a5d = -a6 + s1 * a6d, a5dd = -2.0 * a6d + s1 * a6dd;\n", 83);
sAppendN(&sbOut, "  double a4 = c4 + s * a5,    a4d = a5 + s * a5d,   a4dd = 2.0 * a5d + s * a5dd;\n", 81);
sAppendN(&sbOut, "  double a3 = c3 + s1 * a4,   a3d = -a4 + s1 * a4d, a3dd = -2.0 * a4d + s1 * a4dd;\n", 83);
sAppendN(&sbOut, "  double a2 = c2 + s * a3,    a2d = a3 + s * a3d,   a2dd = 2.0 * a3d + s * a3dd;\n", 81);
sAppendN(&sbOut, "  double a1 = c1 + s1 * a2,   a1d = -a2 + s1 * a2d, a1dd = -2.0 * a2d + s1 * a2dd;\n", 83);
sAppendN(&sbOut, "  (void) a1;\n", 13);
sAppendN(&sbOut, "  double ydd = 2.0 * a1d + s * a1dd;   // d^2/ds^2 of (c0 + s*a1)\n", 66);
sAppendN(&sbOut, "  return ydd / (h * h);\n", 24);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "// rxDelayD3(state, T): third time-derivative d^3/dt'^3[state(t')] at t' = t - T.\n", 82);
sAppendN(&sbOut, "// Used by the third-order forward sensitivities of parameter-dependent delays\n", 79);
sAppendN(&sbOut, "// (breaking-point jump terms).  Constant initial history -> 0.\n", 64);
sAppendN(&sbOut, "double _rxDelayD3(rx_solving_options_ind *_ind, int i, double t, double T) {\n", 77);
sAppendN(&sbOut, "  double td = t - T;\n", 21);
sAppendN(&sbOut, "  if (T > 0.0 && T < _ind->delayMinT) _ind->delayMinT = T;\n", 59);
sAppendN(&sbOut, "  if (!_ind->delayHistOn || _ind->delayHistN == 0 || td <= _ind->delayT0) {\n", 76);
sAppendN(&sbOut, "    return 0.0;\n", 16);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  int n = _ind->delayHistNeq;\n", 30);
sAppendN(&sbOut, "  int stride = _ind->delayHistStride;\n", 38);
sAppendN(&sbOut, "  double *hist = _ind->delayHist;\n", 34);
sAppendN(&sbOut, "  int col = _solveData->op->delayCol[i];\n", 41);
sAppendN(&sbOut, "  if (col < 0) return 0.0;\n", 27);
sAppendN(&sbOut, "  int lo = 0, hi = _ind->delayHistN - 1;\n", 41);
sAppendN(&sbOut, "  while (lo < hi) {\n", 20);
sAppendN(&sbOut, "    int mid = (lo + hi + 1) / 2;\n", 33);
sAppendN(&sbOut, "    if (hist[(size_t) mid * stride + (stride - 3)] <= td) lo = mid; else hi = mid - 1;\n", 87);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  double *rec = hist + (size_t) lo * stride;\n", 45);
sAppendN(&sbOut, "  double xold = rec[stride - 3];\n", 33);
sAppendN(&sbOut, "  double h    = rec[stride - 2];\n", 33);
sAppendN(&sbOut, "  double s  = (td - xold) / h;\n", 31);
sAppendN(&sbOut, "  if (rec[stride - 1] == 1.0) {\n", 32);
sAppendN(&sbOut, "    // third derivative of the ros4 cubic Lagrange interpolant is constant\n", 75);
sAppendN(&sbOut, "    // (6 * leading coefficient); any higher derivative is 0.\n", 62);
sAppendN(&sbOut, "    return (-27.0 * rec[0 * n + col] + 81.0 * rec[1 * n + col] +\n", 65);
sAppendN(&sbOut, "            -81.0 * rec[2 * n + col] + 27.0 * rec[3 * n + col]) / (h * h * h);\n", 79);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "  // third derivative of the dop853 8th-order interpolant: d^3y/dt'^3 =\n", 72);
sAppendN(&sbOut, "  // (1/h^3) d^3y/ds^3, carrying value + first/second/third s-derivatives\n", 74);
sAppendN(&sbOut, "  // (d^3/ds^3 of c+s*A = 3A''+s*A'''; of c+s1*A = -3A''+s1*A''').\n", 67);
sAppendN(&sbOut, "  double s1 = 1.0 - s;\n", 23);
sAppendN(&sbOut, "  double c0 = rec[0*n+col], c1 = rec[1*n+col], c2 = rec[2*n+col], c3 = rec[3*n+col];\n", 85);
sAppendN(&sbOut, "  double c4 = rec[4*n+col], c5 = rec[5*n+col], c6 = rec[6*n+col], c7 = rec[7*n+col];\n", 85);
sAppendN(&sbOut, "  double a6 = c6 + s * c7,   a6d = c7,             a6dd = 0.0,                  a6ddd = 0.0;\n", 93);
sAppendN(&sbOut, "  double a5 = c5 + s1 * a6,  a5d = -a6 + s1 * a6d, a5dd = -2.0*a6d + s1*a6dd,   a5ddd = -3.0*a6dd + s1*a6ddd;\n", 110);
sAppendN(&sbOut, "  double a4 = c4 + s * a5,   a4d = a5 + s * a5d,   a4dd = 2.0*a5d + s*a5dd,     a4ddd = 3.0*a5dd + s*a5ddd;\n", 108);
sAppendN(&sbOut, "  double a3 = c3 + s1 * a4,  a3d = -a4 + s1 * a4d, a3dd = -2.0*a4d + s1*a4dd,   a3ddd = -3.0*a4dd + s1*a4ddd;\n", 110);
sAppendN(&sbOut, "  double a2 = c2 + s * a3,   a2d = a3 + s * a3d,   a2dd = 2.0*a3d + s*a3dd,     a2ddd = 3.0*a3dd + s*a3ddd;\n", 108);
sAppendN(&sbOut, "  double a1 = c1 + s1 * a2,  a1d = -a2 + s1 * a2d, a1dd = -2.0*a2d + s1*a2dd,   a1ddd = -3.0*a2dd + s1*a2ddd;\n", 110);
sAppendN(&sbOut, "  (void) a1; (void) a1d;\n", 25);
sAppendN(&sbOut, "  double yddd = 3.0 * a1dd + s * a1ddd;   // d^3/ds^3 of (c0 + s*a1)\n", 69);
sAppendN(&sbOut, "  return yddd / (h * h * h);\n", 29);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "void _assignFuns0(void) {\n", 26);
sAppendN(&sbOut, "  _evalUdf = (_udf_type) R_GetCCallable(\"rxode2\", \"_rxode2_evalUdf\");\n", 70);
sAppendN(&sbOut, "  _getRxSolve_ = (_getRxSolve_t) R_GetCCallable(\"rxode2\",\"getRxSolve_\");\n", 73);
sAppendN(&sbOut, "  _assign_ptr=(rxode2_assign_ptr) R_GetCCallable(\"rxode2\",\"rxode2_assign_fn_pointers\");\n", 88);
sAppendN(&sbOut, "  _rxRmModelLib=(_rxRmModelLibType) R_GetCCallable(\"rxode2\",\"rxRmModelLib\");\n", 77);
sAppendN(&sbOut, "  _rxGetModelLib=(_rxGetModelLibType) R_GetCCallable(\"rxode2\",\"rxGetModelLib\");\n", 80);
sAppendN(&sbOut, "  _rxode2_rxAssignPtr=(_rx_asgn)R_GetCCallable(\"rxode2\",\"_rxode2_rxAssignPtr\");\n", 80);
sAppendN(&sbOut, "  _rxQr=(_rx_asgn)R_GetCCallable(\"rxode2\",\"_rxode2_rxQr\");\n", 59);
sAppendN(&sbOut, "  _rxIsCurrentC = (_rxIsCurrentC_type)R_GetCCallable(\"rxode2\",\"rxIsCurrentC\");\n", 79);
sAppendN(&sbOut, "  _sumPS  = (_rxSumType) R_GetCCallable(\"PreciseSums\",\"PreciseSums_sum_r\");\n", 76);
sAppendN(&sbOut, "  _prodPS = (_rxProdType) R_GetCCallable(\"PreciseSums\",\"PreciseSums_prod_r\");\n", 78);
sAppendN(&sbOut, "  _prodType=(rxode2_fn0i)R_GetCCallable(\"PreciseSums\", \"PreciseSums_prod_get\");\n", 80);
sAppendN(&sbOut, "  _sumType=(rxode2_fn0i)R_GetCCallable(\"PreciseSums\", \"PreciseSums_sum_get\");\n", 78);
sAppendN(&sbOut, "  _ptrid=(rxode2_fn0i)R_GetCCallable(\"rxode2\", \"rxode2_current_fn_pointer_id\");\n", 80);
sAppendN(&sbOut, "  _compareFactorVal=(rxode2_compareFactorVal_fn) R_GetCCallable(\"rxode2\", \"compareFactorVal\");\n", 95);
sAppendN(&sbOut, "  _compareFactorInt=(rxode2_compareFactorInt_fn) R_GetCCallable(\"rxode2\", \"compareFactorInt\");\n", 95);
sAppendN(&sbOut, "  _update_par_ptr = (_update_par_ptr_p) R_GetCCallable(\"rxode2\",\"_update_par_ptr\");\n", 84);
sAppendN(&sbOut, "  _getParCov = (_getParCov_p) R_GetCCallable(\"rxode2\",\"_getParCov\");\n", 69);
sAppendN(&sbOut, "  _setThreadInd = (_setThreadInd_t) R_GetCCallable(\"rxode2\",\"_setThreadInd\");\n", 78);
sAppendN(&sbOut, "  _rxPushDose   = (_rxPushDose_t)   R_GetCCallable(\"rxode2\",\"_rxPushDose\");\n", 76);
sAppendN(&sbOut, "  // dynamic start\n", 19);
}
void writeBody2(void) {
sAppendN(&sbOut, "  // dynamic stop\n", 18);
sAppendN(&sbOut, "  _solveData = _getRxSolve_();\n", 31);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "void _assignFuns(void) {\n", 25);
sAppendN(&sbOut, "  // Re-initialize if rxode2 was reloaded: the registered function pointer will\n", 80);
sAppendN(&sbOut, "  // differ from the one stored at last init, revealing a stale _solveData.\n", 76);
sAppendN(&sbOut, "  if (_assign_ptr == NULL ||\n", 29);
sAppendN(&sbOut, "      _assign_ptr != (rxode2_assign_ptr)R_GetCCallable(\"rxode2\",\"rxode2_assign_fn_pointers\")){\n", 95);
sAppendN(&sbOut, "    _assignFuns0();\n", 20);
sAppendN(&sbOut, "  }\n", 4);
sAppendN(&sbOut, "}\n", 2);
sAppendN(&sbOut, "void __assignFuns2(rx_solve rx,\n", 32);
sAppendN(&sbOut, "                   rx_solving_options op,\n", 42);
sAppendN(&sbOut, "                   t_F f,\n", 26);
sAppendN(&sbOut, "                   t_LAG lag,\n", 30);
sAppendN(&sbOut, "                   t_RATE rate,\n", 32);
sAppendN(&sbOut, "                   t_DUR dur,\n", 30);
sAppendN(&sbOut, "                   t_calc_mtime mtime,\n", 39);
sAppendN(&sbOut, "                   t_ME me,\n", 28);
sAppendN(&sbOut, "                   t_IndF indf,\n", 32);
sAppendN(&sbOut, "                   t_getTime gettime,\n", 38);
sAppendN(&sbOut, "                   t_locateTimeIndex timeindex,\n", 48);
sAppendN(&sbOut, "                   t_handle_evidL handleEvid,\n", 46);
sAppendN(&sbOut, "                   t_getDur getdur) {\n", 38);
sAppendN(&sbOut, "  // assign start\n", 18);
}
void writeBody3(void) {
sAppendN(&sbOut, "  // assign stop\n", 17);
sAppendN(&sbOut, "}\n", 2);
}
void writeFooter(void) {
sAppendN(&sbOut, "#undef _getRxSolve_\n", 20);
sAppendN(&sbOut, "#undef _evalUdf\n", 16);
sAppendN(&sbOut, "#undef _solveData\n", 18);
sAppendN(&sbOut, "#undef _assign_ptr\n", 19);
sAppendN(&sbOut, "#undef _rxRmModelLib\n", 21);
sAppendN(&sbOut, "#undef _rxGetModelLib\n", 22);
sAppendN(&sbOut, "#undef _old_c\n", 14);
sAppendN(&sbOut, "#undef _ptrid\n", 14);
sAppendN(&sbOut, "#undef _rxIsCurrentC\n", 21);
sAppendN(&sbOut, "#undef _sumPS\n", 14);
sAppendN(&sbOut, "#undef _prodPS\n", 15);
sAppendN(&sbOut, "#undef _prodType\n", 17);
sAppendN(&sbOut, "#undef _sumType\n", 16);
sAppendN(&sbOut, "#undef _update_par_ptr\n", 23);
sAppendN(&sbOut, "#undef _getParCov\n", 18);
sAppendN(&sbOut, "#undef _setThreadInd\n", 21);
sAppendN(&sbOut, "#undef _rxPushDose\n", 19);
sAppendN(&sbOut, "#undef _rxode2_rxAssignPtr\n", 27);
sAppendN(&sbOut, "#undef _rxQr\n", 13);
sAppendN(&sbOut, "#undef _compareFactorVal\n", 25);
sAppendN(&sbOut, "#undef _compareFactorInt\n", 25);
sAppendN(&sbOut, "#undef _sum\n", 12);
sAppendN(&sbOut, "#undef _udf\n", 12);
sAppendN(&sbOut, "#undef _sign\n", 13);
sAppendN(&sbOut, "#undef _prod\n", 13);
sAppendN(&sbOut, "#undef _max\n", 12);
sAppendN(&sbOut, "#undef _min\n", 12);
sAppendN(&sbOut, "#undef _transit4P\n", 18);
sAppendN(&sbOut, "#undef _transit3P\n", 18);
sAppendN(&sbOut, "#undef _rxDelay\n", 16);
sAppendN(&sbOut, "#undef _rxDelayD\n", 17);
sAppendN(&sbOut, "#undef _rxDelayD2\n", 18);
sAppendN(&sbOut, "#undef _rxDelayD3\n", 18);
sAppendN(&sbOut, "#undef _rxPast\n", 15);
sAppendN(&sbOut, "#undef _assignFuns0\n", 20);
sAppendN(&sbOut, "#undef _assignFuns\n", 19);
sAppendN(&sbOut, "#undef _rxord\n", 14);
sAppendN(&sbOut, "#undef __assignFuns2\n", 21);
}
