// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../tools/arrays.c
// Function: array_interpolate
int array_interpolate(
		   double * array,
		   int n_columns,
		   int n_lines,
		   int index_x,   /** from 0 to (n_columns-1) */
		   double x,
		   int * last_index,
		   double * result,
		   int result_size, /** from 1 to n_columns */
		   ErrorMsg errmsg) {

  int inf,sup,mid,i;
  double weight;

  inf=0;
  sup=n_lines-1;

  if (*(array+inf*n_columns+index_x) < *(array+sup*n_columns+index_x)){

    if (x < *(array+inf*n_columns+index_x)) {
      class_sprintf(errmsg,"%s(L:%d) : x=%e < x_min=%e",__func__,__LINE__,x,*(array+inf*n_columns+index_x));
      return _FAILURE_;
    }

    if (x > *(array+sup*n_columns+index_x)) {
      class_sprintf(errmsg,"%s(L:%d) : x=%e > x_max=%e",__func__,__LINE__,x,*(array+sup*n_columns+index_x));
      return _FAILURE_;
    }

    while (sup-inf > 1) {

      mid=(int)(0.5*(inf+sup));
      if (x < *(array+mid*n_columns+index_x)) {sup=mid;}
      else {inf=mid;}

    }

  }

  else {

    if (x < *(array+sup*n_columns+index_x)) {
      class_sprintf(errmsg,"%s(L:%d) : x=%e < x_min=%e",__func__,__LINE__,x,*(array+sup*n_columns+index_x));
      return _FAILURE_;
    }

    if (x > *(array+inf*n_columns+index_x)) {
      class_sprintf(errmsg,"%s(L:%d) : x=%e > x_max=%e",__func__,__LINE__,x,*(array+inf*n_columns+index_x));
      return _FAILURE_;
    }

    while (sup-inf > 1) {

      mid=(int)(0.5*(inf+sup));
      if (x > *(array+mid*n_columns+index_x)) {sup=mid;}
      else {inf=mid;}

    }

  }

  *last_index = inf;

  weight=(x-*(array+inf*n_columns+index_x))/(*(array+sup*n_columns+index_x)-*(array+inf*n_columns+index_x));

  for (i=0; i<result_size; i++)
    *(result+i) = *(array+inf*n_columns+i) * (1.-weight)
      + weight * *(array+sup*n_columns+i);

  *(result+index_x) = x;

  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x48(%rsp), %rbx
decl %edx
movslq %ecx, %r12
leaq (%rdi,%r12,8), %r13
movsd (%r13), %xmm1
movl %edx, %eax
imull %esi, %eax
cltq
leaq (%rdi,%rax,8), %rbp
movsd (%rbp,%r12,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x15b60
ucomisd %xmm0, %xmm1
jbe 0x15bb8
leaq 0xd15c1(%rip), %r14     # 0xe70df
leaq 0xd15d5(%rip), %r15     # 0xe70fa
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %rcx
movl $0x681, %r8d            # imm = 0x681
movb $0x2, %al
movsd %xmm0, (%rsp)
callq 0x8580
incl %eax
movslq %eax, %rsi
movsd (%r13), %xmm1
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
movl $0x681, %r8d            # imm = 0x681
jmp 0x15c5b
ucomisd %xmm0, %xmm2
jbe 0x15c0d
leaq 0xd156e(%rip), %r14     # 0xe70df
leaq 0xd1582(%rip), %r15     # 0xe70fa
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %rcx
movl $0x697, %r8d            # imm = 0x697
movapd %xmm2, %xmm1
movb $0x2, %al
movsd %xmm0, (%rsp)
callq 0x8580
incl %eax
movslq %eax, %rsi
movsd (%rbp,%r12,8), %xmm1
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
movl $0x697, %r8d            # imm = 0x697
jmp 0x15c5b
ucomisd %xmm2, %xmm0
jbe 0x15c6f
leaq 0xd1543(%rip), %r14     # 0xe710c
leaq 0xd152a(%rip), %r15     # 0xe70fa
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %rcx
movl $0x686, %r8d            # imm = 0x686
movapd %xmm2, %xmm1
movb $0x2, %al
movsd %xmm0, (%rsp)
callq 0x8580
incl %eax
movslq %eax, %rsi
movsd (%rbp,%r12,8), %xmm1
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
movl $0x686, %r8d            # imm = 0x686
jmp 0x15c5b
ucomisd %xmm1, %xmm0
jbe 0x15cb0
leaq 0xd14ee(%rip), %r14     # 0xe710c
leaq 0xd14d5(%rip), %r15     # 0xe70fa
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %rcx
movl $0x69c, %r8d            # imm = 0x69C
movb $0x2, %al
movsd %xmm0, (%rsp)
callq 0x8580
incl %eax
movslq %eax, %rsi
movsd (%r13), %xmm1
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
movl $0x69c, %r8d            # imm = 0x69C
movsd (%rsp), %xmm0
movb $0x2, %al
callq 0x8580
pushq $0x1
popq %rax
jmp 0x15d64
xorl %eax, %eax
movsd 0xd0037(%rip), %xmm2    # 0xe5cb0
movl %edx, %ecx
subl %eax, %ecx
cmpl $0x2, %ecx
jl 0x15ced
leal (%rax,%rdx), %ecx
cvtsi2sd %ecx, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movl %ecx, %r10d
imull %esi, %r10d
movslq %r10d, %r10
movsd (%r13,%r10,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %ecx, %edx
cmoval %eax, %ecx
movl %ecx, %eax
jmp 0x15c79
xorl %eax, %eax
movsd 0xcfff6(%rip), %xmm2    # 0xe5cb0
movl %edx, %ecx
subl %eax, %ecx
cmpl $0x2, %ecx
jl 0x15ced
leal (%rax,%rdx), %ecx
cvtsi2sd %ecx, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movl %ecx, %r10d
imull %esi, %r10d
movslq %r10d, %r10
ucomisd (%r13,%r10,8), %xmm0
cmoval %ecx, %edx
cmoval %eax, %ecx
movl %ecx, %eax
jmp 0x15cba
movl 0x40(%rsp), %ecx
movl %eax, (%r8)
imull %esi, %eax
cltq
leaq (%rdi,%rax,8), %rax
movsd (%rax,%r12,8), %xmm1
movapd %xmm0, %xmm4
subsd %xmm1, %xmm4
imull %esi, %edx
movslq %edx, %rdx
leaq (%rdi,%rdx,8), %rdx
movsd (%rdx,%r12,8), %xmm2
subsd %xmm1, %xmm2
divsd %xmm2, %xmm4
movsd 0xcf535(%rip), %xmm1    # 0xe5260
subsd %xmm4, %xmm1
xorl %esi, %esi
testl %ecx, %ecx
cmovlel %esi, %ecx
cmpq %rsi, %rcx
je 0x15d5c
movsd (%rdx,%rsi,8), %xmm2
mulsd %xmm4, %xmm2
movsd (%rax,%rsi,8), %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%r9,%rsi,8)
incq %rsi
jmp 0x15d36
movsd %xmm0, (%r9,%r12,8)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq