// File: /workspace/github_c_makefile_star2_O1/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 $0x1048, %rsp           # imm = 0x1048
movl %edx, %ebp
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
testl %edx, %edx
movq %rcx, 0x20(%rsp)
je 0x97058
movq %rcx, %r10
jmp 0x97070
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x9741f
callq 0x82c0
movq 0x20(%rsp), %r10
movq 0x298(%rbx), %rdi
movslq 0x290(%rbx), %rsi
ucomisd -0x8(%rdi,%rsi,8), %xmm0
ja 0x97094
movsd (%rdi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x973cd
cmpl $0x0, 0x18(%rbx)
je 0x97143
callq 0x8670
movsd %xmm0, 0x20(%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 0x8472d(%rip), %rsi     # 0x11b810
leaq 0x840(%rsp), %r14
movq %r14, %rdi
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movb $0x3, %al
callq 0x30c2c
leaq 0x59f7a(%rip), %rsi     # 0xf1082
leaq 0x89a13(%rip), %rdx     # 0x120b22
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x32a, %rbx            # imm = 0x32A
leaq 0x59f05(%rip), %rsi     # 0xf1034
leaq 0x846c1(%rip), %rdx     # 0x11b7f7
movq %rbx, %rdi
movl $0x58, %ecx
jmp 0x97475
movl %ebp, 0x34(%rsp)
movslq 0xc(%rsp), %r13
movq 0x280(%rbx), %rax
cmpl $0x0, (%rax,%r13,4)
jle 0x97236
movl $0x2, %ecx
movl $0x0, 0x1c(%rsp)
xorl %r15d, %r15d
movsd %xmm0, 0x28(%rsp)
movl %ecx, 0x10(%rsp)
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %eax
cmpl %eax, %r15d
jge 0x97217
movl 0x1c(%rsp), %ecx
movl %ecx, %ebp
shrl %ebp
negl %ebp
movl %r15d, %r14d
cmpl %r14d, %r15d
jbe 0x971b3
imull %r14d, %eax
addl %r15d, %eax
leal 0x1(%r14), %ecx
imull %r14d, %ecx
shrl %ecx
subl %ecx, %eax
jmp 0x971bc
imull %r15d, %eax
addl %ebp, %eax
addl %r14d, %eax
movq 0x2b0(%rbx), %rcx
movq (%rcx,%r13,8), %rcx
movslq %eax, %r12
cmpw $0x1, (%rcx,%r12,2)
jne 0x971fc
callq 0x8670
movq 0x20(%rsp), %rax
leaq (%rax,%r12,8), %rcx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
callq 0x9788a
movsd 0x28(%rsp), %xmm0
movq 0x20(%rsp), %r10
jmp 0x97204
movq $0x0, (%r10,%r12,8)
incl %r14d
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %eax
cmpl %eax, %r14d
jl 0x97199
incl %r15d
movq 0x280(%rbx), %rax
movl 0x10(%rsp), %ecx
addl %ecx, 0x1c(%rsp)
addl $0x2, %ecx
cmpl (%rax,%r13,4), %r15d
jl 0x97174
xorl %r15d, %r15d
cmpl $0x1, 0x34(%rsp)
jne 0x97485
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %eax
testl %eax, %eax
jle 0x9728e
xorl %ecx, %ecx
incl %eax
imull %ecx, %eax
leal 0x1(%rcx), %ebp
imull %ebp, %ecx
shrl %ecx
subl %ecx, %eax
movslq %eax, %r14
movsd (%r10,%r14,8), %xmm0
callq 0x82c0
movq 0x20(%rsp), %r10
movsd %xmm0, (%r10,%r14,8)
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %eax
movl %ebp, %ecx
cmpl %eax, %ebp
jl 0x97255
movq 0x280(%rbx), %rax
cmpl $0x0, (%rax,%r13,4)
jle 0x97485
movl $0x2, %ecx
movl $0x0, 0x28(%rsp)
xorpd %xmm2, %xmm2
xorl %eax, %eax
movq %rbx, 0x38(%rsp)
movl %ecx, 0xc(%rsp)
movl %eax, %r12d
leal 0x1(%r12), %ecx
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %eax
movl %ecx, 0x10(%rsp)
cmpl %eax, %ecx
jge 0x973a6
movl 0x28(%rsp), %ecx
movl %ecx, %ebp
shrl %ebp
negl %ebp
movl 0x10(%rsp), %ecx
movl %ecx, %edi
imull %r12d, %edi
shrl %edi
movl %ecx, %r15d
cmpl %r15d, %r12d
jbe 0x97310
movl %eax, %ecx
imull %r15d, %ecx
addl %r12d, %ecx
leal 0x1(%r15), %edx
imull %r15d, %edx
shrl %edx
subl %edx, %ecx
jmp 0x9731b
movl %r12d, %ecx
imull %eax, %ecx
addl %ebp, %ecx
addl %r15d, %ecx
movq 0x2b0(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
movslq %ecx, %r14
cmpw $0x1, (%rdx,%r14,2)
jne 0x9738f
incl %eax
movl %eax, %ecx
imull %r12d, %ecx
subl %edi, %ecx
movslq %ecx, %rcx
movsd (%r10,%rcx,8), %xmm0
imull %r15d, %eax
leal 0x1(%r15), %ecx
imull %r15d, %ecx
shrl %ecx
subl %ecx, %eax
cltq
mulsd (%r10,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x97368
sqrtsd %xmm0, %xmm0
jmp 0x9737f
movl %edi, %ebx
callq 0x82b0
movl %ebx, %edi
movq 0x38(%rsp), %rbx
xorpd %xmm2, %xmm2
movq 0x20(%rsp), %r10
movsd (%r10,%r14,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r10,%r14,8)
incl %r15d
movq 0x280(%rbx), %rax
movl (%rax,%r13,4), %eax
cmpl %eax, %r15d
jl 0x972f4
movq 0x280(%rbx), %rax
movl 0xc(%rsp), %ecx
addl %ecx, 0x28(%rsp)
addl $0x2, %ecx
movl 0x10(%rsp), %edx
cmpl (%rax,%r13,4), %edx
movl %edx, %eax
jl 0x972b8
jmp 0x976d1
cmpw $0x1, 0xb2a(%rbx)
movslq 0xc(%rsp), %rax
movq %rax, 0x10(%rsp)
jne 0x9749a
movq 0x2b0(%rbx), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
cmpw $0x1, (%rax)
jne 0x97529
callq 0x8670
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %rcx
callq 0x9788a
movq 0x20(%rsp), %r10
jmp 0x97530
leaq 0x843c0(%rip), %rsi     # 0x11b7e6
leaq 0x840(%rsp), %r14
movq %r14, %rdi
movb $0x1, %al
callq 0x30c2c
leaq 0x59c43(%rip), %rsi     # 0xf1082
leaq 0x843a7(%rip), %rdx     # 0x11b7ed
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x32a, %rbx            # imm = 0x32A
leaq 0x59bce(%rip), %rsi     # 0xf1034
leaq 0x8438a(%rip), %rdx     # 0x11b7f7
movq %rbx, %rdi
movl $0x4a, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %r15d
movl %r15d, %eax
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x288(%rbx), %rax
movq 0x2a0(%rbx), %rcx
movq 0x10(%rsp), %r8
movq (%rcx,%r8,8), %rdx
movq 0x2a8(%rbx), %rcx
movq (%rcx,%r8,8), %rcx
movl (%rax,%r8,4), %r8d
leaq 0x32a(%rbx), %r15
subq $0x8, %rsp
leaq 0x48(%rsp), %r9
pushq %r15
pushq %r8
pushq %r10
callq 0x17ee6
addq $0x20, %rsp
cmpl $0x1, %eax
jne 0x976d9
leaq 0x59f01(%rip), %rsi     # 0xf13f0
leaq 0x84336(%rip), %rdx     # 0x11b82c
leaq 0x840(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x59b22(%rip), %rsi     # 0xf1034
leaq 0x842de(%rip), %rdx     # 0x11b7f7
movq %r15, %rdi
movl $0xba, %ecx
movq %rbx, %r8
jmp 0x97478
movq $0x0, (%r10)
xorl %r15d, %r15d
cmpl $0x1, %ebp
jne 0x97485
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
testl %eax, %eax
jle 0x9758e
xorl %ecx, %ecx
incl %eax
imull %ecx, %eax
leal 0x1(%rcx), %ebp
imull %ebp, %ecx
shrl %ecx
subl %ecx, %eax
movslq %eax, %r14
movsd (%r10,%r14,8), %xmm0
callq 0x82c0
movq 0x20(%rsp), %r10
movsd %xmm0, (%r10,%r14,8)
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %ebp, %ecx
cmpl %eax, %ebp
jl 0x97551
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x97485
movl $0x2, %ecx
movl $0x0, 0x1c(%rsp)
xorpd %xmm2, %xmm2
xorl %eax, %eax
movl %ecx, 0x28(%rsp)
movl %eax, %r13d
leal 0x1(%r13), %ecx
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rdx
movl (%rax,%rdx,4), %eax
movl %ecx, 0xc(%rsp)
cmpl %eax, %ecx
jge 0x976ab
movl 0x1c(%rsp), %ecx
movl %ecx, %ebp
shrl %ebp
negl %ebp
movl 0xc(%rsp), %ecx
movl %ecx, %r12d
imull %r13d, %r12d
shrl %r12d
movl %ecx, %r15d
cmpl %r15d, %r13d
jbe 0x97614
movl %eax, %ecx
imull %r15d, %ecx
addl %r13d, %ecx
leal 0x1(%r15), %edx
imull %r15d, %edx
shrl %edx
subl %edx, %ecx
jmp 0x9761f
movl %r13d, %ecx
imull %eax, %ecx
addl %ebp, %ecx
addl %r15d, %ecx
movq 0x2b0(%rbx), %rdx
movq 0x10(%rsp), %r8
movq (%rdx,%r8,8), %rdx
movslq %ecx, %r14
cmpw $0x1, (%rdx,%r14,2)
jne 0x97690
incl %eax
movl %eax, %ecx
imull %r13d, %ecx
subl %r12d, %ecx
movslq %ecx, %rcx
movsd (%r10,%rcx,8), %xmm0
imull %r15d, %eax
leal 0x1(%r15), %ecx
imull %r15d, %ecx
shrl %ecx
subl %ecx, %eax
cltq
mulsd (%r10,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x97672
sqrtsd %xmm0, %xmm0
jmp 0x97680
callq 0x82b0
xorpd %xmm2, %xmm2
movq 0x20(%rsp), %r10
movsd (%r10,%r14,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r10,%r14,8)
incl %r15d
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
cmpl %eax, %r15d
jl 0x975f8
movq 0x280(%rbx), %rax
movl 0x28(%rsp), %ecx
addl %ecx, 0x1c(%rsp)
addl $0x2, %ecx
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rsi
cmpl (%rax,%rsi,4), %edx
movl %edx, %eax
jl 0x975b7
xorl %r15d, %r15d
jmp 0x97485
xorl %r15d, %r15d
testl %ebp, %ebp
jne 0x97485
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
testl %eax, %eax
movq 0x20(%rsp), %rsi
jle 0x9773b
xorl %ecx, %ecx
incl %eax
imull %ecx, %eax
leal 0x1(%rcx), %ebp
imull %ebp, %ecx
shrl %ecx
subl %ecx, %eax
movslq %eax, %r14
movsd (%rsi,%r14,8), %xmm0
callq 0x8670
movq 0x20(%rsp), %rsi
movsd %xmm0, (%rsi,%r14,8)
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %ebp, %ecx
cmpl %eax, %ebp
jl 0x976fe
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x97485
movl $0x2, %ecx
movl $0x0, 0x1c(%rsp)
xorpd %xmm1, %xmm1
xorl %eax, %eax
movl %ecx, 0x28(%rsp)
movl %eax, %r15d
leal 0x1(%r15), %ecx
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rdx
movl (%rax,%rdx,4), %eax
movl %ecx, 0xc(%rsp)
cmpl %eax, %ecx
jge 0x9785c
movl 0x1c(%rsp), %ecx
movl %ecx, %ebp
shrl %ebp
negl %ebp
movl 0xc(%rsp), %ecx
movl %ecx, %r12d
imull %r15d, %r12d
shrl %r12d
movl %ecx, %r13d
cmpl %r13d, %r15d
jbe 0x977c1
movl %eax, %ecx
imull %r13d, %ecx
addl %r15d, %ecx
leal 0x1(%r13), %edx
imull %r13d, %edx
shrl %edx
subl %edx, %ecx
jmp 0x977cc
movl %r15d, %ecx
imull %eax, %ecx
addl %ebp, %ecx
addl %r13d, %ecx
movq 0x2b0(%rbx), %rdx
movq 0x10(%rsp), %r9
movq (%rdx,%r9,8), %rdx
movslq %ecx, %r14
cmpw $0x1, (%rdx,%r14,2)
jne 0x9781d
incl %eax
movl %eax, %ecx
imull %r15d, %ecx
subl %r12d, %ecx
movslq %ecx, %rcx
movsd (%rsi,%rcx,8), %xmm0
imull %r13d, %eax
leal 0x1(%r13), %ecx
imull %r13d, %ecx
shrl %ecx
subl %ecx, %eax
cltq
mulsd (%rsi,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x97827
sqrtsd %xmm0, %xmm0
jmp 0x97835
movq $0x0, (%rsi,%r14,8)
jmp 0x97841
callq 0x82b0
xorpd %xmm1, %xmm1
movq 0x20(%rsp), %rsi
mulsd (%rsi,%r14,8), %xmm0
movsd %xmm0, (%rsi,%r14,8)
incl %r13d
movq 0x280(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
cmpl %eax, %r13d
jl 0x977a5
movq 0x280(%rbx), %rax
movl 0x28(%rsp), %ecx
addl %ecx, 0x1c(%rsp)
addl $0x2, %ecx
xorl %r15d, %r15d
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rdi
cmpl (%rax,%rdi,4), %edx
movl %edx, %eax
jl 0x97764
jmp 0x97485