// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ReadOffFile(char const*)
int R3Mesh::
ReadOffFile(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 nverts = 0;
  int nfaces = 0;
  int nedges = 0;
  int line_count = 0;
  int vertex_count = 0;
  int face_count = 0;
  char buffer[1024];
  char header[64];
  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;

    // Check section
    if (nverts == 0) {
      // Read header keyword
      if (strstr(bufferp, "OFF")) {
        // Check if counts are on first line
        int tmp;
        if (sscanf(bufferp, "%s%d%d%d", header, &tmp, &nfaces, &nedges) == 4) {
          nverts = tmp;
        }
      }
      else {
        // Read counts from second line
        if ((sscanf(bufferp, "%d%d%d", &nverts, &nfaces, &nedges) != 3) || (nverts == 0)) {
          RNFail("Syntax error reading header on line %d in file %s\n", line_count, filename);
          fclose(fp);
          return 0;
        }
      }
    }
    else if (vertex_count < nverts) {
      // Read vertex coordinates
      double x, y, z;
      if (sscanf(bufferp, "%lf%lf%lf", &x, &y, &z) != 3) {
        RNFail("Syntax error with vertex coordinates on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Create vertex
      CreateVertex(R3Point(x, y, z));

      // Increment counter
      vertex_count++;
    }
    else if (face_count < nfaces) {
      // Read number of vertices in face 
      int face_nverts = 0;
      bufferp = strtok(bufferp, " \t");
      if (bufferp) face_nverts = atoi(bufferp);
      else {
        RNFail("Syntax error with face on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Read vertex indices for face
      R3MeshVertex *v1 = NULL;
      R3MeshVertex *v2 = NULL;
      R3MeshVertex *v3 = NULL;
      for (int i = 0; i < face_nverts; i++) {
        bufferp = strtok(NULL, " \t");
        if (bufferp) {
          R3MeshVertex *v = Vertex(atoi(bufferp));
          if (!v1) v1 = v;
          else v3 = v;
        }
        else {
          RNFail("Syntax error with face on line %d in file %s\n", line_count, filename);
          fclose(fp);
          return 0;
        }

        // Create triangle
        if (v1 && v2 && v3 && (v1 != v2) && (v2 != v3) && (v1 != v3)) {
          if (!CreateFace(v1, v2, v3)) {
            // 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(v1);
            degenerate_triangle_vertices.Insert(v2);
            degenerate_triangle_vertices.Insert(v3);
          }
        }

        // Move to next triangle
        v2 = v3;
      }

      // Increment counter
      face_count++;
    }
    else {
      // Should never get here
      RNFail("Found extra text starting at line %d in file %s\n", line_count, filename);
      break;
    }
  }

  // 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 $0x4b8, %rsp            # imm = 0x4B8
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8c8d9(%rip), %rsi     # 0xabdd3
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x1f885
movq %rax, %r12
movq %rbx, 0x10(%rsp)
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x6f80c
leaq 0xb0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0xe730
testq %rax, %rax
je 0x1f7b2
leaq 0xaf(%rsp), %r13
leaq 0x58(%rsp), %rbp
movl $0x0, 0x4(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq %r12, 0x38(%rsp)
movq %r13, %rbx
movsbl 0x1(%rbx), %r15d
incq %rbx
movl %r15d, %edi
callq 0xe080
testl %eax, %eax
jne 0x1f576
movzbl %r15b, %eax
movl 0x4(%rsp), %ecx
incl %ecx
testl %eax, %eax
movl %ecx, 0x4(%rsp)
je 0x1f794
cmpl $0x23, %eax
je 0x1f794
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x1f615
cmpl %eax, 0x30(%rsp)
jge 0x1f666
movq %rbx, %rdi
leaq 0x8311b(%rip), %rsi     # 0xa26e0
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x1f8bf
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movq %rbp, %rdi
callq 0x3c532
movq (%r14), %rax
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq *0x20(%rax)
incl 0x30(%rsp)
jmp 0x1f794
movq %rbx, %rdi
leaq 0x82e3e(%rip), %rsi     # 0xa245d
callq 0xe100
movq %rbx, %rdi
testq %rax, %rax
je 0x1f763
leaq 0x82e2a(%rip), %rsi     # 0xa2461
leaq 0x70(%rsp), %rdx
movq %rbp, %rcx
leaq 0xc(%rsp), %r8
leaq 0x34(%rsp), %r9
xorl %eax, %eax
callq 0xe420
cmpl $0x4, %eax
jne 0x1f794
movl 0x58(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x1f794
movl 0x2c(%rsp), %eax
cmpl 0xc(%rsp), %eax
jge 0x1f8db
movq %rbx, %rdi
leaq 0x82e5b(%rip), %rsi     # 0xa24d9
callq 0xe710
testq %rax, %rax
je 0x1f8f7
movq %rax, %rdi
callq 0xe700
testl %eax, %eax
jle 0x1f750
movl %eax, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r13, %rbx
xorl %edi, %edi
leaq 0x82e28(%rip), %rsi     # 0xa24d9
callq 0xe710
testq %rax, %rax
je 0x1f89e
movq %rax, %rdi
callq 0xe700
movq 0x8(%r14), %rcx
cltq
movq (%rcx,%rax,8), %r13
testq %r12, %r12
cmoveq %r13, %r12
cmoveq %rbx, %r13
testq %r12, %r12
je 0x1f747
cmpq %rbx, %r12
je 0x1f747
testq %rbx, %rbx
je 0x1f747
testq %r13, %r13
je 0x1f747
cmpq %r13, %rbx
je 0x1f747
cmpq %r13, %r12
je 0x1f747
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f747
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x6f8a2
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x6f8a2
decl %r15d
jne 0x1f6a5
incl 0x2c(%rsp)
movq 0x38(%rsp), %r12
leaq 0xaf(%rsp), %r13
jmp 0x1f794
leaq 0x82f89(%rip), %rsi     # 0xa26f3
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0x34(%rsp), %r8
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x1f913
cmpl $0x0, 0x8(%rsp)
je 0x1f913
leaq 0xb0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0xe730
testq %rax, %rax
jne 0x1f573
movq %r12, %rbp
cmpl $0x3, 0x24(%rsp)
jl 0x1f872
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r12
movq 0x8(%rax,%r13,8), %r15
movq 0x10(%rax,%r13,8), %rbx
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f85c
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f85c
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 %rax, %r15
addq $0x18, %rbx
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq (%r14), %r9
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0x3, %r13
movslq 0x24(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %r13
jle 0x1f7c3
movq %rbp, %rdi
callq 0xe430
movl $0x1, %r14d
jmp 0x1f935
leaq 0x82bb9(%rip), %rdi     # 0xa2445
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x1f93f
leaq 0x82c37(%rip), %rdi     # 0xa24dc
xorl %r14d, %r14d
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
movq 0x38(%rsp), %rdi
jmp 0x1f930
leaq 0x82bd7(%rip), %rdi     # 0xa249d
xorl %r14d, %r14d
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x1f92d
leaq 0x82c28(%rip), %rdi     # 0xa250a
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x1f7b2
leaq 0x82bde(%rip), %rdi     # 0xa24dc
xorl %r14d, %r14d
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x1f92d
leaq 0x82b50(%rip), %rdi     # 0xa246a
xorl %r14d, %r14d
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
movq %r12, %rdi
callq 0xe430
leaq 0x18(%rsp), %rdi
callq 0x6f876
movl %r14d, %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
jmp 0x1f966
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0
nop