// File: /workspace/github_cpp_makefile_star2_O2/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 0x7dfcf(%rip), %rsi     # 0x9caf3
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x1ede5
movq %rax, %rbx
movq %r15, 0x10(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x6630a
leaq 0x30(%rsp), %rdi
callq 0x6630a
xorl %ebp, %ebp
leaq 0x70(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0xe760
leaq 0x6f(%rsp), %r13
testq %rax, %rax
je 0x1ed22
movsbl 0x1(%r13), %r15d
incq %r13
movl %r15d, %edi
callq 0xe080
testl %eax, %eax
jne 0x1eb73
movzbl %r15b, %eax
incl %ebp
testl %eax, %eax
je 0x1eb53
cmpl $0x23, %eax
je 0x1eb53
leaq 0x70(%rsp), %rdi
leaq 0x7a2be(%rip), %rsi     # 0x98e60
leaq 0x470(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0xe430
cmpl $0x1, %eax
jne 0x1edfa
movq %r15, %rdi
leaq 0x74abe(%rip), %rsi     # 0x93685
pushq $0x7
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1ecae
movq %r15, %rdi
leaq 0x74ab9(%rip), %rsi     # 0x9369a
pushq $0x5
popq %rdx
callq 0xe480
testl %eax, %eax
jne 0x1eb53
leaq 0x18(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x74a9b(%rip), %rsi     # 0x9369f
movq %r15, %rdx
leaq 0x58(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0xe430
cmpl $0x5, %eax
jne 0x1ee2b
movq 0x40(%rsp), %rax
movslq 0x20(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %r15
movslq 0x18(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %r13
cmpq %r13, %r15
je 0x1eb53
movslq 0x28(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %r12
cmpq %r15, %r12
je 0x1eb53
cmpq %r13, %r12
je 0x1eb53
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1eb53
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x22bfc
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x22bfc
jmp 0x1eb53
leaq 0x18(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x749cb(%rip), %rsi     # 0x9368c
movq %r15, %rdx
leaq 0x54(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0xe430
cmpl $0x5, %eax
jne 0x1ee14
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x37b4c
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x22bfc
jmp 0x1eb53
xorl %ebp, %ebp
movslq 0x3c(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jg 0x1edd8
movq 0x30(%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 0x1edcf
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 0x1edcf
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
jmp 0x1ed24
movq %rbx, %rdi
callq 0xe440
pushq $0x1
popq %rbx
jmp 0x1ee4a
leaq 0x74609(%rip), %rdi     # 0x933f5
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x1ee5e
leaq 0x74863(%rip), %rsi     # 0x93664
movq %rbx, %rdi
movl %ebp, %edx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0xe6a0
jmp 0x1ee40
leaq 0x74632(%rip), %rdi     # 0x9344d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1ee40
leaq 0x74878(%rip), %rdi     # 0x936aa
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
movq %rbx, %rdi
callq 0xe440
xorl %ebx, %ebx
leaq 0x30(%rsp), %rdi
callq 0x66378
leaq 0x40(%rsp), %rdi
callq 0x66378
movl %ebx, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1ee87
jmp 0x1ee87
movq %rax, %rbx
jmp 0x1ee94
jmp 0x1ee87
jmp 0x1ee87
jmp 0x1ee87
jmp 0x1ee87
jmp 0x1ee87
jmp 0x1ee87
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x66378
leaq 0x40(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810