// File: 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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
addq $0x190, %rax            # imm = 0x190
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rax
addq $0x3e0, %rax            # imm = 0x3E0
movq %rax, 0x58(%rsp)
movq 0x70(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x50(%rsp)
movq 0x70(%rsp), %rax
addq $0x38, %rax
movq %rax, 0x48(%rsp)
movq 0x70(%rsp), %rax
addq $0x58, %rax
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0x60(%rsp), %rax
movsd 0x18(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0xf8(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0x50(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x140(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0x48(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x100(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0xb8(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x158(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0xc8(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x160(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0x90(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x118(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq 0x118(%rax), %rax
movsd (%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x160(%rax), %rax
mulsd (%rax), %xmm0
movq 0x60(%rsp), %rax
divsd 0x88(%rax), %xmm0
movsd 0x690e(%rip), %xmm1     # 0x1c190
divsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x108(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0xd0(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x168(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq 0x170(%rax), %rax
movsd 0x68d1(%rip), %xmm0     # 0x1c198
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq 0x138(%rax), %rax
movsd 0x6729(%rip), %xmm0     # 0x1c008
movsd %xmm0, (%rax)
movq 0x58(%rsp), %rax
movsd 0x70(%rax), %xmm0
movq 0x58(%rsp), %rax
movq 0x88(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0xa8(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0x128(%rax), %rax
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movsd 0xe0(%rax), %xmm0
movq 0x60(%rsp), %rax
movsd 0x38(%rax), %xmm1
movq 0x48(%rsp), %rax
movsd 0x66ca(%rip), %xmm2     # 0x1c008
subsd 0x8(%rax), %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movsd %xmm0, 0xc8(%rax)
movl $0x1, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0x15e26
movq 0x60(%rsp), %rax
movq 0xf8(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x60(%rsp), %rax
movsd 0x28(%rax), %xmm0
movq 0x60(%rsp), %rax
movq 0xf8(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
divsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movsd 0x20(%rax), %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0xf8(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movsd 0x50(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x60(%rsp), %rax
movsd 0x6775(%rip), %xmm0     # 0x1c178
mulsd 0x58(%rax), %xmm0
movq 0x68(%rsp), %rax
mulsd 0x8(%rax), %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
mulsd %xmm1, %xmm0
callq 0x9ca0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x140(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movq 0x100(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movq 0x140(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd 0x658b(%rip), %xmm1     # 0x1c008
subsd (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x100(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movq 0x158(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x60(%rsp), %rax
movsd 0x653e(%rip), %xmm0     # 0x1c008
addsd 0xc0(%rax), %xmm0
movq 0x68(%rsp), %rax
movsd 0x8(%rax), %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x158(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movq 0x160(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq 0x158(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x68(%rsp), %rax
mulsd 0x8(%rax), %xmm0
callq 0x9ca0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x160(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movsd 0x90(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x60(%rsp), %rax
movsd 0x6476(%rip), %xmm0     # 0x1c008
subsd 0x98(%rax), %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x118(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movq 0x118(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movq 0x160(%rax), %rax
movslq 0x38(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
divsd 0x88(%rax), %xmm0
movsd 0x6587(%rip), %xmm1     # 0x1c190
divsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x108(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movsd 0xd0(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x60(%rsp), %rax
movsd 0x63c5(%rip), %xmm0     # 0x1c008
subsd 0xd8(%rax), %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x168(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movq 0x170(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x60(%rsp), %rax
movq 0x168(%rax), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6509(%rip), %xmm2     # 0x1c1c0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x170(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movsd 0x6326(%rip), %xmm0     # 0x1c008
addsd 0x8(%rax), %xmm0
movq 0x68(%rsp), %rax
movsd 0x8(%rax), %xmm1
cvtsi2sdl 0x38(%rsp), %xmm2
mulsd %xmm2, %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x62fd(%rip), %xmm0     # 0x1c008
divsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x138(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpl $0x12, 0x38(%rsp)
jge 0x15d7c
movq 0x58(%rsp), %rax
movsd 0x70(%rax), %xmm1
movq 0x58(%rsp), %rax
movsd 0x78(%rax), %xmm2
movq 0x58(%rsp), %rax
subsd 0x70(%rax), %xmm2
movsd 0x6476(%rip), %xmm0     # 0x1c1c8
mulsd %xmm2, %xmm0
cvtsi2sdl 0x38(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x58(%rsp), %rax
movq 0x88(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x15db4
movq 0x58(%rsp), %rax
movsd 0x70(%rax), %xmm0
movq 0x58(%rsp), %rax
movsd 0x78(%rax), %xmm1
movq 0x58(%rsp), %rax
subsd 0x70(%rax), %xmm1
addsd %xmm1, %xmm0
movq 0x58(%rsp), %rax
movq 0x88(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movsd 0xa8(%rax), %xmm0
movsd %xmm0, (%rsp)
movq 0x60(%rsp), %rax
movsd 0x6235(%rip), %xmm0     # 0x1c008
addsd 0xb0(%rax), %xmm0
movq 0x68(%rsp), %rax
movsd 0x8(%rax), %xmm1
cvtsi2sdl 0x38(%rsp), %xmm2
mulsd %xmm2, %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movq 0x128(%rax), %rax
movslq 0x38(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x15960
addq $0x78, %rsp
retq
nopl (%rax,%rax)