// File: DavidLBruce[P]sim3dof/build/../src/MathUtils.cpp
// Function: lut1d(double*, double*, int, double)
double lut1d( double *xlist,
              double *lut,
              int    x_dim,
              double x )
{
  int xu;
  int xl;
  int xm;
  if (x < xlist[0])
    x = xlist[0];
  if (x > xlist[x_dim-1])
    x = xlist[x_dim-1];

  xu = x_dim - 1;
  xl = 0;
  xm = (xu + xl) / 2;

  if (x == xlist[xu])
    xl = xu;
  else if (x == xlist[xl])
    xu = xl;
  else {
    while (xu - xl > 1) {
      xm = (xu + xl) / 2;
      if (xlist[xm] == x)
        xl = xu = xm;
      else if (xlist[xm] > x)
        xu = xm;
      else xl = xm;
    }
  }

  if (xl == xu) {
    return lut[xu];
  } else {
    return lut[xl] + (x - xlist[xl]) *
                         ((lut[xu] - lut[xl]) / (xlist[xu] - xlist[xl]));
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm1
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc494
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0xc4c4
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x2c(%rbp), %eax
addl -0x30(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x34(%rbp)
movsd -0x28(%rbp), %xmm0
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0xc506
jp 0xc506
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0xc58d
movsd -0x28(%rbp), %xmm0
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0xc524
jp 0xc524
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xc58b
jmp 0xc526
movl -0x2c(%rbp), %eax
subl -0x30(%rbp), %eax
cmpl $0x1, %eax
jle 0xc589
movl -0x2c(%rbp), %eax
addl -0x30(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x28(%rbp), %xmm0
jne 0xc563
jp 0xc563
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl %eax, -0x30(%rbp)
jmp 0xc587
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x28(%rbp), %xmm0
jbe 0xc57f
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xc585
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0xc587
jmp 0xc526
jmp 0xc58b
jmp 0xc58d
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0xc5a9
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xc60d
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x28(%rbp), %xmm0
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm3
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm3
divsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
popq %rbp
retq
nopw %cs:(%rax,%rax)