// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: ICPAlignmentTransformation(gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Affine&, gaps::R3Affine&, int, int, int)
static int
ICPAlignmentTransformation(
  R3Mesh *mesh1, const R3Point *points1, int npoints1,
  R3Mesh *mesh2, const R3Point *points2, int npoints2, 
  R3Affine& affine12, R3Affine& affine21,
  int translation, int rotation, int scale)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Allocate arrays of correspondences
  int ncorrespondences = npoints1 + npoints2;
  R3Point *correspondences_buffer1 = new R3Point [2 * ncorrespondences];
  R3Point *correspondences_buffer2 = new R3Point [2 * ncorrespondences];
  assert(correspondences_buffer1 && correspondences_buffer2);

  // Iterate until max_iterations or converged
  RNBoolean converged = FALSE;
  const int max_iterations = 128;
  for (int iteration = 0; iteration < max_iterations; iteration++) {
    // Get arrays of correspondences
    R3Point *correspondences1 = &correspondences_buffer1[iteration%2 * ncorrespondences];
    R3Point *correspondences2 = &correspondences_buffer2[iteration%2 * ncorrespondences];

    // Update correspondences for aligning transformation
    CreatePointCorrespondences(
      mesh1, points1, npoints1, 
      mesh2, points2, npoints2, 
      affine12, affine21, 
      correspondences1, correspondences2, 
      ncorrespondences);

    // Update aligning transformation for correspondences
    R4Matrix matrix = R3AlignPoints(ncorrespondences, correspondences2, correspondences1, 
      NULL, translation, rotation, scale);
    affine12.Reset(matrix);
    affine21 = affine12.Inverse();

    // Check for convergence
    converged = FALSE;
    if (iteration > 0) {
      converged = TRUE;
      R3Point *prev_correspondences1 = &correspondences_buffer1[(1-(iteration%2)) * ncorrespondences];
      R3Point *prev_correspondences2 = &correspondences_buffer2[(1-(iteration%2)) * ncorrespondences];
      for (int i = 0; i < ncorrespondences; i++) {
        if (!R3Contains(correspondences1[i], prev_correspondences1[i])) { converged = FALSE; break; }
        if (!R3Contains(correspondences2[i], prev_correspondences2[i])) { converged = FALSE; break; }
      }
    }
    if (converged) break;
  }

  // Return whether converged
  return converged;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movl %r9d, %r14d
movq %r8, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movl %edx, %r15d
movq %rsi, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0x54c8c
movq %rbx, %rdi
xorl %esi, %esi
callq 0xc5b0
movq %r14, 0x70(%rsp)
movq %r15, 0x58(%rsp)
addl %r14d, %r15d
leal (%r15,%r15), %eax
movslq %eax, %r14
movl $0x18, %ecx
movq %r14, %rax
mulq %rcx
movq $-0x1, %rbx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0xc030
movq %rax, %r13
movq %r15, 0x10(%rsp)
testl %r15d, %r15d
je 0x1164f
shlq $0x3, %r14
leaq (%r14,%r14,2), %r14
xorl %r15d, %r15d
leaq (%r15,%r13), %rdi
callq 0x41b90
addq $0x18, %r15
cmpq %r15, %r14
jne 0x1161b
movq %rbx, %rdi
callq 0xc030
movq %rax, %r12
xorl %r15d, %r15d
leaq (%r12,%r15), %rdi
callq 0x41b90
addq $0x18, %r15
cmpq %r15, %r14
jne 0x1163b
jmp 0x1165a
movq %rbx, %rdi
callq 0xc030
movq %rax, %r12
movq 0x258(%rsp), %rbp
leaq 0x8(%rbp), %rax
movq %rax, 0x40(%rsp)
movl $0x88, %eax
leaq (%rax,%rbp), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x88(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl 0x10(%rsp), %eax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
movb $0x1, 0xf(%rsp)
movq %r13, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movl %r15d, %eax
andl $0x1, %eax
movq 0x10(%rsp), %r14
imull %r14d, %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
addq %rbx, %r12
addq %r13, %rbx
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %r8
movq 0x70(%rsp), %r9
pushq %r12
pushq %rbx
pushq %rbp
movq 0x268(%rsp), %r13
pushq %r13
callq 0x11a3c
addq $0x20, %rsp
leaq 0x198(%rsp), %rdi
movl %r14d, %esi
movq %r12, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
movl 0x260(%rsp), %r9d
movl 0x270(%rsp), %eax
pushq %rax
movl 0x270(%rsp), %eax
pushq %rax
callq 0x21cc5
addq $0x10, %rsp
movq %r13, %rdi
leaq 0x198(%rsp), %rsi
xorl %edx, %edx
callq 0x3c85c
movq %r13, %rdi
callq 0x3c216
movl 0x108(%r13), %edx
andl $0x1, %edx
leaq 0x88(%rsp), %rdi
movq %rax, %rsi
callq 0x3c1aa
movq 0x40(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x40072
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x40072
movq 0x190(%rsp), %rax
movq %rax, 0x108(%rbp)
leaq 0x88(%rsp), %rdi
callq 0x3c1f8
testl %r15d, %r15d
je 0x1180d
cmpl $0x0, 0x10(%rsp)
jle 0x11837
movzbl 0xf(%rsp), %eax
andl $0x1, %eax
imull 0x10(%rsp), %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbp
movq 0x18(%rsp), %rax
leaq (%rax,%rbp), %r14
addq 0x20(%rsp), %rbp
movq 0x28(%rsp), %r13
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1a343
testl %eax, %eax
je 0x1180d
movq %r12, %rdi
movq %r14, %rsi
callq 0x1a343
testl %eax, %eax
je 0x1180d
addq $0x18, %r14
addq $0x18, %r12
addq $0x18, %rbp
addq $0x18, %rbx
decq %r13
jne 0x117d8
jmp 0x11837
incl %r15d
incb 0xf(%rsp)
cmpl $0x80, %r15d
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r12
movq 0x258(%rsp), %rbp
jne 0x116a4
xorl %eax, %eax
jmp 0x1183c
movl $0x1, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x88(%rsp), %rdi
callq 0x3c1f8
jmp 0x11876
movq %rax, %r14
movq %r12, %rdi
jmp 0x1186e
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0xc6c0
movq %r14, %rdi
callq 0xc620