// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../source/thermodynamics.c
// Function: thermodynamics_calculate_opticals
int thermodynamics_calculate_opticals(
                                      struct precision* ppr,
                                      struct thermodynamics* pth
                                      ) {

  /** Summary: */

  /** Define local quantities */
  /* Visibility function value */
  double g;
  /* kappa derivative values*/
  double dkappa,ddkappa,dddkappa,expmkappa;
  int index_tau;

  /** - --> second derivative with respect to tau of dkappa (in view of of spline interpolation) */
  class_call(array_spline_table_line_to_line(pth->tau_table,
                                             pth->tt_size,
                                             pth->thermodynamics_table,
                                             pth->th_size,
                                             pth->index_th_dkappa,
                                             pth->index_th_dddkappa,
                                             _SPLINE_EST_DERIV_,
                                             pth->error_message),
             pth->error_message,
             pth->error_message);

  /** - --> first derivative with respect to tau of dkappa (using spline interpolation) */
  class_call(array_derive_spline_table_line_to_line(pth->tau_table,
                                                    pth->tt_size,
                                                    pth->thermodynamics_table,
                                                    pth->th_size,
                                                    pth->index_th_dkappa,
                                                    pth->index_th_dddkappa,
                                                    pth->index_th_ddkappa,
                                                    pth->error_message),
             pth->error_message,
             pth->error_message);

  /** - --> compute -kappa = [int_{tau_today}^{tau} dtau dkappa/dtau], store temporarily in column "g" */
  class_call(array_integrate_spline_table_line_to_line(pth->tau_table,
                                                       pth->tt_size,
                                                       pth->thermodynamics_table,
                                                       pth->th_size,
                                                       pth->index_th_dkappa,
                                                       pth->index_th_dddkappa,
                                                       pth->index_th_g,
                                                       pth->error_message),
             pth->error_message,
             pth->error_message);


  /** if there is idm_g, calculate its optical contributions */
  if (pth->has_idm_g == _TRUE_) {
    /** - --> second derivative with respect to tau of dmu (in view of of spline interpolation) */
    class_call(array_spline_table_line_to_line(pth->tau_table,
                                               pth->tt_size,
                                               pth->thermodynamics_table,
                                               pth->th_size,
                                               pth->index_th_dmu_idm_g,
                                               pth->index_th_dddmu_idm_g,
                                               _SPLINE_EST_DERIV_,
                                               pth->error_message),
               pth->error_message,
               pth->error_message);

    /** - --> first derivative with respect to tau of dmu (using spline interpolation) */
    class_call(array_derive_spline_table_line_to_line(pth->tau_table,
                                                      pth->tt_size,
                                                      pth->thermodynamics_table,
                                                      pth->th_size,
                                                      pth->index_th_dmu_idm_g,
                                                      pth->index_th_dddmu_idm_g,
                                                      pth->index_th_ddmu_idm_g,
                                                      pth->error_message),
               pth->error_message,
               pth->error_message);

    /** - --> compute -mu = [int_{tau_today}^{tau} dtau dmu/dtau], store temporarily in column "exp_mu_idm_g" */
    class_call(array_integrate_spline_table_line_to_line(pth->tau_table,
                                                         pth->tt_size,
                                                         pth->thermodynamics_table,
                                                         pth->th_size,
                                                         pth->index_th_dmu_idm_g,
                                                         pth->index_th_dddmu_idm_g,
                                                         pth->index_th_exp_mu_idm_g,
                                                         pth->error_message),
               pth->error_message,
               pth->error_message);

  }
  /** - --> compute visibility: \f$ g= (d \kappa/d \tau) e^{- \kappa} \f$ */

  /* loop on z (decreasing z, increasing time) */
  for (index_tau=pth->tt_size-1; index_tau>=0; index_tau--) {

    dkappa = pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_dkappa];
    ddkappa = pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_ddkappa];
    dddkappa = pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_dddkappa];
    expmkappa = exp(pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_g]);

    /* This would be the rate without accounting for idm_g : */
    /* pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_rate] = */
    /*   sqrt(pow(dkappa,2)+pow(ddkappa/dkappa,2)+fabs(dddkappa/dkappa)); */

    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_exp_m_kappa] = expmkappa;

    /* presence of idm_g modifies visibility function */
    if (pth->has_idm_g == _TRUE_) {
      dkappa += pth->thermodynamics_table[index_tau*pth->th_size + pth->index_th_dmu_idm_g];
      ddkappa += pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_ddmu_idm_g];
      dddkappa += pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_dddmu_idm_g];
      pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_exp_mu_idm_g] =
        exp(pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_exp_mu_idm_g]); /* we previously calculated -mu_idm_g and stored it here */
      expmkappa *= pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_exp_mu_idm_g];
    }

    /** - ---> compute g */
    g = dkappa * expmkappa;

    /* for some very extreme models, in the last line, the exponential of a large negative number could go beyond
     * the range covered by the "double" representation numbers, and be set to zero. To avoid a division by zero in
     * the next steps, it is then better to set it to the minimum non-zero double (this has no impact on observables). */
    if (g==0.) g=DBL_MIN;

    /** - ---> compute g' (the plus sign of the second term is correct, see def of -kappa in thermodynamics module!) */
    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_dg] =
      (ddkappa + dkappa * dkappa) * expmkappa;

    /** - ---> compute g''  */
    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_ddg] =
      (dddkappa + dkappa * ddkappa * 3. + dkappa * dkappa * dkappa ) * expmkappa;

    /** - ---> store g */
    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_g] = g;

    /** - ---> compute variation rate */
    class_test(pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_dkappa] == 0.,
               pth->error_message,
               "variation rate diverges");

    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_rate] =
      sqrt(pow(dkappa,2)+pow(ddkappa/dkappa,2)+fabs(dddkappa/dkappa));

  }

  /** - smooth the rate (details of smoothing unimportant: only the
      order of magnitude of the rate matters) */
  class_call(array_smooth(pth->thermodynamics_table,
                          pth->th_size,
                          pth->tt_size,
                          pth->index_th_rate,
                          ppr->thermo_rate_smoothing_radius,
                          pth->error_message),
             pth->error_message,
             pth->error_message);

  /** - --> derivatives of baryon sound speed (only computed if some non-minimal tight-coupling schemes is requested) */
  if (pth->compute_cb2_derivatives == _TRUE_) {

    /** - ---> second derivative with respect to tau of cb2 */
    class_call(array_spline_table_line_to_line(pth->tau_table,
                                               pth->tt_size,
                                               pth->thermodynamics_table,
                                               pth->th_size,
                                               pth->index_th_cb2,
                                               pth->index_th_ddcb2,
                                               _SPLINE_EST_DERIV_,
                                               pth->error_message),
               pth->error_message,
               pth->error_message);

    /** - ---> first derivative with respect to tau of cb2 (using spline interpolation) */
    class_call(array_derive_spline_table_line_to_line(pth->tau_table,
                                                      pth->tt_size,
                                                      pth->thermodynamics_table,
                                                      pth->th_size,
                                                      pth->index_th_cb2,
                                                      pth->index_th_ddcb2,
                                                      pth->index_th_dcb2,
                                                      pth->error_message),
               pth->error_message,
               pth->error_message);
  }

  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1030, %rsp           # imm = 0x1030
movq %rsi, %r14
movq %rdi, %r15
movq 0xf18(%rsi), %rdi
movl 0xf08(%rsi), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xe80(%r14), %r8d
movl 0xe8c(%r14), %r9d
leaq 0x1064(%r14), %rbx
pushq %rbx
pushq $0x1
callq 0x12065
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69d3b
leaq 0x7b6e9(%rip), %rsi     # 0xe53f0
leaq 0x9dc82(%rip), %rdx     # 0x107990
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b30d(%rip), %rsi     # 0xe5034
leaq 0x9dd0e(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xd32, %ecx            # imm = 0xD32
jmp 0x69e27
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xe8c(%r14), %r9d
movl 0xe80(%r14), %r8d
movl 0xe88(%r14), %eax
pushq %rbx
pushq %rax
callq 0x110a3
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69db2
leaq 0x7b66f(%rip), %rsi     # 0xe53f0
leaq 0x9dcd6(%rip), %rdx     # 0x107a5e
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b293(%rip), %rsi     # 0xe5034
leaq 0x9dc94(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xd3e, %ecx            # imm = 0xD3E
jmp 0x69e27
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xe80(%r14), %r8d
movl 0xe8c(%r14), %r9d
movl 0xe94(%r14), %eax
pushq %rbx
pushq %rax
callq 0x117fe
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69e46
leaq 0x7b5f8(%rip), %rsi     # 0xe53f0
leaq 0x9dd26(%rip), %rdx     # 0x107b25
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b21c(%rip), %rsi     # 0xe5034
leaq 0x9dc1d(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xd4a, %ecx            # imm = 0xD4A
movq %r14, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x1030, %rsp           # imm = 0x1030
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpw $0x1, 0x36(%r14)
jne 0x69fba
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xec8(%r14), %r8d
movl 0xed0(%r14), %r9d
pushq %rbx
pushq $0x1
callq 0x12065
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69ec6
leaq 0x7b55e(%rip), %rsi     # 0xe53f0
leaq 0x9dd50(%rip), %rdx     # 0x107be9
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b182(%rip), %rsi     # 0xe5034
leaq 0x9db83(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xd59, %ecx            # imm = 0xD59
jmp 0x69e27
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xed0(%r14), %r9d
movl 0xec8(%r14), %r8d
movl 0xecc(%r14), %eax
pushq %rbx
pushq %rax
callq 0x110a3
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69f40
leaq 0x7b4e4(%rip), %rsi     # 0xe53f0
leaq 0x9dd88(%rip), %rdx     # 0x107c9b
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b108(%rip), %rsi     # 0xe5034
leaq 0x9db09(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xd65, %ecx            # imm = 0xD65
jmp 0x69e27
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xec8(%r14), %r8d
movl 0xed0(%r14), %r9d
movl 0xed4(%r14), %eax
pushq %rbx
pushq %rax
callq 0x117fe
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69fba
leaq 0x7b46a(%rip), %rsi     # 0xe53f0
leaq 0x9ddde(%rip), %rdx     # 0x107d6b
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b08e(%rip), %rsi     # 0xe5034
leaq 0x9da8f(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xd71, %ecx            # imm = 0xD71
jmp 0x69e27
movl 0xf08(%r14), %ebp
movq 0xf20(%r14), %rdi
movl 0xf04(%r14), %esi
testl %ebp, %ebp
jle 0x6a1ff
decl %ebp
imull %ebp, %esi
movslq 0xe80(%r14), %rax
movslq %esi, %rcx
addq %rcx, %rax
movsd (%rdi,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xe88(%r14), %rax
addq %rcx, %rax
movsd (%rdi,%rax,8), %xmm0
movsd %xmm0, (%rsp)
movslq 0xe8c(%r14), %rax
addq %rcx, %rax
movsd (%rdi,%rax,8), %xmm0
movaps %xmm0, 0x10(%rsp)
movslq 0xe94(%r14), %rax
addq %rcx, %rax
movsd (%rdi,%rax,8), %xmm0
callq 0x86a0
movapd %xmm0, %xmm8
movq 0xf20(%r14), %rax
movslq 0xf04(%r14), %rcx
movslq %ebp, %r12
imulq %r12, %rcx
movslq 0xe90(%r14), %rdx
addq %rcx, %rdx
movsd %xmm0, (%rax,%rdx,8)
cmpw $0x1, 0x36(%r14)
jne 0x6a0ff
movslq 0xec8(%r14), %rdx
movslq %ecx, %rcx
addq %rcx, %rdx
movsd 0x8(%rsp), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xecc(%r14), %rdx
addq %rcx, %rdx
movsd (%rsp), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rsp)
movslq 0xed0(%r14), %rdx
addq %rcx, %rdx
movapd 0x10(%rsp), %xmm0
addsd (%rax,%rdx,8), %xmm0
movapd %xmm0, 0x10(%rsp)
movslq 0xed4(%r14), %rdx
addq %rcx, %rdx
movsd (%rax,%rdx,8), %xmm0
movsd %xmm8, 0x28(%rsp)
callq 0x86a0
movsd 0x28(%rsp), %xmm8
movq 0xf20(%r14), %rax
movslq 0xf04(%r14), %rcx
imulq %r12, %rcx
movslq 0xed4(%r14), %rdx
addq %rcx, %rdx
movsd %xmm0, (%rax,%rdx,8)
mulsd %xmm0, %xmm8
movsd 0x8(%rsp), %xmm5
movapd %xmm5, %xmm0
mulsd %xmm8, %xmm0
movapd %xmm0, %xmm1
xorpd %xmm4, %xmm4
cmpeqsd %xmm4, %xmm1
movapd %xmm1, %xmm2
andnpd %xmm0, %xmm2
movsd 0x9a4cd(%rip), %xmm0    # 0x1045f8
andpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd %xmm5, %xmm0
mulsd %xmm5, %xmm0
movapd %xmm0, %xmm2
movsd (%rsp), %xmm6
addsd %xmm6, %xmm2
mulsd %xmm8, %xmm2
movslq 0xe98(%r14), %rdx
movslq %ecx, %rcx
addq %rcx, %rdx
movsd %xmm2, (%rax,%rdx,8)
movapd %xmm5, %xmm2
mulsd %xmm6, %xmm2
mulsd 0x7c5d9(%rip), %xmm2    # 0xe6748
movapd 0x10(%rsp), %xmm7
addsd %xmm7, %xmm2
movapd %xmm0, %xmm3
mulsd %xmm5, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm8, %xmm3
movslq 0xe9c(%r14), %rdx
addq %rcx, %rdx
movsd %xmm3, (%rax,%rdx,8)
movslq 0xe94(%r14), %rdx
addq %rcx, %rdx
movlpd %xmm1, (%rax,%rdx,8)
movslq 0xe80(%r14), %rdx
addq %rcx, %rdx
movsd (%rax,%rdx,8), %xmm1
ucomisd %xmm4, %xmm1
jne 0x6a1c3
jnp 0x6a260
movapd %xmm7, %xmm2
movapd %xmm6, %xmm3
divsd %xmm5, %xmm3
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm0
divsd %xmm5, %xmm2
andpd 0x7b05d(%rip), %xmm2    # 0xe5240
addsd %xmm0, %xmm2
sqrtsd %xmm2, %xmm0
movslq 0xefc(%r14), %rdx
addq %rdx, %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x69fc1
movl 0xefc(%r14), %ecx
movl 0xf08(%r14), %edx
movl 0x1c8(%r15), %r8d
movq %rbx, %r9
callq 0x1908a
cmpl $0x1, %eax
jne 0x6a2b7
leaq 0x7b1c4(%rip), %rsi     # 0xe53f0
leaq 0x9dc72(%rip), %rdx     # 0x107ea5
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7ade8(%rip), %rsi     # 0xe5034
leaq 0x9d7e9(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xdb4, %ecx            # imm = 0xDB4
jmp 0x69e27
leaq 0x9dbd9(%rip), %rsi     # 0x107e40
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2e014
leaq 0x7ae05(%rip), %rsi     # 0xe5082
leaq 0x9dbd4(%rip), %rdx     # 0x107e58
leaq 0x830(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7ad94(%rip), %rsi     # 0xe5034
leaq 0x9d795(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xda4, %ecx            # imm = 0xDA4
movq %r15, %r8
jmp 0x69e2a
xorl %ebp, %ebp
cmpw $0x1, 0x30(%r14)
jne 0x69e34
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xef0(%r14), %r8d
movl 0xef8(%r14), %r9d
pushq %rbx
pushq $0x1
callq 0x12065
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x6a339
leaq 0x7b0eb(%rip), %rsi     # 0xe53f0
leaq 0x9dc28(%rip), %rdx     # 0x107f34
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7ad0f(%rip), %rsi     # 0xe5034
leaq 0x9d710(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xdc3, %ecx            # imm = 0xDC3
jmp 0x69e27
movq 0xf18(%r14), %rdi
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xef8(%r14), %r9d
movl 0xef0(%r14), %r8d
movl 0xef4(%r14), %eax
pushq %rbx
pushq %rax
callq 0x110a3
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69e34
leaq 0x7b06d(%rip), %rsi     # 0xe53f0
leaq 0x9dc50(%rip), %rdx     # 0x107fda
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7ac91(%rip), %rsi     # 0xe5034
leaq 0x9d692(%rip), %rdx     # 0x107a3c
movq %rbx, %rdi
movl $0xdcf, %ecx            # imm = 0xDCF
jmp 0x69e27