// File: tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: main
int main(int argc, char **argv)
{
  // Check number of arguments
  if (!ParseArgs(argc, argv)) exit(1);

  // Read mesh1
  R3Mesh *mesh1 = ReadMesh(input1_name);
  if (!mesh1) exit(-1);

  // Read weights1
  int nweights1 = 0;
  RNScalar *weights1 = NULL;
  if (weights1_name) {
    if (!ReadWeights(weights1_name, weights1, nweights1)) exit(-1);
  }

  // Check if aligning to second model
  if (input2_name) {
    // Read mesh2
    R3Mesh *mesh2 = ReadMesh(input2_name);
    if (!mesh2) exit(-1);

    // Read weights2
    int nweights2 = 0;
    RNScalar *weights2 = NULL;
    if (weights2_name) {
      if (!ReadWeights(weights2_name, weights2, nweights2)) exit(-1);
    }

    // Align mesh1 to mesh2
    if (!Align(mesh1, mesh2, 
      weights1, nweights1, weights2, nweights2,
      egi_rotation,
      pca_translation, pca_rotation, pca_scale, 
      ransac_translation, ransac_rotation, ransac_scale, 
      icp_translation, icp_rotation, icp_scale,
      sample_method)) 
      exit(-1);
  }
  else {
    // Align mesh1 to canonical coordinate system
    if (!Align(mesh1, egi_rotation, pca_translation, pca_rotation, pca_scale)) exit(-1);
  }

  // Output mesh1
  if (!WriteMesh(mesh1, output1_name)) exit(-1);

  // Return success 
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl $0x0, 0xa4(%rsp)
movl %edi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movl 0xa0(%rsp), %edi
movq 0x98(%rsp), %rsi
callq 0x10270
cmpl $0x0, %eax
jne 0x1003e
movl $0x1, %edi
callq 0xd4f0
movq 0xbdb73(%rip), %rdi     # 0xcdbb8
callq 0x10af0
movq %rax, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
jne 0x10067
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
movl $0x0, 0x8c(%rsp)
movq $0x0, 0x80(%rsp)
cmpq $0x0, 0xbdb3a(%rip)     # 0xcdbc0
je 0x100b6
movq 0xbdb31(%rip), %rdi     # 0xcdbc0
leaq 0x80(%rsp), %rsi
leaq 0x8c(%rsp), %rdx
callq 0x10c50
cmpq $0x0, %rax
jne 0x100b4
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
jmp 0x100b6
cmpq $0x0, 0xbdb0a(%rip)     # 0xcdbc8
je 0x101f7
movq 0xbdafd(%rip), %rdi     # 0xcdbc8
callq 0x10af0
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x100e7
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
movl $0x0, 0x74(%rsp)
movq $0x0, 0x68(%rsp)
cmpq $0x0, 0xbdad0(%rip)     # 0xcdbd0
je 0x1012a
movq 0xbdac7(%rip), %rdi     # 0xcdbd0
leaq 0x68(%rsp), %rsi
leaq 0x74(%rsp), %rdx
callq 0x10c50
cmpq $0x0, %rax
jne 0x10128
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
jmp 0x1012a
movq 0x90(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
movl 0x8c(%rsp), %ecx
movq 0x68(%rsp), %r8
movl 0x74(%rsp), %r9d
movl 0xbda82(%rip), %eax     # 0xcdbd8
movl %eax, 0x5c(%rsp)
movl 0xbda7c(%rip), %eax     # 0xcdbdc
movl %eax, 0x60(%rsp)
movl 0xbda75(%rip), %r13d    # 0xcdbe0
movl 0xbda72(%rip), %r12d    # 0xcdbe4
movl 0xbda6f(%rip), %r15d    # 0xcdbe8
movl 0xbda6c(%rip), %r14d    # 0xcdbec
movl 0xbda6a(%rip), %ebp     # 0xcdbf0
movl 0xbda68(%rip), %ebx     # 0xcdbf4
movl 0xbda65(%rip), %r11d    # 0xcdbf8
movl 0xbda62(%rip), %r10d    # 0xcdbfc
movl 0xbda60(%rip), %eax     # 0xcdc00
movl %eax, 0x64(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, (%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x64(%rsp), %eax
movl %r13d, 0x10(%rsp)
movl %r12d, 0x18(%rsp)
movl %r15d, 0x20(%rsp)
movl %r14d, 0x28(%rsp)
movl %ebp, 0x30(%rsp)
movl %ebx, 0x38(%rsp)
movl %r11d, 0x40(%rsp)
movl %r10d, 0x48(%rsp)
movl %eax, 0x50(%rsp)
callq 0x10e70
cmpl $0x0, %eax
jne 0x101f5
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
jmp 0x1022e
movq 0x90(%rsp), %rdi
movl 0xbd9d3(%rip), %esi     # 0xcdbd8
movl 0xbd9d1(%rip), %edx     # 0xcdbdc
movl 0xbd9cf(%rip), %ecx     # 0xcdbe0
movl 0xbd9cc(%rip), %r8d     # 0xcdbe4
callq 0x12a10
cmpl $0x0, %eax
jne 0x1022c
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
jmp 0x1022e
movq 0x90(%rsp), %rdi
movq 0xbd9cb(%rip), %rsi     # 0xcdc08
callq 0x13030
cmpl $0x0, %eax
jne 0x10251
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd4f0
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)