// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %rdi, %rbp
subsd %xmm0, %xmm3
movsd 0xa24f0(%rip), %xmm0    # 0xfc038
addsd %xmm0, %xmm3
subsd %xmm1, %xmm4
addsd %xmm0, %xmm4
movsd %xmm4, 0x8(%rsp)
subsd %xmm2, %xmm5
addsd %xmm0, %xmm5
movsd %xmm5, 0x10(%rsp)
movapd %xmm3, %xmm0
callq 0x6377f
movsd %xmm0, 0x30(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x6377f
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x6377f
movq 0x70(%rsp), %r8
movsd 0x28(%rsp), %xmm11
movsd 0x20(%rsp), %xmm10
movsd 0x18(%rsp), %xmm9
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x18(%rbp), %rdx
movq 0x30(%rbp), %rsi
movsd 0xa7b1c(%rip), %xmm2    # 0x1016e8
xorl %edi, %edi
movapd 0xa69fa(%rip), %xmm1    # 0x1005d0
movl %edi, %edi
cmpq %rdi, %rcx
jbe 0x59cdb
cvtsi2sdl (%rax,%rdi,4), %xmm4
cvtsi2sdl (%rdx,%rdi,4), %xmm5
cvtsi2sdl (%rsi,%rdi,4), %xmm6
addsd 0x30(%rsp), %xmm4
movsd 0xa243a(%rip), %xmm7    # 0xfc038
movapd %xmm7, %xmm3
subsd %xmm4, %xmm3
addsd 0x8(%rsp), %xmm5
movapd %xmm7, %xmm4
subsd %xmm5, %xmm4
addsd %xmm0, %xmm6
movapd %xmm7, %xmm5
subsd %xmm6, %xmm5
movsd 0x50(%rbp), %xmm6
mulsd %xmm4, %xmm6
movsd 0x48(%rbp), %xmm7
mulsd %xmm3, %xmm7
addsd %xmm6, %xmm7
movsd 0x60(%rbp), %xmm6
mulsd %xmm5, %xmm6
addsd %xmm7, %xmm6
movsd 0x68(%rbp), %xmm7
mulsd %xmm5, %xmm7
movsd 0x58(%rbp), %xmm8
mulsd %xmm4, %xmm8
addsd %xmm7, %xmm8
movsd 0x70(%rbp), %xmm7
mulsd %xmm5, %xmm7
mulsd %xmm8, %xmm8
mulsd %xmm6, %xmm6
addsd %xmm8, %xmm6
mulsd %xmm7, %xmm7
addsd %xmm6, %xmm7
sqrtsd %xmm7, %xmm6
ucomisd %xmm6, %xmm2
jbe 0x59cd4
xorpd %xmm1, %xmm3
movlpd %xmm3, (%r13)
xorpd %xmm1, %xmm4
movlpd %xmm4, (%r12)
xorpd %xmm1, %xmm5
movlpd %xmm5, (%r15)
movsd (%r13), %xmm2
addsd %xmm11, %xmm2
movsd %xmm2, (%r14)
movsd (%r12), %xmm2
addsd %xmm10, %xmm2
movsd %xmm2, (%rbx)
movsd (%r15), %xmm2
addsd %xmm9, %xmm2
movsd %xmm2, (%r8)
movapd %xmm6, %xmm2
incl %edi
jmp 0x59bd6
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq