// File: /workspace/github_c_makefile_star2_O3/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 0x6c48(%rip), %eax      # 0xc180
cmpl $-0x63, %eax
je 0x556f
ucomiss 0x9cd4(%rip), %xmm0     # 0xf218
jne 0x556f
jp 0x556f
movss 0x9cd0(%rip), %xmm2     # 0xf220
ucomiss %xmm1, %xmm2
jne 0x556f
jp 0x556f
movl 0x9ccf(%rip), %ecx      # 0xf22c
movl %ecx, 0x8(%rbx)
movq 0x9cbd(%rip), %rcx      # 0xf224
movq %rcx, (%rbx)
jmp 0x5860
movlps %xmm0, 0x9ca2(%rip)     # 0xf218
movss %xmm1, 0x9ca2(%rip)     # 0xf220
movl $0xfffffffe, 0x6bf8(%rip) # imm = 0xFFFFFFFE
movss %xmm1, 0xc(%rsp)
movq %r14, %rdi
movaps %xmm0, 0x20(%rsp)
callq 0x3c7d
testl %eax, %eax
je 0x55b3
movl $0xffffffff, 0x6bd7(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5860
movq %rbx, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0x3645(%rip), %rbx      # 0x8c0c
leaq 0x30d9(%rip), %r15      # 0x86a7
leaq 0x70(%rsp), %r12
leaq 0x30e1(%rip), %r13      # 0x86bb
xorl %eax, %eax
movss 0xc(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movq %rax, 0x18(%rsp)
movss (%rbx,%rax,4), %xmm2
mulss 0x3c4(%r14), %xmm2
addss %xmm0, %xmm2
movaps %xmm2, 0x60(%rsp)
cvtss2sd %xmm2, %xmm2
movsd %xmm2, 0x58(%rsp)
xorl %ebp, %ebp
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 0x3c7d
testl %eax, %eax
je 0x568e
cmpl $0x1, (%r14)
movq %r13, %rdi
jle 0x5675
movq %r12, %rsi
xorl %eax, %eax
callq 0x25b2
movss 0xc(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
incq %rbp
cmpq $0x3, %rbp
jne 0x560f
jmp 0x57aa
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 0x577e
leal 0x1(%rax), %ecx
cmpl 0x3c8(%r14), %ecx
setge %dl
movd %xmm0, %ecx
testl %ecx, %ecx
sets %sil
orb %dl, %sil
jne 0x577e
leal 0x1(%rcx), %edx
cmpl 0x3cc(%r14), %edx
jge 0x577e
movl %ecx, %edx
movl %eax, %eax
shlq $0x3, %rax
addq 0x1470(%r14), %rax
movb $0x1, %cl
leaq (%rdx,%rdx,2), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
xorps %xmm2, %xmm2
movq (%rax,%rsi,8), %rsi
addq %rdx, %rsi
movb $0x1, %dil
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r8
movss (%rsi,%r8,4), %xmm1
ucomiss %xmm2, %xmm1
jne 0x575c
jp 0x575c
leaq (%rsi,%r8,4), %r8
movss 0x8(%r8), %xmm1
ucomiss %xmm2, %xmm1
jne 0x575c
jnp 0x5794
movl $0x1, %r8d
testb $0x1, %dil
movl $0x0, %edi
jne 0x573a
movl $0x1, %esi
testb $0x1, %cl
movl $0x0, %ecx
jne 0x572d
jmp 0x57c7
cmpl $0x1, (%r14)
leaq 0x2f4f(%rip), %rdi      # 0x86d8
jg 0x566b
jmp 0x5675
cmpl $0x1, (%r14)
leaq 0x2f57(%rip), %rdi      # 0x86f6
jg 0x566b
jmp 0x5675
movq 0x18(%rsp), %rax
incq %rax
cmpq $0x3, %rax
jne 0x55e7
movl 0x69be(%rip), %eax      # 0xc180
jmp 0x5860
cmpl $0x1, (%r14)
jle 0x581e
leaq 0x2f3a(%rip), %rdi      # 0x870e
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
movaps %xmm3, 0x30(%rsp)
callq 0x25b2
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, 0x99f4(%rip)     # 0xf224
movl $0x0, 0x99ee(%rip)      # 0xf228
movd %xmm0, 0x99ea(%rip)     # 0xf22c
movd %xmm0, 0x8(%rcx)
movq 0x99d6(%rip), %rax      # 0xf224
movq %rax, (%rcx)
xorl %eax, %eax
orl 0x18(%rsp), %ebp
setne %al
movl %eax, 0x6920(%rip)      # 0xc180
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq