// File: /workspace/github_c_makefile_star2_O3/radforddc[P]icpc_siggen/icpc_fieldgen_opt.c
// Function: interpolate
int interpolate(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup) {
  int    n, i, j, i2, j2, zmin, rmin, zmax, rmax;
  int    L  = lrint(old_setup->xtal_length/old_setup->xtal_grid)+3;
  int    R  = lrint(old_setup->xtal_radius/old_setup->xtal_grid)+3;
  int    L2 = lrint(setup->xtal_length/setup->xtal_grid)+3;
  int    R2 = lrint(setup->xtal_radius/setup->xtal_grid)+3;
  float  f, f1r, f1z, f2r, f2z;
  double ***v = setup->v, **ov = old_setup->v[1];

  /* the previous calculation was on a coarser grid...
     now copy/expand the potential to the new finer grid
  */
  n = (int) (old_setup->xtal_grid / setup->xtal_grid + 0.5);
  f = 1.0 / (float) n;
  printf("\ngrid %.4f -> %.4f; ratio = %d %.3f\n\n",
         old_setup->xtal_grid, setup->xtal_grid, n, f);
  for (i = i2 = 1; i < L-1; i++) {
    zmin = i2;
    zmax = i2 + n;
    if (zmax > L2-1) zmax = L2-1;
    for (j = j2 = 1; j < R-1; j++) {
      f1z = 0.0;
      rmin = j2;
      rmax = j2 + n;
      if (rmax > R2-1) rmax = R2-1;
      for (i2 = zmin; i2 < zmax; i2++) {
        f2z = 1.0 - f1z;
        f1r = 0.0;
        for (j2 = rmin; j2 < rmax; j2++) {
          f2r = 1.0 - f1r;
          v[0][i2][j2] = v[1][i2][j2] =      // linear interpolation
            f2z*f2r*ov[i][j  ] + f1z*f2r*ov[i+1][j  ] +
            f2z*f1r*ov[i][j+1] + f1z*f1r*ov[i+1][j+1];
          f1r += f;
        }
        f1z += f;
      }
      j2 = rmax;
    }
    i2 = zmax;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbp
movq %rdi, %rbx
movss 0x4(%rsi), %xmm0
divss 0x54(%rsi), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r14
movss 0x8(%rbp), %xmm0
divss 0x54(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, 0x20(%rsp)
movss 0x4(%rbx), %xmm0
divss 0x54(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, 0x8(%rsp)
movss 0x8(%rbx), %xmm0
divss 0x54(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r12
movq 0x3e8(%rbp), %r15
movss 0x54(%rbp), %xmm1
movss 0x54(%rbx), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addsd 0x37b7(%rip), %xmm1     # 0x9038
cvttsd2si %xmm1, %r13d
cvttpd2dq %xmm1, %xmm1
cvtdq2ps %xmm1, %xmm1
movss 0x38ab(%rip), %xmm3     # 0x9140
divss %xmm1, %xmm3
movss %xmm3, 0x18(%rsp)
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
leaq 0x4443(%rip), %rdi      # 0x9cf7
movl %r13d, %esi
movb $0x3, %al
callq 0x10f0
movss 0x18(%rsp), %xmm6
movss 0x3874(%rip), %xmm5     # 0x9140
movq %r14, 0x10(%rsp)
cmpl $0x7ffffffd, %r14d      # imm = 0x7FFFFFFD
ja 0x5a7b
movq 0x10(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
leal 0x2(%rax), %edx
addl $0x2, %r12d
movl $0x1, %esi
movl $0x1, %ecx
movq %r12, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
leal (%rsi,%r13), %edi
movq 0x8(%rsp), %rax
cmpl %eax, %edi
cmovgel %eax, %edi
cmpl $0x7ffffffd, 0x20(%rsp) # imm = 0x7FFFFFFD
ja 0x5a6b
movslq %esi, %rax
movq %rax, 0x40(%rsp)
movslq %edi, %r8
movl $0x1, %r10d
movl $0x1, %r9d
movq %rsi, 0x48(%rsp)
movl %edi, 0x1c(%rsp)
leal (%r10,%r13), %r11d
cmpl %r12d, %r11d
cmovgel %r12d, %r11d
cmpl %edi, %esi
jge 0x5a59
leaq 0x1(%r9), %rax
movq %rax, 0x50(%rsp)
movslq %r10d, %r14
movslq %r11d, %rdi
xorps %xmm0, %xmm0
movq 0x40(%rsp), %rbp
cmpl %r11d, %r10d
jge 0x5a27
movaps %xmm5, %xmm1
subss %xmm0, %xmm1
movq (%r15,%rcx,8), %rax
movq 0x8(%r15,%rcx,8), %rdx
movq 0x3e0(%rbx), %r12
movq 0x3e8(%rbx), %rsi
movq (%rsi,%rbp,8), %rsi
movq (%r12,%rbp,8), %r13
xorps %xmm2, %xmm2
movq %r14, %r12
movaps %xmm5, %xmm3
subss %xmm2, %xmm3
movaps %xmm1, %xmm4
mulss %xmm3, %xmm4
cvtss2sd %xmm4, %xmm4
mulss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd (%rdx,%r9,8), %xmm3
mulsd (%rax,%r9,8), %xmm4
addsd %xmm3, %xmm4
movaps %xmm1, %xmm3
mulss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd 0x8(%rax,%r9,8), %xmm3
addsd %xmm4, %xmm3
movaps %xmm0, %xmm4
mulss %xmm2, %xmm4
cvtss2sd %xmm4, %xmm4
mulsd 0x8(%rdx,%r9,8), %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%rsi,%r12,8)
movsd %xmm4, (%r13,%r12,8)
addss %xmm6, %xmm2
incq %r12
cmpq %r12, %rdi
jne 0x59b8
addss %xmm6, %xmm0
incq %rbp
cmpq %r8, %rbp
jne 0x5983
movq 0x50(%rsp), %rax
movq %rax, %r9
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %r13
movq 0x28(%rsp), %rdx
movq 0x48(%rsp), %rsi
movl 0x1c(%rsp), %edi
jmp 0x5a5f
incq %r9
movq %r9, %rax
movl %r11d, %r10d
cmpq %rdx, %rax
jne 0x5959
incq %rcx
movl %edi, %esi
cmpq 0x10(%rsp), %rcx
jne 0x591d
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq