// File: Luthaf[P]zeopp/mindist.cc
// Function: MIN_PER_DISTANCE::closest_periodic_image(double, double, double, double, double, double, double&, double&, double&, double&, double&, double&)
void MIN_PER_DISTANCE::closest_periodic_image(double a1, double b1, double c1, double a2, double b2, double c2, 
					      double &minDa, double &minDb, double &minDc, double &return_a, double &return_b, double &return_c){
  double va_a = a1, va_b = b1, va_c = c1;
  double vb_a = a2, vb_b = b2, vb_c = c2;
  double vc_a = 0.5, vc_b = 0.5, vc_c = 0.5;
  
  double vd_a = vb_a - va_a + vc_a;
  double vd_b = vb_b - va_b + vc_b;
  double vd_c = vb_c - va_c + vc_c;
  double ve_a = trans_to_origuc(vd_a);
  double ve_b = trans_to_origuc(vd_b);
  double ve_c = trans_to_origuc(vd_c);
  
  double minDist = DBL_MAX;
  
  for(unsigned int index = 0; index < ivals.size(); index++){
    double vi_a = ivals[index], vi_b = jvals[index], vi_c = kvals[index];
    double da = vc_a - (ve_a+vi_a);
    double db = vc_b - (ve_b+vi_b);
    double dc = vc_c - (ve_c+vi_c);
    double dx, dy, dz;
    abc_to_xyz(da, db, dc, dx, dy, dz);
    double dist = sqrt(dx*dx + dy*dy + dz*dz);
    if(dist < minDist){
      minDist = dist;
      minDa = -da; minDb = -db; minDc = -dc;
/*      return_a = ve_a+vi_a;
      return_b = ve_b+vi_b;
      return_c = ve_c+vi_c;*/ //incorrect - returns closest point with respect to the centre, c, not with respect to the stationary point, a
      return_a = a1+minDa;
      return_b = b1+minDb;
      return_c = c1+minDc;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movsd %xmm3, -0x28(%rbp)
movsd %xmm4, -0x30(%rbp)
movsd %xmm5, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x140(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd 0x11a65e(%rip), %xmm0   # 0x1f2078
movsd %xmm0, -0x98(%rbp)
movsd 0x11a64e(%rip), %xmm0   # 0x1f2078
movsd %xmm0, -0xa0(%rbp)
movsd 0x11a63e(%rip), %xmm0   # 0x1f2078
movsd %xmm0, -0xa8(%rbp)
movsd -0x80(%rbp), %xmm0
subsd -0x68(%rbp), %xmm0
addsd -0x98(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0x88(%rbp), %xmm0
subsd -0x70(%rbp), %xmm0
addsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0x90(%rbp), %xmm0
subsd -0x78(%rbp), %xmm0
addsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xb0(%rbp), %xmm0
callq 0xe9290
movsd %xmm0, -0xc8(%rbp)
movsd -0xb8(%rbp), %xmm0
callq 0xe9290
movsd %xmm0, -0xd0(%rbp)
movsd -0xc0(%rbp), %xmm0
callq 0xe9290
movsd %xmm0, -0xd8(%rbp)
movsd 0x11e86b(%rip), %xmm0   # 0x1f6348
movsd %xmm0, -0xe0(%rbp)
movl $0x0, -0xe4(%rbp)
movq -0x140(%rbp), %rdi
movl -0xe4(%rbp), %eax
movq %rax, -0x148(%rbp)
callq 0x10090
movq %rax, %rcx
movq -0x148(%rbp), %rax
cmpq %rcx, %rax
jae 0xd7d69
movq -0x140(%rbp), %rdi
movl -0xe4(%rbp), %eax
movl %eax, %esi
callq 0xed60
movq -0x140(%rbp), %rdi
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0xf0(%rbp)
addq $0x18, %rdi
movl -0xe4(%rbp), %eax
movl %eax, %esi
callq 0xed60
movq -0x140(%rbp), %rdi
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
addq $0x30, %rdi
movl -0xe4(%rbp), %eax
movl %eax, %esi
callq 0xed60
movq -0x140(%rbp), %rdi
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0x98(%rbp), %xmm0
movsd -0xc8(%rbp), %xmm1
addsd -0xf0(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd -0xd0(%rbp), %xmm1
addsd -0xf8(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd -0xd8(%rbp), %xmm1
addsd -0x100(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x118(%rbp)
movsd -0x108(%rbp), %xmm0
movsd -0x110(%rbp), %xmm1
movsd -0x118(%rbp), %xmm2
leaq -0x120(%rbp), %rsi
leaq -0x128(%rbp), %rdx
leaq -0x130(%rbp), %rcx
callq 0xd72e0
movsd -0x120(%rbp), %xmm1
movsd -0x120(%rbp), %xmm2
movsd -0x128(%rbp), %xmm0
mulsd -0x128(%rbp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x130(%rbp), %xmm0
movsd -0x130(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x53f0
movsd %xmm0, -0x138(%rbp)
movsd -0x138(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd7d53
movsd -0x138(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0x108(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x110(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x118(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x10(%rbp), %xmm0
movq -0x40(%rbp), %rax
addsd (%rax), %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x18(%rbp), %xmm0
movq -0x48(%rbp), %rax
addsd (%rax), %xmm0
movq -0x60(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
movq -0x50(%rbp), %rax
addsd (%rax), %xmm0
movq 0x10(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0xd7d55
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0xd7aef
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw %cs:(%rax,%rax)