// File: /workspace/github_c_makefile_star2_O3/PoulinV[P]AxiCLASS/build/../source/primordial.c
// Function: primordial_spectrum_at_k
int primordial_spectrum_at_k(
                             struct primordial * ppm,
                             int index_md,
                             enum linear_or_logarithmic mode,
                             double input,
                             double * output /* array with argument output[index_ic1_ic2] (must be already allocated) */
                             ) {

  /** Summary: */

  /** - define local variables */

  int index_ic1,index_ic2,index_ic1_ic2;
  double lnk;
  int last_index;

  /** - infer ln(k) from input. In linear mode, reject negative value of input k value. */

  if (mode == linear) {
    class_test(input<=0.,
               ppm->error_message,
               "k = %e",input);
    lnk=log(input);
  }
  else {
    lnk = input;
  }

  /** - if ln(k) is not in the interpolation range, return an error, unless
      we are in the case of a analytic spectrum, for which a direct computation is possible */

  if ((lnk > ppm->lnk[ppm->lnk_size-1]) || (lnk < ppm->lnk[0])) {

    class_test(ppm->primordial_spec_type != analytic_Pk,
               ppm->error_message,
               "k=%e out of range [%e : %e]",exp(lnk),exp(ppm->lnk[0]),exp(ppm->lnk[ppm->lnk_size-1]));

    /* direct computation */

    for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
      for (index_ic2 = index_ic1; index_ic2 < ppm->ic_size[index_md]; index_ic2++) {

        index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic2,ppm->ic_size[index_md]);

        if (ppm->is_non_zero[index_md][index_ic1_ic2] == _TRUE_) {

          class_call(primordial_analytic_spectrum(ppm,
                                                  index_md,
                                                  index_ic1_ic2,
                                                  exp(lnk),
                                                  &(output[index_ic1_ic2])),
                     ppm->error_message,
                     ppm->error_message);
        }
        else {
          output[index_ic1_ic2] = 0.;
        }
      }
    }

    /* if mode==linear, output is already in the correct format. Otherwise, apply necessary transformation. */

    if (mode == logarithmic) {

      for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
        index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic1,ppm->ic_size[index_md]);
        output[index_ic1_ic2] = log(output[index_ic1_ic2]);
      }
      for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
        for (index_ic2 = index_ic1+1; index_ic2 < ppm->ic_size[index_md]; index_ic2++) {
          index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic2,ppm->ic_size[index_md]);
          if (ppm->is_non_zero[index_md][index_ic1_ic2] == _TRUE_) {
            output[index_ic1_ic2] /= sqrt(output[index_symmetric_matrix(index_ic1,index_ic1,ppm->ic_size[index_md])]*
                                          output[index_symmetric_matrix(index_ic2,index_ic2,ppm->ic_size[index_md])]);
          }
        }
      }
    }
  }

  /** - otherwise, interpolate in the pre-computed table */

  else {
    // printf("interpolate! need to bypass\n");

    if(ppm->is_primordial_Pk_different ==_TRUE_){
      if (ppm->is_non_zero[index_md][index_ic1_ic2] == _TRUE_) {

        class_call(primordial_analytic_spectrum(ppm,
                                                index_md,
                                                index_ic1_ic2,
                                                exp(lnk),
                                                &(output[index_ic1_ic2])),
                   ppm->error_message,
                   ppm->error_message);
      }
      else {
        output[index_ic1_ic2] = 0.;
      }

      /* if mode==linear, output is already in the correct format. Otherwise, apply necessary transformation. */

      if (mode == logarithmic) {

        for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
          index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic1,ppm->ic_size[index_md]);
          output[index_ic1_ic2] = log(output[index_ic1_ic2]);
        }
        for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
          for (index_ic2 = index_ic1+1; index_ic2 < ppm->ic_size[index_md]; index_ic2++) {
            index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic2,ppm->ic_size[index_md]);
            if (ppm->is_non_zero[index_md][index_ic1_ic2] == _TRUE_) {
              output[index_ic1_ic2] /= sqrt(output[index_symmetric_matrix(index_ic1,index_ic1,ppm->ic_size[index_md])]*
                                            output[index_symmetric_matrix(index_ic2,index_ic2,ppm->ic_size[index_md])]);
            }
          }
        }
      }


    }else{
    class_call(array_interpolate_spline(
                                        ppm->lnk,
                                        ppm->lnk_size,
                                        ppm->lnpk[index_md],
                                        ppm->ddlnpk[index_md],
                                        ppm->ic_ic_size[index_md],
                                        lnk,
                                        &last_index,
                                        output,
                                        ppm->ic_ic_size[index_md],
                                        ppm->error_message),
               ppm->error_message,
               ppm->error_message);

    /* if mode==logarithmic, output is already in the correct format. Otherwise, apply necessary transformation. */

    if (mode == linear) {

      for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
        index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic1,ppm->ic_size[index_md]);
        output[index_ic1_ic2]=exp(output[index_ic1_ic2]);
      }
      for (index_ic1 = 0; index_ic1 < ppm->ic_size[index_md]; index_ic1++) {
        for (index_ic2 = index_ic1+1; index_ic2 < ppm->ic_size[index_md]; index_ic2++) {
          index_ic1_ic2 = index_symmetric_matrix(index_ic1,index_ic2,ppm->ic_size[index_md]);
          if (ppm->is_non_zero[index_md][index_ic1_ic2] == _TRUE_) {
            output[index_ic1_ic2] *= sqrt(output[index_symmetric_matrix(index_ic1,index_ic1,ppm->ic_size[index_md])]*
                                          output[index_symmetric_matrix(index_ic2,index_ic2,ppm->ic_size[index_md])]);
          }
          else {
            output[index_ic1_ic2] = 0.;
          }
        }
      }
    }
    }
  }

  return _SUCCESS_;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movl %edx, %ebp
movl %esi, 0x10(%rsp)
movq %rdi, %rbx
testl %edx, %edx
movq %rcx, 0x8(%rsp)
je 0x943dc
movq %rcx, %r10
jmp 0x943f4
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x94730
callq 0x82c0
movq 0x8(%rsp), %r10
movq 0x298(%rbx), %rdi
movslq 0x290(%rbx), %rsi
ucomisd -0x8(%rdi,%rsi,8), %xmm0
ja 0x94418
movsd (%rdi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x946e8
cmpl $0x0, 0x18(%rbx)
je 0x944c7
callq 0x8670
movsd %xmm0, 0x8(%rsp)
movq 0x298(%rbx), %rax
movsd (%rax), %xmm0
callq 0x8670
movsd %xmm0, 0x10(%rsp)
movq 0x298(%rbx), %rax
movslq 0x290(%rbx), %rcx
movsd -0x8(%rax,%rcx,8), %xmm0
callq 0x8670
movapd %xmm0, %xmm2
leaq 0x843e9(%rip), %rsi     # 0x118850
leaq 0x830(%rsp), %r14
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movb $0x3, %al
callq 0x30af0
leaq 0x59bf6(%rip), %rsi     # 0xee082
leaq 0x896cf(%rip), %rdx     # 0x11db62
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30af0
addq $0x32a, %rbx            # imm = 0x32A
leaq 0x59b81(%rip), %rsi     # 0xee034
leaq 0x8437d(%rip), %rdx     # 0x118837
movq %rbx, %rdi
movl $0x58, %ecx
jmp 0x94786
movl %ebp, 0x1c(%rsp)
movslq 0x10(%rsp), %r13
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %ecx
testl %ecx, %ecx
jle 0x945a8
movl $0x2, %edx
xorl %esi, %esi
xorl %r15d, %r15d
movsd %xmm0, 0x28(%rsp)
cmpl %ecx, %r15d
jge 0x94597
movl %edx, 0x24(%rsp)
movl %esi, 0x20(%rsp)
movl %esi, %ebp
shrl %ebp
negl %ebp
movl %r15d, %r14d
cmpl %r14d, %r15d
jbe 0x94527
imull %r14d, %ecx
addl %r15d, %ecx
leal 0x1(%r14), %edx
imull %r14d, %edx
shrl %edx
subl %edx, %ecx
jmp 0x94530
imull %r15d, %ecx
addl %ebp, %ecx
addl %r14d, %ecx
movq 0x2b0(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
movslq %ecx, %r12
cmpw $0x1, (%rdx,%r12,2)
jne 0x94577
callq 0x8670
movq 0x8(%rsp), %rax
leaq (%rax,%r12,8), %rcx
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %r12d, %edx
callq 0x94ad8
movsd 0x28(%rsp), %xmm0
movq 0x8(%rsp), %r10
movq 0x280(%rbx), %rax
jmp 0x9457f
movq $0x0, (%r10,%r12,8)
incl %r14d
movl (%rax,%r13,4), %ecx
cmpl %ecx, %r14d
jl 0x9450d
movl (%rax,%r13,4), %ecx
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %esi
incl %r15d
addl %edx, %esi
addl $0x2, %edx
cmpl %ecx, %r15d
jl 0x944f3
xorl %eax, %eax
cmpl $0x1, 0x1c(%rsp)
jne 0x94795
testl %ecx, %ecx
jle 0x94795
xorl %edx, %edx
incl %ecx
imull %edx, %ecx
leal 0x1(%rdx), %ebp
imull %ebp, %edx
shrl %edx
subl %edx, %ecx
movslq %ecx, %r14
movsd (%r10,%r14,8), %xmm0
callq 0x82c0
movq 0x8(%rsp), %r10
movsd %xmm0, (%r10,%r14,8)
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %ecx
movl %ebp, %edx
cmpl %ecx, %ebp
jl 0x945bf
testl %ecx, %ecx
jle 0x946e1
xorl %edx, %edx
xorpd %xmm2, %xmm2
movl %edx, %r15d
leal 0x1(%r15), %edx
cmpl %ecx, %edx
jge 0x946d9
movl %edx, %r12d
imull %r15d, %r12d
shrl %r12d
movl %edx, 0x10(%rsp)
cmpl %edx, %r15d
jbe 0x9463c
movl %ecx, %esi
imull %edx, %esi
addl %r15d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
shrl %edi
subl %edi, %esi
jmp 0x94649
movl %ecx, %edi
imull %r15d, %edi
movl %edx, %esi
subl %r12d, %esi
addl %edi, %esi
movq 0x2b0(%rbx), %rdi
movq (%rdi,%r13,8), %rdi
movslq %esi, %rbp
cmpw $0x1, (%rdi,%rbp,2)
jne 0x94695
incl %ecx
movl %ecx, %eax
imull %r15d, %eax
subl %r12d, %eax
cltq
movsd (%r10,%rax,8), %xmm0
imull %edx, %ecx
leal 0x1(%rdx), %r14d
imull %r14d, %edx
shrl %edx
subl %edx, %ecx
movslq %ecx, %rax
mulsd (%r10,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x9469c
sqrtsd %xmm0, %xmm0
jmp 0x946aa
incl %edx
movl %edx, %r14d
jmp 0x946c1
callq 0x82b0
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %r10
movsd (%r10,%rbp,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r10,%rbp,8)
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %ecx
movl %r14d, %edx
cmpl %ecx, %r14d
jl 0x94623
movl (%rax,%r13,4), %ecx
movl 0x10(%rsp), %edx
cmpl %ecx, %edx
jl 0x94606
xorl %eax, %eax
jmp 0x94795
cmpw $0x1, 0xb2a(%rbx)
movslq 0x10(%rsp), %r12
jne 0x947a7
movq 0x2b0(%rbx), %rax
movq (%rax,%r12,8), %rax
cmpw $0x1, (%rax)
jne 0x94831
callq 0x8670
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movq 0x8(%rsp), %rcx
callq 0x94ad8
movq 0x8(%rsp), %r10
jmp 0x94838
leaq 0x840ef(%rip), %rsi     # 0x118826
leaq 0x830(%rsp), %r14
movq %r14, %rdi
movb $0x1, %al
callq 0x30af0
leaq 0x59932(%rip), %rsi     # 0xee082
leaq 0x840d6(%rip), %rdx     # 0x11882d
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30af0
addq $0x32a, %rbx            # imm = 0x32A
leaq 0x598bd(%rip), %rsi     # 0xee034
leaq 0x840b9(%rip), %rdx     # 0x118837
movq %rbx, %rdi
movl $0x4a, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x30af0
movl $0x1, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x288(%rbx), %rax
movq 0x2a0(%rbx), %rcx
movq (%rcx,%r12,8), %rdx
movq 0x2a8(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movl (%rax,%r12,4), %r8d
leaq 0x32a(%rbx), %r15
subq $0x8, %rsp
leaq 0x38(%rsp), %r9
pushq %r15
pushq %r8
pushq %r10
callq 0x18071
addq $0x20, %rsp
cmpl $0x1, %eax
jne 0x94984
leaq 0x59bf9(%rip), %rsi     # 0xee3f0
leaq 0x8406e(%rip), %rdx     # 0x11886c
leaq 0x830(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x5981a(%rip), %rsi     # 0xee034
leaq 0x84016(%rip), %rdx     # 0x118837
movq %r15, %rdi
movl $0xba, %ecx
movq %rbx, %r8
jmp 0x94789
movq $0x0, (%r10)
xorl %eax, %eax
cmpl $0x1, %ebp
jne 0x94795
movq 0x280(%rbx), %rcx
movl (%rcx,%r12,4), %ecx
testl %ecx, %ecx
jle 0x94795
xorl %eax, %eax
incl %ecx
imull %eax, %ecx
leal 0x1(%rax), %ebp
imull %ebp, %eax
shrl %eax
subl %eax, %ecx
movslq %ecx, %r14
movsd (%r10,%r14,8), %xmm0
callq 0x82c0
movq 0x8(%rsp), %r10
movsd %xmm0, (%r10,%r14,8)
movq 0x280(%rbx), %rdx
movl (%rdx,%r12,4), %ecx
movl %ebp, %eax
cmpl %ecx, %ebp
jl 0x94858
testl %ecx, %ecx
jle 0x946e1
xorl %esi, %esi
xorpd %xmm2, %xmm2
movl %esi, %r15d
leal 0x1(%r15), %esi
cmpl %ecx, %esi
jge 0x94975
movl %esi, %eax
movl %esi, %r13d
imull %r15d, %r13d
shrl %r13d
movl %esi, 0x10(%rsp)
cmpl %eax, %r15d
jbe 0x948d7
movl %ecx, %esi
imull %eax, %esi
addl %r15d, %esi
leal 0x1(%rax), %edi
imull %eax, %edi
shrl %edi
subl %edi, %esi
jmp 0x948e4
movl %ecx, %edi
imull %r15d, %edi
movl %eax, %esi
subl %r13d, %esi
addl %edi, %esi
movq 0x2b0(%rbx), %rdi
movq (%rdi,%r12,8), %rdi
movslq %esi, %rbp
cmpw $0x1, (%rdi,%rbp,2)
jne 0x94931
incl %ecx
movl %ecx, %edx
imull %r15d, %edx
subl %r13d, %edx
movslq %edx, %rdx
movsd (%r10,%rdx,8), %xmm0
imull %eax, %ecx
leal 0x1(%rax), %r14d
imull %r14d, %eax
shrl %eax
subl %eax, %ecx
movslq %ecx, %rax
mulsd (%r10,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x94938
sqrtsd %xmm0, %xmm0
jmp 0x94946
incl %eax
movl %eax, %r14d
jmp 0x9495d
callq 0x82b0
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %r10
movsd (%r10,%rbp,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r10,%rbp,8)
movq 0x280(%rbx), %rdx
movl (%rdx,%r12,4), %ecx
movl %r14d, %eax
cmpl %ecx, %r14d
jl 0x948be
movl (%rdx,%r12,4), %ecx
movl 0x10(%rsp), %esi
xorl %eax, %eax
cmpl %ecx, %esi
jl 0x9489f
jmp 0x94795
xorl %eax, %eax
testl %ebp, %ebp
jne 0x94795
movq 0x280(%rbx), %rcx
movl (%rcx,%r12,4), %ecx
testl %ecx, %ecx
movq 0x8(%rsp), %r8
jle 0x94795
xorl %eax, %eax
incl %ecx
imull %eax, %ecx
leal 0x1(%rax), %ebp
imull %ebp, %eax
shrl %eax
subl %eax, %ecx
movslq %ecx, %r14
movsd (%r8,%r14,8), %xmm0
callq 0x8670
movq 0x8(%rsp), %r8
movsd %xmm0, (%r8,%r14,8)
movq 0x280(%rbx), %rdx
movl (%rdx,%r12,4), %ecx
movl %ebp, %eax
cmpl %ecx, %ebp
jl 0x949a8
testl %ecx, %ecx
jle 0x946e1
xorl %esi, %esi
xorpd %xmm1, %xmm1
movl %esi, %r15d
leal 0x1(%r15), %esi
cmpl %ecx, %esi
jge 0x94ac9
movl %esi, %eax
movl %esi, %r13d
imull %r15d, %r13d
shrl %r13d
movl %esi, 0x10(%rsp)
cmpl %eax, %r15d
jbe 0x94a27
movl %ecx, %esi
imull %eax, %esi
addl %r15d, %esi
leal 0x1(%rax), %edi
imull %eax, %edi
shrl %edi
subl %edi, %esi
jmp 0x94a34
movl %ecx, %edi
imull %r15d, %edi
movl %eax, %esi
subl %r13d, %esi
addl %edi, %esi
movq 0x2b0(%rbx), %rdi
movq (%rdi,%r12,8), %rdi
movslq %esi, %rbp
cmpw $0x1, (%rdi,%rbp,2)
jne 0x94a81
incl %ecx
movl %ecx, %edx
imull %r15d, %edx
subl %r13d, %edx
movslq %edx, %rdx
movsd (%r8,%rdx,8), %xmm0
imull %eax, %ecx
leal 0x1(%rax), %r14d
imull %r14d, %eax
shrl %eax
subl %eax, %ecx
movslq %ecx, %rax
mulsd (%r8,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x94a90
sqrtsd %xmm0, %xmm0
jmp 0x94a9e
movq $0x0, (%r8,%rbp,8)
incl %eax
movl %eax, %r14d
jmp 0x94ab1
callq 0x82b0
xorpd %xmm1, %xmm1
movq 0x8(%rsp), %r8
mulsd (%r8,%rbp,8), %xmm0
movsd %xmm0, (%r8,%rbp,8)
movq 0x280(%rbx), %rdx
movl (%rdx,%r12,4), %ecx
movl %r14d, %eax
cmpl %ecx, %r14d
jl 0x94a0e
movl (%rdx,%r12,4), %ecx
movl 0x10(%rsp), %esi
xorl %eax, %eax
cmpl %ecx, %esi
jl 0x949ef
jmp 0x94795