// File: tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: Align(gaps::R3Mesh*, int, int, int, int)
static int
Align(R3Mesh *mesh1, int egi_rotation, int pca_translation, int pca_rotation, int pca_scale)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Initialize transformation
  R3Affine affine = R3identity_affine;

  // Transform to align mesh1 principal axes to canonical coordinate system
  if (pca_translation || pca_rotation || pca_scale) {
    R3Affine pca = mesh1->PCANormalizationTransformation(pca_translation, (pca_rotation==1), pca_scale);
    mesh1->Transform(pca);
    affine.Transform(pca);
  }

  // Transform to align mesh1 orientation with extended gaussian image
  if (egi_rotation) {
    R3Point centroid1 = mesh1->Centroid();
    R3Affine egi = mesh1->EGINormalizationTransformation(pca_translation, egi_rotation, pca_scale);
    mesh1->Transform(egi);
    R3Affine prev = affine;
    affine = R3identity_affine;
    if (!pca_translation) affine.Translate(centroid1.Vector());
    affine.Transform(egi);
    if (!pca_translation) affine.Translate(-(centroid1.Vector()));
    affine.Transform(prev);
  }

  // Print statistics
  if (print_verbose) {
    const R4Matrix& m = affine.Matrix();
    printf("Computed alignment transformation ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  Matrix[0][0-3] = %g %g %g %g\n", m[0][0], m[0][1], m[0][2], m[0][3]);
    printf("  Matrix[1][0-3] = %g %g %g %g\n", m[1][0], m[1][1], m[1][2], m[1][3]);
    printf("  Matrix[2][0-3] = %g %g %g %g\n", m[2][0], m[2][1], m[2][2], m[2][3]);
    printf("  Matrix[3][0-3] = %g %g %g %g\n", m[3][0], m[3][1], m[3][2], m[3][3]);
    printf("  Scale = %g\n", affine.ScaleFactor());
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x578, %rsp            # imm = 0x578
movq %rdi, 0x570(%rsp)
movl %esi, 0x56c(%rsp)
movl %edx, 0x568(%rsp)
movl %ecx, 0x564(%rsp)
movl %r8d, 0x560(%rsp)
leaq 0x550(%rsp), %rdi
callq 0xa2b30
leaq 0x550(%rsp), %rdi
callq 0x15df0
leaq 0x440(%rsp), %rdi
leaq 0xbb78b(%rip), %rsi     # 0xce1f0
callq 0x15e80
cmpl $0x0, 0x568(%rsp)
jne 0x12a8c
cmpl $0x0, 0x564(%rsp)
jne 0x12a8c
cmpl $0x0, 0x560(%rsp)
je 0x12b45
movq 0x570(%rsp), %rsi
movl 0x568(%rsp), %edx
movl 0x564(%rsp), %eax
subl $0x1, %eax
sete %al
movzbl %al, %ecx
movl 0x560(%rsp), %r8d
leaq 0x330(%rsp), %rdi
callq 0x399e0
jmp 0x12ac2
movq 0x570(%rsp), %rdi
movq (%rdi), %rax
movq 0x118(%rax), %rax
leaq 0x330(%rsp), %rsi
callq *%rax
jmp 0x12ae0
leaq 0x440(%rsp), %rdi
leaq 0x330(%rsp), %rsi
callq 0x72880
jmp 0x12af7
leaq 0x330(%rsp), %rdi
callq 0x725b0
jmp 0x12b45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x328(%rsp)
movl %eax, 0x324(%rsp)
jmp 0x1300c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x328(%rsp)
movl %eax, 0x324(%rsp)
leaq 0x330(%rsp), %rdi
callq 0x725b0
jmp 0x1300c
cmpl $0x0, 0x56c(%rsp)
je 0x12d08
movq 0x570(%rsp), %rsi
leaq 0x308(%rsp), %rdi
callq 0x38470
jmp 0x12b6a
movq 0x570(%rsp), %rsi
movl 0x568(%rsp), %edx
movl 0x56c(%rsp), %ecx
movl 0x560(%rsp), %r8d
leaq 0x1f8(%rsp), %rdi
callq 0x39d90
jmp 0x12b97
movq 0x570(%rsp), %rdi
movq (%rdi), %rax
movq 0x118(%rax), %rax
leaq 0x1f8(%rsp), %rsi
callq *%rax
jmp 0x12bb5
leaq 0xe8(%rsp), %rdi
leaq 0x440(%rsp), %rsi
callq 0x15e80
jmp 0x12bcc
leaq 0xbb61d(%rip), %rsi     # 0xce1f0
leaq 0x440(%rsp), %rdi
callq 0x16070
jmp 0x12be2
cmpl $0x0, 0x568(%rsp)
jne 0x12c5b
leaq 0xd0(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x15f70
jmp 0x12c03
leaq 0x440(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x15f30
jmp 0x12c1a
jmp 0x12c5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x328(%rsp)
movl %eax, 0x324(%rsp)
jmp 0x12cf6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x328(%rsp)
movl %eax, 0x324(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x725b0
jmp 0x12cf6
leaq 0x440(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x72880
jmp 0x12c72
cmpl $0x0, 0x568(%rsp)
jne 0x12cc3
leaq 0xa0(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x15f70
jmp 0x12c93
leaq 0xb8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x7f7a0
jmp 0x12caa
leaq 0x440(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x15f30
jmp 0x12cc1
jmp 0x12cc3
leaq 0x440(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x72880
jmp 0x12cda
leaq 0xe8(%rsp), %rdi
callq 0x725b0
leaq 0x1f8(%rsp), %rdi
callq 0x725b0
jmp 0x12d08
leaq 0x1f8(%rsp), %rdi
callq 0x725b0
jmp 0x1300c
cmpl $0x0, 0xbaf01(%rip)     # 0xcdc10
je 0x12ff2
leaq 0x440(%rsp), %rdi
callq 0x160e0
movq %rax, 0x98(%rsp)
leaq 0x94716(%rip), %rdi     # 0xa7447
xorl %eax, %eax
callq 0xd070
jmp 0x12d3a
leaq 0x550(%rsp), %rdi
callq 0xa2bf0
movsd %xmm0, 0x90(%rsp)
jmp 0x12d52
movsd 0x90(%rsp), %xmm0
leaq 0x944b1(%rip), %rdi     # 0xa7213
movb $0x1, %al
callq 0xd070
jmp 0x12d6b
movq 0x98(%rsp), %rdi
xorl %esi, %esi
movl %esi, 0x74(%rsp)
callq 0x160f0
movl 0x74(%rsp), %esi
movsd (%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x74(%rsp), %esi
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x74(%rsp), %esi
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movsd 0x18(%rax), %xmm3
leaq 0x9459f(%rip), %rdi     # 0xa739a
movb $0x4, %al
callq 0xd070
jmp 0x12e04
movq 0x98(%rsp), %rdi
movl $0x1, %esi
movl %esi, 0x54(%rsp)
callq 0x160f0
movl 0x54(%rsp), %esi
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x54(%rsp), %esi
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x54(%rsp), %esi
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x18(%rax), %xmm3
leaq 0x9452f(%rip), %rdi     # 0xa73ba
movb $0x4, %al
callq 0xd070
jmp 0x12e94
movq 0x98(%rsp), %rdi
movl $0x2, %esi
movl %esi, 0x34(%rsp)
callq 0x160f0
movl 0x34(%rsp), %esi
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x34(%rsp), %esi
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x34(%rsp), %esi
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
movsd 0x18(%rax), %xmm3
leaq 0x944bf(%rip), %rdi     # 0xa73da
movb $0x4, %al
callq 0xd070
jmp 0x12f24
movq 0x98(%rsp), %rdi
movl $0x3, %esi
movl %esi, 0x14(%rsp)
callq 0x160f0
movl 0x14(%rsp), %esi
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x14(%rsp), %esi
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movl 0x14(%rsp), %esi
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x98(%rsp), %rdi
callq 0x160f0
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movsd 0x18(%rax), %xmm3
leaq 0x9444f(%rip), %rdi     # 0xa73fa
movb $0x4, %al
callq 0xd070
jmp 0x12fb4
leaq 0x440(%rsp), %rdi
callq 0x72750
movsd %xmm0, 0x8(%rsp)
jmp 0x12fc9
movsd 0x8(%rsp), %xmm0
leaq 0x94444(%rip), %rdi     # 0xa741a
movb $0x1, %al
callq 0xd070
jmp 0x12fdf
movq 0xb9fba(%rip), %rax     # 0xccfa0
movq (%rax), %rdi
callq 0xd4c0
jmp 0x12ff0
jmp 0x12ff2
leaq 0x440(%rsp), %rdi
callq 0x725b0
movl $0x1, %eax
addq $0x578, %rsp            # imm = 0x578
retq
leaq 0x440(%rsp), %rdi
callq 0x725b0
movq 0x328(%rsp), %rdi
callq 0xd6a0
nopw %cs:(%rax,%rax)