// File: /workspace/github_cpp_makefile_star2_O3/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
movl %r9d, %r15d
movq %r8, %r13
movq %rcx, 0x8(%rsp)
movl %edx, %r14d
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
leal (%r15,%r14), %eax
movslq %eax, %rbx
movl $0x18, %ecx
movq %rbx, %rax
mulq %rcx
movq $-0x1, %r12
cmovnoq %rax, %r12
movq %r12, %rdi
callq 0xc030
movq %rax, %rbp
testl %ebx, %ebx
je 0x11922
movq %r13, 0x28(%rsp)
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %r13
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rdi
callq 0x41b90
addq $0x18, %rbx
cmpq %rbx, %r13
jne 0x118df
movq %r15, 0x20(%rsp)
movq %r12, %rdi
callq 0xc030
movq %rax, %rbx
xorl %r15d, %r15d
leaq (%rbx,%r15), %rdi
callq 0x41b90
addq $0x18, %r15
cmpq %r15, %r13
jne 0x11904
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r13
jmp 0x1192d
movq %r12, %rdi
callq 0xc030
movq %rax, %rbx
movq 0x80(%rsp), %rax
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %r14d, %edx
movq 0x8(%rsp), %rcx
movq %r13, %r8
movl %r15d, %r9d
pushq %rbx
movq %rbp, 0x18(%rsp)
pushq %rbp
pushq 0x98(%rsp)
movq %rax, %rbp
pushq %rax
callq 0x11a3c
addq $0x20, %rsp
xorpd %xmm1, %xmm1
movl %eax, 0x18(%rsp)
testl %eax, %eax
jle 0x119c8
movl 0x18(%rsp), %r12d
leaq 0x30(%rsp), %r15
movq 0x10(%rsp), %r13
movq %rbx, %r14
movsd %xmm1, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x41ba2
movq %r15, %rdi
movq %rbp, %rsi
callq 0x424a8
movq %r15, %rdi
movq %r14, %rsi
callq 0x1d304
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
addq $0x18, %r14
addq $0x18, %r13
decq %r12
jne 0x11986
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rsp), %xmm0
divsd %xmm0, %xmm1
ucomisd 0x46653(%rip), %xmm1    # 0x58030
jb 0x119e8
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x119f1
movapd %xmm1, %xmm0
callq 0xc2b0
movsd %xmm0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0xc4a0
movq %rbx, %rdi
callq 0xc4a0
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq %rbx, %rdi
jmp 0x11a2c
movq %rbp, %rdi
movq %rax, %r14
movq %r12, %rsi
callq 0xc6c0
movq %r14, %rdi
callq 0xc620