// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movss 0x4(%rsi), %xmm0
divss 0x54(%rsi), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, 0x8(%rsp)
movss 0x8(%r12), %xmm0
divss 0x54(%r12), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %rbp
movss 0x4(%rbx), %xmm0
divss 0x54(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r14
movss 0x8(%rbx), %xmm0
divss 0x54(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r15
movq 0x3e8(%r12), %r13
movss 0x54(%r12), %xmm1
movss 0x54(%rbx), %xmm2
cvtss2sd %xmm1, %xmm0
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addsd 0x3055(%rip), %xmm1     # 0x8070
cvttsd2si %xmm1, %esi
cvttpd2dq %xmm1, %xmm1
cvtdq2ps %xmm1, %xmm1
movss 0x3116(%rip), %xmm3     # 0x8144
divss %xmm1, %xmm3
movss %xmm3, 0x14(%rsp)
cvtss2sd %xmm2, %xmm1
cvtss2sd %xmm3, %xmm2
leaq 0x3c40(%rip), %rdi      # 0x8c87
movq %rsi, 0x18(%rsp)
movb $0x3, %al
callq 0x10f0
movss 0x14(%rsp), %xmm7
addl $0x2, %r14d
movq %r14, 0x20(%rsp)
addl $0x2, %r15d
addl $0x2, %ebp
movslq %ebp, %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
addl $0x2, %eax
cltq
movq %rax, 0x28(%rsp)
pushq $0x1
popq %rax
pushq $0x1
popq %rdi
movl %eax, %ecx
movss 0x30b4(%rip), %xmm6     # 0x8144
cmpq 0x28(%rsp), %rdi
jge 0x51b5
movq 0x18(%rsp), %rax
leal (%rcx,%rax), %edx
movq 0x20(%rsp), %rax
cmpl %eax, %edx
cmovgel %eax, %edx
movslq %ecx, %r14
movl %edx, 0x8(%rsp)
movslq %edx, %r10
pushq $0x1
popq %rsi
pushq $0x1
popq %rax
movl %eax, %ecx
cmpq 0x30(%rsp), %rsi
jge 0x51a7
movq 0x18(%rsp), %rax
leal (%rcx,%rax), %ebp
cmpl %r15d, %ebp
cmovgel %r15d, %ebp
leaq 0x1(%rsi), %r11
movslq %ecx, %r8
movslq %ebp, %rcx
xorps %xmm0, %xmm0
movq %r14, %r9
cmpq %r10, %r9
jge 0x519d
movaps %xmm6, %xmm1
subss %xmm0, %xmm1
xorps %xmm2, %xmm2
movq %r8, %rdx
cmpq %rcx, %rdx
jge 0x5191
movaps %xmm6, %xmm3
subss %xmm2, %xmm3
movaps %xmm1, %xmm4
mulss %xmm3, %xmm4
cvtss2sd %xmm4, %xmm4
mulss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
movq 0x8(%r13,%rdi,8), %r12
mulsd (%r12,%rsi,8), %xmm3
movq (%r13,%rdi,8), %rax
mulsd (%rax,%rsi,8), %xmm4
movaps %xmm1, %xmm5
mulss %xmm2, %xmm5
cvtss2sd %xmm5, %xmm5
mulsd (%rax,%r11,8), %xmm5
addsd %xmm3, %xmm4
addsd %xmm4, %xmm5
movaps %xmm0, %xmm3
mulss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd (%r12,%r11,8), %xmm3
addsd %xmm5, %xmm3
movq 0x3e8(%rbx), %rax
movq (%rax,%r9,8), %rax
movsd %xmm3, (%rax,%rdx,8)
movq 0x3e0(%rbx), %rax
movq (%rax,%r9,8), %rax
movsd %xmm3, (%rax,%rdx,8)
addss %xmm7, %xmm2
incq %rdx
jmp 0x50ff
addss %xmm7, %xmm0
incq %r9
jmp 0x50e9
movq %r11, %rsi
movl %ebp, %ecx
jmp 0x50bf
incq %rdi
movl 0x8(%rsp), %eax
movl %eax, %ecx
jmp 0x5090
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq