// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rsi, %rbx
movq %rdi, %r15
movslq 0xf08(%rsi), %r13
leaq (,%r13,8), %rdi
callq 0x8460
testq %rax, %rax
je 0x6de92
movq %rax, %r14
movq 0xf18(%rbx), %rax
movsd -0x8(%rax,%r13,8), %xmm0
movl $0x1, %ebp
leaq 0x4(%rsp), %rcx
movq %r15, %rdi
movl $0x1, %esi
xorl %edx, %edx
movq %r12, %r8
callq 0x5c63f
cmpl $0x1, %eax
jne 0x6ded8
addq $0xd1c, %r15            # imm = 0xD1C
leaq 0x805a3(%rip), %rsi     # 0xee3f0
leaq 0xa2a49(%rip), %rdx     # 0x11089d
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30af0
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x801c0(%rip), %rsi     # 0xee034
leaq 0xa29fb(%rip), %rdx     # 0x110876
movq %rbx, %rdi
movl $0xcc8, %ecx            # imm = 0xCC8
movq %r14, %r8
xorl %eax, %eax
callq 0x30af0
jmp 0x6e111
shll $0x3, %r13d
leaq 0x80167(%rip), %rsi     # 0xee004
leaq 0xa29c0(%rip), %rdx     # 0x110864
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x30af0
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x80170(%rip), %rsi     # 0xee034
leaq 0xa29ab(%rip), %rdx     # 0x110876
movq %rbx, %rdi
movl $0xcbc, %ecx            # imm = 0xCBC
jmp 0x6e102
movl 0xf08(%rbx), %esi
testl %esi, %esi
jle 0x6dfd5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0xf18(%rbx), %rax
addl %ebp, %esi
movslq %esi, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r14,%r13,8)
movq %r15, %rdi
movl $0x1, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rcx
movq %r12, %r8
callq 0x5c63f
cmpl $0x1, %eax
je 0x6e0be
movslq 0x390(%r15), %rax
movsd (%r12,%rax,8), %xmm0
mulsd 0x84c1b(%rip), %xmm0    # 0xf2b58
movslq 0x38c(%r15), %rax
divsd (%r12,%rax,8), %xmm0
movq 0xf20(%rbx), %rdx
movslq 0xf08(%rbx), %rsi
leal (%rsi,%rbp), %eax
movl 0xe88(%rbx), %r8d
movl 0xf04(%rbx), %ecx
imull %ecx, %eax
movslq 0xe80(%rbx), %rdi
cltq
addq %rdi, %rax
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
shufpd $0x2, 0x9f858(%rip), %xmm1 # xmm1 = xmm1[0],mem[1]
addsd 0x802d0(%rip), %xmm0    # 0xee260
movapd %xmm0, %xmm2
movhpd (%rdx,%rax,8), %xmm2    # xmm2 = xmm2[0],mem[0]
divpd %xmm2, %xmm1
movapd %xmm1, %xmm2
addsd 0x9f77f(%rip), %xmm2    # 0x10d728
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulsd %xmm2, %xmm1
divsd %xmm0, %xmm1
movl %r13d, %eax
imull %ecx, %eax
addl %r8d, %eax
cltq
movsd %xmm1, (%rdx,%rax,8)
incq %r13
decl %ebp
cmpq %rsi, %r13
jl 0x6deee
jmp 0x6dfe9
movq 0xf20(%rbx), %rdx
movl 0xe88(%rbx), %r8d
movl 0xf04(%rbx), %ecx
movl 0xe8c(%rbx), %r9d
leaq 0x1064(%rbx), %r15
movq %r14, %rdi
pushq %r15
pushq $0x1
callq 0x13d65
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e044
leaq 0x803dd(%rip), %rsi     # 0xee3f0
leaq 0xa295e(%rip), %rdx     # 0x110978
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x80001(%rip), %rsi     # 0xee034
leaq 0xa283c(%rip), %rdx     # 0x110876
movq %r15, %rdi
movl $0xcea, %ecx            # imm = 0xCEA
jmp 0x6e0b9
movl 0xf08(%rbx), %esi
movq 0xf20(%rbx), %rdx
movl 0xf04(%rbx), %ecx
movl 0xe88(%rbx), %r8d
movl 0xe8c(%rbx), %r9d
movl 0xe94(%rbx), %eax
movq %r14, %rdi
pushq %r15
pushq %rax
callq 0x134f5
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x6e125
leaq 0x80366(%rip), %rsi     # 0xee3f0
leaq 0xa2997(%rip), %rdx     # 0x110a28
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7ff8a(%rip), %rsi     # 0xee034
leaq 0xa27c5(%rip), %rdx     # 0x110876
movq %r15, %rdi
movl $0xcf6, %ecx            # imm = 0xCF6
movq %rbx, %r8
jmp 0x6e105
addq $0xd1c, %r15            # imm = 0xD1C
leaq 0x80324(%rip), %rsi     # 0xee3f0
leaq 0xa2838(%rip), %rdx     # 0x11090b
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30af0
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x7ff41(%rip), %rsi     # 0xee034
leaq 0xa277c(%rip), %rdx     # 0x110876
movq %rbx, %rdi
movl $0xcd6, %ecx            # imm = 0xCD6
movq %r14, %r8
xorl %eax, %eax
callq 0x30af0
movl $0x1, %ebp
movl %ebp, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x8410
movl 0xf08(%rbx), %edx
testl %edx, %edx
jle 0x6e207
movq 0xf20(%rbx), %rax
movl 0xf04(%rbx), %ecx
leal -0x1(%rdx), %esi
movl %ecx, %edi
imull %esi, %edi
movslq 0xe80(%rbx), %r8
movslq %edi, %rdi
addq %r8, %rdi
movq 0xf18(%rbx), %r8
movsd (%r8,%rsi,8), %xmm1
mulsd 0x9f5be(%rip), %xmm1    # 0x10d730
divsd (%rax,%rdi,8), %xmm1
xorl %ebp, %ebp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorpd %xmm2, %xmm2
movsd 0x8329d(%rip), %xmm3    # 0xf1428
movsd %xmm1, 0x8(%rsp)
addl %r14d, %edx
imull %ecx, %edx
movslq 0xe94(%rbx), %rcx
movslq %edx, %rdx
addq %rcx, %rdx
movsd (%rax,%rdx,8), %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x6e1b9
sqrtsd %xmm0, %xmm0
jmp 0x6e1d0
callq 0x82b0
movsd 0x83262(%rip), %xmm3    # 0xf1428
xorpd %xmm2, %xmm2
movsd 0x8(%rsp), %xmm1
mulsd %xmm3, %xmm0
movq 0xf20(%rbx), %rax
movl 0xf04(%rbx), %ecx
movl %ecx, %edx
imull %ebp, %edx
movslq 0xf00(%rbx), %rsi
movslq %edx, %rdx
addq %rsi, %rdx
movsd %xmm0, (%rax,%rdx,8)
incl %ebp
movl 0xf08(%rbx), %edx
decl %r14d
cmpl %edx, %ebp
jl 0x6e191
xorl %ebp, %ebp
jmp 0x6e111