// File: /workspace/github_c_makefile_star2_O2/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), %edi
leal -0x2(%rsi), %r10d
cmpl %edi, %eax
cmovll %eax, %r10d
xorl %eax, %eax
testl %r10d, %r10d
cmovgl %r10d, %eax
movq 0x40(%rsp), %r14
ucomisd (%rbx,%rax,8), %xmm0
jae 0x17d97
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x17e04
leaq 0xcf600(%rip), %r15     # 0xe7359
leaq 0xcf5e7(%rip), %r12     # 0xe7347
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 0x8580
incl %eax
movslq %eax, %rsi
movsd (%rbx), %xmm1
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
movl $0xaa2, %r8d            # imm = 0xAA2
jmp 0x17def
movslq %edi, %r13
movsd (%rbx,%r13,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x17e40
leaq 0xcf35b(%rip), %r15     # 0xe710c
leaq 0xcf58f(%rip), %r12     # 0xe7347
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 0x8580
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 0x8580
pushq $0x1
popq %rax
jmp 0x17f28
leal -0x1(%rax), %esi
movslq %eax, %rdi
movsd -0x8(%rbx,%rdi,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x17eaf
pushq $-0x2
popq %rdi
xorl %r10d, %r10d
ucomisd %xmm0, %xmm1
jbe 0x17e79
leal (%rsi,%rdi), %r11d
testl %r11d, %r11d
cmovlel %r10d, %r11d
movl %esi, %eax
movsd (%rbx,%r11,8), %xmm1
decl %edi
movl %r11d, %esi
jmp 0x17e20
leal 0x1(%rax), %r11d
movl %eax, %r10d
ucomisd 0x8(%rbx,%r10,8), %xmm0
jbe 0x17ef2
pushq $0x2
popq %r14
movl %r11d, %r10d
movslq %r11d, %r11
ucomisd (%rbx,%r11,8), %xmm0
jbe 0x17eb6
leal (%r10,%r14), %r11d
cmpl %esi, %r11d
cmovgel %edi, %r11d
incl %r14d
movl %r10d, %eax
jmp 0x17e58
movsd 0xcde2f(%rip), %xmm2    # 0xe5cb0
movl %eax, %edi
subl %esi, %edi
cmpl $0x2, %edi
jl 0x17eaf
leal (%rsi,%rax), %edi
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 %edi, %esi
jmp 0x17e81
movl %eax, %r11d
movl %esi, %eax
jmp 0x17ef2
movsd 0xcddf2(%rip), %xmm2    # 0xe5cb0
movl %r10d, %esi
subl %eax, %esi
cmpl $0x2, %esi
jl 0x17eef
leal (%rax,%r10), %esi
cvtsi2sd %esi, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %esi
movslq %esi, %rsi
movsd (%rbx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %r10d
cmoval %eax, %esi
movl %esi, %eax
jmp 0x17ebe
movl %r10d, %r11d
movl %eax, (%rdx)
movslq %r11d, %rdx
movsd (%rbx,%rdx,8), %xmm1
cltq
subsd (%rbx,%rax,8), %xmm1
movsd %xmm1, (%rcx)
subsd (%rbx,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r9)
movsd 0xcd343(%rip), %xmm1    # 0xe5260
subsd %xmm0, %xmm1
movsd %xmm1, (%r8)
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq