// File: tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: CreatePointSurfaceCorrespondences(gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Affine const&, gaps::R3Affine const&, gaps::R3Point*, gaps::R3Point*, int)
static int
CreatePointSurfaceCorrespondences(
  R3Mesh *mesh1, const R3Point *points1, int npoints1,
  R3Mesh *mesh2, const R3Point *points2, int npoints2, 
  const R3Affine& affine12, const R3Affine& affine21,
  R3Point *correspondences1, R3Point *correspondences2, 
  int max_correspondences)
{
  // Initialize number of correspondences
  assert(max_correspondences == npoints1 + npoints2);
  int ncorrespondences = 0;

  // Compute correspondences for points1 -> mesh2
  static R3MeshSearchTree *tree2 = NULL;
  if (!tree2) tree2 = new R3MeshSearchTree(mesh2);
  else assert(mesh2 == tree2->mesh);
  for (int i = 0; i < npoints1; i++) {
    R3Point position1 = points1[i];
    position1.Transform(affine12);
    R3MeshIntersection closest;
    tree2->FindClosest(position1, closest);
    assert(ncorrespondences < max_correspondences);
    correspondences1[ncorrespondences] = points1[i];
    correspondences2[ncorrespondences] = closest.point;
    ncorrespondences++;
  }

  // Compute correspondences for points2 -> mesh1
  static R3MeshSearchTree *tree1 = NULL;
  if (!tree1) tree1 = new R3MeshSearchTree(mesh1);
  else assert(mesh1 == tree1->mesh);
  for (int i = 0; i < npoints2; i++) {
    R3Point position2 = points2[i];
    position2.Transform(affine21);
    R3MeshIntersection closest;
    tree1->FindClosest(position2, closest);
    assert(ncorrespondences < max_correspondences);
    correspondences1[ncorrespondences] = closest.point;
    correspondences2[ncorrespondences] = points2[i];
    ncorrespondences++;
  }

  // Return number of correspondences
  assert(ncorrespondences == npoints1 + npoints2);
  assert(ncorrespondences == max_correspondences);
  return ncorrespondences;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movl 0x130(%rsp), %eax
movq 0x128(%rsp), %rax
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rax
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movl %edx, 0xf4(%rsp)
movq %rcx, 0xe8(%rsp)
movq %r8, 0xe0(%rsp)
movl %r9d, 0xdc(%rsp)
movl $0x0, 0xd8(%rsp)
cmpq $0x0, 0xb8138(%rip)     # 0xcdc38
jne 0x15b5c
movl $0x18, %edi
callq 0xd3d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movq 0xe8(%rsp), %rsi
callq 0x18340
jmp 0x15b26
movq 0x8(%rsp), %rax
movq %rax, 0xb8106(%rip)     # 0xcdc38
jmp 0x15b5e
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x18, %esi
callq 0xd3e0
jmp 0x15dd5
jmp 0x15b5e
movl $0x0, 0xc8(%rsp)
movl 0xc8(%rsp), %eax
cmpl 0xf4(%rsp), %eax
jge 0x15c6e
movq 0xf8(%rsp), %rsi
movslq 0xc8(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x7d390
movq 0x110(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x7dfc0
leaq 0x70(%rsp), %rdi
callq 0x182c0
movq 0xb8071(%rip), %rdi     # 0xcdc38
leaq 0xb7fd2(%rip), %rax     # 0xcdba0
movsd (%rax), %xmm1
leaq 0xb0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
xorps %xmm0, %xmm0
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x1b730
movq 0xf8(%rsp), %rsi
movslq 0xc8(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
movq 0x120(%rsp), %rdi
movslq 0xd8(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x7d9e0
leaq 0x70(%rsp), %rsi
addq $0x20, %rsi
movq 0x128(%rsp), %rdi
movslq 0xd8(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x7d9e0
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x15b69
cmpq $0x0, 0xb7fca(%rip)     # 0xcdc40
jne 0x15ccf
movl $0x18, %edi
callq 0xd3d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movq 0x100(%rsp), %rsi
callq 0x18340
jmp 0x15c9b
movq (%rsp), %rax
movq %rax, 0xb7f9a(%rip)     # 0xcdc40
jmp 0x15cd1
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0x18, %esi
callq 0xd3e0
jmp 0x15dd5
jmp 0x15cd1
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x15dc6
movq 0xe0(%rsp), %rsi
movslq 0x6c(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq 0x50(%rsp), %rdi
callq 0x7d390
movq 0x118(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x7dfc0
leaq 0x10(%rsp), %rdi
callq 0x182c0
movq 0xb7f15(%rip), %rdi     # 0xcdc40
leaq 0xb7e6e(%rip), %rax     # 0xcdba0
movsd (%rax), %xmm1
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
xorps %xmm0, %xmm0
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x1b730
leaq 0x10(%rsp), %rsi
addq $0x20, %rsi
movq 0x120(%rsp), %rdi
movslq 0xd8(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x7d9e0
movq 0xe0(%rsp), %rsi
movslq 0x6c(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
movq 0x128(%rsp), %rdi
movslq 0xd8(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x7d9e0
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x15cd9
movl 0xd8(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xd0(%rsp), %rdi
callq 0xd6a0
nopw %cs:(%rax,%rax)
nopl (%rax)