// File: /workspace/github_c_makefile_star2_O3/PoulinV[P]AxiCLASS/build/../source/thermodynamics.c
// Function: thermodynamics_sources
int thermodynamics_sources(
                           double mz,
                           double * y,
                           double * dy,
                           int index_z,
                           void * thermo_parameters_and_workspace,
                           ErrorMsg error_message
                           ) {

  /** Summary: */

  /** Define local variables */
  /* Shorthand notations */
  double z,x=0.,Tmat,Trad,dTmat;
  /* Varying fundamental constants */
  double sigmaTrescale = 1.,alpha = 1.,me = 1.;
  /* Recfast smoothing */
  double x_previous, weight,s;
  /* Structures as shorthand_notation */
  struct thermodynamics_parameters_and_workspace * ptpaw;
  struct background * pba;
  struct thermodynamics * pth;
  struct thermo_workspace * ptw;
  struct thermo_diffeq_workspace * ptdw;
  struct thermo_vector * ptv;
  int ap_current;

  /* Redshift */
  z = -mz;

  /** - Rename structure fields (just to avoid heavy notations) */

  /* Structs */
  ptpaw = thermo_parameters_and_workspace;
  pba = ptpaw->pba;
  pth = ptpaw->pth;
  /* Thermo workspace & vector */
  ptw = ptpaw->ptw;
  ptdw = ptw->ptdw;
  ptv = ptdw->ptv;
  /* Approximation flag */
  ap_current = ptdw->ap_current;

  if (pth->has_exotic_injection == _TRUE_) {
    /* Tell heating module that it should store the heating at this z in its internal table */
    (pth->in).to_store = _TRUE_;
  }

  /** - Recalculate all quantities at this current redshift: we need
      at least pvecback, ptdw->x_reio, dy[ptv->index_ti_D_Tmat] */

  class_call(thermodynamics_derivs(mz,y,dy,thermo_parameters_and_workspace,error_message),
             error_message,
             error_message);

  /* Assign local variables (note that pvecback is filled through derivs) */
  Trad = ptw->Tcmb*(1.+z);
  Tmat = y[ptv->index_ti_D_Tmat] + Trad;
  /* Note that dy[index_ti_Q] represents dQ/d(-z), thus we need -dy here */
  dTmat = -dy[ptv->index_ti_D_Tmat] + Trad/(1.+z);

  /* Get sigmaT rescale factor from fundamental constants */
  if (pth->has_varconst == _TRUE_) {
    class_call(background_varconst_of_z(pba, z, &alpha, &me),
               pba->error_message,
               pth->error_message);
    sigmaTrescale = alpha*alpha/me/me;
  }

  /* get x */
  x = ptdw->x_reio;

  /** - In the recfast case, we manually smooth the results a bit */

  /* Smoothing if we are shortly after an approximation switch, i.e. if z is within 2 delta after the switch*/
  if ((ap_current != 0) && (z > ptdw->ap_z_limits[ap_current-1]-2*ptdw->ap_z_limits_delta[ap_current])) {

    class_call(thermodynamics_ionization_fractions(z,y,pba,pth,ptw,ap_current-1),
               pth->error_message,
               error_message);

    x_previous = ptdw->x_reio;
    // get s from 0 to 1
    s = (ptdw->ap_z_limits[ap_current-1]-z)/(2*ptdw->ap_z_limits_delta[ap_current]);
    // infer f2(x) = smooth function interpolating from 0 to 1
    weight = f2(s);

    /* get smoothed x */
    x = weight*x+(1.-weight)*x_previous;
  }

  /** - Store the results in the table. Results are obtained in order of decreasing z, and stored in order of growing z */

  /* ionization fraction */
  pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_xe] = x;

  /* Tb */
  pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_Tb] = Tmat;

  /* Baryon temperature derivative */
  pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_dTb] = dTmat;

  /* wb = (k_B/mu) Tb */
  pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_wb]
    = _k_B_ / ( _c_ * _c_ * _m_H_ ) * (1. + (1./_not4_ - 1.) * ptw->YHe + x * (1.-ptw->YHe)) * Tmat;

  /* cb2 = (k_B/mu) Tb (1-1/3 dlnTb/dlna) = (k_B/mu) Tb (1 + 1/3 (1+z) dlnTb/dz) */
  pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_cb2]
    = _k_B_ / ( _c_ * _c_ * _m_H_ ) * (1. + (1./_not4_ - 1.) * ptw->YHe + x * (1.-ptw->YHe)) * Tmat * (1. + (1.+z) * dTmat / Tmat / 3.);

  /* dkappa/dtau = a n_e x_e sigma_T = a^{-2} n_e(today) x_e sigma_T (in units of 1/Mpc) */
  pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_dkappa]
    = (1.+z) * (1.+z) * ptw->SIunit_nH0 * x * sigmaTrescale * _sigma_ * _Mpc_over_m_;

  if (pba->has_idm == _TRUE_) {
    pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_T_idm] = ptdw->T_idm;
    pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_c2_idm] = ptdw->c2_idm;
    if (pth->has_idm_g == _TRUE_) {
      pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_dmu_idm_g] = ptdw->dmu_idm_g;
    }
    if (pth->has_idm_dr == _TRUE_) {
      pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_dmu_idm_dr]  = ptdw->dmu_idm_dr;
      pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_ddmu_idm_dr] = ptdw->Sinv_idm_dr * ptdw->dmu_idm_dr; /* store temporarily in 'wrong' column */
    }
    if (pth->has_idm_b == _TRUE_){
      pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size+pth->index_th_R_idm_b] = ptdw->R_idm_b;
    }
  }
  if (pba->has_idr == _TRUE_) {
    pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_T_idr] = pba->T_idr*(1.+z);
    pth->thermodynamics_table[(pth->tt_size-index_z-1)*pth->th_size + pth->index_th_dmu_idr] = ptdw->dmu_idr;
  }

  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x878, %rsp            # imm = 0x878
movq %r8, %r12
movl %edx, 0x34(%rsp)
movq %rsi, %r14
movq %rdi, %r13
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movq (%rcx), %r15
movq 0x10(%rcx), %rdx
movq 0x18(%rcx), %rax
movq %rax, 0x10(%rsp)
movq 0x80(%rax), %rax
movq 0xb8(%rax), %rbp
movq %rax, %rbx
movslq 0x90(%rax), %rax
movq %rax, 0x48(%rsp)
cmpw $0x1, 0xb8(%rdx)
jne 0x6d3f4
movl $0x1, 0x62c(%rdx)
movq %rdx, 0x8(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %rcx, %rdx
movq %r12, %rcx
movapd %xmm0, 0x50(%rsp)
callq 0x6c82a
testl %eax, %eax
je 0x6d46b
leaq 0x80fd5(%rip), %rsi     # 0xee3f0
leaq 0xa2f17(%rip), %rdx     # 0x110339
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x80bf9(%rip), %rsi     # 0xee034
leaq 0xa2f44(%rip), %rdx     # 0x110386
movq %r12, %rdi
movl $0xb85, %ecx            # imm = 0xB85
movq %rbx, %r8
xorl %eax, %eax
callq 0x30af0
movl $0x1, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd 0x80dbd(%rip), %xmm0    # 0xee230
movapd 0x50(%rsp), %xmm1
xorpd %xmm1, %xmm0
movsd 0x80ddb(%rip), %xmm5    # 0xee260
movapd %xmm5, %xmm4
subsd %xmm1, %xmm4
movq 0x10(%rsp), %rcx
movsd 0x38(%rcx), %xmm6
mulsd %xmm4, %xmm6
movslq 0xc(%rbp), %rax
movsd (%r13,%rax,8), %xmm7
addsd %xmm6, %xmm7
divsd %xmm4, %xmm6
subsd (%r14,%rax,8), %xmm6
movq 0x8(%rsp), %rdx
cmpw $0x1, 0xe78(%rdx)
movq %r15, %rbp
movq %rbx, %r14
jne 0x6d581
movsd %xmm7, 0x18(%rsp)
movsd %xmm6, 0x20(%rsp)
movsd %xmm4, 0x28(%rsp)
leaq 0x40(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbp, %rdi
movapd %xmm0, 0x60(%rsp)
callq 0x5e941
cmpl $0x1, %eax
jne 0x6d547
addq $0xd1c, %rbp            # imm = 0xD1C
leaq 0x80ee6(%rip), %rsi     # 0xee3f0
leaq 0xa035f(%rip), %rdx     # 0x10d870
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x30af0
movq 0x8(%rsp), %rdi
addq $0x1064, %rdi           # imm = 0x1064
leaq 0x80afe(%rip), %rsi     # 0xee034
leaq 0xa2e49(%rip), %rdx     # 0x110386
movl $0xb91, %ecx            # imm = 0xB91
jmp 0x6d637
movsd 0x40(%rsp), %xmm5
mulsd %xmm5, %xmm5
movsd 0x38(%rsp), %xmm0
divsd %xmm0, %xmm5
divsd %xmm0, %xmm5
movq 0x8(%rsp), %rdx
movsd 0x28(%rsp), %xmm4
movq 0x10(%rsp), %rcx
movsd 0x20(%rsp), %xmm6
movsd 0x18(%rsp), %xmm7
movapd 0x60(%rsp), %xmm0
movsd 0x18(%r14), %xmm8
movq 0x48(%rsp), %r15
testl %r15d, %r15d
je 0x6d6d0
movq 0xa0(%r14), %rax
movq 0xa8(%r14), %rsi
movsd -0x8(%rax,%r15,8), %xmm3
movsd (%rsi,%r15,8), %xmm1
addsd %xmm1, %xmm1
subsd %xmm1, %xmm3
ucomisd %xmm3, %xmm0
jbe 0x6d6d0
movsd %xmm8, 0x10(%rsp)
movsd %xmm7, 0x18(%rsp)
movsd %xmm6, 0x20(%rsp)
movsd %xmm5, 0x8(%rsp)
movsd %xmm4, 0x28(%rsp)
leaq -0x1(%r15), %r8
movq %r13, %rdi
movq %rbp, %rsi
movq %rdx, %rbx
movq %rcx, %r13
callq 0x6fe5b
testl %eax, %eax
je 0x6d63f
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x80de8(%rip), %rsi     # 0xee3f0
leaq 0xa2d8e(%rip), %rdx     # 0x11039d
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x80a0c(%rip), %rsi     # 0xee034
leaq 0xa2d57(%rip), %rdx     # 0x110386
movq %r12, %rdi
movl $0xb9f, %ecx            # imm = 0xB9F
movq %r14, %r8
jmp 0x6d44d
movq 0xa0(%r14), %rax
movq 0xa8(%r14), %rcx
movapd 0x50(%rsp), %xmm8
addsd -0x8(%rax,%r15,8), %xmm8
movsd (%rcx,%r15,8), %xmm0
addsd %xmm0, %xmm0
divsd %xmm0, %xmm8
movapd %xmm8, %xmm0
divsd 0x83e08(%rip), %xmm8    # 0xf1480
addsd 0x8164f(%rip), %xmm8    # 0xeecd0
mulsd %xmm0, %xmm0
mulsd %xmm0, %xmm8
mulsd 0x81fdd(%rip), %xmm8    # 0xef670
movsd 0x80bc5(%rip), %xmm0    # 0xee260
subsd %xmm8, %xmm0
mulsd 0x18(%r14), %xmm0
mulsd 0x10(%rsp), %xmm8
addsd %xmm0, %xmm8
movq %rbx, %rdx
movsd 0x28(%rsp), %xmm4
movq %r13, %rcx
movsd 0x8(%rsp), %xmm5
movsd 0x20(%rsp), %xmm6
movsd 0x18(%rsp), %xmm7
movl 0x34(%rsp), %esi
notl %esi
addl 0xf08(%rdx), %esi
movq 0xf20(%rdx), %r8
movslq 0xf04(%rdx), %rax
movslq %esi, %rsi
imulq %rax, %rsi
movslq 0xe7c(%rdx), %rax
addq %rsi, %rax
movsd %xmm8, (%r8,%rax,8)
movslq 0xee4(%rdx), %rax
movslq %esi, %rdi
addq %rdi, %rax
movsd %xmm7, (%r8,%rax,8)
movslq 0xee8(%rdx), %rax
addq %rdi, %rax
movsd %xmm6, (%r8,%rax,8)
movsd 0x18(%rcx), %xmm2
movsd 0x9ff1f(%rip), %xmm0    # 0x10d650
movsd 0x80b27(%rip), %xmm1    # 0xee260
movapd %xmm1, %xmm3
subsd %xmm2, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
mulsd %xmm8, %xmm3
addsd %xmm2, %xmm3
movsd 0x9fefe(%rip), %xmm2    # 0x10d658
mulsd %xmm2, %xmm3
mulsd %xmm7, %xmm3
movslq 0xeec(%rdx), %rax
addq %rdi, %rax
movsd %xmm3, (%r8,%rax,8)
movsd 0x18(%rcx), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm4, %xmm6
divsd %xmm7, %xmm6
divsd 0x81ef5(%rip), %xmm6    # 0xef680
addsd %xmm1, %xmm0
addsd %xmm1, %xmm6
subsd %xmm3, %xmm1
mulsd %xmm8, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm7, %xmm1
mulsd %xmm1, %xmm6
movslq 0xef0(%rdx), %rax
addq %rdi, %rax
movsd %xmm6, (%r8,%rax,8)
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
mulsd 0x30(%rcx), %xmm0
mulsd %xmm8, %xmm0
mulsd %xmm0, %xmm5
mulsd 0x8548e(%rip), %xmm5    # 0xf2c68
mulsd 0x85306(%rip), %xmm5    # 0xf2ae8
movslq 0xe80(%rdx), %rax
addq %rdi, %rax
movsd %xmm5, (%r8,%rax,8)
cmpw $0x1, 0x4b6(%rbp)
jne 0x6d899
movsd 0x38(%r14), %xmm0
movslq 0xea0(%rdx), %rax
addq %rdi, %rax
movsd %xmm0, (%r8,%rax,8)
movsd 0x50(%r14), %xmm0
movslq 0xea4(%rdx), %rax
addq %rdi, %rax
movsd %xmm0, (%r8,%rax,8)
cmpw $0x1, 0x36(%rdx)
jne 0x6d849
movsd 0x48(%r14), %xmm0
movslq 0xec8(%rdx), %rax
addq %rdi, %rax
movsd %xmm0, (%r8,%rax,8)
cmpw $0x1, 0x38(%rdx)
jne 0x6d87c
movsd 0x58(%r14), %xmm0
movslq 0xeac(%rdx), %rax
addq %rdi, %rax
movsd %xmm0, (%r8,%rax,8)
mulsd 0x68(%r14), %xmm0
movslq 0xeb0(%rdx), %rax
addq %rdi, %rax
movsd %xmm0, (%r8,%rax,8)
cmpw $0x1, 0x34(%rdx)
jne 0x6d899
movsd 0x30(%r14), %xmm0
movslq 0xed8(%rdx), %rax
addq %rdi, %rax
movsd %xmm0, (%r8,%rax,8)
xorl %eax, %eax
cmpw $0x1, 0x4c6(%rbp)
jne 0x6d459
mulsd 0x100(%rbp), %xmm4
movslq 0xea8(%rdx), %rsi
addq %rdi, %rsi
movsd %xmm4, (%r8,%rsi,8)
movsd 0x60(%r14), %xmm0
movslq 0xeb8(%rdx), %rsi
addq %rdi, %rsi
movsd %xmm0, (%r8,%rsi,8)
jmp 0x6d459