// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq %r9, (%rsp)
movq %rdx, 0x28(%rsp)
leal (%r9,%rdx), %r15d
movslq %r15d, %r14
movl $0x18, %ecx
movq %r14, %rax
mulq %rcx
movq $-0x1, %r13
cmovnoq %rax, %r13
movq %r13, %rdi
callq 0xc030
movq %rax, %rbp
testl %r14d, %r14d
je 0x118d1
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %rbx
xorl %r12d, %r12d
leaq (%r12,%rbp), %rdi
callq 0x424b8
addq $0x18, %r12
cmpq %r12, %rbx
jne 0x118bf
movq %r13, %rdi
callq 0xc030
movq %rax, %rbx
testl %r15d, %r15d
je 0x118fe
shlq $0x3, %r14
leaq (%r14,%r14,2), %r14
xorl %r15d, %r15d
leaq (%rbx,%r15), %rdi
callq 0x424b8
addq $0x18, %r15
cmpq %r15, %r14
jne 0x118ec
movq 0x80(%rsp), %rax
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
movq (%rsp), %r9
pushq %rbx
movq %rbp, 0x10(%rsp)
pushq %rbp
pushq 0x98(%rsp)
movq %rax, %rbp
pushq %rax
callq 0x11a0e
addq $0x20, %rsp
xorpd %xmm1, %xmm1
movl %eax, 0x10(%rsp)
testl %eax, %eax
jle 0x1199c
movl 0x10(%rsp), %r13d
leaq 0x30(%rsp), %r15
movq 0x8(%rsp), %r12
movq %rbx, %r14
movsd %xmm1, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x424ca
movq %r15, %rdi
movq %rbp, %rsi
callq 0x42dd6
movq %r15, %rdi
movq %r14, %rsi
callq 0x1d8bc
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm1
addq $0x18, %r14
addq $0x18, %r12
decq %r13
jne 0x1195c
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rsp), %xmm0
divsd %xmm0, %xmm1
ucomisd 0x4767f(%rip), %xmm1    # 0x59030
jb 0x119bc
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x119c5
movapd %xmm1, %xmm0
callq 0xc2b0
movsd %xmm0, (%rsp)
movq 0x8(%rsp), %rdi
callq 0xc4a0
movq %rbx, %rdi
callq 0xc4a0
movsd (%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 0x119fe
movq %rbp, %rdi
movq %rax, %r14
movq %r13, %rsi
callq 0xc6c0
movq %r14, %rdi
callq 0xc620