// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %rbp
movsd %xmm0, 0x20(%rsp)
subsd %xmm0, %xmm3
movsd 0xd5246(%rip), %xmm0    # 0x14d038
addsd %xmm0, %xmm3
movsd %xmm1, 0x28(%rsp)
subsd %xmm1, %xmm4
addsd %xmm0, %xmm4
movsd %xmm4, 0x8(%rsp)
movsd %xmm2, 0x30(%rsp)
subsd %xmm2, %xmm5
addsd %xmm0, %xmm5
movsd %xmm5, 0x18(%rsp)
movapd %xmm3, %xmm0
callq 0x839b6
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x839b6
movsd %xmm0, 0x8(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x839b6
movsd 0x8(%rsp), %xmm11
movsd 0x10(%rsp), %xmm10
movsd 0xd51d8(%rip), %xmm9    # 0x14d038
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
je 0x77fa1
sarq $0x2, %rcx
movq 0x70(%rsp), %rdx
movq 0x18(%rbp), %rsi
movq 0x30(%rbp), %rdi
movsd 0xda8ee(%rip), %xmm2    # 0x152778
xorl %r8d, %r8d
movapd 0xd97ab(%rip), %xmm1    # 0x151640
xorps %xmm4, %xmm4
cvtsi2sdl (%rax,%r8,4), %xmm4
xorps %xmm5, %xmm5
cvtsi2sdl (%rsi,%r8,4), %xmm5
xorps %xmm6, %xmm6
cvtsi2sdl (%rdi,%r8,4), %xmm6
addsd %xmm10, %xmm4
movapd %xmm9, %xmm3
subsd %xmm4, %xmm3
addsd %xmm11, %xmm5
movapd %xmm9, %xmm4
subsd %xmm5, %xmm4
addsd %xmm0, %xmm6
movapd %xmm9, %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
xorps %xmm6, %xmm6
sqrtsd %xmm7, %xmm6
ucomisd %xmm6, %xmm2
jbe 0x77f92
xorpd %xmm1, %xmm3
movlpd %xmm3, (%r13)
xorpd %xmm1, %xmm4
movlpd %xmm4, (%r12)
xorpd %xmm1, %xmm5
movlpd %xmm5, (%r15)
movsd (%r13), %xmm2
addsd 0x20(%rsp), %xmm2
movsd %xmm2, (%r14)
movsd (%r12), %xmm2
addsd 0x28(%rsp), %xmm2
movsd %xmm2, (%rbx)
movsd (%r15), %xmm2
addsd 0x30(%rsp), %xmm2
movsd %xmm2, (%rdx)
movapd %xmm6, %xmm2
incq %r8
movl %r8d, %r9d
cmpq %r9, %rcx
ja 0x77e95
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq