// File: /workspace/github_cpp_makefile_star2_O1/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 0x55d5
cvtsi2sdl 0x20(%rbx), %xmm0
cvtsi2sd %ebp, %xmm1
mulsd 0x8(%rbx), %xmm1
addsd %xmm0, %xmm1
addsd 0x2b18(%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 0x55e2
movq 0x128(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
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 0x2aa4(%rip), %xmm1     # 0x80d0
callq 0x2340
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 0x2340
mulsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movq 0x350(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd 0x2971(%rip), %xmm0     # 0x8008
subsd 0x1c0(%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
callq 0x2340
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 0x56e8
movsd %xmm1, (%rax,%r14,8)
leaq -0x1(%r14), %r15
movq 0x2a8(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x48(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd 0x218(%rbx), %xmm1
addsd 0x29b8(%rip), %xmm1     # 0x80d0
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x370(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x288(%rbx), %rax
movq 0x290(%rbx), %rcx
movsd (%rax,%r14,8), %xmm0
divsd 0x2995(%rip), %xmm0     # 0x80e0
movsd (%rcx,%r14,8), %xmm1
movsd %xmm1, (%rsp)
movsd 0x28aa(%rip), %xmm1     # 0x8008
subsd 0x1a0(%rbx), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x310(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd 0x1a0(%rbx), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x338(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movsd 0x508(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x57b5
movsd %xmm1, (%rax,%r14,8)
movq 0x350(%rbx), %rcx
movsd 0x2844(%rip), %xmm0     # 0x8008
subsd (%rcx,%r14,8), %xmm0
movq 0x340(%rbx), %rcx
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rcx,%r14,8)
movsd (%rax,%r14,8), %xmm0
movq 0x298(%rbx), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x48(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd 0x218(%rbx), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
movq 0x358(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x338(%rbx), %rcx
movq 0x348(%rbx), %rdx
movsd (%rcx,%r14,8), %xmm0
movq 0x350(%rbx), %rsi
mulsd (%rsi,%r14,8), %xmm0
movsd %xmm0, (%rdx,%r14,8)
movq 0x2f0(%rbx), %rdx
movsd (%rdx,%r14,8), %xmm0
mulsd (%rcx,%r14,8), %xmm0
movq 0x48(%rbx), %rcx
movsd 0x27a4(%rip), %xmm1     # 0x8008
subsd (%rcx,%r14,8), %xmm1
mulsd %xmm0, %xmm1
movq 0x3c0(%rbx), %rdx
movsd %xmm1, (%rdx,%r14,8)
movq 0x2f8(%rbx), %rcx
movq 0x3b8(%rbx), %rsi
addsd (%rcx,%r14,8), %xmm1
movsd %xmm1, (%rsi,%r14,8)
movq 0x3c8(%rbx), %rcx
movsd (%rdx,%r15,8), %xmm1
mulsd 0x2866(%rip), %xmm1     # 0x8110
divsd 0x2866(%rip), %xmm1     # 0x8118
addsd (%rcx,%r15,8), %xmm1
movsd %xmm1, (%rcx,%r14,8)
movsd 0x518(%rbx), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x58e9
addsd 0x2734(%rip), %xmm0     # 0x8008
movsd %xmm0, (%rcx,%r14,8)
cmpl $0x0, 0x520(%rbx)
jne 0x58e9
movl %ebp, 0x520(%rbx)
movsd (%rcx,%r14,8), %xmm0
movq 0x300(%rbx), %rcx
movq 0x3d0(%rbx), %rdx
addsd (%rcx,%r14,8), %xmm0
movsd %xmm0, (%rdx,%r14,8)
movq 0x330(%rbx), %rcx
movq 0x340(%rbx), %rdx
movsd (%rdx,%r14,8), %xmm1
subsd (%rax,%r14,8), %xmm1
movsd %xmm1, (%rcx,%r14,8)
movsd 0x500(%rbx), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x593d
movsd %xmm0, (%rcx,%r14,8)
movq 0x50(%rbx), %rax
movsd (%rax,%r14,8), %xmm1
mulsd (%rcx,%r14,8), %xmm1
movq 0x320(%rbx), %rax
movsd %xmm1, (%rax,%r14,8)
movsd 0x510(%rbx), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x596e
movsd %xmm0, (%rax,%r14,8)
movsd (%rcx,%r14,8), %xmm1
subsd (%rax,%r14,8), %xmm1
movq 0x308(%rbx), %rax
movsd %xmm1, (%rax,%r14,8)
movsd 0x4d0(%rbx), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x599b
movsd %xmm0, (%rax,%r14,8)
movsd (%rax,%r14,8), %xmm1
mulsd 0x2737(%rip), %xmm1     # 0x80e0
movq 0x288(%rbx), %rcx
movq 0x318(%rbx), %rax
divsd (%rcx,%r14,8), %xmm1
movsd %xmm1, (%rax,%r14,8)
movsd 0x4f8(%rbx), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x59d7
movsd %xmm0, (%rax,%r14,8)
movsd 0x190(%rbx), %xmm1
movsd 0x198(%rbx), %xmm0
addsd 0x2619(%rip), %xmm0     # 0x8008
movsd %xmm0, (%rsp)
movsd (%rax,%r14,8), %xmm0
divsd (%rax,%r15,8), %xmm0
divsd 0x8(%rbx), %xmm1
callq 0x2340
mulsd (%rsp), %xmm0
addsd 0x26b9(%rip), %xmm0     # 0x80d0
movq 0x328(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq