// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../source/thermodynamics.c
// Function: thermodynamics_calculate_damping_scale
int thermodynamics_calculate_damping_scale(
                                           struct background* pba,
                                           struct thermodynamics* pth,
                                           double* pvecback
                                           ) {

  /** Summary: */

  /** Define local variables */
  double R;
  /* Initial time and dkappa/dtau */
  double tau_ini,dkappa_ini;
  double* tau_table_growing;
  int index_tau;
  int last_index_back;

  class_alloc(tau_table_growing,pth->tt_size*sizeof(double),pth->error_message);

  /* find the value of last_index_back for
     tau_table[pth->tt_size-1] = tau_table_growing[0] in order to
     speed up subsequent interpolations in the loop */
  class_call(background_at_tau(pba,
                               pth->tau_table[pth->tt_size-1],
                               normal_info,
                               inter_normal,
                               &last_index_back,
                               pvecback),
             pba->error_message,
             pth->error_message);

  /* compute integrand and store temporarily in column "ddkappa" */
  for (index_tau=0; index_tau < pth->tt_size; index_tau++) {

    tau_table_growing[index_tau] = pth->tau_table[pth->tt_size-1-index_tau];

    class_call(background_at_tau(pba,
                                 tau_table_growing[index_tau],
                                 normal_info,
                                 inter_closeby,
                                 &last_index_back,
                                 pvecback),
               pba->error_message,
               pth->error_message);

    R = 3./4.*pvecback[pba->index_bg_rho_b]/pvecback[pba->index_bg_rho_g];

    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_ddkappa] =
      1./6./pth->thermodynamics_table[(pth->tt_size-1-index_tau)*pth->th_size+pth->index_th_dkappa]
      *(R*R/(1+R)+16./15.)/(1.+R);

  }

  /* compute second derivative of integrand, and store temporarily in column "dddkappa" */
  class_call(array_spline_table_line_to_line(tau_table_growing,
                                             pth->tt_size,
                                             pth->thermodynamics_table,
                                             pth->th_size,
                                             pth->index_th_ddkappa,
                                             pth->index_th_dddkappa,
                                             _SPLINE_EST_DERIV_,
                                             pth->error_message),
             pth->error_message,
             pth->error_message);

  /* compute integratal and store temporarily in column "g" */
  class_call(array_integrate_spline_table_line_to_line(tau_table_growing,
                                                       pth->tt_size,
                                                       pth->thermodynamics_table,
                                                       pth->th_size,
                                                       pth->index_th_ddkappa,
                                                       pth->index_th_dddkappa,
                                                       pth->index_th_g,
                                                       pth->error_message),
             pth->error_message,
             pth->error_message);

  free(tau_table_growing);

  /* we could now write the result as r_d = 2pi * sqrt(integral),
   *  but we will first better acount for the contribution frokm the tau_ini boundary.
   *  Close to this boundary, R=0 and the integrand is just 16/(15*6)/kappa'
   *  Using kappa' propto 1/a^2 and tau propro a during RD, we get the analytic result:
   *  int_0^{tau_ini} dtau / kappa' = tau_ini / 3 / kappa'_ini
   *  Thus r_d = 2pi * sqrt( 16/(15*6*3) * (tau_ini/ kappa'_ini) * integral)
   */

  tau_ini = pth->tau_table[pth->tt_size-1];
  dkappa_ini = pth->thermodynamics_table[(pth->tt_size-1)*pth->th_size+pth->index_th_dkappa];

  for (index_tau=0; index_tau < pth->tt_size; index_tau++) {

    pth->thermodynamics_table[index_tau*pth->th_size+pth->index_th_r_d] =
      2.*_PI_*sqrt(16./(15.*6.*3.)*tau_ini/dkappa_ini
                   +pth->thermodynamics_table[(pth->tt_size-1-index_tau)*pth->th_size+pth->index_th_g]);

  }

  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x818, %rsp            # imm = 0x818
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
movslq 0xf08(%rsi), %rbx
leaq (,%rbx,8), %rdi
callq 0x8490
testq %rax, %rax
je 0x69934
movq %rax, %r15
movq 0xf18(%r14), %rax
movsd -0x8(%rax,%rbx,8), %xmm0
pushq $0x1
popq %rbx
leaq 0x4(%rsp), %rcx
movq %r12, %rdi
movl %ebx, %esi
xorl %edx, %edx
movq %r13, %r8
callq 0x5887d
cmpl $0x1, %eax
jne 0x69978
addq $0xd1c, %r12            # imm = 0xD1C
leaq 0x7bb01(%rip), %rsi     # 0xe53f0
leaq 0x9de47(%rip), %rdx     # 0x10773d
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2e014
addq $0x1064, %r14           # imm = 0x1064
leaq 0x7b71e(%rip), %rsi     # 0xe5034
leaq 0x9ddf9(%rip), %rdx     # 0x107716
movq %r14, %rdi
movl $0xcc8, %ecx            # imm = 0xCC8
movq %r15, %r8
xorl %eax, %eax
callq 0x2e014
jmp 0x69b38
shll $0x3, %ebx
leaq 0x7b6c6(%rip), %rsi     # 0xe5004
leaq 0x9ddbf(%rip), %rdx     # 0x107704
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2e014
addq $0x1064, %r14           # imm = 0x1064
leaq 0x7b6d0(%rip), %rsi     # 0xe5034
leaq 0x9ddab(%rip), %rdx     # 0x107716
movq %r14, %rdi
movl $0xcbc, %ecx            # imm = 0xCBC
jmp 0x69b2b
movl 0xf08(%r14), %esi
pushq $-0x1
popq %rbp
xorl %ebx, %ebx
movslq %esi, %rax
cmpq %rax, %rbx
jge 0x69a72
movq 0xf18(%r14), %rax
addl %ebp, %esi
movslq %esi, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r15,%rbx,8)
movq %r12, %rdi
pushq $0x1
popq %rdx
movl %edx, %esi
leaq 0x4(%rsp), %rcx
movq %r13, %r8
callq 0x5887d
cmpl $0x1, %eax
je 0x69ae7
movslq 0x390(%r12), %rax
movsd (%r13,%rax,8), %xmm0
mulsd 0x8003c(%rip), %xmm0    # 0xe9a18
movslq 0x38c(%r12), %rax
divsd (%r13,%rax,8), %xmm0
movq 0xf20(%r14), %rax
movl 0xf04(%r14), %ecx
movl 0xf08(%r14), %esi
leal (%rsi,%rbp), %edx
imull %ecx, %edx
movslq 0xe80(%r14), %rdi
movslq %edx, %rdx
addq %rdi, %rdx
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
shufpd $0x2, 0x9ac5c(%rip), %xmm1 # xmm1 = xmm1[0],mem[1]
addsd 0x7b834(%rip), %xmm0    # 0xe5260
movapd %xmm0, %xmm2
movhpd (%rax,%rdx,8), %xmm2    # xmm2 = xmm2[0],mem[0]
divpd %xmm2, %xmm1
movapd %xmm1, %xmm2
addsd 0x9abab(%rip), %xmm2    # 0x1045f0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulsd %xmm2, %xmm1
divsd %xmm0, %xmm1
movl %ebx, %edx
imull %ecx, %edx
movslq 0xe88(%r14), %rcx
movslq %edx, %rdx
addq %rcx, %rdx
movsd %xmm1, (%rax,%rdx,8)
incq %rbx
decl %ebp
jmp 0x69984
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xe88(%r14), %r8d
movl 0xe8c(%r14), %r9d
leaq 0x1064(%r14), %r12
movq %r15, %rdi
pushq %r12
pushq $0x1
callq 0x12065
popq %rcx
popq %rdx
cmpl $0x1, %eax
jne 0x69b4c
leaq 0x7b93d(%rip), %rsi     # 0xe53f0
leaq 0x9dd5e(%rip), %rdx     # 0x107818
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b561(%rip), %rsi     # 0xe5034
leaq 0x9dc3c(%rip), %rdx     # 0x107716
movq %r12, %rdi
movl $0xcea, %ecx            # imm = 0xCEA
movq %r14, %r8
jmp 0x69b2e
addq $0xd1c, %r12            # imm = 0xD1C
leaq 0x7b8fb(%rip), %rsi     # 0xe53f0
leaq 0x9dcaf(%rip), %rdx     # 0x1077ab
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2e014
addq $0x1064, %r14           # imm = 0x1064
leaq 0x7b518(%rip), %rsi     # 0xe5034
leaq 0x9dbf3(%rip), %rdx     # 0x107716
movq %r14, %rdi
movl $0xcd6, %ecx            # imm = 0xCD6
movq %r15, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xf08(%r14), %esi
movq 0xf20(%r14), %rdx
movl 0xf04(%r14), %ecx
movl 0xe88(%r14), %r8d
movl 0xe8c(%r14), %r9d
movl 0xe94(%r14), %eax
movq %r15, %rdi
pushq %r12
pushq %rax
callq 0x117fe
popq %rcx
popq %rdx
cmpl $0x1, %eax
pushq $0x1
popq %rbx
jne 0x69bc9
leaq 0x7b85e(%rip), %rsi     # 0xe53f0
leaq 0x9dd2f(%rip), %rdx     # 0x1078c8
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x7b482(%rip), %rsi     # 0xe5034
leaq 0x9db5d(%rip), %rdx     # 0x107716
movq %r12, %rdi
movl $0xcf6, %ecx            # imm = 0xCF6
movq %r14, %r8
jmp 0x69928
movq %r15, %rdi
callq 0x8430
movq 0xf18(%r14), %rax
movslq 0xf08(%r14), %rdx
movsd -0x8(%rax,%rdx,8), %xmm1
movq %rdx, %rsi
decq %rsi
movq 0xf20(%r14), %rax
movl 0xf04(%r14), %ecx
imull %ecx, %esi
movslq 0xe80(%r14), %rdi
movslq %esi, %rsi
mulsd 0x9a9da(%rip), %xmm1    # 0x1045e8
addq %rdi, %rsi
divsd (%rax,%rsi,8), %xmm1
pushq $-0x1
popq %rbx
xorpd %xmm2, %xmm2
movsd 0x7e7c3(%rip), %xmm3    # 0xe83e8
xorl %ebp, %ebp
movsd %xmm1, 0x8(%rsp)
cmpl %edx, %ebp
jge 0x69ca5
addl %ebx, %edx
imull %edx, %ecx
movslq 0xe94(%r14), %rdx
movslq %ecx, %rcx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x69c58
sqrtsd %xmm0, %xmm0
jmp 0x69c6f
callq 0x82d0
movsd 0x7e783(%rip), %xmm3    # 0xe83e8
xorpd %xmm2, %xmm2
movsd 0x8(%rsp), %xmm1
mulsd %xmm3, %xmm0
movq 0xf20(%r14), %rax
movl 0xf04(%r14), %ecx
movl %ecx, %edx
imull %ebp, %edx
movslq 0xf00(%r14), %rsi
movslq %edx, %rdx
addq %rsi, %rdx
movsd %xmm0, (%rax,%rdx,8)
incl %ebp
movl 0xf08(%r14), %edx
decl %ebx
jmp 0x69c2d
xorl %ebx, %ebx
jmp 0x69b38