// File: /workspace/github_cpp_makefile_star2_O3/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), %eax
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x5bc7
movq 0x288(%rbx), %rcx
xorl %r14d, %r14d
movq 0x308(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x266d(%rip), %xmm0     # 0x80e0
divsd (%rcx,%r14,8), %xmm0
movsd 0x2587(%rip), %xmm1     # 0x8008
subsd 0x190(%rbx), %xmm1
callq 0x2340
movsd 0x263a(%rip), %xmm2     # 0x80d0
addsd %xmm2, %xmm0
movsd 0x2566(%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), %rcx
movq 0x2c8(%rbx), %rax
mulsd (%rcx,%r14,8), %xmm0
mulsd (%rax,%r14,8), %xmm0
movq 0x378(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x348(%rbx), %rax
movq 0x358(%rbx), %rdx
movsd (%rax,%r14,8), %xmm0
addsd (%rdx,%r14,8), %xmm0
movq 0x3a0(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x3d0(%rbx), %rax
movq 0x478(%rbx), %rdx
movsd (%rdx,%r14,8), %xmm0
addsd 0x25f7(%rip), %xmm0     # 0x8120
movsd (%rax,%r14,8), %xmm1
addsd 0x25f1(%rip), %xmm1     # 0x8128
divsd %xmm1, %xmm0
movq 0x490(%rbx), %rsi
movsd %xmm0, (%rsi,%r14,8)
movsd (%rdx,%r14,8), %xmm0
subsd 0x3e0(%rbx), %xmm0
movsd (%rax,%r14,8), %xmm1
addsd 0x25cc(%rip), %xmm1     # 0x8130
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movq 0x498(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd (%rdx,%r14,8), %xmm0
divsd 0x25b1(%rip), %xmm0     # 0x8138
movq 0x4a0(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0x5a5e
testl %eax, %eax
jle 0x5bc3
movq 0x378(%rbx), %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
addsd (%rcx,%rdx,8), %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x5bb4
jmp 0x5bc7
xorpd %xmm1, %xmm1
movsd 0x8(%rbx), %xmm0
mulsd 0x278(%rbx), %xmm0
mulsd %xmm1, %xmm0
addsd 0x280(%rbx), %xmm0
movsd %xmm0, 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)
cmpl $0x2, %eax
jl 0x5d22
movl $0x1, %r14d
xorpd %xmm2, %xmm2
movq 0x328(%rbx), %r10
movsd (%r10,%r14,8), %xmm0
ucomisd %xmm0, %xmm2
jae 0x5cbb
movsd 0x23ae(%rip), %xmm1     # 0x8008
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd 0x8(%rbx), %xmm1
callq 0x2340
xorpd %xmm2, %xmm2
movsd 0x238c(%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 0x5cce
movq 0x388(%rbx), %r10
movq $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
movslq %eax, %r10
cmpq %r10, %r14
jl 0x5c3f
addq $0x8, %rsp
popq %rbx
popq %r14
retq