// File: 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 %r14
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq 0x158(%rsp), %rax
movq 0x150(%rsp), %rax
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movl %edx, 0x11c(%rsp)
movq %rcx, 0x110(%rsp)
movq %r8, 0x108(%rsp)
movl %r9d, 0x104(%rsp)
cmpl $0x0, 0x11c(%rsp)
jne 0x1469c
leaq 0xb9516(%rip), %rax     # 0xcdba0
movsd (%rax), %xmm0
movsd %xmm0, 0x130(%rsp)
jmp 0x14a3f
cmpl $0x0, 0x104(%rsp)
jne 0x146bf
leaq 0xb94f3(%rip), %rax     # 0xcdba0
movsd (%rax), %xmm0
movsd %xmm0, 0x130(%rsp)
jmp 0x14a3f
movl 0x11c(%rsp), %eax
addl 0x104(%rsp), %eax
movl %eax, 0x100(%rsp)
movslq 0x100(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x80(%rsp)
callq 0xd030
movq %rax, %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x90(%rsp)
cmpq $0x0, %rax
je 0x14773
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x7d340
jmp 0x1475b
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x78(%rsp)
jne 0x1474a
movq 0x90(%rsp), %rax
movq %rax, 0xf8(%rsp)
movslq 0x100(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x50(%rsp)
callq 0xd030
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x60(%rsp)
cmpq $0x0, %rax
je 0x14810
movq 0x60(%rsp), %rax
movq 0x58(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x7d340
jmp 0x147f8
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x48(%rsp)
jne 0x147e7
movq 0x60(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x128(%rsp), %rdi
movq 0x120(%rsp), %rsi
movl 0x11c(%rsp), %edx
movq 0x110(%rsp), %rcx
movq 0x108(%rsp), %r8
movl 0x104(%rsp), %r9d
movq 0x150(%rsp), %r14
movq 0x158(%rsp), %rbx
movq 0xf8(%rsp), %r11
movq 0xe0(%rsp), %r10
movl 0x100(%rsp), %eax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movl %eax, 0x20(%rsp)
callq 0x15550
movl %eax, 0xdc(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x149cf
movq 0xf8(%rsp), %rsi
movslq 0xcc(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x7d390
movq 0x150(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x7dfc0
movq 0xe0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x2d870
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm2
movsd 0xd0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x148ad
movq 0x80(%rsp), %rsi
movq 0x90(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
callq 0xd760
jmp 0x14a53
movq 0x50(%rsp), %rsi
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
callq 0xd760
jmp 0x14a53
movsd 0xd0(%rsp), %xmm0
cvtsi2sdl 0xdc(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0xd2f0
movsd %xmm0, 0x98(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x14a10
movq 0x30(%rsp), %rdi
callq 0xd530
movq 0xe0(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x14a2d
movq 0x28(%rsp), %rdi
callq 0xd530
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x130(%rsp)
movsd 0x130(%rsp), %xmm0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r14
retq
movq 0xf0(%rsp), %rdi
callq 0xd6a0