// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: ReadMTurkSegmentationProperties(gaps::R3Mesh*, char const*)
static R3MeshPropertySet *
ReadMTurkSegmentationProperties(R3Mesh *mesh, const char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Reading mturk segmentation properties ...\n");
    fflush(stdout);
  }

  // Open file
  FILE* fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open json file %s\n", filename);
    return 0;
  }

  // Read file 
  std::string text;
  fseek(fp, 0, SEEK_END);
  long const size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  char* buffer = new char[size + 1];
  unsigned long const usize = static_cast<unsigned long const>(size);
  if (fread(buffer, 1, usize, fp) != usize) { RNFail("Unable to read %s\n", filename); return 0; }
  else { buffer[size] = 0; text = buffer; }
  delete[] buffer;

  // Close file
  fclose(fp);

  // Parse file
  Json::Value json_root;
  Json::Reader json_reader;
  if (!json_reader.parse(text, json_root, false)) {
    RNFail("Unable to parse %s\n", filename);
    return 0;
  }

  // Allocate property set
  R3MeshPropertySet *properties = new R3MeshPropertySet(mesh);
  if (!properties) {
    RNFail("Unable to allocate Aproperty set.\n");
    return NULL;
  }

  // Allocate property
  R3MeshProperty *segmentation = new R3MeshProperty(mesh, "Segment");

  // Parse segment identifiers
  if (json_root.isMember("segIndices")) {
    Json::Value json_segments = json_root["segIndices"];
    for (Json::ArrayIndex index = 0; index < json_segments.size(); index++) {
      Json::Value json_segment = json_segments[index];
      segmentation->SetVertexValue(index, json_segment.asInt());
    }
  }

  // Insert property
  InsertProperty(properties, segmentation);

  // Print statistics
  if (print_verbose) {
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Properties = %d\n", properties->NProperties());
    fflush(stdout);
  }

  // Return property set
  return properties;
}

// Assembly:
subq $0x298, %rsp            # imm = 0x298
movq %rdi, 0x288(%rsp)
movq %rsi, 0x280(%rsp)
leaq 0x270(%rsp), %rdi
callq 0xe98e0
leaq 0x270(%rsp), %rdi
callq 0x1f280
cmpl $0x0, 0x1ad1a8(%rip)    # 0x1ca740
je 0x1d5b7
leaq 0x1570ac(%rip), %rdi    # 0x17464d
movb $0x0, %al
callq 0x11090
movq 0x1a39e1(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movq 0x280(%rsp), %rdi
leaq 0x1570b2(%rip), %rsi    # 0x174678
callq 0x11800
movq %rax, 0x268(%rsp)
cmpq $0x0, 0x268(%rsp)
jne 0x1d605
movq 0x280(%rsp), %rsi
leaq 0x15708e(%rip), %rdi    # 0x17467b
movb $0x0, %al
callq 0xedb00
movq $0x0, 0x290(%rsp)
jmp 0x1db7d
leaq 0x248(%rsp), %rdi
callq 0x11980
movq 0x268(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x11760
jmp 0x1d62a
movq 0x268(%rsp), %rdi
callq 0x11030
movq %rax, 0x70(%rsp)
jmp 0x1d63e
movq 0x70(%rsp), %rax
movq %rax, 0x230(%rsp)
movq 0x268(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x11760
jmp 0x1d65e
movq 0x230(%rsp), %rdi
incq %rdi
callq 0x11040
movq %rax, 0x68(%rsp)
jmp 0x1d675
movq 0x68(%rsp), %rax
movq %rax, 0x228(%rsp)
movq 0x230(%rsp), %rax
movq %rax, 0x220(%rsp)
movq 0x228(%rsp), %rdi
movq 0x220(%rsp), %rdx
movq 0x268(%rsp), %rcx
movl $0x1, %esi
callq 0x11a10
movq %rax, 0x60(%rsp)
jmp 0x1d6bb
movq 0x60(%rsp), %rax
cmpq 0x220(%rsp), %rax
je 0x1d717
movq 0x280(%rsp), %rsi
leaq 0x156fbf(%rip), %rdi    # 0x174698
xorl %eax, %eax
callq 0xedb00
jmp 0x1d6e2
movq $0x0, 0x290(%rsp)
movl $0x1, 0x21c(%rsp)
jmp 0x1db5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0x1db6e
movq 0x228(%rsp), %rax
movq 0x230(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x228(%rsp), %rsi
leaq 0x248(%rsp), %rdi
callq 0x11930
jmp 0x1d742
jmp 0x1d744
movq 0x228(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x1d761
movq 0x58(%rsp), %rdi
callq 0x118b0
movq 0x268(%rsp), %rdi
callq 0x11670
jmp 0x1d770
leaq 0x1f0(%rsp), %rdi
xorl %esi, %esi
callq 0xfad90
jmp 0x1d781
leaq 0xe0(%rsp), %rdi
callq 0xedd40
jmp 0x1d790
leaq 0xe0(%rsp), %rdi
leaq 0x248(%rsp), %rsi
leaq 0x1f0(%rsp), %rdx
xorl %ecx, %ecx
callq 0xedf70
movb %al, 0x57(%rsp)
jmp 0x1d7b5
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x1d825
jmp 0x1d7bf
movq 0x280(%rsp), %rsi
leaq 0x156edd(%rip), %rdi    # 0x1746ab
xorl %eax, %eax
callq 0xedb00
jmp 0x1d7d7
movq $0x0, 0x290(%rsp)
movl $0x1, 0x21c(%rsp)
jmp 0x1db27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0x1db50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0x1db43
movl $0x18, %edi
callq 0x11680
movq %rax, 0x48(%rsp)
jmp 0x1d836
movq 0x48(%rsp), %rdi
movq 0x288(%rsp), %rsi
callq 0x24ce0
jmp 0x1d84a
movq 0x48(%rsp), %rax
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x1d8b6
leaq 0x156e56(%rip), %rdi    # 0x1746bf
xorl %eax, %eax
callq 0xedb00
jmp 0x1d872
movq $0x0, 0x290(%rsp)
movl $0x1, 0x21c(%rsp)
jmp 0x1db27
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
movl $0x18, %esi
callq 0x11690
jmp 0x1db43
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, 0x40(%rsp)
jmp 0x1d8c7
movq 0x40(%rsp), %rdi
movq 0x288(%rsp), %rsi
leaq 0x156d6a(%rip), %rdx    # 0x174645
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1d8e6
movq 0x40(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x156de8(%rip), %rsi    # 0x1746e2
leaq 0x1f0(%rsp), %rdi
callq 0xff890
movb %al, 0x3f(%rsp)
jmp 0x1d90d
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x1d91a
jmp 0x1da8d
leaq 0x156dc1(%rip), %rsi    # 0x1746e2
leaq 0x1f0(%rsp), %rdi
callq 0xf99c0
movq %rax, 0x30(%rsp)
jmp 0x1d935
movq 0x30(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xfb370
jmp 0x1d949
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0xf94b0
movl %eax, 0x2c(%rsp)
jmp 0x1d972
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jae 0x1da6c
movl 0xa4(%rsp), %esi
leaq 0xa8(%rsp), %rdi
callq 0xfde50
movq %rax, 0x20(%rsp)
jmp 0x1d99d
movq 0x20(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0xfb370
jmp 0x1d9ae
movq 0xd0(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xf8210
movl %eax, 0x1c(%rsp)
jmp 0x1d9d6
movl 0x18(%rsp), %esi
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %eax
cvtsi2sd %eax, %xmm0
callq 0x2ae50
jmp 0x1d9ee
leaq 0x78(%rsp), %rdi
callq 0xfb7f0
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x1d954
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1db43
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
jmp 0x1da7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x240(%rsp)
movl %eax, 0x23c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xfb7f0
jmp 0x1da7b
leaq 0xa8(%rsp), %rdi
callq 0xfb7f0
jmp 0x1da8d
leaq 0xa8(%rsp), %rdi
callq 0xfb7f0
jmp 0x1db43
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
jmp 0x1daa6
cmpl $0x0, 0x1acc93(%rip)    # 0x1ca740
je 0x1db0c
leaq 0x270(%rsp), %rdi
callq 0xe99a0
movsd %xmm0, 0x8(%rsp)
jmp 0x1dac4
movsd 0x8(%rsp), %xmm0
leaq 0x15673b(%rip), %rdi    # 0x17420c
movb $0x1, %al
callq 0x11090
jmp 0x1dada
movq 0xd8(%rsp), %rdi
callq 0x1f400
movl %eax, %esi
leaq 0x1567c7(%rip), %rdi    # 0x1742b7
xorl %eax, %eax
callq 0x11090
jmp 0x1daf9
movq 0x1a3490(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
jmp 0x1db0a
jmp 0x1db0c
movq 0xd8(%rsp), %rax
movq %rax, 0x290(%rsp)
movl $0x1, 0x21c(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x20160
leaq 0x1f0(%rsp), %rdi
callq 0xfb7f0
jmp 0x1db5f
leaq 0xe0(%rsp), %rdi
callq 0x20160
leaq 0x1f0(%rsp), %rdi
callq 0xfb7f0
jmp 0x1db6e
leaq 0x248(%rsp), %rdi
callq 0x11400
jmp 0x1db7d
leaq 0x248(%rsp), %rdi
callq 0x11400
jmp 0x1db8d
movq 0x290(%rsp), %rax
addq $0x298, %rsp            # imm = 0x298
retq
movq 0x240(%rsp), %rdi
callq 0x11bb0
nopw (%rax,%rax)