// File: /workspace/github_c_makefile_star2_O1/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), %r10d
leal -0x2(%rsi), %r11d
cmpl %r10d, %eax
cmovll %eax, %r11d
xorl %edi, %edi
testl %r11d, %r11d
cmovgl %r11d, %edi
movq 0x40(%rsp), %r14
ucomisd (%rbx,%rdi,8), %xmm0
jae 0x19d86
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x19df5
leaq 0xd9641(%rip), %r15     # 0xf3389
leaq 0xd9628(%rip), %r12     # 0xf3377
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 0x19dde
movslq %r10d, %r13
movsd (%rbx,%r13,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x19e3c
leaq 0xd939c(%rip), %r15     # 0xf313c
leaq 0xd95d0(%rip), %r12     # 0xf3377
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 0x19f59
leal -0x1(%rdi), %esi
movslq %edi, %rax
movsd -0x8(%rbx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x19e7d
movslq %esi, %rax
movsd (%rbx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x19e8c
movl $0xfffffffe, %r10d      # imm = 0xFFFFFFFE
xorl %r11d, %r11d
leal (%rsi,%r10), %edi
testl %edi, %edi
cmovlel %r11d, %edi
movsd (%rbx,%rdi,8), %xmm1
decl %r10d
ucomisd %xmm0, %xmm1
movl %esi, %eax
movl %edi, %esi
ja 0x19e1e
jmp 0x19e90
leal 0x1(%rdi), %r11d
movl %edi, %eax
ucomisd 0x8(%rbx,%rax,8), %xmm0
jbe 0x19e84
movl %r11d, %eax
ucomisd (%rbx,%rax,8), %xmm0
jbe 0x19ed9
movl $0x2, %r14d
leal (%r11,%r14), %eax
cmpl %esi, %eax
cmovgel %r10d, %eax
movslq %eax, %rdi
incl %r14d
ucomisd (%rbx,%rdi,8), %xmm0
movl %r11d, %edi
movl %eax, %r11d
ja 0x19e5e
jmp 0x19edc
movl %edi, %eax
jmp 0x19f23
movl %r11d, %eax
jmp 0x19f21
movl %edi, %eax
movl %esi, %edi
movl %eax, %esi
subl %edi, %esi
cmpl $0x2, %esi
jl 0x19f21
movsd 0xd7e2b(%rip), %xmm2    # 0xf1cd0
leal (%rdi,%rax), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %esi
movslq %esi, %rsi
movsd (%rbx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %eax
cmoval %edi, %esi
movl %eax, %r10d
subl %esi, %r10d
movl %esi, %edi
cmpl $0x1, %r10d
jg 0x19ea5
jmp 0x19f23
movl %r11d, %eax
movl %eax, %esi
subl %edi, %esi
cmpl $0x2, %esi
jl 0x19f21
movsd 0xd7de3(%rip), %xmm2    # 0xf1cd0
leal (%rdi,%rax), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %esi
movslq %esi, %rsi
movsd (%rbx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %eax
cmoval %edi, %esi
movl %eax, %r10d
subl %esi, %r10d
movl %esi, %edi
cmpl $0x1, %r10d
jg 0x19eed
jmp 0x19f23
movl %edi, %esi
movl %esi, (%rdx)
cltq
movsd (%rbx,%rax,8), %xmm1
movslq %esi, %rax
subsd (%rbx,%rax,8), %xmm1
movsd %xmm1, (%rcx)
subsd (%rbx,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r9)
movsd 0xd7312(%rip), %xmm1    # 0xf1260
subsd %xmm0, %xmm1
movsd %xmm1, (%r8)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq