// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movsd %xmm0, 0x18(%rsp)
subsd %xmm0, %xmm3
movsd 0xd6306(%rip), %xmm0    # 0x14f038
addsd %xmm0, %xmm3
movsd %xmm1, 0x20(%rsp)
subsd %xmm1, %xmm4
addsd %xmm0, %xmm4
movsd %xmm4, 0x8(%rsp)
movsd %xmm2, 0x28(%rsp)
subsd %xmm2, %xmm5
addsd %xmm0, %xmm5
movsd %xmm5, (%rsp)
movapd %xmm3, %xmm0
callq 0x84e6b
movsd %xmm0, 0x38(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x84e6b
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
callq 0x84e6b
movq (%rbp), %rax
cmpq %rax, 0x8(%rbp)
je 0x78f2c
movapd %xmm0, %xmm4
movsd 0xdb92f(%rip), %xmm6    # 0x1546d0
movl $0x1, %ebx
xorl %ecx, %ecx
xorpd %xmm5, %xmm5
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq 0x18(%rbp), %rax
movq 0x30(%rbp), %rdx
xorps %xmm1, %xmm1
cvtsi2sdl (%rax,%rcx,4), %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl (%rdx,%rcx,4), %xmm2
addsd 0x38(%rsp), %xmm0
movsd 0xd6258(%rip), %xmm3    # 0x14f038
movapd %xmm3, %xmm7
subsd %xmm0, %xmm7
addsd 0x8(%rsp), %xmm1
movapd %xmm3, %xmm8
subsd %xmm1, %xmm8
addsd %xmm4, %xmm2
subsd %xmm2, %xmm3
movsd 0x50(%rbp), %xmm0
mulsd %xmm8, %xmm0
movsd 0x48(%rbp), %xmm1
mulsd %xmm7, %xmm1
addsd %xmm0, %xmm1
movsd 0x60(%rbp), %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
movsd 0x68(%rbp), %xmm0
mulsd %xmm3, %xmm0
movsd 0x58(%rbp), %xmm1
mulsd %xmm8, %xmm1
addsd %xmm0, %xmm1
movsd 0x70(%rbp), %xmm0
mulsd %xmm3, %xmm0
mulsd %xmm1, %xmm1
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm0
addsd %xmm2, %xmm0
ucomisd %xmm5, %xmm0
jb 0x78e64
sqrtsd %xmm0, %xmm0
jmp 0x78ea3
movsd %xmm6, (%rsp)
movapd %xmm7, 0x60(%rsp)
movapd %xmm8, 0x50(%rsp)
movapd %xmm3, 0x40(%rsp)
callq 0x4350
movapd 0x40(%rsp), %xmm3
movapd 0x50(%rsp), %xmm8
movapd 0x60(%rsp), %xmm7
movsd (%rsp), %xmm6
xorpd %xmm5, %xmm5
movsd 0x10(%rsp), %xmm4
ucomisd %xmm0, %xmm6
jbe 0x78f10
movapd 0xda6ff(%rip), %xmm1    # 0x1535b0
xorpd %xmm1, %xmm7
movlpd %xmm7, (%r13)
xorpd %xmm1, %xmm8
movlpd %xmm8, (%r12)
xorpd %xmm1, %xmm3
movlpd %xmm3, (%r15)
movsd (%r13), %xmm1
addsd 0x18(%rsp), %xmm1
movsd %xmm1, (%r14)
movsd (%r12), %xmm1
addsd 0x20(%rsp), %xmm1
movq 0x30(%rsp), %rax
movsd %xmm1, (%rax)
movsd (%r15), %xmm1
addsd 0x28(%rsp), %xmm1
movq 0xb0(%rsp), %rax
movsd %xmm1, (%rax)
movapd %xmm0, %xmm6
movl %ebx, %ecx
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
incl %ebx
cmpq %rcx, %rdx
ja 0x78db2
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop