// File: /workspace/github_c_makefile_star2_O2/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 $0x278, %rsp            # imm = 0x278
movq %rsi, %r14
movq %rdi, %rbx
movl 0x6f4e(%rip), %eax      # 0xc180
cmpl $-0x63, %eax
je 0x5269
ucomiss 0x9fda(%rip), %xmm0     # 0xf218
jne 0x5269
jp 0x5269
movss 0x9fd6(%rip), %xmm2     # 0xf220
ucomiss %xmm1, %xmm2
jne 0x5269
jp 0x5269
movl 0x9fd5(%rip), %ecx      # 0xf22c
movl %ecx, 0x8(%rbx)
movq 0x9fc3(%rip), %rcx      # 0xf224
movq %rcx, (%rbx)
jmp 0x5534
movlps %xmm0, 0x9fa8(%rip)     # 0xf218
movss %xmm1, 0x9fa8(%rip)     # 0xf220
movl $0xfffffffe, 0x6efe(%rip) # imm = 0xFFFFFFFE
movss %xmm1, 0xc(%rsp)
movq %r14, %rdi
movaps %xmm0, 0x20(%rsp)
callq 0x3ab8
testl %eax, %eax
je 0x52a8
orl $-0x1, 0x6ee0(%rip)     # 0xc180
pushq $-0x1
popq %rax
jmp 0x5534
movq %rbx, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0x3950(%rip), %rbx      # 0x8c0c
leaq 0x33e4(%rip), %r15      # 0x86a7
leaq 0x70(%rsp), %r12
leaq 0x33ec(%rip), %r13      # 0x86bb
xorl %eax, %eax
movss 0xc(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
cmpq $0x3, %rax
je 0x552e
movq %rax, 0x18(%rsp)
movss (%rbx,%rax,4), %xmm2
mulss 0x3c4(%r14), %xmm2
addss %xmm0, %xmm2
cvtss2sd %xmm2, %xmm3
movsd %xmm3, 0x58(%rsp)
xorl %ebp, %ebp
movaps %xmm2, 0x60(%rsp)
cmpq $0x3, %rbp
je 0x5489
movss (%rbx,%rbp,4), %xmm0
mulss 0x3b8(%r14), %xmm0
addss %xmm1, %xmm0
movaps 0x40(%rsp), %xmm1
movss %xmm0, %xmm1            # xmm1 = xmm0[0],xmm1[1,2,3]
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
cvtss2sd %xmm0, %xmm0
movq %r12, %rdi
movq %r15, %rsi
movsd 0x58(%rsp), %xmm1
movb $0x2, %al
callq 0x2190
movdqa 0x40(%rsp), %xmm0
movaps 0x60(%rsp), %xmm1
movq %r14, %rdi
callq 0x3ab8
testl %eax, %eax
je 0x538e
cmpl $0x1, (%r14)
movq %r13, %rdi
jle 0x537e
movq %r12, %rsi
xorl %eax, %eax
callq 0x24d5
movss 0xc(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
incq %rbp
jmp 0x530e
movaps 0x30(%rsp), %xmm3
movaps 0x60(%rsp), %xmm0
movlhps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
shufps $0xe2, %xmm0, %xmm3     # xmm3 = xmm3[2,0],xmm0[2,3]
movss 0x3b0(%r14), %xmm0
movss 0x3b8(%r14), %xmm1
movss 0x3bc(%r14), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movss 0x3c4(%r14), %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movaps %xmm3, %xmm1
subps %xmm2, %xmm1
divps %xmm0, %xmm1
cvttps2dq %xmm1, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %eax
testl %eax, %eax
js 0x545d
leal 0x1(%rax), %ecx
cmpl 0x3c8(%r14), %ecx
setge %dl
movd %xmm0, %ecx
testl %ecx, %ecx
sets %sil
orb %dl, %sil
jne 0x545d
leal 0x1(%rcx), %edx
cmpl 0x3cc(%r14), %edx
jge 0x545d
movl %ecx, %ecx
movl %eax, %eax
shlq $0x3, %rax
addq 0x1470(%r14), %rax
imulq $0xc, %rcx, %rcx
xorl %edx, %edx
xorps %xmm2, %xmm2
cmpq $0x2, %rdx
je 0x5496
movq (%rax,%rdx,8), %rsi
addq %rcx, %rsi
pushq $0x8
popq %rdi
cmpq $0x20, %rdi
je 0x5458
movss -0x8(%rsi,%rdi), %xmm1
ucomiss %xmm2, %xmm1
jne 0x5452
jp 0x5452
movss (%rsi,%rdi), %xmm1
ucomiss %xmm2, %xmm1
jne 0x5452
jnp 0x5473
addq $0xc, %rdi
jmp 0x5433
incq %rdx
jmp 0x5423
cmpl $0x1, (%r14)
leaq 0x3270(%rip), %rdi      # 0x86d8
jg 0x5374
jmp 0x537e
cmpl $0x1, (%r14)
leaq 0x3278(%rip), %rdi      # 0x86f6
jg 0x5374
jmp 0x537e
movq 0x18(%rsp), %rax
incq %rax
jmp 0x52dc
cmpl $0x1, (%r14)
jle 0x54ed
leaq 0x326b(%rip), %rdi      # 0x870e
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
movaps %xmm3, 0x30(%rsp)
callq 0x24d5
movss 0x3b0(%r14), %xmm0
movss 0x3b8(%r14), %xmm1
movss 0x3bc(%r14), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movss 0x3c4(%r14), %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movaps 0x30(%rsp), %xmm1
subps %xmm2, %xmm1
divps %xmm0, %xmm1
cvttps2dq %xmm1, %xmm0
movq 0x10(%rsp), %rcx
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, 0x9d25(%rip)     # 0xf224
andl $0x0, 0x9d22(%rip)      # 0xf228
movd %xmm0, 0x9d1e(%rip)     # 0xf22c
movd %xmm0, 0x8(%rcx)
movq 0x9d0a(%rip), %rax      # 0xf224
movq %rax, (%rcx)
xorl %eax, %eax
orl 0x18(%rsp), %ebp
setne %al
movl %eax, 0x6c54(%rip)      # 0xc180
jmp 0x5534
movl 0x6c4c(%rip), %eax      # 0xc180
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq