// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rdi, %r14
leaq 0x7fad3(%rip), %rsi     # 0x9caf3
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x1d2bc
movq %rax, %rbx
movq %r15, 0x10(%rsp)
movq %rsp, %rax
andl $0x0, (%rax)
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
leaq 0x24(%rsp), %rax
andl $0x0, (%rax)
leaq 0x28(%rsp), %rdi
callq 0x6630a
leaq 0xb0(%rsp), %r15
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movq %r15, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0xe760
testq %rax, %rax
je 0x1d343
leaq 0xaf(%rsp), %r13
movsbl 0x1(%r13), %r12d
incq %r13
movl %r12d, %edi
callq 0xe080
testl %eax, %eax
jne 0x1d094
movzbl %r12b, %eax
incl %ebp
testl %eax, %eax
je 0x1d073
cmpl $0x23, %eax
je 0x1d073
movl (%rsp), %eax
testl %eax, %eax
jne 0x1d143
movq %r13, %rdi
leaq 0x76341(%rip), %rsi     # 0x9340d
callq 0xe100
movq %r13, %rdi
testq %rax, %rax
je 0x1d110
leaq 0x76331(%rip), %rsi     # 0x93411
leaq 0x70(%rsp), %rdx
leaq 0x58(%rsp), %rcx
leaq 0x4(%rsp), %r8
leaq 0x24(%rsp), %r9
xorl %eax, %eax
callq 0xe430
cmpl $0x4, %eax
jne 0x1d073
movl 0x58(%rsp), %eax
movl %eax, (%rsp)
jmp 0x1d073
leaq 0x7658c(%rip), %rsi     # 0x936a3
movq %rsp, %rdx
leaq 0x4(%rsp), %rcx
leaq 0x24(%rsp), %r8
xorl %eax, %eax
callq 0xe430
cmpl $0x3, %eax
jne 0x1d2d5
cmpl $0x0, (%rsp)
jne 0x1d073
jmp 0x1d2d5
cmpl %eax, 0x20(%rsp)
jge 0x1d1a8
movq %r13, %rdi
leaq 0x7653d(%rip), %rsi     # 0x93690
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0xe430
cmpl $0x3, %eax
jne 0x1d2fa
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x37b4c
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0x20(%rax)
incl 0x20(%rsp)
jmp 0x1d073
movl 0x1c(%rsp), %eax
cmpl 0x4(%rsp), %eax
jge 0x1d32e
movq %r13, %rdi
leaq 0x762c9(%rip), %rsi     # 0x93489
callq 0xe740
testq %rax, %rax
je 0x1d424
movq %rax, %rdi
callq 0xe730
movl %eax, %r13d
testl %eax, %eax
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %esi, %esi
xorl %r12d, %r12d
movq %r12, 0x8(%rsp)
subl $0x1, %r13d
jb 0x1d2b3
movq %rsi, %r12
xorl %edi, %edi
leaq 0x76285(%rip), %rsi     # 0x93489
callq 0xe740
testq %rax, %rax
je 0x1d314
movq %rax, %rdi
callq 0xe730
movq 0x8(%r14), %rcx
cltq
movq (%rcx,%rax,8), %rax
movq %r12, %rsi
testq %r12, %r12
cmoveq %rax, %rsi
cmoveq 0x8(%rsp), %rax
movq %rax, %r12
testq %rsi, %rsi
je 0x1d1e9
cmpq 0x8(%rsp), %rsi
je 0x1d1e9
cmpq $0x0, 0x8(%rsp)
je 0x1d1e9
testq %r12, %r12
je 0x1d1e9
cmpq %r12, 0x8(%rsp)
je 0x1d1e9
cmpq %r12, %rsi
je 0x1d1e9
movq (%r14), %rax
movq %r14, %rdi
movq %rsi, 0x38(%rsp)
movq 0x8(%rsp), %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
movq 0x38(%rsp), %rsi
jne 0x1d1e9
leaq 0x28(%rsp), %rdi
callq 0x22bfc
leaq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x22bfc
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x22bfc
movq 0x38(%rsp), %rsi
jmp 0x1d1e9
incl 0x1c(%rsp)
jmp 0x1d073
leaq 0x76132(%rip), %rdi     # 0x933f5
xorl %r14d, %r14d
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x1d40f
leaq 0x7613e(%rip), %rdi     # 0x9341a
xorl %r14d, %r14d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
movq %rbx, %rdi
callq 0xe440
jmp 0x1d405
leaq 0x7614c(%rip), %rdi     # 0x9344d
xorl %r14d, %r14d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1d2ed
leaq 0x76171(%rip), %rdi     # 0x9348c
xorl %r14d, %r14d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1d2ed
leaq 0x76185(%rip), %rdi     # 0x934ba
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
xorl %ebp, %ebp
movslq 0x34(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jg 0x1d3f9
movq 0x28(%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 0x1d3f0
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 0x1d3f0
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 0x1d345
movq %rbx, %rdi
callq 0xe440
pushq $0x1
popq %r14
leaq 0x28(%rsp), %rdi
callq 0x66378
movl %r14d, %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x76061(%rip), %rdi     # 0x9348c
xorl %r14d, %r14d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1d2ed
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
jmp 0x1d453
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810