// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: RMSD(gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Affine const&, gaps::R3Affine const&)
static RNScalar 
RMSD(R3Mesh *mesh1, const R3Point *points1, int npoints1,
     R3Mesh *mesh2, const R3Point *points2, int npoints2, 
     const R3Affine& affine12, const R3Affine& affine21)
{
  // Check number of points
  if (npoints1 == 0) return RN_INFINITY;
  if (npoints2 == 0) return RN_INFINITY;

  // Create array of correspondences
  int max_correspondences = npoints1 + npoints2;
  R3Point *correspondences1 = new R3Point [ max_correspondences ];
  R3Point *correspondences2 = new R3Point [ max_correspondences ];
  int ncorrespondences = CreatePointCorrespondences(
    mesh1, points1, npoints1, 
    mesh2, points2, npoints2, 
    affine12, affine21,
    correspondences1, correspondences2, 
    max_correspondences);

  // Add SSD of correspondences
  RNScalar ssd = 0;
  for (int i = 0; i < ncorrespondences; i++) {
    R3Point position1 = correspondences1[i];
    position1.Transform(affine12);
    R3Point& position2 = correspondences2[i];
    RNScalar d = R3Distance(position1, position2);
    ssd += d * d;
  }

  // Compute the RMSD
  RNScalar rmsd = sqrt(ssd / ncorrespondences);

  // Delete arrays of correspondences
  delete [] correspondences1;
  delete [] correspondences2;

  // Return RMSD
  return rmsd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq %r9, (%rsp)
movq %rdx, 0x20(%rsp)
leal (%r9,%rdx), %r15d
movslq %r15d, %rbp
pushq $0x18
popq %rcx
movq %rbp, %rax
mulq %rcx
pushq $-0x1
popq %r13
cmovnoq %rax, %r13
movq %r13, %rdi
callq 0xc030
movq %rax, %rbx
testl %ebp, %ebp
je 0x10a45
imulq $0x18, %rbp, %r14
xorl %r12d, %r12d
leaq (%rbx,%r12), %rdi
callq 0x3c81e
addq $0x18, %r12
cmpq %r12, %r14
jne 0x10a33
movq %r13, %rdi
callq 0xc030
movq %rax, %r14
testl %r15d, %r15d
je 0x10a6e
imulq $0x18, %rbp, %r15
xorl %r12d, %r12d
leaq (%r14,%r12), %rdi
callq 0x3c81e
addq $0x18, %r12
cmpq %r12, %r15
jne 0x10a5c
movq 0x80(%rsp), %r13
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %r8
movq (%rsp), %r9
pushq %r14
pushq %rbx
pushq 0x98(%rsp)
pushq %r13
callq 0x10b70
addq $0x20, %rsp
xorl %r12d, %r12d
testl %eax, %eax
movl $0x0, %ecx
movl %eax, 0x8(%rsp)
cmovgl %eax, %ecx
imulq $0x18, %rcx, %rbp
xorpd %xmm1, %xmm1
leaq 0x30(%rsp), %r15
cmpq %r12, %rbp
je 0x10b06
leaq (%rbx,%r12), %rsi
movq %r15, %rdi
movsd %xmm1, (%rsp)
callq 0x3c830
movq %r15, %rdi
movq %r13, %rsi
callq 0x3ce66
leaq (%r14,%r12), %rsi
movq %r15, %rdi
callq 0x1b472
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
addq $0x18, %r12
jmp 0x10ac6
cvtsi2sdl 0x8(%rsp), %xmm0
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x10b20
sqrtsd %xmm1, %xmm0
jmp 0x10b29
movapd %xmm1, %xmm0
callq 0xc2c0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0xc4c0
movq %r14, %rdi
callq 0xc4c0
movsd (%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq %r14, %rbx
jmp 0x10b5d
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc6f0
movq %r15, %rdi
callq 0xc650