// File: /workspace/github_c_makefile_star2_O2/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
movq %rcx, 0x8(%rsp)
movl %edx, %ebp
movl %esi, 0x10(%rsp)
movq %rdi, %rbx
testl %edx, %edx
jne 0x8f856
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x8fa85
callq 0x82e0
movq 0x298(%rbx), %rdi
movslq 0x290(%rbx), %rsi
ucomisd -0x8(%rdi,%rsi,8), %xmm0
movslq 0x10(%rsp), %r13
ja 0x8f87f
movsd (%rdi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x8fa47
cmpl $0x0, 0x18(%rbx)
je 0x8f928
callq 0x86a0
movsd %xmm0, 0x8(%rsp)
movq 0x298(%rbx), %rax
movsd (%rax), %xmm0
callq 0x86a0
movsd %xmm0, 0x10(%rsp)
movq 0x298(%rbx), %rax
movslq 0x290(%rbx), %rcx
movsd -0x8(%rax,%rcx,8), %xmm0
callq 0x86a0
movapd %xmm0, %xmm2
leaq 0x7fe22(%rip), %rsi     # 0x10f6f0
leaq 0x830(%rsp), %r14
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movb $0x3, %al
callq 0x2e014
leaq 0x5578f(%rip), %rsi     # 0xe5082
leaq 0x85108(%rip), %rdx     # 0x114a02
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
addq $0x32a, %rbx            # imm = 0x32A
leaq 0x5571a(%rip), %rsi     # 0xe5034
leaq 0x7fdb6(%rip), %rdx     # 0x10f6d7
pushq $0x58
jmp 0x8fad5
movl %ebp, 0x18(%rsp)
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
pushq $0x2
popq %rdx
xorl %esi, %esi
xorl %r14d, %r14d
movsd %xmm0, 0x28(%rsp)
cmpl %eax, %r14d
jge 0x8f9fb
movl %edx, 0x24(%rsp)
movl %esi, 0x20(%rsp)
movl %esi, %r15d
shrl %r15d
negl %r15d
leal 0x1(%r14), %edx
movl %edx, 0x1c(%rsp)
movl %r14d, %ebp
cmpl %eax, %ebp
jge 0x8f9e2
cmpl %ebp, %r14d
jbe 0x8f985
imull %ebp, %eax
addl %r14d, %eax
leal 0x1(%rbp), %edx
imull %ebp, %edx
shrl %edx
subl %edx, %eax
jmp 0x8f98e
imull %r14d, %eax
addl %r15d, %eax
addl %ebp, %eax
movq 0x2b0(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
movslq %eax, %r12
cmpw $0x1, (%rdx,%r12,2)
jne 0x8f9d0
callq 0x86a0
movq 0x8(%rsp), %rax
leaq (%rax,%r12,8), %rcx
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %r12d, %edx
callq 0x8febd
movsd 0x28(%rsp), %xmm0
movq 0x280(%rbx), %rcx
jmp 0x8f9da
movq 0x8(%rsp), %rax
andq $0x0, (%rax,%r12,8)
incl %ebp
movl (%rcx,%r13,4), %eax
jmp 0x8f96a
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %esi
addl %edx, %esi
addl $0x2, %edx
movl 0x1c(%rsp), %edi
movl %edi, %r14d
jmp 0x8f945
cmpl $0x1, 0x18(%rsp)
jne 0x8fcaa
xorl %edx, %edx
movq 0x8(%rsp), %r15
cmpl %eax, %edx
jge 0x8faeb
incl %eax
imull %edx, %eax
leal 0x1(%rdx), %ebp
imull %ebp, %edx
shrl %edx
subl %edx, %eax
movslq %eax, %r14
movsd (%r15,%r14,8), %xmm0
callq 0x82e0
movsd %xmm0, (%r15,%r14,8)
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
movl %ebp, %edx
jmp 0x8fa0d
cmpw $0x1, 0xb2a(%rbx)
jne 0x8fbcb
movq 0x2b0(%rbx), %rax
movq (%rax,%r13,8), %rax
cmpw $0x1, (%rax)
jne 0x8fc57
callq 0x86a0
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movq 0x8(%rsp), %rcx
callq 0x8febd
jmp 0x8fc60
leaq 0x7fc3a(%rip), %rsi     # 0x10f6c6
leaq 0x830(%rsp), %r14
movq %r14, %rdi
movb $0x1, %al
callq 0x2e014
leaq 0x555dd(%rip), %rsi     # 0xe5082
leaq 0x7fc21(%rip), %rdx     # 0x10f6cd
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
addq $0x32a, %rbx            # imm = 0x32A
leaq 0x55568(%rip), %rsi     # 0xe5034
leaq 0x7fc04(%rip), %rdx     # 0x10f6d7
pushq $0x4a
popq %rcx
movq %rbx, %rdi
movq %r15, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rax
jmp 0x8fcac
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
cmpl %eax, %r15d
jge 0x8fcaa
leal 0x1(%r15), %edx
movl %edx, %r12d
imull %r15d, %r12d
shrl %r12d
movl %edx, 0x10(%rsp)
cmpl %eax, %edx
jge 0x8fbbf
cmpl %edx, %r15d
jbe 0x8fb2e
movl %eax, %esi
imull %edx, %esi
addl %r15d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
shrl %edi
subl %edi, %esi
jmp 0x8fb3b
movl %eax, %edi
imull %r15d, %edi
movl %edx, %esi
subl %r12d, %esi
addl %edi, %esi
movq 0x2b0(%rbx), %rdi
movq (%rdi,%r13,8), %rdi
movslq %esi, %r14
cmpw $0x1, (%rdi,%r14,2)
jne 0x8fb89
incl %eax
movl %eax, %ecx
imull %r15d, %ecx
subl %r12d, %ecx
movslq %ecx, %rcx
movq 0x8(%rsp), %rsi
movsd (%rsi,%rcx,8), %xmm0
imull %edx, %eax
leal 0x1(%rdx), %ebp
imull %ebp, %edx
shrl %edx
subl %edx, %eax
cltq
mulsd (%rsi,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x8fb8f
sqrtsd %xmm0, %xmm0
jmp 0x8fb98
incl %edx
movl %edx, %ebp
jmp 0x8fbb4
callq 0x82d0
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %rax
movsd (%rax,%r14,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r14,8)
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
movl %ebp, %edx
jmp 0x8fb0d
movl 0x10(%rsp), %edx
movl %edx, %r15d
jmp 0x8faf2
movq 0x288(%rbx), %rax
movq 0x2a0(%rbx), %rcx
movq (%rcx,%r13,8), %rdx
movq 0x2a8(%rbx), %rcx
movq (%rcx,%r13,8), %rcx
movl (%rax,%r13,4), %r8d
leaq 0x32a(%rbx), %r15
subq $0x8, %rsp
leaq 0x38(%rsp), %r9
pushq %r15
pushq %r8
pushq 0x20(%rsp)
callq 0x1604b
addq $0x20, %rsp
cmpl $0x1, %eax
jne 0x8fca2
leaq 0x557d3(%rip), %rsi     # 0xe53f0
leaq 0x7fae8(%rip), %rdx     # 0x10f70c
leaq 0x830(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x553f4(%rip), %rsi     # 0xe5034
leaq 0x7fa90(%rip), %rdx     # 0x10f6d7
movq %r15, %rdi
movl $0xba, %ecx
movq %rbx, %r8
jmp 0x8fadc
movq 0x8(%rsp), %rax
andq $0x0, (%rax)
cmpl $0x1, %ebp
jne 0x8fcaa
xorl %edx, %edx
movq 0x8(%rsp), %r15
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
cmpl %eax, %edx
jge 0x8fcbe
incl %eax
imull %edx, %eax
leal 0x1(%rdx), %ebp
imull %ebp, %edx
shrl %edx
subl %edx, %eax
movslq %eax, %r14
movsd (%r15,%r14,8), %xmm0
callq 0x82e0
movsd %xmm0, (%r15,%r14,8)
movl %ebp, %edx
jmp 0x8fc6c
testl %ebp, %ebp
je 0x8fd9a
xorl %eax, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
cmpl %eax, %r15d
jge 0x8fcaa
leal 0x1(%r15), %edx
movl %edx, %ebp
imull %r15d, %ebp
shrl %ebp
movl %edx, 0x10(%rsp)
cmpl %eax, %edx
jge 0x8fd8e
cmpl %edx, %r15d
jbe 0x8fcfb
movl %eax, %esi
imull %edx, %esi
addl %r15d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
shrl %edi
subl %edi, %esi
jmp 0x8fd07
movl %eax, %edi
imull %r15d, %edi
movl %edx, %esi
subl %ebp, %esi
addl %edi, %esi
movq 0x2b0(%rbx), %rdi
movq (%rdi,%r13,8), %rdi
movslq %esi, %r14
cmpw $0x1, (%rdi,%r14,2)
jne 0x8fd56
incl %eax
movl %eax, %ecx
imull %r15d, %ecx
subl %ebp, %ecx
movslq %ecx, %rcx
movq 0x8(%rsp), %rsi
movsd (%rsi,%rcx,8), %xmm0
imull %edx, %eax
leal 0x1(%rdx), %r12d
imull %r12d, %edx
shrl %edx
subl %edx, %eax
cltq
mulsd (%rsi,%rax,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x8fd5d
sqrtsd %xmm0, %xmm0
jmp 0x8fd66
incl %edx
movl %edx, %r12d
jmp 0x8fd82
callq 0x82d0
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %rax
movsd (%rax,%r14,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r14,8)
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
movl %r12d, %edx
jmp 0x8fcda
movl 0x10(%rsp), %edx
movl %edx, %r15d
jmp 0x8fcc5
xorl %edx, %edx
movq 0x8(%rsp), %r15
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
cmpl %eax, %edx
jge 0x8fdd7
incl %eax
imull %edx, %eax
leal 0x1(%rdx), %ebp
imull %ebp, %edx
shrl %edx
subl %edx, %eax
movslq %eax, %r14
movsd (%r15,%r14,8), %xmm0
callq 0x86a0
movsd %xmm0, (%r15,%r14,8)
movl %ebp, %edx
jmp 0x8fda1
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
cmpl %eax, %r15d
jge 0x8fcaa
leal 0x1(%r15), %edx
movl %edx, %ebp
imull %r15d, %ebp
shrl %ebp
movl %edx, 0x10(%rsp)
cmpl %eax, %edx
jge 0x8feb1
cmpl %edx, %r15d
jbe 0x8fe18
movl %eax, %esi
imull %edx, %esi
addl %r15d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
shrl %edi
subl %edi, %esi
jmp 0x8fe24
movl %eax, %edi
imull %r15d, %edi
movl %edx, %esi
subl %ebp, %esi
addl %edi, %esi
movq 0x2b0(%rbx), %rdi
movq (%rdi,%r13,8), %rdi
movslq %esi, %r14
cmpw $0x1, (%rdi,%r14,2)
jne 0x8fe73
incl %eax
movl %eax, %ecx
imull %r15d, %ecx
subl %ebp, %ecx
movslq %ecx, %rcx
movq 0x8(%rsp), %rsi
movsd (%rsi,%rcx,8), %xmm0
imull %edx, %eax
leal 0x1(%rdx), %r12d
imull %r12d, %edx
shrl %edx
subl %edx, %eax
cltq
mulsd (%rsi,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x8fe84
sqrtsd %xmm0, %xmm0
jmp 0x8fe8d
movq 0x8(%rsp), %rax
andq $0x0, (%rax,%r14,8)
incl %edx
movl %edx, %r12d
jmp 0x8fea5
callq 0x82d0
xorpd %xmm1, %xmm1
movq 0x8(%rsp), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rax,%r14,8)
movq 0x280(%rbx), %rcx
movl (%rcx,%r13,4), %eax
movl %r12d, %edx
jmp 0x8fdf7
movl 0x10(%rsp), %edx
movl %edx, %r15d
jmp 0x8fdde