// File: /workspace/github_c_makefile_star2_O1/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, 0x30(%rsp)
movss 0x4(%rbx), %xmm0
divss 0x54(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, 0x10(%rsp)
movss 0x8(%rbx), %xmm0
divss 0x54(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, 0x20(%rsp)
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 0x3796(%rip), %xmm1     # 0x9038
cvttsd2si %xmm1, %esi
cvttpd2dq %xmm1, %xmm1
cvtdq2ps %xmm1, %xmm1
movss 0x3873(%rip), %xmm3     # 0x9128
divss %xmm1, %xmm3
movss %xmm3, 0x2c(%rsp)
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
leaq 0x43a3(%rip), %rdi      # 0x9c77
movq %rsi, 0x38(%rsp)
movb $0x3, %al
callq 0x10f0
movss 0x2c(%rsp), %xmm6
movss 0x383a(%rip), %xmm5     # 0x9128
movq %r14, 0x18(%rsp)
cmpl $0x7ffffffd, %r14d      # imm = 0x7FFFFFFD
ja 0x5a8b
movq 0x18(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x48(%rsp)
movq 0x20(%rsp), %rax
addl $0x2, %eax
movq %rax, 0x20(%rsp)
movl $0x1, %edx
movl $0x1, %ecx
movq 0x38(%rsp), %rax
leal (%rdx,%rax), %esi
movq 0x10(%rsp), %rax
cmpl %eax, %esi
cmovgel %eax, %esi
movl %esi, 0xc(%rsp)
cmpl $0x7ffffffd, 0x30(%rsp) # imm = 0x7FFFFFFD
ja 0x5a77
movslq %edx, %rax
movq %rax, 0x40(%rsp)
movslq 0xc(%rsp), %r8
movl $0x1, %r10d
movl $0x1, %r9d
movq %rdx, 0x50(%rsp)
movq 0x38(%rsp), %rax
leal (%r10,%rax), %r11d
movq 0x20(%rsp), %rax
cmpl %eax, %r11d
cmovgel %eax, %r11d
cmpl 0xc(%rsp), %edx
jge 0x5a61
movslq %r10d, %r13
movslq %r11d, %r12
xorps %xmm0, %xmm0
movq 0x40(%rsp), %rdi
cmpl %r11d, %r10d
jge 0x5a51
movaps %xmm5, %xmm1
subss %xmm0, %xmm1
movq (%r15,%rcx,8), %rbp
movq 0x8(%r15,%rcx,8), %rax
movq 0x3e0(%rbx), %rsi
movq 0x3e8(%rbx), %rdx
movq (%rdx,%rdi,8), %rdx
movq (%rsi,%rdi,8), %rsi
xorps %xmm2, %xmm2
movq %r13, %r14
movaps %xmm5, %xmm3
subss %xmm2, %xmm3
movaps %xmm1, %xmm4
mulss %xmm3, %xmm4
cvtss2sd %xmm4, %xmm4
mulss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd (%rax,%r9,8), %xmm3
mulsd (%rbp,%r9,8), %xmm4
addsd %xmm3, %xmm4
movaps %xmm1, %xmm3
mulss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd 0x8(%rbp,%r9,8), %xmm3
addsd %xmm4, %xmm3
movaps %xmm0, %xmm4
mulss %xmm2, %xmm4
cvtss2sd %xmm4, %xmm4
mulsd 0x8(%rax,%r9,8), %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%rdx,%r14,8)
movsd %xmm4, (%rsi,%r14,8)
addss %xmm6, %xmm2
incq %r14
cmpq %r14, %r12
jne 0x59e2
addss %xmm6, %xmm0
incq %rdi
cmpq %r8, %rdi
jne 0x59ad
incq %r9
movl %r11d, %r10d
cmpq 0x48(%rsp), %r9
movq 0x50(%rsp), %rdx
jne 0x5980
incq %rcx
movl 0xc(%rsp), %eax
movl %eax, %edx
cmpq 0x18(%rsp), %rcx
jne 0x593e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq