// File: tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: ParseArgs(int, char**)
static int 
ParseArgs(int argc, char **argv)
{
  // Parse arguments
  argc--; argv++;
  while (argc > 0) {
    if ((*argv)[0] == '-') {
      if (!strcmp(*argv, "-v")) print_verbose = 1;
      else if (!strcmp(*argv, "-debug")) print_debug = 1;
      else if (!strcmp(*argv, "-weights1")) { argc--; argv++; weights1_name = *argv; }
      else if (!strcmp(*argv, "-weights2")) { argc--; argv++; weights2_name = *argv; }
      else if (!strcmp(*argv, "-pca")) { pca_translation = pca_rotation = pca_scale = 1; }
      else if (!strcmp(*argv, "-pca_translation")) { pca_translation = 1; }
      else if (!strcmp(*argv, "-pca_rotation")) { pca_rotation = 1; }
      else if (!strcmp(*argv, "-pca_scale")) { pca_scale = 1; }
      else if (!strcmp(*argv, "-ransac")) { ransac_translation = ransac_rotation = ransac_scale = 1; }
      else if (!strcmp(*argv, "-ransac_translation")) { ransac_translation = 1; }
      else if (!strcmp(*argv, "-ransac_rotation")) { ransac_rotation = 1; }
      else if (!strcmp(*argv, "-ransac_scale")) { ransac_scale = 1; }
      else if (!strcmp(*argv, "-icp")) { icp_translation = icp_rotation = icp_scale = 1; }
      else if (!strcmp(*argv, "-icp_translation")) { icp_translation = 1; }
      else if (!strcmp(*argv, "-icp_rotation")) { icp_rotation = 1; }
      else if (!strcmp(*argv, "-icp_scale")) { icp_scale = 1; }
      else if (!strcmp(*argv, "-no_pca")) { pca_translation = pca_rotation = pca_scale = 0; }
      else if (!strcmp(*argv, "-no_icp")) { icp_translation = icp_rotation = icp_scale = 0; }
      else if (!strcmp(*argv, "-no_ransac")) { ransac_translation = ransac_rotation = ransac_scale = 0; }
      else if (!strcmp(*argv, "-no_translation")) { pca_translation = 0; icp_translation = 0; ransac_translation = 0; }
      else if (!strcmp(*argv, "-no_rotation")) { pca_rotation = 0; icp_rotation = 0; ransac_rotation = 0; }
      else if (!strcmp(*argv, "-no_scale")) { pca_scale = 0; icp_scale = 0; ransac_scale = 0; }
      else if (!strcmp(*argv, "-axial_rotation")) { pca_rotation = 2; icp_rotation = 0; ransac_rotation = 0; }
      else if (!strcmp(*argv, "-max_points")) { argc--; argv++; max_points = atoi(*argv); }
      else if (!strcmp(*argv, "-min_weight")) { argc--; argv++; min_weight = atof(*argv); } 
      else if (!strcmp(*argv, "-correspondence_method")) { argc--; argv++; correspondence_method = atoi(*argv); }
      else if (!strcmp(*argv, "-sample_method")) { argc--; argv++; sample_method = atoi(*argv); }
      else if (!strcmp(*argv, "-sample_faces")) sample_method = 0; 
      else if (!strcmp(*argv, "-sample_edges")) sample_method = 1; 
      else if (!strcmp(*argv, "-sample_vertices")) sample_method = 2; 
      else { RNFail("Invalid program argument: %s", *argv); exit(1); }
      argv++; argc--;
    }
    else {
      if (!input1_name) input1_name = *argv;
      else if (!input2_name) input2_name = *argv;
      else if (!output1_name) output1_name = *argv;
      else { RNFail("Invalid program argument: %s", *argv); exit(1); }
      argv++; argc--;
    }
  }

  // Fix alignment method (default is pca + icp)
  if (!pca_translation && !pca_rotation && !pca_scale && 
      !ransac_translation && !ransac_rotation && !ransac_scale && 
      !icp_translation && !icp_rotation && !icp_scale) {
    pca_translation = pca_rotation = pca_scale = 1;
    icp_translation = icp_rotation = icp_scale = 1;
  }

  // Fix output filename
  if (input2_name && !output1_name) {
    output1_name = input2_name;
    input2_name = NULL;
  }

  // Check input filename
  if (!input1_name || !output1_name) {
    printf("Usage: mshalign input1 [input2] output1 [options]\n");
    return 0;
  }

  // Return OK status 
  return 1;
}

// Assembly:
subq $0x18, %rsp
movl %edi, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x109f5
movq 0x8(%rsp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x10962
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96d74(%rip), %rsi     # 0xa7038
callq 0xd570
cmpl $0x0, %eax
jne 0x102dd
movl $0x1, 0xbd938(%rip)     # 0xcdc10
jmp 0x10944
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96d4f(%rip), %rsi     # 0xa703b
callq 0xd570
cmpl $0x0, %eax
jne 0x10305
movl $0x1, 0xbd914(%rip)     # 0xcdc14
jmp 0x10942
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96d2e(%rip), %rsi     # 0xa7042
callq 0xd570
cmpl $0x0, %eax
jne 0x1034b
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xbd87a(%rip)     # 0xcdbc0
jmp 0x10940
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96cf2(%rip), %rsi     # 0xa704c
callq 0xd570
cmpl $0x0, %eax
jne 0x10391
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xbd844(%rip)     # 0xcdbd0
jmp 0x1093e
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96cb6(%rip), %rsi     # 0xa7056
callq 0xd570
cmpl $0x0, %eax
jne 0x103cd
movl $0x1, 0xbd830(%rip)     # 0xcdbe4
movl $0x1, 0xbd822(%rip)     # 0xcdbe0
movl $0x1, 0xbd814(%rip)     # 0xcdbdc
jmp 0x1093c
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96c7f(%rip), %rsi     # 0xa705b
callq 0xd570
cmpl $0x0, %eax
jne 0x103f5
movl $0x1, 0xbd7ec(%rip)     # 0xcdbdc
jmp 0x1093a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96c68(%rip), %rsi     # 0xa706c
callq 0xd570
cmpl $0x0, %eax
jne 0x1041d
movl $0x1, 0xbd7c8(%rip)     # 0xcdbe0
jmp 0x10938
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96c4e(%rip), %rsi     # 0xa707a
callq 0xd570
cmpl $0x0, %eax
jne 0x10445
movl $0x1, 0xbd7a4(%rip)     # 0xcdbe4
jmp 0x10936
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96c31(%rip), %rsi     # 0xa7085
callq 0xd570
cmpl $0x0, %eax
jne 0x10481
movl $0x1, 0xbd788(%rip)     # 0xcdbf0
movl $0x1, 0xbd77a(%rip)     # 0xcdbec
movl $0x1, 0xbd76c(%rip)     # 0xcdbe8
jmp 0x10934
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96bfd(%rip), %rsi     # 0xa708d
callq 0xd570
cmpl $0x0, %eax
jne 0x104a9
movl $0x1, 0xbd744(%rip)     # 0xcdbe8
jmp 0x10932
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96be9(%rip), %rsi     # 0xa70a1
callq 0xd570
cmpl $0x0, %eax
jne 0x104d1
movl $0x1, 0xbd720(%rip)     # 0xcdbec
jmp 0x10930
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96bd2(%rip), %rsi     # 0xa70b2
callq 0xd570
cmpl $0x0, %eax
jne 0x104f9
movl $0x1, 0xbd6fc(%rip)     # 0xcdbf0
jmp 0x1092e
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96bb8(%rip), %rsi     # 0xa70c0
callq 0xd570
cmpl $0x0, %eax
jne 0x10535
movl $0x1, 0xbd6e0(%rip)     # 0xcdbfc
movl $0x1, 0xbd6d2(%rip)     # 0xcdbf8
movl $0x1, 0xbd6c4(%rip)     # 0xcdbf4
jmp 0x1092c
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96b81(%rip), %rsi     # 0xa70c5
callq 0xd570
cmpl $0x0, %eax
jne 0x1055d
movl $0x1, 0xbd69c(%rip)     # 0xcdbf4
jmp 0x1092a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96b6a(%rip), %rsi     # 0xa70d6
callq 0xd570
cmpl $0x0, %eax
jne 0x10585
movl $0x1, 0xbd678(%rip)     # 0xcdbf8
jmp 0x10928
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96b50(%rip), %rsi     # 0xa70e4
callq 0xd570
cmpl $0x0, %eax
jne 0x105ad
movl $0x1, 0xbd654(%rip)     # 0xcdbfc
jmp 0x10926
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96b33(%rip), %rsi     # 0xa70ef
callq 0xd570
cmpl $0x0, %eax
jne 0x105e9
movl $0x0, 0xbd614(%rip)     # 0xcdbe4
movl $0x0, 0xbd606(%rip)     # 0xcdbe0
movl $0x0, 0xbd5f8(%rip)     # 0xcdbdc
jmp 0x10924
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96aff(%rip), %rsi     # 0xa70f7
callq 0xd570
cmpl $0x0, %eax
jne 0x10625
movl $0x0, 0xbd5f0(%rip)     # 0xcdbfc
movl $0x0, 0xbd5e2(%rip)     # 0xcdbf8
movl $0x0, 0xbd5d4(%rip)     # 0xcdbf4
jmp 0x10922
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96acb(%rip), %rsi     # 0xa70ff
callq 0xd570
cmpl $0x0, %eax
jne 0x10661
movl $0x0, 0xbd5a8(%rip)     # 0xcdbf0
movl $0x0, 0xbd59a(%rip)     # 0xcdbec
movl $0x0, 0xbd58c(%rip)     # 0xcdbe8
jmp 0x10920
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96a9a(%rip), %rsi     # 0xa710a
callq 0xd570
cmpl $0x0, %eax
jne 0x1069d
movl $0x0, 0xbd558(%rip)     # 0xcdbdc
movl $0x0, 0xbd566(%rip)     # 0xcdbf4
movl $0x0, 0xbd550(%rip)     # 0xcdbe8
jmp 0x1091e
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96a6e(%rip), %rsi     # 0xa711a
callq 0xd570
cmpl $0x0, %eax
jne 0x106d9
movl $0x0, 0xbd520(%rip)     # 0xcdbe0
movl $0x0, 0xbd52e(%rip)     # 0xcdbf8
movl $0x0, 0xbd518(%rip)     # 0xcdbec
jmp 0x1091c
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96a3f(%rip), %rsi     # 0xa7127
callq 0xd570
cmpl $0x0, %eax
jne 0x10715
movl $0x0, 0xbd4e8(%rip)     # 0xcdbe4
movl $0x0, 0xbd4f6(%rip)     # 0xcdbfc
movl $0x0, 0xbd4e0(%rip)     # 0xcdbf0
jmp 0x1091a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96a0d(%rip), %rsi     # 0xa7131
callq 0xd570
cmpl $0x0, %eax
jne 0x10751
movl $0x2, 0xbd4a8(%rip)     # 0xcdbe0
movl $0x0, 0xbd4b6(%rip)     # 0xcdbf8
movl $0x0, 0xbd4a0(%rip)     # 0xcdbec
jmp 0x10918
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x969e1(%rip), %rsi     # 0xa7141
callq 0xd570
cmpl $0x0, %eax
jne 0x1079b
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0xd5f0
movl %eax, 0xbcc4a(%rip)     # 0xcd3e0
jmp 0x10916
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x969a3(%rip), %rsi     # 0xa714d
callq 0xd570
cmpl $0x0, %eax
jne 0x107e7
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0xd510
movsd %xmm0, 0xbd436(%rip)    # 0xcdc18
jmp 0x10914
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96963(%rip), %rsi     # 0xa7159
callq 0xd570
cmpl $0x0, %eax
jne 0x10831
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0xd5f0
movl %eax, 0xbd3f4(%rip)     # 0xcdc20
jmp 0x10912
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x96930(%rip), %rsi     # 0xa7170
callq 0xd570
cmpl $0x0, %eax
jne 0x1087b
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0xd5f0
movl %eax, 0xbd38a(%rip)     # 0xcdc00
jmp 0x10910
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x968f5(%rip), %rsi     # 0xa717f
callq 0xd570
cmpl $0x0, %eax
jne 0x108a0
movl $0x0, 0xbd362(%rip)     # 0xcdc00
jmp 0x1090e
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x968de(%rip), %rsi     # 0xa718d
callq 0xd570
cmpl $0x0, %eax
jne 0x108c5
movl $0x1, 0xbd33d(%rip)     # 0xcdc00
jmp 0x1090c
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x968c7(%rip), %rsi     # 0xa719b
callq 0xd570
cmpl $0x0, %eax
jne 0x108ea
movl $0x2, 0xbd318(%rip)     # 0xcdc00
jmp 0x1090a
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq 0x968b3(%rip), %rdi     # 0xa71ac
movb $0x0, %al
callq 0xa6d50
movl $0x1, %edi
callq 0xd4f0
jmp 0x1090c
jmp 0x1090e
jmp 0x10910
jmp 0x10912
jmp 0x10914
jmp 0x10916
jmp 0x10918
jmp 0x1091a
jmp 0x1091c
jmp 0x1091e
jmp 0x10920
jmp 0x10922
jmp 0x10924
jmp 0x10926
jmp 0x10928
jmp 0x1092a
jmp 0x1092c
jmp 0x1092e
jmp 0x10930
jmp 0x10932
jmp 0x10934
jmp 0x10936
jmp 0x10938
jmp 0x1093a
jmp 0x1093c
jmp 0x1093e
jmp 0x10940
jmp 0x10942
jmp 0x10944
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x109f0
cmpq $0x0, 0xbd24e(%rip)     # 0xcdbb8
jne 0x1097d
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xbd23d(%rip)     # 0xcdbb8
jmp 0x109d7
cmpq $0x0, 0xbd243(%rip)     # 0xcdbc8
jne 0x10998
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xbd232(%rip)     # 0xcdbc8
jmp 0x109d5
cmpq $0x0, 0xbd268(%rip)     # 0xcdc08
jne 0x109b3
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xbd257(%rip)     # 0xcdc08
jmp 0x109d3
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq 0x967ea(%rip), %rdi     # 0xa71ac
movb $0x0, %al
callq 0xa6d50
movl $0x1, %edi
callq 0xd4f0
jmp 0x109d5
jmp 0x109d7
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x10296
cmpl $0x0, 0xbd1e0(%rip)     # 0xcdbdc
jne 0x10a86
cmpl $0x0, 0xbd1d7(%rip)     # 0xcdbe0
jne 0x10a86
cmpl $0x0, 0xbd1d2(%rip)     # 0xcdbe4
jne 0x10a86
cmpl $0x0, 0xbd1cd(%rip)     # 0xcdbe8
jne 0x10a86
cmpl $0x0, 0xbd1c8(%rip)     # 0xcdbec
jne 0x10a86
cmpl $0x0, 0xbd1c3(%rip)     # 0xcdbf0
jne 0x10a86
cmpl $0x0, 0xbd1be(%rip)     # 0xcdbf4
jne 0x10a86
cmpl $0x0, 0xbd1b9(%rip)     # 0xcdbf8
jne 0x10a86
cmpl $0x0, 0xbd1b4(%rip)     # 0xcdbfc
jne 0x10a86
movl $0x1, 0xbd190(%rip)     # 0xcdbe4
movl $0x1, 0xbd182(%rip)     # 0xcdbe0
movl $0x1, 0xbd174(%rip)     # 0xcdbdc
movl $0x1, 0xbd18a(%rip)     # 0xcdbfc
movl $0x1, 0xbd17c(%rip)     # 0xcdbf8
movl $0x1, 0xbd16e(%rip)     # 0xcdbf4
cmpq $0x0, 0xbd13a(%rip)     # 0xcdbc8
je 0x10ab3
cmpq $0x0, 0xbd170(%rip)     # 0xcdc08
jne 0x10ab3
movq 0xbd127(%rip), %rax     # 0xcdbc8
movq %rax, 0xbd160(%rip)     # 0xcdc08
movq $0x0, 0xbd115(%rip) # 0xcdbc8
cmpq $0x0, 0xbd0fd(%rip)     # 0xcdbb8
je 0x10ac7
cmpq $0x0, 0xbd143(%rip)     # 0xcdc08
jne 0x10adf
leaq 0x966fb(%rip), %rdi     # 0xa71c9
movb $0x0, %al
callq 0xd070
movl $0x0, 0x14(%rsp)
jmp 0x10ae7
movl $0x1, 0x14(%rsp)
movl 0x14(%rsp), %eax
addq $0x18, %rsp
retq