// File: tomfunkhouser[P]gaps/apps/grd2msh/grd2msh.cpp
// Function: CreateMesh(gaps::R3Grid*, double)
static R3Mesh *
CreateMesh(R3Grid *grid, RNScalar threshold)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Create mesh
  R3Mesh *mesh = new R3Mesh();
  if (!mesh) {
    RNFail("Unable to allocate mesh\n");
    return NULL;
  }

  // Extract isosurface from grid
  const int max_points = 16 * 1024 * 1024;
  static R3Point points[max_points];
  int npoints = grid->GenerateIsoSurface(threshold, points, max_points);
  if (npoints == 0) {
    RNFail("Empty isosurface for threshold: %g\n", threshold);
    return NULL;
  }

  // Create mesh
  R3Point *pointsp = points;
  for (int i = 0; i < npoints; i += 3) {
    R3MeshVertex *v1 = mesh->CreateVertex(grid->WorldPosition(*(pointsp++)));
    R3MeshVertex *v2 = mesh->CreateVertex(grid->WorldPosition(*(pointsp++)));
    R3MeshVertex *v3 = mesh->CreateVertex(grid->WorldPosition(*(pointsp++)));
    mesh->CreateFace(v3, v2, v1);
  }

  // Print statistics
  if (print_verbose) {
    printf("Created mesh ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Faces = %d\n", mesh->NFaces());
    printf("  # Edges = %d\n", mesh->NEdges());
    printf("  # Vertices = %d\n", mesh->NVertices());
    fflush(stdout);
  }

  // Return mesh
  return mesh;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movsd %xmm0, 0xe0(%rsp)
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xd0600
movq 0x30(%rsp), %rdi
callq 0x13070
movl $0x138, %edi            # imm = 0x138
callq 0xf4c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
callq 0x133a0
jmp 0x12c00
movq 0x38(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
jne 0x12c5f
leaq 0x120549(%rip), %rdi    # 0x133168
movb $0x0, %al
callq 0xd4820
movq $0x0, 0xf0(%rsp)
jmp 0x12f59
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
movl $0x138, %esi            # imm = 0x138
callq 0xf4d0
jmp 0x12f69
movl $0x1000000, 0xb8(%rsp) # imm = 0x1000000
cmpb $0x0, 0x1816590f(%rip)  # 0x18178580
jne 0x12cd0
leaq 0x18165906(%rip), %rdi  # 0x18178580
callq 0xf8e0
cmpl $0x0, %eax
je 0x12cd0
leaq 0x1658f5(%rip), %rax    # 0x178580
movq %rax, 0x28(%rsp)
jmp 0x12c92
movq 0x28(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6e430
jmp 0x12ca3
movq 0x20(%rsp), %rax
addq $0x18, %rax
leaq 0x1658cd(%rip), %rcx    # 0x178580
addq $0x18000000, %rcx       # imm = 0x18000000
cmpq %rcx, %rax
movq %rax, 0x28(%rsp)
jne 0x12c92
leaq 0x181658b5(%rip), %rdi  # 0x18178580
callq 0xf310
movq 0xe8(%rsp), %rdi
movsd 0xe0(%rsp), %xmm0
leaq 0x165898(%rip), %rsi    # 0x178580
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x69b80
movl %eax, 0xb4(%rsp)
cmpl $0x0, 0xb4(%rsp)
jne 0x12d50
movsd 0xe0(%rsp), %xmm0
leaq 0x12046e(%rip), %rdi    # 0x133181
movb $0x1, %al
callq 0xd4820
movq $0x0, 0xf0(%rsp)
jmp 0x12f59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0x1816583a(%rip), %rdi  # 0x18178580
callq 0xf300
jmp 0x12f69
leaq 0x165829(%rip), %rax    # 0x178580
movq %rax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0xb4(%rsp), %eax
jge 0x12ead
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xe8(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq %rdx, %rax
addq $0x18, %rax
movq %rax, 0xa8(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x132a0
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
leaq 0x80(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, 0x98(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xe8(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq %rdx, %rax
addq $0x18, %rax
movq %rax, 0xa8(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x132a0
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
leaq 0x60(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, 0x78(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xe8(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq %rdx, %rax
addq $0x18, %rax
movq %rax, 0xa8(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x132a0
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
leaq 0x40(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, 0x58(%rsp)
movq 0xc8(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movl 0xa4(%rsp), %eax
addl $0x3, %eax
movl %eax, 0xa4(%rsp)
jmp 0x12d6a
cmpl $0x0, 0x1656c4(%rip)    # 0x178578
je 0x12f49
leaq 0x1202e4(%rip), %rdi    # 0x1331a5
movb $0x0, %al
callq 0xf070
leaq 0xd0(%rsp), %rdi
callq 0xd06c0
leaq 0x1201d1(%rip), %rdi    # 0x1330ad
movb $0x1, %al
callq 0xf070
movq 0xc8(%rsp), %rdi
callq 0x13330
movl %eax, %esi
leaq 0x1202be(%rip), %rdi    # 0x1331b7
movb $0x0, %al
callq 0xf070
movq 0xc8(%rsp), %rdi
callq 0x13350
movl %eax, %esi
leaq 0x1202b1(%rip), %rdi    # 0x1331c7
movb $0x0, %al
callq 0xf070
movq 0xc8(%rsp), %rdi
callq 0x13370
movl %eax, %esi
leaq 0x1202a4(%rip), %rdi    # 0x1331d7
movb $0x0, %al
callq 0xf070
movq 0x15c04f(%rip), %rax    # 0x16ef90
movq (%rax), %rdi
callq 0xf5d0
movq 0xc8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
addq $0xf8, %rsp
retq
movq 0xc0(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)