// File: /workspace/github_c_makefile_star2_O1/radforddc[P]icpc_siggen/fields.c
// Function: nearest_field_grid_index
static int nearest_field_grid_index(cyl_pt pt, cyl_int_pt *ipt,
				    MJD_Siggen_Setup *setup){
  /* returns <0 if outside crystal or too far from a valid grid point
              0 if interpolation is okay
              1 if we can find a point but extrapolation is needed
  */
  static cyl_pt  last_pt;
  static cyl_int_pt last_ipt;
  static int     last_ret = -99;
  cyl_pt new_pt;
  int    dr, dz;
  float  d[3] = {0.0, -1.0, 1.0};

  if (last_ret != -99 &&
      pt.r == last_pt.r && pt.z == last_pt.z) {
    *ipt = last_ipt;
    return last_ret;
  }
  last_pt = pt;
  last_ret = -2;

  if (outside_detector_cyl(pt, setup)) {
    last_ret = -1;
  } else{
    new_pt.phi = 0.0;
    for (dz=0; dz<3; dz++) {
      new_pt.z = pt.z + d[dz]*setup->zstep;
      for (dr=0; dr<3; dr++) {
	new_pt.r = pt.r + d[dr]*setup->rstep;
	if (efield_exists(new_pt, setup)) {
	  last_ipt.r = (new_pt.r - setup->rmin)/setup->rstep;  // CHECKED: do NOT use lrintf
	  last_ipt.phi = 0;
	  last_ipt.z = (new_pt.z - setup->zmin)/setup->zstep;  // CHECKED: do NOT use lrintf
	  *ipt = last_ipt;
	  if (dr == 0 && dz == 0) {
	    last_ret = 0;
	  } else {
	    last_ret = 1;
	  }
	  return last_ret;
	}
      }
    }
  }

  return last_ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rsi, %r14
movq %rdi, %rbx
movl 0x694f(%rip), %eax      # 0xc180
cmpl $-0x63, %eax
je 0x5868
ucomiss 0x99db(%rip), %xmm0     # 0xf218
jne 0x5868
jp 0x5868
movss 0x99d7(%rip), %xmm2     # 0xf220
ucomiss %xmm1, %xmm2
jne 0x5868
jp 0x5868
movl 0x99d6(%rip), %ecx      # 0xf22c
movl %ecx, 0x8(%rbx)
movq 0x99c4(%rip), %rcx      # 0xf224
movq %rcx, (%rbx)
jmp 0x5b04
movlps %xmm0, 0x99a9(%rip)     # 0xf218
movss %xmm1, 0x99a9(%rip)     # 0xf220
movl $0xfffffffe, 0x68ff(%rip) # imm = 0xFFFFFFFE
movss %xmm1, 0x4(%rsp)
movq %r14, %rdi
movaps %xmm0, 0x30(%rsp)
callq 0x3d98
testl %eax, %eax
je 0x58ad
movl $0xffffffff, 0x68de(%rip) # imm = 0xFFFFFFFF
movl 0x68d8(%rip), %eax      # 0xc180
jmp 0x5b04
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x3303(%rip), %rbx      # 0x8bc4
leaq 0x2d97(%rip), %r15      # 0x865f
leaq 0x50(%rsp), %r12
leaq 0x2d9f(%rip), %r13      # 0x8673
xorl %eax, %eax
movaps 0x30(%rsp), %xmm0
movq %rax, 0x8(%rsp)
movss (%rbx,%rax,4), %xmm1
mulss 0x3c4(%r14), %xmm1
addss 0x4(%rsp), %xmm1
movss %xmm1, (%rsp)
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x28(%rsp)
xorl %ebp, %ebp
movss (%rbx,%rbp,4), %xmm1
mulss 0x3b8(%r14), %xmm1
addss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm0
movss %xmm1, %xmm0            # xmm0 = xmm1[0],xmm0[1,2,3]
movaps %xmm0, 0x10(%rsp)
movaps %xmm1, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movq %r12, %rdi
movq %r15, %rsi
movsd 0x28(%rsp), %xmm1
movb $0x2, %al
callq 0x2190
movaps 0x10(%rsp), %xmm0
movss (%rsp), %xmm1
movq %r14, %rdi
callq 0x3d98
testl %eax, %eax
je 0x5980
cmpl $0x1, (%r14)
movq %r13, %rdi
jle 0x596d
movq %r12, %rsi
xorl %eax, %eax
callq 0x25b2
incq %rbp
cmpq $0x3, %rbp
movaps 0x30(%rsp), %xmm0
jne 0x5905
jmp 0x5a67
movaps 0x40(%rsp), %xmm2
movaps %xmm2, %xmm0
subss 0x3b0(%r14), %xmm0
divss 0x3b8(%r14), %xmm0
cvttss2si %xmm0, %eax
testl %eax, %eax
js 0x5a3b
leal 0x1(%rax), %ecx
cmpl 0x3c8(%r14), %ecx
jge 0x5a3b
movss (%rsp), %xmm0
subss 0x3bc(%r14), %xmm0
divss 0x3c4(%r14), %xmm0
cvttss2si %xmm0, %ecx
testl %ecx, %ecx
js 0x5a3b
leal 0x1(%rcx), %edx
cmpl 0x3cc(%r14), %edx
jge 0x5a3b
movl %eax, %eax
shlq $0x3, %rax
addq 0x1470(%r14), %rax
movl %ecx, %ecx
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorps %xmm1, %xmm1
movq (%rax,%rdx,8), %rsi
addq %rcx, %rsi
addq $0x8, %rsi
xorl %edi, %edi
movss -0x8(%rsi,%rdi), %xmm0
ucomiss %xmm1, %xmm0
jne 0x5a23
jp 0x5a23
movss (%rsi,%rdi), %xmm0
ucomiss %xmm1, %xmm0
jne 0x5a23
jnp 0x5a51
addq $0xc, %rdi
cmpq $0xc, %rdi
je 0x5a0a
leaq 0x1(%rdx), %rsi
testq %rdx, %rdx
movq %rsi, %rdx
je 0x59fd
jmp 0x5a7e
cmpl $0x1, (%r14)
leaq 0x2c4a(%rip), %rdi      # 0x8690
jg 0x5963
jmp 0x596d
cmpl $0x1, (%r14)
leaq 0x2c52(%rip), %rdi      # 0x86ae
jg 0x5963
jmp 0x596d
movq 0x8(%rsp), %rax
incq %rax
cmpq $0x3, %rax
jne 0x58db
jmp 0x58a2
cmpl $0x2, (%r14)
jl 0x5a9c
leaq 0x2c3b(%rip), %rdi      # 0x86c6
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x25b2
movaps 0x40(%rsp), %xmm2
subss 0x3b0(%r14), %xmm2
divss 0x3b8(%r14), %xmm2
cvttss2si %xmm2, %eax
movl %eax, 0x976c(%rip)      # 0xf224
movl $0x0, 0x9766(%rip)      # 0xf228
movss (%rsp), %xmm0
subss 0x3bc(%r14), %xmm0
divss 0x3c4(%r14), %xmm0
cvttss2si %xmm0, %eax
movl %eax, 0x9749(%rip)      # 0xf22c
movq 0x20(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq 0x9732(%rip), %rax      # 0xf224
movq %rax, (%rcx)
xorl %eax, %eax
orl 0x8(%rsp), %ebp
setne %al
movl %eax, 0x667c(%rip)      # 0xc180
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq