// File: /workspace/github_cpp_makefile_star2_O2/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
xorl %r14d, %r14d
movslq (%rbx), %rax
cmpq %rax, %r14
jge 0x56f8
movq 0x288(%rbx), %rax
movq 0x308(%rbx), %rcx
movsd (%rcx,%r14,8), %xmm0
mulsd 0x2b11(%rip), %xmm0     # 0x80e0
divsd (%rax,%r14,8), %xmm0
movsd 0x2a2b(%rip), %xmm1     # 0x8008
subsd 0x190(%rbx), %xmm1
callq 0x2330
movsd 0x2a16(%rip), %xmm1     # 0x8008
subsd 0x190(%rbx), %xmm1
movsd 0x2ace(%rip), %xmm2     # 0x80d0
addsd %xmm2, %xmm0
divsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movq 0x368(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x288(%rbx), %rax
mulsd (%rax,%r14,8), %xmm0
movq 0x2c8(%rbx), %rax
mulsd (%rax,%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 0x2a9b(%rip), %xmm0     # 0x8120
movsd (%rax,%r14,8), %xmm1
addsd 0x2a95(%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 0x2a70(%rip), %xmm1     # 0x8130
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movq 0x498(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd (%rcx,%r14,8), %xmm0
divsd 0x2a55(%rip), %xmm0     # 0x8138
movq 0x4a0(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
jmp 0x55a7
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
xorpd %xmm0, %xmm0
cmpq %rcx, %rdx
je 0x571e
movq 0x378(%rbx), %rsi
addsd (%rsi,%rcx,8), %xmm0
incq %rcx
jmp 0x5708
movsd 0x8(%rbx), %xmm1
mulsd 0x278(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd 0x280(%rbx), %xmm1
movsd %xmm1, 0x380(%rbx)
movq 0x348(%rbx), %rcx
movsd (%rcx), %xmm0
movq 0x390(%rbx), %rdx
movsd %xmm0, (%rdx)
movq 0x358(%rbx), %rsi
movq 0x398(%rbx), %rdi
movsd (%rsi), %xmm0
movsd %xmm0, (%rdi)
movq 0x3a0(%rbx), %r8
movq 0x3a8(%rbx), %r9
movsd (%r8), %xmm0
movsd %xmm0, (%r9)
pushq $0x1
popq %r14
xorpd %xmm2, %xmm2
movslq %eax, %r10
cmpq %r10, %r14
jge 0x586d
movq 0x328(%rbx), %r10
movsd (%r10,%r14,8), %xmm0
ucomisd %xmm0, %xmm2
jae 0x5810
movsd 0x2856(%rip), %xmm1     # 0x8008
addsd %xmm1, %xmm0
cvtsi2sd %r14d, %xmm1
mulsd 0x8(%rbx), %xmm1
callq 0x2330
xorpd %xmm2, %xmm2
movsd 0x2837(%rip), %xmm1     # 0x8008
divsd %xmm0, %xmm1
movq 0x388(%rbx), %r10
movsd %xmm1, (%r10,%r14,8)
movq 0x390(%rbx), %rdx
movq 0x348(%rbx), %rcx
movq 0x398(%rbx), %rdi
movq 0x358(%rbx), %rsi
movq 0x3a8(%rbx), %r9
movq 0x3a0(%rbx), %r8
movl (%rbx), %eax
jmp 0x5820
movq 0x388(%rbx), %r10
andq $0x0, (%r10,%r14,8)
xorpd %xmm1, %xmm1
mulsd (%rcx,%r14,8), %xmm1
addsd -0x8(%rdx,%r14,8), %xmm1
movsd %xmm1, (%rdx,%r14,8)
movsd (%r10,%r14,8), %xmm0
mulsd (%rsi,%r14,8), %xmm0
addsd -0x8(%rdi,%r14,8), %xmm0
movsd %xmm0, (%rdi,%r14,8)
movsd (%r10,%r14,8), %xmm0
mulsd (%r8,%r14,8), %xmm0
addsd -0x8(%r9,%r14,8), %xmm0
movsd %xmm0, (%r9,%r14,8)
incq %r14
jmp 0x578b
addq $0x8, %rsp
popq %rbx
popq %r14
retq