// File: /workspace/github_c_makefile_star2_O3/PoulinV[P]AxiCLASS/build/../tools/arrays.c
// Function: array_spline_hunt
int array_spline_hunt(double* x_array,
                       int x_size,
                       double x,
                       int* last,
                       double* h,
                       double* a,
                       double* b,
                       ErrorMsg errmsg){
  /* Define local quantities */
  int inf,sup,mid,inc;
  int last_index = *last;

  /* Error checking */
  if(last_index>=x_size-1){last_index=x_size-2;}
  if(last_index<0){last_index=0;}

  /* Hunt ! */
  inc=1;
  if (x >= x_array[last_index]) {
    if (x > x_array[x_size-1]) {
      class_sprintf(errmsg,"%s(L:%d) : x=%e > x_max=%e",__func__,__LINE__,
        x,x_array[x_size-1]);
      return _FAILURE_;
    }
    /* try closest neighboor upward */
    inf = last_index;
    sup = inf + inc;
    if (x > x_array[sup]) {
      /* hunt upward */
      while (x > x_array[sup]) {
        inf = sup;
        inc += 1;
        sup += inc;
        if (sup > x_size-1) {
          sup = x_size-1;
        }
      }
      /* bisect */
      while (sup-inf > 1) {
        mid=(int)(0.5*(inf+sup));
        if (x < x_array[mid]) {sup=mid;}
        else {inf=mid;}
      }
    }
  }
  else {
    if (x < x_array[0]) {
      class_sprintf(errmsg,"%s(L:%d) : x=%.20e < x_min=%.20e",__func__,__LINE__,
        x,x_array[0]);
      return _FAILURE_;
    }
    /* try closest neighboor downward */
    sup = last_index;
    inf = sup - inc;
    if (x < x_array[inf]) {
      /* hunt downward */
      while (x < x_array[inf]) {
        sup = inf;
        inc += 1;
        inf -= inc;
        if (inf < 0) {
          inf = 0;
        }
      }
      /* bisect */
      while (sup-inf > 1) {
        mid=(int)(0.5*(inf+sup));
        if (x < x_array[mid]) {sup=mid;}
        else {inf=mid;}
      }
    }
  }

  /* We have found the prey */
  last_index = inf;
  *last = last_index;
  *h = x_array[sup] - x_array[inf];
  *b = (x-x_array[inf])/(*h);
  *a = 1.0-(*b);

  return _SUCCESS_;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movl (%rdx), %eax
leal -0x1(%rsi), %r11d
leal -0x2(%rsi), %r10d
cmpl %r11d, %eax
cmovll %eax, %r10d
xorl %edi, %edi
testl %r10d, %r10d
cmovgl %r10d, %edi
movq 0x40(%rsp), %r14
ucomisd (%rbx,%rdi,8), %xmm0
jae 0x19edf
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x19f4e
leaq 0xd64f8(%rip), %r15     # 0xf0399
leaq 0xd64df(%rip), %r12     # 0xf0387
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
movl $0xaa2, %r8d            # imm = 0xAA2
movb $0x2, %al
movsd %xmm0, 0x8(%rsp)
callq 0x8550
incl %eax
movslq %eax, %rsi
movsd (%rbx), %xmm1
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
movl $0xaa2, %r8d            # imm = 0xAA2
jmp 0x19f37
movslq %r11d, %r13
movsd (%rbx,%r13,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x19fc9
leaq 0xd6253(%rip), %r15     # 0xf014c
leaq 0xd6487(%rip), %r12     # 0xf0387
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
movl $0xa87, %r8d            # imm = 0xA87
movb $0x2, %al
movsd %xmm0, 0x8(%rsp)
callq 0x8550
incl %eax
movslq %eax, %rsi
movsd (%rbx,%r13,8), %xmm1
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
movl $0xa87, %r8d            # imm = 0xA87
movsd 0x8(%rsp), %xmm0
movb $0x2, %al
callq 0x8550
movl $0x1, %eax
jmp 0x1a07c
leal -0x1(%rdi), %esi
movslq %edi, %rax
movsd -0x8(%rbx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1a03d
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
xorl %r10d, %r10d
movl %esi, %eax
leal (%rax,%rdi), %esi
testl %esi, %esi
cmovlel %r10d, %esi
movsd (%rbx,%rsi,8), %xmm1
decl %edi
ucomisd %xmm0, %xmm1
ja 0x19f6c
cmpl $0x2, %eax
jl 0x1a03f
movsd 0xd4d3b(%rip), %xmm2    # 0xeecd0
leal (%rsi,%rax), %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %edi
movslq %edi, %rdi
movsd (%rbx,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %edi, %eax
cmoval %esi, %edi
movl %eax, %r10d
subl %edi, %r10d
movl %edi, %esi
cmpl $0x1, %r10d
jg 0x19f95
jmp 0x1a046
leal 0x1(%rdi), %eax
movl %edi, %r10d
ucomisd 0x8(%rbx,%r10,8), %xmm0
jbe 0x1a046
movl $0x2, %edi
movl %eax, %r10d
leal (%r10,%rdi), %eax
cmpl %esi, %eax
cmovgel %r11d, %eax
movslq %eax, %r14
incl %edi
ucomisd (%rbx,%r14,8), %xmm0
ja 0x19fdd
movl %eax, %esi
subl %r10d, %esi
cmpl $0x2, %esi
jl 0x1a043
movsd 0xd4cc7(%rip), %xmm2    # 0xeecd0
leal (%r10,%rax), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %esi
movslq %esi, %rdi
movsd (%rbx,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %edi, %eax
cmoval %r10d, %edi
movl %eax, %esi
subl %edi, %esi
movl %edi, %r10d
cmpl $0x1, %esi
jg 0x1a009
jmp 0x1a046
movl %edi, %eax
movl %esi, %edi
jmp 0x1a046
movl %r10d, %edi
movl %edi, (%rdx)
cltq
movsd (%rbx,%rax,8), %xmm1
movslq %edi, %rax
subsd (%rbx,%rax,8), %xmm1
movsd %xmm1, (%rcx)
subsd (%rbx,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r9)
movsd 0xd41ef(%rip), %xmm1    # 0xee260
subsd %xmm0, %xmm1
movsd %xmm1, (%r8)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq