// File: /workspace/github_cpp_makefile_star2_O1/JRLamontagne[P]cdice_sa/CDICE.cpp
// Function: calcExog(DICE*)
void calcExog(DICE *dice)
{
  Config *config = &dice->config;
  Econ *econ = &dice->econ;
  Carb *carb = &dice->carb;
  Clim *clim = &dice->clim;
  Dvars *dvars = &dice->dvars;
  Ctrl *ctrl = &dice->ctrl;
  int tlim = config->nPeriods;
  
  
  // Initial timestep calculations and settings ----------------------------
  econ->l[0] = econ->pop0;			// Population
  econ->ga[0] = econ->ga0;			// Growth rate for TFP
  econ->al[0] = econ->a0;			// Level of TFP
  econ->gsig[0] = econ->gsigma1;		// Change in sigma (cumulative improvement in energy efficiency)
  econ->sigma[0] = econ->sig0;		// CO2-equivalent-emissions output ratio
  econ->pbacktime[0] = econ->pback;	// Cost of backstop
  econ->cost1[0] = econ->pbacktime[0]*econ->sigma[0]/econ->expcost2/1000;

  // Adjusted cost for backstop
  econ->etree[0] = econ->eland0;		// Emissions from deforestation
  econ->cumetree[0] = 100.0;
  econ->rr[0] = 1;					// Average utility social discount rate
  carb->forcoth[0] = carb->fex0;		// Forcing from other GHG

  // Fraction of emissions under control regime
  econ->cpricebase[0] = econ->cprice0; // Carbon price in base case
  econ->sig0 = econ->e0 / (econ->q0 * (1 - dvars->miu0));
                                       // Carbon Intensity 2010 [kgCO2 per output 2005 USD 2010]
  
  // Iterate over the full time series
  for (int t=1; t<tlim; t++)
    {
      // Population
      econ->l[t] = econ->l[t-1]*(pow(econ->popasym/econ->l[t-1], econ->popadj));
      
      // Growth rate for TFP
      econ->ga[t] = econ->ga0*exp(-1*econ->dela*config->tstep*double(t));
      
      // Level for TFP
      econ->al[t] = econ->al[t-1]/(1-econ->ga[t-1]);
      
      // Change in sigma (cumulative improvement in energy efficiency)
      econ->gsig[t] = econ->gsig[t-1]*(pow(1+econ->dsig, config->tstep));
      
      // CO2-equivalent-emissions output ratio
      econ->sigma[t] = econ->sigma[t-1]*exp(econ->gsig[t-1]*config->tstep);
      
      // Cost of backstop
      econ->pbacktime[t] = econ->pback*pow(1-econ->gback, double(t));
      
      // Adjusted cost for backstop
      econ->cost1[t] = econ->pbacktime[t]*econ->sigma[t]/econ->expcost2/1000;
      
      // Emissions from deforestation
      econ->etree[t] = econ->eland0*pow(1-econ->deland, double(t));
      
      // Cumulative emissions from land
      econ->cumetree[t] = econ->cumetree[t-1]+econ->etree[t-1]*(5/3.666);
      
      // Average utility social discount rate	
      econ->rr[t] = 1/pow(1+econ->prstp, config->tstep*double(t));
      
      // Forcings from other GHG
      if (t < 18) {
	carb->forcoth[t] = carb->fex0+(1.0/17.0)*(carb->fex1-carb->fex0)*double(t);
      }
      else {
	carb->forcoth[t] = carb->fex0+(carb->fex1-carb->fex0);
      }
      
      // Carbon price in base case
      econ->cpricebase[t] = econ->cprice0*pow(1+econ->gcprice, config->tstep*double(t));
      
    }
  
  return;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movslq (%rdi), %r14
movsd 0x1a8(%rdi), %xmm0
movq 0x288(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x1e0(%rdi), %xmm0
movq 0x2d0(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x1d8(%rdi), %xmm0
movq 0x290(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x248(%rdi), %xmm0
movq 0x2e8(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x258(%rdi), %xmm0
movq 0x2f0(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x220(%rdi), %xmm0
movq 0x2a8(%rdi), %rcx
movsd %xmm0, (%rcx)
mulsd (%rax), %xmm0
divsd 0x218(%rdi), %xmm0
divsd 0x357e(%rip), %xmm0     # 0x80e0
movq 0x298(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x260(%rdi), %xmm0
movq 0x2f8(%rdi), %rax
movsd %xmm0, (%rax)
movq 0x300(%rdi), %rax
movabsq $0x4059000000000000, %rcx # imm = 0x4059000000000000
movq %rcx, (%rax)
movq 0x2c8(%rdi), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
movsd 0x450(%rdi), %xmm0
movq 0x468(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x238(%rdi), %xmm0
movq 0x2b8(%rdi), %rax
movsd %xmm0, (%rax)
movsd 0x3432(%rip), %xmm0     # 0x8008
subsd 0x40(%rdi), %xmm0
mulsd 0x1c8(%rdi), %xmm0
movsd 0x270(%rdi), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x258(%rdi)
cmpq $0x2, %r14
jl 0x4ecd
movq %rdi, %rbx
movl $0x1, %r15d
movq 0x288(%rbx), %rax
movsd -0x8(%rax,%r15,8), %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x1b0(%rbx), %xmm1
movsd 0x1b8(%rbx), %xmm0
divsd %xmm2, %xmm0
callq 0x2340
mulsd 0x8(%rsp), %xmm0
movq 0x288(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x1e8(%rbx), %xmm0
xorpd 0x34e6(%rip), %xmm0     # 0x8140
mulsd 0x8(%rbx), %xmm0
movsd 0x1e0(%rbx), %xmm1
movsd %xmm1, (%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd %xmm1, 0x8(%rsp)
mulsd %xmm1, %xmm0
callq 0x2360
mulsd (%rsp), %xmm0
movq 0x2d0(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x290(%rbx), %rcx
movsd 0x3364(%rip), %xmm1     # 0x8008
movapd %xmm1, %xmm0
movapd %xmm1, %xmm2
subsd -0x8(%rax,%r15,8), %xmm0
movsd -0x8(%rcx,%r15,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%r15,8)
movq 0x2e8(%rbx), %rax
movsd -0x8(%rax,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movsd 0x250(%rbx), %xmm0
addsd %xmm2, %xmm0
movsd 0x8(%rbx), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x2e8(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x2f0(%rbx), %rcx
movsd -0x8(%rcx,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movsd -0x8(%rax,%r15,8), %xmm0
mulsd 0x8(%rbx), %xmm0
callq 0x2360
mulsd (%rsp), %xmm0
movq 0x2f0(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x220(%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd 0x32be(%rip), %xmm0     # 0x8008
subsd 0x228(%rbx), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x2a8(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x298(%rbx), %rax
movq 0x2f0(%rbx), %rcx
mulsd (%rcx,%r15,8), %xmm0
divsd 0x218(%rbx), %xmm0
divsd 0x334d(%rip), %xmm0     # 0x80e0
movsd %xmm0, (%rax,%r15,8)
movsd 0x260(%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd 0x325a(%rip), %xmm0     # 0x8008
subsd 0x268(%rbx), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x2f8(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x300(%rbx), %rcx
movsd -0x8(%rax,%r15,8), %xmm0
mulsd 0x32ff(%rip), %xmm0     # 0x80e8
addsd -0x8(%rcx,%r15,8), %xmm0
movsd %xmm0, (%rcx,%r15,8)
movsd 0x198(%rbx), %xmm0
addsd 0x3202(%rip), %xmm0     # 0x8008
movsd 0x8(%rbx), %xmm1
mulsd 0x8(%rsp), %xmm1
callq 0x2340
movsd 0x31ea(%rip), %xmm2     # 0x8008
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movq 0x2c8(%rbx), %rax
movsd %xmm1, (%rax,%r15,8)
movsd 0x450(%rbx), %xmm1
cmpq $0x11, %r15
ja 0x4e69
movsd 0x458(%rbx), %xmm0
subsd %xmm1, %xmm0
mulsd 0x329b(%rip), %xmm0     # 0x80f0
movsd 0x8(%rsp), %xmm3
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movapd %xmm3, %xmm1
jmp 0x4e7f
movsd 0x458(%rbx), %xmm0
subsd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x468(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x238(%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd 0x240(%rbx), %xmm0
addsd %xmm2, %xmm0
mulsd 0x8(%rbx), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x2b8(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq %r15, %r14
jne 0x4c0a
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq