// File: /workspace/github_cpp_makefile_star2_O1/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 $0x888, %rsp            # imm = 0x888
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8aa07(%rip), %rsi     # 0xabd93
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x21736
movq %rax, %r14
movq %r15, 0x20(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x705ec
leaq 0x10(%rsp), %rdi
callq 0x705ec
leaq 0x480(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
je 0x2166b
leaq 0x68(%rsp), %r15
xorl %r13d, %r13d
movq %r15, %r12
leaq 0x47f(%rsp), %rbp
movsbl 0x1(%rbp), %r15d
incq %rbp
movl %r15d, %edi
callq 0xe080
testl %eax, %eax
jne 0x213ea
movzbl %r15b, %ecx
incl %r13d
movl $0x2, %eax
testl %ecx, %ecx
je 0x2155d
cmpl $0x23, %ecx
movq %r12, %r15
je 0x21645
leaq 0x480(%rsp), %rdi
leaq 0x86a87(%rip), %rsi     # 0xa7eb4
leaq 0x80(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x1, %eax
jne 0x21565
movl 0x80(%rsp), %eax
movl $0x74726556, %ecx       # imm = 0x74726556
xorl %ecx, %eax
movl 0x83(%rsp), %ecx
movl $0x786574, %edx         # imm = 0x786574
xorl %edx, %ecx
orl %eax, %ecx
je 0x21590
movl 0x80(%rsp), %eax
movl $0x65636146, %ecx       # imm = 0x65636146
xorl %ecx, %eax
movzbl 0x84(%rsp), %ecx
orl %eax, %ecx
jne 0x21643
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x81258(%rip), %rsi     # 0xa26f2
leaq 0x80(%rsp), %rdx
movq %r15, %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
movl %eax, %ebp
cmpl $0x5, %eax
jne 0x2161b
movq 0x40(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rdx
movslq 0x28(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rcx
cmpq %rcx, %rdx
je 0x21639
movslq 0x38(%rsp), %rsi
movq -0x8(%rax,%rsi,8), %r12
cmpq %rdx, %r12
je 0x21639
cmpq %rcx, %r12
je 0x21639
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %rdx, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21639
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movq 0x58(%rsp), %rsi
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
callq 0x70682
movq 0x50(%rsp), %rsi
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
callq 0x70682
jmp 0x21639
movq %r12, %r15
jmp 0x21645
movq %r14, %rdi
leaq 0x81148(%rip), %rsi     # 0xa26b7
movl %r13d, %edx
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xe670
movq %r14, %rdi
callq 0xe430
movl $0x1, %eax
jmp 0x21645
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x8113c(%rip), %rsi     # 0xa26df
leaq 0x80(%rsp), %rdx
leaq 0x64(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
movl %eax, %ebp
cmpl $0x5, %eax
jne 0x21603
movsd 0x38(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movq %r15, %rdi
callq 0x3ca26
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movl 0x4c(%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x70682
jmp 0x21639
leaq 0x80e96(%rip), %rdi     # 0xa24a0
movl %r13d, %esi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x21631
leaq 0x810db(%rip), %rdi     # 0xa26fd
movl %r13d, %esi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
movq %r14, %rdi
callq 0xe430
movl $0x1, %eax
cmpl $0x5, %ebp
jne 0x21645
xorl %eax, %eax
testb $0x1, %al
jne 0x2174b
leaq 0x480(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
jne 0x213df
cmpl $0x3, 0x1c(%rsp)
jl 0x21727
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 (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21711
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21711
addq $0x18, %r13
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r13
addq $0x18, %r12
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r12
addq $0x18, %r15
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq (%rbx), %r9
movq %rbx, %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 0x21678
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x2174d
leaq 0x80d0b(%rip), %rdi     # 0xa2448
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x21761
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x70656
leaq 0x40(%rsp), %rdi
callq 0x70656
movl %ebx, %eax
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2178a
jmp 0x2178a
jmp 0x2178a
jmp 0x2178a
jmp 0x2178a
movq %rax, %rbx
jmp 0x21797
jmp 0x2178a
jmp 0x2178a
jmp 0x2178a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70656
leaq 0x40(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0
nop