// File: JRLamontagne[P]cdice_sa/CDICE.cpp
// Function: postProcess(DICE*)
void postProcess(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;


  // Calculate additional model outputs ------------------------------------------
  for (int t=0; t<config->nPeriods; t++)
    {
      
      // One period utility function
      econ->periodu[t] = (pow((econ->c[t]*1000.0/econ->l[t]),(1.0-econ->elasmu))-1.0)/(1.0-econ->elasmu)-1.0;
      
      // Period utility
      econ->cemutotper[t] = econ->periodu[t]*econ->l[t]*econ->rr[t];
      
      // Total cost of abatement and damages
      econ->totalcost[t] = econ->damages[t] + econ->abatecost[t];
      
      // Atmospheric fraction 1850
		carb->atfrac[t] = (carb->mat[t]-588)/(econ->ccatot[t]+0.000001);
		
		// Atmospheric fraction 2010
		carb->atfrac2010[t] = (carb->mat[t] - carb->mat0)/(0.00001+econ->ccatot[t]-econ->ccatot[0]);
		
		// Atmospheric concentration (mixing ratio)
		carb->ppm[t] = carb->mat[t]/2.13;
      
    }
  
  
  // Discounted utility of consumption (objective function) -----------------------
  double tempsum;
  tempsum = 0.0;
  
  for (int t=0; t<config->nPeriods; t++)
    {
      tempsum += econ->cemutotper[t];
    }
  
  econ->utility = (config->tstep*econ->scale1*tempsum)+econ->scale2;
  
  
  // Present Value Calculations for damages and abatement costs --------------------
  econ->pv_damages[0] = econ->damages[0];
  econ->pv_abatecost[0] = econ->abatecost[0];
  econ->pv_totalcost[0] = econ->totalcost[0];
  
  for (int t=1; t<config->nPeriods; t++)
    {
      if(econ->ri[t] <= 0.0) {
	econ->ri_disc[t] = 0.0;
      }
      else {
	econ->ri_disc[t] = 1.0/pow(1.0 + econ->ri[t],config->tstep*double(t));
      }
      
      econ->pv_damages[t] = econ->pv_damages[t-1]+econ->ri_disc[t]*econ->damages[t];
      econ->pv_abatecost[t] = econ->pv_abatecost[t-1]+econ->ri_disc[t]*econ->abatecost[t];
      econ->pv_totalcost[t] = econ->pv_totalcost[t-1]+econ->ri_disc[t]*econ->totalcost[t];
    }
  
  return;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rax
addq $0x190, %rax            # imm = 0x190
movq %rax, 0x40(%rsp)
movq 0x50(%rsp), %rax
addq $0x3e0, %rax            # imm = 0x3E0
movq %rax, 0x38(%rsp)
movq 0x50(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x30(%rsp)
movq 0x50(%rsp), %rax
addq $0x38, %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
addq $0x58, %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x17c96
movq 0x40(%rsp), %rax
movq 0x178(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd 0x4711(%rip), %xmm0     # 0x1c190
mulsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0xf8(%rax), %rax
movslq 0x1c(%rsp), %rcx
divsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movsd 0x4561(%rip), %xmm1     # 0x1c008
subsd (%rax), %xmm1
callq 0x9c60
movsd 0x4550(%rip), %xmm1     # 0x1c008
subsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movsd 0x453f(%rip), %xmm1     # 0x1c008
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd 0x452f(%rip), %xmm1     # 0x1c008
subsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movq 0x1d8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movq 0x1d8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0xf8(%rax), %rax
movslq 0x1c(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x138(%rax), %rax
movslq 0x1c(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x1e8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movq 0x1b8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x1c8(%rax), %rax
movslq 0x1c(%rsp), %rcx
addsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x210(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x4625(%rip), %xmm1     # 0x1c1d0
subsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movq 0x240(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd 0x4630(%rip), %xmm1     # 0x1c1f8
addsd (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movq 0x38(%rsp), %rax
movq 0xb0(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x38(%rsp), %rax
subsd (%rax), %xmm0
movq 0x40(%rsp), %rax
movq 0x240(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd 0x45d1(%rip), %xmm1     # 0x1c1f0
addsd (%rax,%rcx,8), %xmm1
movq 0x40(%rsp), %rax
movq 0x240(%rax), %rax
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movq 0x38(%rsp), %rax
movq 0xb8(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x457c(%rip), %xmm1     # 0x1c1e8
divsd %xmm1, %xmm0
movq 0x38(%rsp), %rax
movq 0xc0(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x17a55
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x17ce3
movq 0x40(%rsp), %rax
movq 0x1e8(%rax), %rax
movslq 0xc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x17ca7
movq 0x48(%rsp), %rax
movsd 0x8(%rax), %xmm0
movq 0x40(%rsp), %rax
mulsd 0xe8(%rax), %xmm0
movsd 0x10(%rsp), %xmm2
movq 0x40(%rsp), %rax
movsd 0xf0(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movsd %xmm0, 0x1f0(%rax)
movq 0x40(%rsp), %rax
movq 0x1b8(%rax), %rax
movsd (%rax), %xmm0
movq 0x40(%rsp), %rax
movq 0x200(%rax), %rax
movsd %xmm0, (%rax)
movq 0x40(%rsp), %rax
movq 0x1c8(%rax), %rax
movsd (%rax), %xmm0
movq 0x40(%rsp), %rax
movq 0x208(%rax), %rax
movsd %xmm0, (%rax)
movq 0x40(%rsp), %rax
movq 0x210(%rax), %rax
movsd (%rax), %xmm0
movq 0x40(%rsp), %rax
movq 0x218(%rax), %rax
movsd %xmm0, (%rax)
movl $0x1, 0x8(%rsp)
movl 0x8(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x17f6c
movq 0x40(%rsp), %rax
movq 0x198(%rax), %rax
movslq 0x8(%rsp), %rcx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jb 0x17dd1
movq 0x40(%rsp), %rax
movq 0x1f8(%rax), %rax
movslq 0x8(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x17e2d
movq 0x40(%rsp), %rax
movq 0x198(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd 0x421e(%rip), %xmm0     # 0x1c008
addsd (%rax,%rcx,8), %xmm0
movq 0x48(%rsp), %rax
movsd 0x8(%rax), %xmm1
cvtsi2sdl 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm1
callq 0x9c60
movaps %xmm0, %xmm1
movsd 0x41f5(%rip), %xmm0     # 0x1c008
divsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movq 0x1f8(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movq 0x200(%rax), %rax
movl 0x8(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x40(%rsp), %rax
movq 0x1f8(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x1b8(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movq 0x200(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movq 0x208(%rax), %rax
movl 0x8(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x40(%rsp), %rax
movq 0x1f8(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x1c8(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movq 0x208(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movq 0x218(%rax), %rax
movl 0x8(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x40(%rsp), %rax
movq 0x1f8(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movq 0x210(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
movq 0x218(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x17d8a
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)