// File: /workspace/github_c_makefile_star2_O1/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 0x7091c
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 0x5ed53
cmpl $0x1, %eax
jne 0x70962
addq $0xd1c, %r15            # imm = 0xD1C
leaq 0x80b19(%rip), %rsi     # 0xf13f0
leaq 0xa2f6f(%rip), %rdx     # 0x11384d
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x80736(%rip), %rsi     # 0xf1034
leaq 0xa2f21(%rip), %rdx     # 0x113826
movq %rbx, %rdi
movl $0xcc8, %ecx            # imm = 0xCC8
movq %r14, %r8
xorl %eax, %eax
callq 0x30c2c
jmp 0x70ba0
shll $0x3, %r13d
leaq 0x806dd(%rip), %rsi     # 0xf1004
leaq 0xa2ee6(%rip), %rdx     # 0x113814
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x30c2c
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x806e6(%rip), %rsi     # 0xf1034
leaq 0xa2ed1(%rip), %rdx     # 0x113826
movq %rbx, %rdi
movl $0xcbc, %ecx            # imm = 0xCBC
jmp 0x70b91
movl 0xf08(%rbx), %esi
testl %esi, %esi
jle 0x70a64
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 0x5ed53
cmpl $0x1, %eax
je 0x70b4d
movslq 0x390(%r15), %rax
movsd (%r12,%rax,8), %xmm0
mulsd 0x85161(%rip), %xmm0    # 0xf5b28
movslq 0x38c(%r15), %rax
divsd (%r12,%rax,8), %xmm0
movq 0xf20(%rbx), %rax
movl 0xf08(%rbx), %ecx
addl %ebp, %ecx
movl 0xf04(%rbx), %edx
imull %edx, %ecx
movslq 0xe80(%rbx), %rsi
movslq %ecx, %rcx
addq %rsi, %rcx
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
shufpd $0x2, 0x9fd86(%rip), %xmm1 # xmm1 = xmm1[0],mem[1]
addsd 0x8084e(%rip), %xmm0    # 0xf1260
movapd %xmm0, %xmm2
movhpd (%rax,%rcx,8), %xmm2    # xmm2 = xmm2[0],mem[0]
divpd %xmm2, %xmm1
movapd %xmm1, %xmm2
addsd 0x9fcdd(%rip), %xmm2    # 0x110708
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulsd %xmm2, %xmm1
divsd %xmm0, %xmm1
movl %r13d, %ecx
imull %edx, %ecx
movslq 0xe88(%rbx), %rdx
movslq %ecx, %rcx
addq %rdx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incq %r13
movslq 0xf08(%rbx), %rsi
decl %ebp
cmpq %rsi, %r13
jl 0x70978
movq 0xf20(%rbx), %rdx
movl 0xf04(%rbx), %ecx
movl 0xe88(%rbx), %r8d
movl 0xe8c(%rbx), %r9d
leaq 0x1064(%rbx), %r15
movq %r14, %rdi
pushq %r15
pushq $0x1
callq 0x13c30
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70ad3
leaq 0x8094e(%rip), %rsi     # 0xf13f0
leaq 0xa2e7f(%rip), %rdx     # 0x113928
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x80572(%rip), %rsi     # 0xf1034
leaq 0xa2d5d(%rip), %rdx     # 0x113826
movq %r15, %rdi
movl $0xcea, %ecx            # imm = 0xCEA
jmp 0x70b48
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 0x1337c
addq $0x10, %rsp
cmpl $0x1, %eax
jne 0x70bb4
leaq 0x808d7(%rip), %rsi     # 0xf13f0
leaq 0xa2eb8(%rip), %rdx     # 0x1139d8
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x804fb(%rip), %rsi     # 0xf1034
leaq 0xa2ce6(%rip), %rdx     # 0x113826
movq %r15, %rdi
movl $0xcf6, %ecx            # imm = 0xCF6
movq %rbx, %r8
jmp 0x70b94
addq $0xd1c, %r15            # imm = 0xD1C
leaq 0x80895(%rip), %rsi     # 0xf13f0
leaq 0xa2d59(%rip), %rdx     # 0x1138bb
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x1064, %rbx           # imm = 0x1064
leaq 0x804b2(%rip), %rsi     # 0xf1034
leaq 0xa2c9d(%rip), %rdx     # 0x113826
movq %rbx, %rdi
movl $0xcd6, %ecx            # imm = 0xCD6
movq %r14, %r8
xorl %eax, %eax
callq 0x30c2c
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), %eax
testl %eax, %eax
jle 0x70cb0
movq 0xf18(%rbx), %rcx
movq 0xf20(%rbx), %rdx
movslq 0xf04(%rbx), %rsi
movslq 0xf08(%rbx), %rdi
leaq -0x1(%rdi), %r8
movslq %r8d, %r8
imulq %rsi, %r8
movslq 0xe80(%rbx), %rsi
addq %r8, %rsi
movsd -0x8(%rcx,%rdi,8), %xmm1
mulsd 0x9fb07(%rip), %xmm1    # 0x110710
divsd (%rdx,%rsi,8), %xmm1
xorl %r14d, %r14d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorpd %xmm2, %xmm2
movsd 0x837f6(%rip), %xmm3    # 0xf4418
movsd %xmm1, 0x8(%rsp)
movq 0xf20(%rbx), %rcx
addl %ebp, %eax
movslq 0xf04(%rbx), %rdx
cltq
imulq %rdx, %rax
movslq 0xe94(%rbx), %rdx
addq %rax, %rdx
movsd (%rcx,%rdx,8), %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x70c5d
sqrtsd %xmm0, %xmm0
jmp 0x70c74
callq 0x82b0
movsd 0x837ae(%rip), %xmm3    # 0xf4418
xorpd %xmm2, %xmm2
movsd 0x8(%rsp), %xmm1
mulsd %xmm3, %xmm0
movq 0xf20(%rbx), %rax
movslq 0xf04(%rbx), %rcx
movslq %r14d, %r14
imulq %r14, %rcx
movslq 0xf00(%rbx), %rdx
addq %rcx, %rdx
movsd %xmm0, (%rax,%rdx,8)
incl %r14d
movl 0xf08(%rbx), %eax
decl %ebp
cmpl %eax, %r14d
jl 0x70c28
xorl %ebp, %ebp
jmp 0x70ba0