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

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

  // Allocate properites
  R3MeshProperty *distance_to_correspondence = new R3MeshProperty(mesh, "DistanceToCorrespondence");

  // Compute distance normalization scale factor
  RNScalar sqrt_area = sqrt(mesh->Area());
  if (sqrt_area == 0) return NULL;
  RNScalar scale = 1 / sqrt_area;

  // Open map file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open map file: %s\n", filename);
    return NULL;
  }

  // Read map file and compute distance to each correspondence
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(i);

    // Read corresponding vertex ID
    int id;
    if (fscanf(fp, "%d", &id) != (unsigned int) 1) { 
      RNFail("Unable to read %s\n", filename); 
      return NULL; 
    }

    // Compute distance from vertex to its correspondence
    RNScalar distance = 10;
    if ((id >= 0) && (id < mesh->NVertices())) {
      R3MeshVertex *corresponding_vertex = mesh->Vertex(id);
      distance = scale * mesh->DijkstraDistance(vertex, corresponding_vertex);
    }

    // Set property value
    distance_to_correspondence->SetVertexValue(i, distance);
  }

  // Close map file
  fclose(fp);

  // Insert property
  InsertProperty(properties, distance_to_correspondence);

  // 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 $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xe98e0
leaq 0x80(%rsp), %rdi
callq 0x1f280
cmpl $0x0, 0x1af318(%rip)    # 0x1ca740
je 0x1b447
leaq 0x1593ea(%rip), %rdi    # 0x17481b
movb $0x0, %al
callq 0x11090
movq 0x1a5b51(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movl $0x18, %edi
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movq 0x98(%rsp), %rsi
callq 0x24ce0
jmp 0x1b46b
movq 0x18(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x1b4be
leaq 0x158dd2(%rip), %rdi    # 0x174256
movb $0x0, %al
callq 0xedb00
movq $0x0, 0xa0(%rsp)
jmp 0x1b74f
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x18, %esi
callq 0x11690
jmp 0x1b75f
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movq 0x98(%rsp), %rsi
leaq 0x159357(%rip), %rdx    # 0x174839
xorl %eax, %eax
movl %eax, %ecx
callq 0x27ef0
jmp 0x1b4ed
movq 0x10(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x98(%rsp), %rdi
callq 0x2f940
callq 0x114f0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1b553
jp 0x1b553
movq $0x0, 0xa0(%rsp)
jmp 0x1b74f
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1b75f
movsd 0x158abd(%rip), %xmm0   # 0x174018
divsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x90(%rsp), %rdi
leaq 0x1604ad(%rip), %rsi    # 0x17ba23
callq 0x11800
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x1b5af
movq 0x90(%rsp), %rsi
leaq 0x1592bb(%rip), %rdi    # 0x174852
movb $0x0, %al
callq 0xedb00
movq $0x0, 0xa0(%rsp)
jmp 0x1b74f
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x98(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x1b6da
movq 0x98(%rsp), %rdi
movl 0x44(%rsp), %esi
callq 0x1f380
movq %rax, 0x38(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x16009e(%rip), %rsi    # 0x17b69a
leaq 0x34(%rsp), %rdx
movb $0x0, %al
callq 0x11c40
cmpl $0x1, %eax
je 0x1b634
movq 0x90(%rsp), %rsi
leaq 0x15907c(%rip), %rdi    # 0x174698
movb $0x0, %al
callq 0xedb00
movq $0x0, 0xa0(%rsp)
jmp 0x1b74f
movsd 0x158a0c(%rip), %xmm0   # 0x174048
movsd %xmm0, 0x28(%rsp)
cmpl $0x0, 0x34(%rsp)
jl 0x1b6b6
movl 0x34(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x98(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x1b6b6
movq 0x98(%rsp), %rdi
movl 0x34(%rsp), %esi
callq 0x1f380
movq %rax, 0x20(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x98(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x42180
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x60(%rsp), %rdi
movl 0x44(%rsp), %esi
movsd 0x28(%rsp), %xmm0
callq 0x2ae50
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x1b5b7
movq 0x48(%rsp), %rdi
callq 0x11670
movq 0x78(%rsp), %rdi
movq 0x60(%rsp), %rsi
xorl %edx, %edx
callq 0x148a0
cmpl $0x0, 0x1af044(%rip)    # 0x1ca740
je 0x1b742
leaq 0x80(%rsp), %rdi
callq 0xe99a0
leaq 0x158afa(%rip), %rdi    # 0x17420c
movb $0x1, %al
callq 0x11090
movq 0x78(%rsp), %rdi
callq 0x1f400
movl %eax, %esi
leaq 0x158b8b(%rip), %rdi    # 0x1742b7
movb $0x0, %al
callq 0x11090
movq 0x1a5856(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movq 0x78(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
addq $0xa8, %rsp
retq
movq 0x70(%rsp), %rdi
callq 0x11bb0
nopl (%rax)