// File: /workspace/github_c_makefile_star2_O3/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 0x13d65
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e29f
leaq 0x80185(%rip), %rsi     # 0xee3f0
leaq 0xa287e(%rip), %rdx     # 0x110af0
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7fda9(%rip), %rsi     # 0xee034
leaq 0xa290a(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xd32, %ecx            # imm = 0xD32
jmp 0x6e38f
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 0x12d5d
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e318
leaq 0x80109(%rip), %rsi     # 0xee3f0
leaq 0xa28d0(%rip), %rdx     # 0x110bbe
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7fd2d(%rip), %rsi     # 0xee034
leaq 0xa288e(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xd3e, %ecx            # imm = 0xD3E
jmp 0x6e38f
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 0x134f5
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e3b0
leaq 0x80090(%rip), %rsi     # 0xee3f0
leaq 0xa291e(%rip), %rdx     # 0x110c85
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7fcb4(%rip), %rsi     # 0xee034
leaq 0xa2815(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xd4a, %ecx            # imm = 0xD4A
movq %r14, %r8
xorl %eax, %eax
callq 0x30af0
movl $0x1, %ebp
movl %ebp, %eax
addq $0x1030, %rsp           # imm = 0x1030
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpw $0x1, 0x36(%r14)
jne 0x6e52a
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 0x13d65
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e432
leaq 0x7fff2(%rip), %rsi     # 0xee3f0
leaq 0xa2944(%rip), %rdx     # 0x110d49
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7fc16(%rip), %rsi     # 0xee034
leaq 0xa2777(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xd59, %ecx            # imm = 0xD59
jmp 0x6e38f
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 0x12d5d
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e4ae
leaq 0x7ff76(%rip), %rsi     # 0xee3f0
leaq 0xa297a(%rip), %rdx     # 0x110dfb
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7fb9a(%rip), %rsi     # 0xee034
leaq 0xa26fb(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xd65, %ecx            # imm = 0xD65
jmp 0x6e38f
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 0x134f5
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e52a
leaq 0x7fefa(%rip), %rsi     # 0xee3f0
leaq 0xa29ce(%rip), %rdx     # 0x110ecb
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7fb1e(%rip), %rsi     # 0xee034
leaq 0xa267f(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xd71, %ecx            # imm = 0xD71
jmp 0x6e38f
movl 0xf04(%r14), %esi
movl 0xf08(%r14), %r12d
movq 0xf20(%r14), %rdi
testl %r12d, %r12d
jle 0x6e799
movl 0xe80(%r14), %r8d
movl 0xe94(%r14), %edx
incl %r12d
leal -0x2(%r12), %ebp
imull %ebp, %esi
addl %esi, %r8d
movslq %r8d, %rax
movsd (%rdi,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xe88(%r14), %rax
movslq %esi, %rcx
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)
addl %edx, %ecx
movslq %ecx, %rax
movsd (%rdi,%rax,8), %xmm0
callq 0x8670
movapd %xmm0, %xmm8
movq 0xf20(%r14), %rdi
movl 0xf04(%r14), %esi
movl %esi, %eax
imull %ebp, %eax
movslq 0xe90(%r14), %rdx
movslq %eax, %rcx
addq %rcx, %rdx
movsd %xmm0, (%rdi,%rdx,8)
cmpw $0x1, 0x36(%r14)
jne 0x6e67d
movslq 0xec8(%r14), %rax
addq %rcx, %rax
movsd 0x8(%rsp), %xmm0
addsd (%rdi,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xecc(%r14), %rax
addq %rcx, %rax
movsd (%rsp), %xmm0
addsd (%rdi,%rax,8), %xmm0
movsd %xmm0, (%rsp)
movslq 0xed0(%r14), %rax
addq %rcx, %rax
movapd 0x10(%rsp), %xmm0
addsd (%rdi,%rax,8), %xmm0
movapd %xmm0, 0x10(%rsp)
movslq 0xed4(%r14), %rax
addq %rcx, %rax
movsd (%rdi,%rax,8), %xmm0
movsd %xmm8, 0x28(%rsp)
callq 0x8670
movsd 0x28(%rsp), %xmm8
movq 0xf20(%r14), %rdi
movl 0xf04(%r14), %esi
imull %esi, %ebp
movslq 0xed4(%r14), %rax
movslq %ebp, %rcx
addq %rax, %rcx
movsd %xmm0, (%rdi,%rcx,8)
mulsd %xmm0, %xmm8
movl %ebp, %eax
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 0x9f08f(%rip), %xmm0    # 0x10d738
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), %rcx
movslq %eax, %r9
addq %r9, %rcx
movsd %xmm2, (%rdi,%rcx,8)
movapd %xmm5, %xmm2
mulsd %xmm6, %xmm2
mulsd 0x80f93(%rip), %xmm2    # 0xef680
movapd 0x10(%rsp), %xmm7
addsd %xmm7, %xmm2
movapd %xmm0, %xmm3
mulsd %xmm5, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm8, %xmm3
movslq 0xe9c(%r14), %rcx
addq %r9, %rcx
movsd %xmm3, (%rdi,%rcx,8)
movl 0xe94(%r14), %edx
leal (%rdx,%r9), %ecx
movslq %ecx, %rcx
movlpd %xmm1, (%rdi,%rcx,8)
movl 0xe80(%r14), %r8d
addl %r8d, %r9d
movslq %r9d, %rcx
movsd (%rdi,%rcx,8), %xmm1
ucomisd %xmm4, %xmm1
jne 0x6e748
jnp 0x6e87a
movapd %xmm7, %xmm2
movapd %xmm6, %xmm3
divsd %xmm5, %xmm3
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm0
divsd %xmm5, %xmm2
andpd 0x7fad8(%rip), %xmm2    # 0xee240
addsd %xmm0, %xmm2
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
movl 0xefc(%r14), %ecx
addl %ecx, %eax
cltq
movsd %xmm0, (%rdi,%rax,8)
decl %r12d
cmpl $0x1, %r12d
ja 0x6e559
movl 0xf08(%r14), %r12d
jmp 0x6e7a0
movl 0xefc(%r14), %ecx
movl 0x1c8(%r15), %r8d
movl %r12d, %edx
movq %rbx, %r9
callq 0x1b2e7
cmpl $0x1, %eax
jne 0x6e7f2
leaq 0x7fc32(%rip), %rsi     # 0xee3f0
leaq 0xa2840(%rip), %rdx     # 0x111005
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7f856(%rip), %rsi     # 0xee034
leaq 0xa23b7(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xdb4, %ecx            # imm = 0xDB4
jmp 0x6e38f
xorl %ebp, %ebp
cmpw $0x1, 0x30(%r14)
jne 0x6e39e
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 0x13d65
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e8d1
leaq 0x7fbaa(%rip), %rsi     # 0xee3f0
leaq 0xa2847(%rip), %rdx     # 0x111094
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7f7ce(%rip), %rsi     # 0xee034
leaq 0xa232f(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xdc3, %ecx            # imm = 0xDC3
jmp 0x6e38f
leaq 0xa271f(%rip), %rsi     # 0x110fa0
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x30af0
leaq 0x7f7eb(%rip), %rsi     # 0xee082
leaq 0xa271a(%rip), %rdx     # 0x110fb8
leaq 0x830(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7f77a(%rip), %rsi     # 0xee034
leaq 0xa22db(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xda4, %ecx            # imm = 0xDA4
movq %r15, %r8
jmp 0x6e392
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 0x12d5d
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e39e
leaq 0x7fad3(%rip), %rsi     # 0xee3f0
leaq 0xa2816(%rip), %rdx     # 0x11113a
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7f6f7(%rip), %rsi     # 0xee034
leaq 0xa2258(%rip), %rdx     # 0x110b9c
movq %rbx, %rdi
movl $0xdcf, %ecx            # imm = 0xDCF
jmp 0x6e38f