// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ReadHoppeFile(char const*)
int R3Mesh::
ReadHoppeFile(const char *filename)
{
  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "r"))) {
    RNFail("Unable to open file %s\n", filename);
    return 0;
  }

  // Read file
  int line_count = 0;
  char buffer[1024];
  RNArray<R3MeshVertex *> vertices;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fgets(buffer, 1023, fp)) {
    // Increment line counter
    line_count++;

    // Skip white space
    char *bufferp = buffer;
    while (isspace(*bufferp)) bufferp++;

    // Skip blank lines and comments
    if (*bufferp == '#') continue;
    if (*bufferp == '\0') continue;

    // Read command
    char cmd[1024];
    if (sscanf(buffer, "%s", cmd) != (unsigned int) 1) {
      fprintf(fp, "Syntax error line %d in file %s\n", line_count, filename);
      fclose(fp);
      return 0;
    }

    // Check command
    if (!strcmp(cmd, "Vertex")) {
      // Read vertex info
      int index;
      double x, y, z;
      if (sscanf(bufferp, "%s%d%lf%lf%lf", cmd, &index, &x, &y, &z) != (unsigned int) 5) {
        RNFail("Syntax error with vertex coordinates on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Create vertex
      R3MeshVertex *vertex = CreateVertex(R3Point(x, y, z));
      vertices.Insert(vertex);
    }
    else if (!strcmp(cmd, "Face")) {
      // Read face info
      int index, i0, i1, i2;
      if (sscanf(bufferp, "%s%d%d%d%d", cmd, &index, &i0, &i1, &i2) != (unsigned int) 5) {
        RNFail("Syntax error with face info on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Find vertices
      assert((i0 >= 0) && (i0 < vertices.NEntries()));
      assert((i1 >= 0) && (i1 < vertices.NEntries()));
      assert((i2 >= 0) && (i2 < vertices.NEntries()));
      R3MeshVertex *v0 = vertices.Kth(i0-1);
      R3MeshVertex *v1 = vertices.Kth(i1-1);
      R3MeshVertex *v2 = vertices.Kth(i2-1);

      // Create triangle
      if ((v0 != v1) && (v0 != v2) && (v1 != v2)) {
        if (!CreateFace(v0, v1, v2)) {
          // Must have been degeneracy (e.g., flips or three faces sharing an edge)
          // Remember for later processing (to preserve vertex indices)
          degenerate_triangle_vertices.Insert(v0);
          degenerate_triangle_vertices.Insert(v1);
          degenerate_triangle_vertices.Insert(v2);
        }
      }
    }
  }

  // Create degenerate triangles
  for (int i = 0; i <= degenerate_triangle_vertices.NEntries()-3; i+=3) {
    R3MeshVertex *v1 = degenerate_triangle_vertices.Kth(i+0);
    R3MeshVertex *v2 = degenerate_triangle_vertices.Kth(i+1);
    R3MeshVertex *v3 = degenerate_triangle_vertices.Kth(i+2);
    if (!CreateFace(v1, v2, v3)) {
      if (!CreateFace(v1, v3, v2)) {
        // Note: these vertices are allocated separately, and so they will not be deleted (memory leak)
        R3MeshVertex *v1a = CreateVertex(VertexPosition(v1));
        R3MeshVertex *v2a = CreateVertex(VertexPosition(v2));
        R3MeshVertex *v3a = CreateVertex(VertexPosition(v3));
        CreateFace(v1a, v2a, v3a);
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x878, %rsp            # imm = 0x878
movq %rsi, %r15
movq %rdi, %r14
leaq 0x8ac75(%rip), %rsi     # 0xabdd3
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x21459
movq %rax, %rbx
movq %r15, 0x20(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x6f80c
leaq 0x10(%rsp), %rdi
callq 0x6f80c
leaq 0x470(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0xe730
testq %rax, %rax
je 0x2138e
leaq 0x46f(%rsp), %r13
xorl %ebp, %ebp
movq %r13, %r15
movsbl 0x1(%r15), %r12d
incq %r15
movl %r12d, %edi
callq 0xe080
testl %eax, %eax
jne 0x211b6
movzbl %r12b, %eax
incl %ebp
testl %eax, %eax
je 0x21370
cmpl $0x23, %eax
je 0x21370
leaq 0x470(%rsp), %rdi
leaq 0x86cc0(%rip), %rsi     # 0xa7eb0
leaq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x1, %eax
jne 0x2146e
movl 0x70(%rsp), %eax
movl $0x74726556, %ecx       # imm = 0x74726556
xorl %ecx, %eax
movl 0x73(%rsp), %ecx
movl $0x786574, %edx         # imm = 0x786574
xorl %edx, %ecx
orl %eax, %ecx
je 0x212fb
movl 0x70(%rsp), %eax
movl $0x65636146, %ecx       # imm = 0x65636146
xorl %ecx, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
jne 0x21370
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x814a1(%rip), %rsi     # 0xa26ef
leaq 0x70(%rsp), %rdx
leaq 0x58(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
cmpl $0x5, %eax
jne 0x2149f
movq 0x40(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %r13
movslq 0x28(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %r15
cmpq %r15, %r13
je 0x212f1
movslq 0x38(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %r12
cmpq %r13, %r12
je 0x212f1
cmpq %r15, %r12
je 0x212f1
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x212f1
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x6f8a2
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x6f8a2
leaq 0x46f(%rsp), %r13
jmp 0x21370
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x813ce(%rip), %rsi     # 0xa26dc
leaq 0x70(%rsp), %rdx
leaq 0x54(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
cmpl $0x5, %eax
jne 0x21488
movsd 0x38(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x3c532
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movl 0x4c(%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x6f8a2
leaq 0x470(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0xe730
testq %rax, %rax
jne 0x211b3
cmpl $0x3, 0x1c(%rsp)
jl 0x2144a
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %r13
movq 0x8(%rax,%rbp,8), %r12
movq 0x10(%rax,%rbp,8), %r15
movq (%r14), %rax
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21434
movq (%r14), %rax
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21434
addq $0x18, %r13
movq (%r14), %rax
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r13
addq $0x18, %r12
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r12
addq $0x18, %r15
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq (%r14), %r9
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0x3, %rbp
movslq 0x1c(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jle 0x2139b
movq %rbx, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x214be
leaq 0x80fe5(%rip), %rdi     # 0xa2445
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x214d2
leaq 0x8123f(%rip), %rsi     # 0xa26b4
movq %rbx, %rdi
movl %ebp, %edx
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xe670
jmp 0x214b4
leaq 0x8100e(%rip), %rdi     # 0xa249d
movl %ebp, %esi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x214b4
leaq 0x81254(%rip), %rdi     # 0xa26fa
movl %ebp, %esi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
movq %rbx, %rdi
callq 0xe430
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x6f876
leaq 0x40(%rsp), %rdi
callq 0x6f876
movl %ebx, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x214fb
jmp 0x214fb
movq %rax, %rbx
jmp 0x21508
jmp 0x214fb
jmp 0x214fb
jmp 0x214fb
jmp 0x214fb
jmp 0x214fb
jmp 0x214fb
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6f876
leaq 0x40(%rsp), %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0