// File: /workspace/github_c_makefile_star2_O1/PoulinV[P]AxiCLASS/build/../external/HyRec2020/hydrogen.c
// Function: interpolate_rates
void interpolate_rates(double Alpha[2], double DAlpha[2], double Beta[2], double *R2p2s, double TR, double TM_TR,
                       HYREC_ATOMIC *atomic, double fsR, double meR, int *error, char error_message[SIZE_ErrorM]) {
  unsigned l, k, i;
  long iTM, iTR;
  double frac1, frac2;
  double logTR, T_RATIO;
  double coeff1[4], coeff2[4], temp[4];
  double Alpha_eq[2];
  char sub_message[128];

  /* Check that TM/TR is in range */
  if (TM_TR < T_RATIO_MIN) {
    sprintf(sub_message, "in interpolate_rates: TM/TR = %f is out of range.\n", TM_TR);
    strcat(error_message, sub_message);
    *error = 1;
    return;
  }
  
  /* T_RATIO is defined to be min(TM_TR, TR_TM) */
  if (TM_TR > 1.) {
    T_RATIO = 1./TM_TR; i = 2;
  }
  else {
    T_RATIO = TM_TR; i = 0;
  }

  /* Check if log(TR) is in the range tabulated */
  if (TR < TR_MIN || TR > TR_MAX) {
    sprintf(sub_message, "in interpolate_rates: TR = %f is out of range.\n", TR);
    strcat(error_message, sub_message);
    *error = 1;
    return;
  }

  /**** TR-only-dependent functions ****/

  /* Identify location to interpolate in log(TR) */
  logTR = log(TR);

  iTR = (long)floor((logTR - log(TR_MIN))/atomic->DlogTR);
  if (iTR < 1) iTR = 1;
  if (iTR > NTR-3) iTR = NTR-3;
  frac2 = (logTR - log(TR_MIN))/atomic->DlogTR - iTR;
  coeff2[0] = frac2*(frac2-1.)*(2.-frac2)/6.;
  coeff2[1] = (1.+frac2)*(1.-frac2)*(2.-frac2)/2.;
  coeff2[2] = (1.+frac2)*frac2*(2.-frac2)/2.;
  coeff2[3] = (1.+frac2)*frac2*(frac2-1.)/6.;

  for (l = 0; l <= 1; l++) {
    /* Alpha evaluated at Tm = Tr */
    Alpha_eq[l] = square(fsR/meR)* exp(atomic->logAlpha_tab[l][NTM-1][iTR-1]*coeff2[0]
                                +atomic->logAlpha_tab[l][NTM-1][iTR]*coeff2[1]
                                +atomic->logAlpha_tab[l][NTM-1][iTR+1]*coeff2[2]
                                +atomic->logAlpha_tab[l][NTM-1][iTR+2]*coeff2[3]);

    /* Beta obtained by detailed balance from Alpha(Tr, Tr) */
    /* prefactor = pow(2.0 * M_PI * mue *TR / hPc / hPc, 1.5)) * exp(-0.25*EI/TR) */
    Beta[l] = Alpha_eq[l] * SAHA_FACT(fsR, meR) * TR*sqrt(TR) *exp(-0.25*EI/TR)/(2.*l+1.);
  }

  /* Effective 2p->2s rate */
  *R2p2s = fsR*fsR*fsR*fsR*fsR*meR *
          exp(atomic->logR2p2s_tab[iTR-1]*coeff2[0]
          +atomic->logR2p2s_tab[iTR]*coeff2[1]
          +atomic->logR2p2s_tab[iTR+1]*coeff2[2]
          +atomic->logR2p2s_tab[iTR+2]*coeff2[3]);

  /**** Effective recombination coefficients Alpha(Tm, Tr) ****/

  /* Identify location to interpolate in T_RATIO */
  iTM = (long)floor((T_RATIO - T_RATIO_MIN)/atomic->DT_RATIO);
  if (iTM < 1) iTM = 1;
  if (iTM > NTM-3) iTM = NTM-3;
  frac1 = (T_RATIO - T_RATIO_MIN)/atomic->DT_RATIO - iTM;
  coeff1[0] = frac1*(frac1-1.)*(2.-frac1)/6.;
  coeff1[1] = (1.+frac1)*(1.-frac1)*(2.-frac1)/2.;
  coeff1[2] = (1.+frac1)*frac1*(2.-frac1)/2.;
  coeff1[3] = (1.+frac1)*frac1*(frac1-1.)/6.;

  for (l = 0; l <= 1; l++) {
    /* effective recombination coefficient to each level */
    for (k = 0; k < 4; k++) {
      temp[k] = atomic->logAlpha_tab[l+i][iTM-1+k][iTR-1]*coeff2[0]
                + atomic->logAlpha_tab[l+i][iTM-1+k][iTR]*coeff2[1]
                + atomic->logAlpha_tab[l+i][iTM-1+k][iTR+1]*coeff2[2]
                + atomic->logAlpha_tab[l+i][iTM-1+k][iTR+2]*coeff2[3];
    }

    Alpha[l] = square(fsR/meR)* exp(temp[0]*coeff1[0]+temp[1]*coeff1[1]
                                    +temp[2]*coeff1[2]+temp[3]*coeff1[3]);

    DAlpha[l] = Alpha[l] - Alpha_eq[l];
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r9, %rbp
movsd %xmm0, 0x8(%rsp)
movq %rsi, 0x78(%rsp)
movsd 0x18342(%rip), %xmm0    # 0xf1290
ucomisd %xmm1, %xmm0
jbe 0xd8f6c
leaq 0x545e9(%rip), %rsi     # 0x12d544
leaq 0xa0(%rsp), %rdi
movapd %xmm1, %xmm0
jmp 0xd94a5
movsd 0x543f4(%rip), %xmm0    # 0x12d368
ucomisd 0x8(%rsp), %xmm0
ja 0xd9490
movsd 0x8(%rsp), %xmm0
ucomisd 0x1b54a(%rip), %xmm0    # 0xf44d8
ja 0xd9490
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r13
movq %rdi, %r15
movsd 0x182b8(%rip), %xmm4    # 0xf1260
movapd %xmm4, %xmm0
divsd %xmm1, %xmm0
cmpltsd %xmm1, %xmm4
andpd %xmm4, %xmm0
movapd %xmm1, 0x80(%rsp)
andnpd %xmm1, %xmm4
orpd %xmm0, %xmm4
movapd %xmm4, 0x60(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm3, 0x50(%rsp)
movsd %xmm2, 0x48(%rsp)
callq 0x82c0
addsd 0x54381(%rip), %xmm0    # 0x12d370
divsd 0x7a0(%rbx), %xmm0
movsd %xmm0, (%rsp)
callq 0x85e0
cvttsd2si %xmm0, %rax
cmpq $0x2, %rax
movl $0x1, %ecx
cmovgeq %rax, %rcx
cmpq $0x61, %rcx
movl $0x61, %ebp
cmovlq %rcx, %rbp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd (%rsp), %xmm5
subsd %xmm0, %xmm5
movsd 0x187b0(%rip), %xmm0    # 0xf17e8
addsd %xmm5, %xmm0
movapd %xmm5, %xmm1
mulsd %xmm0, %xmm1
movsd 0x18cb4(%rip), %xmm2    # 0xf1d00
subsd %xmm5, %xmm2
mulsd %xmm2, %xmm1
movsd 0x19614(%rip), %xmm4    # 0xf2670
divsd %xmm4, %xmm1
movsd %xmm1, 0x40(%rsp)
movapd %xmm5, %xmm1
movsd 0x181ee(%rip), %xmm3    # 0xf1260
addsd %xmm3, %xmm1
subsd %xmm5, %xmm3
movapd %xmm5, %xmm6
mulsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
movsd 0x18c42(%rip), %xmm5    # 0xf1cd0
mulsd %xmm5, %xmm3
movsd %xmm3, 0x28(%rsp)
mulsd %xmm6, %xmm1
mulsd %xmm1, %xmm2
mulsd %xmm5, %xmm2
movsd %xmm2, 0x38(%rsp)
mulsd %xmm0, %xmm1
divsd %xmm4, %xmm1
movsd %xmm1, 0x30(%rsp)
movsd 0x48(%rsp), %xmm2
movapd %xmm2, %xmm0
movsd 0x50(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movapd %xmm2, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x54260(%rip), %xmm0    # 0x12d348
movsd 0x8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %r12d, %r12d
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0xd6f4c
movsd %xmm0, (%rsp)
movq 0x460(%rbx,%r12,8), %rax
movq 0x138(%rax), %rax
movsd (%rax,%rbp,8), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd -0x8(%rax,%rbp,8), %xmm1
mulsd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x8(%rax,%rbp,8), %xmm2
mulsd 0x38(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x10(%rax,%rbp,8), %xmm0
mulsd 0x30(%rsp), %xmm0
addsd %xmm2, %xmm0
callq 0x8670
mulsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd %xmm0, 0x90(%rsp,%r12,8)
movsd 0x20(%rsp), %xmm0
callq 0xd6f51
movapd %xmm0, %xmm1
mulsd 0x5418e(%rip), %xmm1    # 0x12d320
movsd 0x8(%rsp), %xmm0
ucomisd 0x180b8(%rip), %xmm0    # 0xf1258
movsd 0x10(%rsp), %xmm0
jae 0xd91bf
movsd 0x8(%rsp), %xmm0
movsd %xmm1, 0x70(%rsp)
callq 0x82b0
movsd 0x70(%rsp), %xmm1
movsd (%rsp), %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x8670
mulsd (%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
addsd %xmm1, %xmm1
addsd 0x18065(%rip), %xmm1    # 0xf1260
divsd %xmm1, %xmm0
movsd %xmm0, (%r13,%r12,8)
incq %r12
cmpq $0x1, %r12
je 0xd9108
xorl %r13d, %r13d
movsd 0x18042(%rip), %xmm1    # 0xf1260
movapd 0x80(%rsp), %xmm0
ucomisd %xmm1, %xmm0
seta %r13b
movsd 0x48(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd 0x50(%rsp), %xmm1
movsd %xmm1, (%rsp)
movsd 0x480(%rbx,%rbp,8), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd 0x478(%rbx,%rbp,8), %xmm1
mulsd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x488(%rbx,%rbp,8), %xmm2
mulsd 0x38(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x490(%rbx,%rbp,8), %xmm0
mulsd 0x30(%rsp), %xmm0
addsd %xmm2, %xmm0
callq 0x8670
mulsd (%rsp), %xmm0
movsd %xmm0, (%r14)
movapd 0x60(%rsp), %xmm0
addsd 0x540bf(%rip), %xmm0    # 0x12d378
divsd 0x7a8(%rbx), %xmm0
movapd %xmm0, 0x60(%rsp)
callq 0x85e0
cvttsd2si %xmm0, %rax
cmpq $0x2, %rax
movl $0x1, %ecx
cmovgeq %rax, %rcx
cmpq $0x25, %rcx
movl $0x25, %r12d
cmovlq %rcx, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movapd 0x60(%rsp), %xmm6
subsd %xmm0, %xmm6
movsd 0x184e2(%rip), %xmm0    # 0xf17e8
addsd %xmm6, %xmm0
movapd %xmm6, %xmm3
mulsd %xmm0, %xmm3
movsd 0x189e6(%rip), %xmm1    # 0xf1d00
subsd %xmm6, %xmm1
mulsd %xmm1, %xmm3
movsd 0x19346(%rip), %xmm2    # 0xf2670
divsd %xmm2, %xmm3
movsd %xmm3, 0x18(%rsp)
movapd %xmm6, %xmm4
movsd 0x17f20(%rip), %xmm5    # 0xf1260
addsd %xmm5, %xmm4
subsd %xmm6, %xmm5
mulsd %xmm4, %xmm5
mulsd %xmm1, %xmm5
movsd 0x18978(%rip), %xmm3    # 0xf1cd0
mulsd %xmm3, %xmm5
movsd %xmm5, 0x8(%rsp)
mulsd %xmm6, %xmm4
mulsd %xmm4, %xmm1
mulsd %xmm3, %xmm1
movsd %xmm1, 0x20(%rsp)
mulsd %xmm0, %xmm4
divsd %xmm2, %xmm4
movsd %xmm4, 0x10(%rsp)
addl %r13d, %r13d
xorl %r14d, %r14d
movq %r14, %rax
orq %r13, %rax
movq 0x460(%rbx,%rax,8), %rax
leaq (%rax,%r12,8), %rax
addq $-0x8, %rax
xorl %ecx, %ecx
movsd 0x40(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
movsd 0x30(%rsp), %xmm4
movsd 0x28(%rsp), %xmm5
movq (%rax,%rcx,8), %rdx
movsd (%rdx,%rbp,8), %xmm0
mulsd %xmm5, %xmm0
movsd -0x8(%rdx,%rbp,8), %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x8(%rdx,%rbp,8), %xmm0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movsd 0x10(%rdx,%rbp,8), %xmm1
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0xa0(%rsp,%rcx,8)
incq %rcx
cmpq $0x4, %rcx
jne 0xd93b8
movsd 0x58(%rsp), %xmm0
callq 0xd6f4c
movsd %xmm0, (%rsp)
movsd 0xa8(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0xb0(%rsp), %xmm2
mulsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0xb8(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
addsd %xmm2, %xmm0
callq 0x8670
mulsd (%rsp), %xmm0
movsd %xmm0, (%r15,%r14,8)
subsd 0x90(%rsp,%r14,8), %xmm0
movq 0x78(%rsp), %rax
movsd %xmm0, (%rax,%r14,8)
leaq 0x1(%r14), %rax
testq %r14, %r14
movq %rax, %r14
je 0xd9388
jmp 0xd94c8
leaq 0x540e0(%rip), %rsi     # 0x12d577
leaq 0xa0(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x8090
movq 0x160(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x8660
movl $0x1, (%rbp)
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq