// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movlpd %xmm0, -0x20(%rbp)
movss %xmm1, -0x18(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x10(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x8(%rbp)
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq 0x6261(%rip), %rax      # 0xdbc0
movq %rax, -0x50(%rbp)
movl 0x625f(%rip), %eax      # 0xdbc8
movl %eax, -0x48(%rbp)
cmpl $-0x63, 0x97ed(%rip)    # 0x11160
je 0x79ba
movss -0x10(%rbp), %xmm0
ucomiss 0xc873(%rip), %xmm0     # 0x141f4
jne 0x79ba
jp 0x79ba
movss -0x8(%rbp), %xmm0
ucomiss 0xc86b(%rip), %xmm0     # 0x141fc
jne 0x79ba
jp 0x79ba
movq -0x28(%rbp), %rax
movq 0xc860(%rip), %rcx      # 0x14200
movq %rcx, (%rax)
movl 0xc85f(%rip), %ecx      # 0x14208
movl %ecx, 0x8(%rax)
movl 0x97ae(%rip), %eax      # 0x11160
movl %eax, -0x4(%rbp)
jmp 0x7b78
movl -0x8(%rbp), %eax
movl %eax, 0xc839(%rip)      # 0x141fc
movq -0x10(%rbp), %rax
movq %rax, 0xc826(%rip)      # 0x141f4
movl $0xfffffffe, 0x9788(%rip) # imm = 0xFFFFFFFE
movq -0x30(%rbp), %rdi
movl -0x8(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
movss -0x58(%rbp), %xmm1
callq 0x4dc0
cmpl $0x0, %eax
je 0x7a0d
movl $0xffffffff, 0x9758(%rip) # imm = 0xFFFFFFFF
jmp 0x7b6f
xorps %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x3, -0x44(%rbp)
jge 0x7b6d
movss -0x8(%rbp), %xmm1
movslq -0x44(%rbp), %rax
movss -0x50(%rbp,%rax,4), %xmm0
movq -0x30(%rbp), %rax
movss 0x3c4(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x34(%rbp)
movl $0x0, -0x40(%rbp)
cmpl $0x3, -0x40(%rbp)
jge 0x7b5d
movss -0x10(%rbp), %xmm1
movslq -0x40(%rbp), %rax
movss -0x50(%rbp,%rax,4), %xmm0
movq -0x30(%rbp), %rax
movss 0x3b8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x3c(%rbp), %rax
movq %rax, -0x70(%rbp)
movsd -0x70(%rbp), %xmm0
movss -0x68(%rbp), %xmm1
callq 0x8910
cmpl $0x0, %eax
je 0x7b4d
movss -0x3c(%rbp), %xmm0
movq -0x30(%rbp), %rax
subss 0x3b0(%rax), %xmm0
movq -0x30(%rbp), %rax
divss 0x3b8(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, 0xc728(%rip)      # 0x14200
movl $0x0, 0xc722(%rip)      # 0x14204
movss -0x34(%rbp), %xmm0
movq -0x30(%rbp), %rax
subss 0x3bc(%rax), %xmm0
movq -0x30(%rbp), %rax
divss 0x3c4(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, 0xc6ff(%rip)      # 0x14208
movq -0x28(%rbp), %rax
movq 0xc6ec(%rip), %rcx      # 0x14200
movq %rcx, (%rax)
movl 0xc6eb(%rip), %ecx      # 0x14208
movl %ecx, 0x8(%rax)
cmpl $0x0, -0x40(%rbp)
jne 0x7b38
cmpl $0x0, -0x44(%rbp)
jne 0x7b38
movl $0x0, 0x962a(%rip)      # 0x11160
jmp 0x7b42
movl $0x1, 0x961e(%rip)      # 0x11160
movl 0x9618(%rip), %eax      # 0x11160
movl %eax, -0x4(%rbp)
jmp 0x7b78
jmp 0x7b4f
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x7a55
jmp 0x7b5f
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x7a1c
jmp 0x7b6f
movl 0x95eb(%rip), %eax      # 0x11160
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)