// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
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 0x13c30
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70d4a
leaq 0x806da(%rip), %rsi     # 0xf13f0
leaq 0xa2d83(%rip), %rdx     # 0x113aa0
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x802fe(%rip), %rsi     # 0xf1034
leaq 0xa2e0f(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xd32, %ecx            # imm = 0xD32
jmp 0x70e3a
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 0x12be6
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70dc3
leaq 0x8065e(%rip), %rsi     # 0xf13f0
leaq 0xa2dd5(%rip), %rdx     # 0x113b6e
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x80282(%rip), %rsi     # 0xf1034
leaq 0xa2d93(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xd3e, %ecx            # imm = 0xD3E
jmp 0x70e3a
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 0x1337c
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70e5d
leaq 0x805e5(%rip), %rsi     # 0xf13f0
leaq 0xa2e23(%rip), %rdx     # 0x113c35
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x80209(%rip), %rsi     # 0xf1034
leaq 0xa2d1a(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xd4a, %ecx            # imm = 0xD4A
movq %r14, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %ebp
movl %ebp, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpw $0x1, 0x36(%r14)
jne 0x70fd7
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 0x13c30
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70edf
leaq 0x80545(%rip), %rsi     # 0xf13f0
leaq 0xa2e47(%rip), %rdx     # 0x113cf9
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x80169(%rip), %rsi     # 0xf1034
leaq 0xa2c7a(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xd59, %ecx            # imm = 0xD59
jmp 0x70e3a
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 0x12be6
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70f5b
leaq 0x804c9(%rip), %rsi     # 0xf13f0
leaq 0xa2e7d(%rip), %rdx     # 0x113dab
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x800ed(%rip), %rsi     # 0xf1034
leaq 0xa2bfe(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xd65, %ecx            # imm = 0xD65
jmp 0x70e3a
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 0x1337c
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70fd7
leaq 0x8044d(%rip), %rsi     # 0xf13f0
leaq 0xa2ed1(%rip), %rdx     # 0x113e7b
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x80071(%rip), %rsi     # 0xf1034
leaq 0xa2b82(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xd71, %ecx            # imm = 0xD71
jmp 0x70e3a
movl 0xf08(%r14), %r12d
testl %r12d, %r12d
jle 0x71259
incl %r12d
leal -0x2(%r12), %eax
movq 0xf20(%r14), %rcx
movslq 0xf04(%r14), %rdx
movslq %eax, %r13
imulq %r13, %rdx
movslq 0xe80(%r14), %rax
addq %rdx, %rax
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xe88(%r14), %rax
movslq %edx, %rdx
addq %rdx, %rax
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rsp)
movslq 0xe8c(%r14), %rax
addq %rdx, %rax
movsd (%rcx,%rax,8), %xmm0
movaps %xmm0, 0x10(%rsp)
movslq 0xe94(%r14), %rax
addq %rdx, %rax
movsd (%rcx,%rax,8), %xmm0
callq 0x8670
movapd %xmm0, %xmm8
movq 0xf20(%r14), %rax
movslq 0xf04(%r14), %rcx
imulq %r13, %rcx
movslq 0xe90(%r14), %rdx
addq %rcx, %rdx
movsd %xmm0, (%rax,%rdx,8)
cmpw $0x1, 0x36(%r14)
jne 0x71124
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 0x8670
movsd 0x28(%rsp), %xmm8
movq 0xf20(%r14), %rax
movslq 0xf04(%r14), %rcx
imulq %r13, %rcx
movslq 0xed4(%r14), %rdx
addq %rcx, %rdx
movsd %xmm0, (%rax,%rdx,8)
mulsd %xmm0, %xmm8
movsd 0x8(%rsp), %xmm6
movapd %xmm6, %xmm0
mulsd %xmm8, %xmm0
movapd %xmm0, %xmm1
xorpd %xmm4, %xmm4
cmpeqsd %xmm4, %xmm1
movapd %xmm1, %xmm2
andnpd %xmm0, %xmm2
movsd 0x9f5c8(%rip), %xmm0    # 0x110718
andpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd %xmm6, %xmm5
mulsd %xmm6, %xmm5
movapd %xmm5, %xmm2
movsd (%rsp), %xmm7
addsd %xmm7, %xmm2
mulsd %xmm8, %xmm2
movq 0xf20(%r14), %rax
movslq 0xf04(%r14), %rcx
imulq %r13, %rcx
movslq 0xe98(%r14), %rdx
addq %rcx, %rdx
movsd %xmm2, (%rax,%rdx,8)
movapd %xmm6, %xmm2
mulsd %xmm7, %xmm2
mulsd 0x814dd(%rip), %xmm2    # 0xf2680
movapd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm2
movapd %xmm5, %xmm3
mulsd %xmm6, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm8, %xmm3
movslq 0xe9c(%r14), %rdx
movslq %ecx, %rcx
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 0x711fa
jnp 0x7134c
movapd %xmm7, %xmm2
divsd %xmm6, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm5
divsd %xmm6, %xmm0
andpd 0x8002a(%rip), %xmm0    # 0xf1240
addsd %xmm5, %xmm0
ucomisd %xmm4, %xmm0
jb 0x71226
sqrtsd %xmm0, %xmm0
jmp 0x7122b
callq 0x82b0
movq 0xf20(%r14), %rax
movslq 0xf04(%r14), %rcx
imulq %r13, %rcx
movslq 0xefc(%r14), %rdx
addq %rcx, %rdx
movsd %xmm0, (%rax,%rdx,8)
decl %r12d
cmpl $0x1, %r12d
jg 0x70fea
movq 0xf20(%r14), %rdi
movl 0xefc(%r14), %ecx
movl 0xf04(%r14), %esi
movl 0xf08(%r14), %edx
movl 0x1c8(%r15), %r8d
movq %rbx, %r9
callq 0x1b210
cmpl $0x1, %eax
jne 0x712c4
leaq 0x80160(%rip), %rsi     # 0xf13f0
leaq 0xa2d1e(%rip), %rdx     # 0x113fb5
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x7fd84(%rip), %rsi     # 0xf1034
leaq 0xa2895(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xdb4, %ecx            # imm = 0xDB4
jmp 0x70e3a
xorl %ebp, %ebp
cmpw $0x1, 0x30(%r14)
jne 0x70e49
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 0x13c30
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x713a3
leaq 0x800d8(%rip), %rsi     # 0xf13f0
leaq 0xa2d25(%rip), %rdx     # 0x114044
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x7fcfc(%rip), %rsi     # 0xf1034
leaq 0xa280d(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xdc3, %ecx            # imm = 0xDC3
jmp 0x70e3a
leaq 0xa2bfd(%rip), %rsi     # 0x113f50
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x30c2c
leaq 0x7fd19(%rip), %rsi     # 0xf1082
leaq 0xa2bf8(%rip), %rdx     # 0x113f68
leaq 0x830(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x7fca8(%rip), %rsi     # 0xf1034
leaq 0xa27b9(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xda4, %ecx            # imm = 0xDA4
movq %r15, %r8
jmp 0x70e3d
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 0x12be6
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70e49
leaq 0x80001(%rip), %rsi     # 0xf13f0
leaq 0xa2cf4(%rip), %rdx     # 0x1140ea
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x7fc25(%rip), %rsi     # 0xf1034
leaq 0xa2736(%rip), %rdx     # 0x113b4c
movq %rbx, %rdi
movl $0xdcf, %ecx            # imm = 0xDCF
jmp 0x70e3a