// File: /workspace/github_cpp_makefile_star2_O2/JRLamontagne[P]cdice_sa/CDICE.cpp
// Function: calcEcon(DICE*, int)
void calcEcon(DICE *dice, int t)
{

  Config *config = &dice->config;
  Econ *econ = &dice->econ;
  Carb *carb = &dice->carb;
  Clim *clim = &dice->clim;
  Dvars *dvars = &dice->dvars;
  Ctrl *ctrl = &dice->ctrl;
  Lims *lims = &dice->lims;
	
  // Variable to hold the appropriate temperature
  double temp = 0.0;
  
  // Where do we get the temperature from?
  if(ctrl->use_doeclim == 1) {
    int this_index = config->hind_ns+(t*config->tstep)-1;
    temp = clim->temp[this_index] - clim->temp[config->i1900];
    //temp = clim->temp[this_index];
  }
  else {
    temp = clim->tatm[t];
  }
  
  // Apply the limit test to this temperature (for numeric
  // stability in the damage calculation
  if(temp < lims->tatm_lo) {
    temp = lims->tatm_lo;
  }
  if(temp > lims->tatm_up) {
    temp = lims->tatm_up;
  }  

  // Marginal cost of abatement
  econ->mcabate[t] = econ->pbacktime[t]*pow(dvars->miu[t],(econ->expcost2-1));
  
  // Damages fraction
  econ->damfrac[t] = econ->a1*temp+econ->a2*pow(temp,econ->a3);
  
  // Capital stock
  econ->k[t] = pow((1-econ->dk),config->tstep)*econ->k[t-1]+config->tstep*econ->i[t-1];
  if(econ->k[t] < lims->k_lo) {
    econ->k[t] = lims->k_lo;
  }
  
  // Carbon price
  econ->cprice[t] = econ->pbacktime[t]*pow(dvars->miu[t],(econ->expcost2-1));
  
  // Gross world product (gross abatement and damages)
  econ->ygross[t] = econ->al[t]*pow((econ->l[t]/1000.0),(1.0-econ->gama))*pow(econ->k[t],econ->gama);
  if(econ->ygross[t] < lims->ygross_lo) {
    econ->ygross[t] = lims->ygross_lo;
  }
  
  // Net output damages
  econ->ynet[t] = econ->ygross[t]*(1.0-econ->damfrac[t]);
  
  // Abatement costs
  econ->abatecost[t] = econ->ygross[t]*econ->cost1[t]*pow(dvars->miu[t],econ->expcost2);
  
  // Damages
  econ->damages[t] = econ->ygross[t]*econ->damfrac[t];
  
  // Industrial emissions
  econ->eind[t] = econ->sigma[t]*econ->ygross[t]*(1.0-dvars->miu[t]);
  
  // Total emissions	
  econ->e[t] = econ->eind[t]+econ->etree[t];
  
  // Cumulative emissions
  econ->cca[t] = econ->cca[t-1]+econ->eind[t-1]*5.0/3.666;
  if(econ->cca[t] > lims->cca_up) {
    econ->cca[t] = lims->cca_up + 1.0;
    if(lims->cca_up_step == 0) {
    	lims->cca_up_step = t;
    }
  }
  
  // Cumulative total emissions
  econ->ccatot[t] = econ->cca[t] + econ->cumetree[t];

  // Gross world product (net of abatement and damages)
  econ->y[t] = econ->ynet[t]-econ->abatecost[t];
  if(econ->y[t] < lims->y_lo) {
    econ->y[t] = lims->y_lo;
  }
  
  // Investment
  econ->i[t] = dvars->s[t]*econ->y[t];
  if(econ->i[t] < lims->i_lo) {
    econ->i[t] = lims->i_lo;
  }

  // Consumption
  econ->c[t] = econ->y[t]-econ->i[t];
  if(econ->c[t] < lims->c_lo) {
    econ->c[t] = lims->c_lo;
  }
  // Per capita consumption
  econ->cpc[t] = 1000.0*econ->c[t]/econ->l[t];
  if(econ->cpc[t] < lims->cpc_lo) {
    econ->cpc[t] = lims->cpc_lo;
  }

  // Real interest rate
  econ->ri[t-1] = (1.0+econ->prstp)*pow((econ->cpc[t]/econ->cpc[t-1]),(econ->elasmu/config->tstep))-1.0;
  
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x1, 0x58(%rdi)
movslq %esi, %r14
jne 0x5145
cvtsi2sdl 0x20(%rbx), %xmm0
cvtsi2sd %ebp, %xmm1
mulsd 0x8(%rbx), %xmm1
addsd %xmm0, %xmm1
addsd 0x2fa8(%rip), %xmm1     # 0x80d0
cvttsd2si %xmm1, %eax
movq 0x140(%rbx), %rcx
cltq
movsd (%rcx,%rax,8), %xmm0
movslq 0x2c(%rbx), %rax
subsd (%rcx,%rax,8), %xmm0
jmp 0x5152
movq 0x128(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movq %r14, %r15
movsd 0x4e8(%rbx), %xmm1
maxsd %xmm0, %xmm1
movsd 0x4f0(%rbx), %xmm0
minsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movq 0x2a8(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x48(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd 0x218(%rbx), %xmm1
addsd 0x2f31(%rip), %xmm1     # 0x80d0
callq 0x2330
mulsd 0x8(%rsp), %xmm0
movq 0x360(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd 0x208(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x210(%rbx), %xmm1
movsd 0x200(%rbx), %xmm2
movsd (%rsp), %xmm0
mulsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
callq 0x2330
mulsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movq 0x350(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd 0x2dfe(%rip), %xmm0     # 0x8008
subsd 0x1c0(%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
callq 0x2330
movsd 0x8(%rbx), %xmm1
movq 0x320(%rbx), %rax
mulsd -0x8(%rax,%r14,8), %xmm1
movq 0x310(%rbx), %rax
mulsd -0x8(%rax,%r14,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%r14,8)
movsd 0x4b0(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x525b
movsd %xmm1, (%rax,%r15,8)
decq %r14
movq 0x2a8(%rbx), %rax
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x48(%rbx), %rax
movsd (%rax,%r15,8), %xmm0
movsd 0x218(%rbx), %xmm1
addsd 0x2e46(%rip), %xmm1     # 0x80d0
callq 0x2330
mulsd (%rsp), %xmm0
movq 0x370(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x288(%rbx), %rax
movq 0x290(%rbx), %rcx
movsd (%rax,%r15,8), %xmm0
divsd 0x2e23(%rip), %xmm0     # 0x80e0
movsd (%rcx,%r15,8), %xmm1
movsd %xmm1, (%rsp)
movsd 0x2d38(%rip), %xmm1     # 0x8008
subsd 0x1a0(%rbx), %xmm1
callq 0x2330
mulsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x310(%rbx), %rax
movsd (%rax,%r15,8), %xmm0
movsd 0x1a0(%rbx), %xmm1
callq 0x2330
mulsd (%rsp), %xmm0
movq 0x338(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x508(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x532b
movsd %xmm1, (%rax,%r15,8)
movapd %xmm1, %xmm0
movq 0x340(%rbx), %rcx
movq 0x350(%rbx), %rdx
movsd 0x2cc7(%rip), %xmm1     # 0x8008
subsd (%rdx,%r15,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%r15,8)
movsd (%rax,%r15,8), %xmm0
movq 0x298(%rbx), %rax
mulsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x48(%rbx), %rax
movsd (%rax,%r15,8), %xmm0
movsd 0x218(%rbx), %xmm1
callq 0x2330
mulsd (%rsp), %xmm0
movq 0x358(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movq 0x338(%rbx), %rcx
movq 0x348(%rbx), %rdx
movsd (%rcx,%r15,8), %xmm0
movq 0x350(%rbx), %rsi
mulsd (%rsi,%r15,8), %xmm0
movsd %xmm0, (%rdx,%r15,8)
movq 0x2f0(%rbx), %rdx
movsd (%rdx,%r15,8), %xmm0
mulsd (%rcx,%r15,8), %xmm0
movq 0x48(%rbx), %rcx
movsd 0x2c30(%rip), %xmm1     # 0x8008
subsd (%rcx,%r15,8), %xmm1
mulsd %xmm0, %xmm1
movq 0x3c0(%rbx), %rdx
movsd %xmm1, (%rdx,%r15,8)
movq 0x2f8(%rbx), %rcx
movq 0x3b8(%rbx), %rsi
addsd (%rcx,%r15,8), %xmm1
movsd %xmm1, (%rsi,%r15,8)
movq 0x3c8(%rbx), %rcx
movsd (%rdx,%r14,8), %xmm0
mulsd 0x2cf2(%rip), %xmm0     # 0x8110
divsd 0x2cf2(%rip), %xmm0     # 0x8118
addsd (%rcx,%r14,8), %xmm0
movsd %xmm0, (%rcx,%r15,8)
movsd 0x518(%rbx), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5461
addsd 0x2bc0(%rip), %xmm1     # 0x8008
movsd %xmm1, (%rcx,%r15,8)
cmpl $0x0, 0x520(%rbx)
jne 0x545d
movl %ebp, 0x520(%rbx)
movapd %xmm1, %xmm0
movq 0x300(%rbx), %rcx
movq 0x3d0(%rbx), %rdx
addsd (%rcx,%r15,8), %xmm0
movsd %xmm0, (%rdx,%r15,8)
movq 0x330(%rbx), %rcx
movq 0x340(%rbx), %rdx
movsd (%rdx,%r15,8), %xmm1
subsd (%rax,%r15,8), %xmm1
movsd %xmm1, (%rcx,%r15,8)
movsd 0x500(%rbx), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x54b3
movsd %xmm0, (%rcx,%r15,8)
movapd %xmm0, %xmm1
movq 0x50(%rbx), %rax
mulsd (%rax,%r15,8), %xmm1
movq 0x320(%rbx), %rax
movsd %xmm1, (%rax,%r15,8)
movsd 0x510(%rbx), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x54e2
movsd %xmm0, (%rax,%r15,8)
movapd %xmm0, %xmm1
movsd (%rcx,%r15,8), %xmm0
subsd %xmm1, %xmm0
movq 0x308(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x4d0(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5511
movsd %xmm1, (%rax,%r15,8)
movapd %xmm1, %xmm0
mulsd 0x2bc7(%rip), %xmm0     # 0x80e0
movq 0x288(%rbx), %rcx
movq 0x318(%rbx), %rax
divsd (%rcx,%r15,8), %xmm0
movsd %xmm0, (%rax,%r15,8)
movsd 0x4f8(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x554b
movsd %xmm1, (%rax,%r15,8)
movapd %xmm1, %xmm0
movsd 0x190(%rbx), %xmm1
movsd 0x198(%rbx), %xmm2
addsd 0x2aa5(%rip), %xmm2     # 0x8008
movsd %xmm2, (%rsp)
divsd (%rax,%r14,8), %xmm0
divsd 0x8(%rbx), %xmm1
callq 0x2330
mulsd (%rsp), %xmm0
addsd 0x2b4b(%rip), %xmm0     # 0x80d0
movq 0x328(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq