// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl (%rdi), %ecx
testl %ecx, %ecx
jle 0x5b8f
xorl %r14d, %r14d
movq 0x288(%rbx), %rax
movq 0x308(%rbx), %rcx
movsd (%rcx,%r14,8), %xmm0
mulsd 0x2681(%rip), %xmm0     # 0x80e0
divsd (%rax,%r14,8), %xmm0
movsd 0x259b(%rip), %xmm1     # 0x8008
subsd 0x190(%rbx), %xmm1
callq 0x2340
movsd 0x264e(%rip), %xmm2     # 0x80d0
addsd %xmm2, %xmm0
movsd 0x257a(%rip), %xmm1     # 0x8008
subsd 0x190(%rbx), %xmm1
divsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movq 0x368(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x288(%rbx), %rax
movq 0x2c8(%rbx), %rcx
mulsd (%rax,%r14,8), %xmm0
mulsd (%rcx,%r14,8), %xmm0
movq 0x378(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x348(%rbx), %rax
movq 0x358(%rbx), %rcx
movsd (%rax,%r14,8), %xmm0
addsd (%rcx,%r14,8), %xmm0
movq 0x3a0(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x3d0(%rbx), %rax
movq 0x478(%rbx), %rcx
movsd (%rcx,%r14,8), %xmm0
addsd 0x260b(%rip), %xmm0     # 0x8120
movsd (%rax,%r14,8), %xmm1
addsd 0x2605(%rip), %xmm1     # 0x8128
divsd %xmm1, %xmm0
movq 0x490(%rbx), %rdx
movsd %xmm0, (%rdx,%r14,8)
movsd (%rcx,%r14,8), %xmm0
subsd 0x3e0(%rbx), %xmm0
movsd (%rax,%r14,8), %xmm1
addsd 0x25e0(%rip), %xmm1     # 0x8130
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movq 0x498(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd (%rcx,%r14,8), %xmm0
divsd 0x25c5(%rip), %xmm0     # 0x8138
movq 0x4a0(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movslq (%rbx), %rcx
cmpq %rcx, %r14
jl 0x5a43
testl %ecx, %ecx
jle 0x5bb1
movq 0x378(%rbx), %rax
movl %ecx, %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
addsd (%rax,%rdx,8), %xmm0
incq %rdx
cmpq %rdx, %rcx
jne 0x5ba2
jmp 0x5bb5
xorpd %xmm0, %xmm0
movsd 0x8(%rbx), %xmm1
mulsd 0x278(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd 0x280(%rbx), %xmm1
movsd %xmm1, 0x380(%rbx)
movq 0x348(%rbx), %rax
movsd (%rax), %xmm0
movq 0x390(%rbx), %rax
movsd %xmm0, (%rax)
movq 0x358(%rbx), %rax
movq 0x398(%rbx), %rcx
movsd (%rax), %xmm0
movsd %xmm0, (%rcx)
movq 0x3a0(%rbx), %rax
movq 0x3a8(%rbx), %rcx
movsd (%rax), %xmm0
movsd %xmm0, (%rcx)
cmpl $0x2, (%rbx)
jl 0x5d00
movl $0x1, %r14d
movq 0x328(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x5c68
movsd 0x23c2(%rip), %xmm1     # 0x8008
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd 0x8(%rbx), %xmm1
callq 0x2340
movsd 0x23a4(%rip), %xmm1     # 0x8008
divsd %xmm0, %xmm1
movq 0x388(%rbx), %rax
movsd %xmm1, (%rax,%r14,8)
movq 0x390(%rbx), %rax
movq 0x348(%rbx), %rcx
movq 0x388(%rbx), %rdx
movsd (%rdx,%r14,8), %xmm0
mulsd (%rcx,%r14,8), %xmm0
addsd -0x8(%rax,%r14,8), %xmm0
movsd %xmm0, (%rax,%r14,8)
movq 0x358(%rbx), %rax
movq 0x398(%rbx), %rcx
movsd (%rdx,%r14,8), %xmm0
mulsd (%rax,%r14,8), %xmm0
addsd -0x8(%rcx,%r14,8), %xmm0
movsd %xmm0, (%rcx,%r14,8)
movq 0x3a0(%rbx), %rax
movq 0x3a8(%rbx), %rcx
movsd (%rdx,%r14,8), %xmm0
mulsd (%rax,%r14,8), %xmm0
addsd -0x8(%rcx,%r14,8), %xmm0
movsd %xmm0, (%rcx,%r14,8)
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0x5c27
addq $0x8, %rsp
popq %rbx
popq %r14
retq