// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
leaq 0x8c741(%rip), %rsi     # 0xabd93
movq %r14, %rdi
callq 0xe530
testq %rax, %rax
je 0x1fa10
movq %rax, %r13
movq %r14, 0x8(%rsp)
xorl %eax, %eax
movl %eax, (%rsp)
movl %eax, 0x4(%rsp)
movl %eax, 0x28(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x705ec
leaq 0xb0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r13, %rdx
callq 0xe730
testq %rax, %rax
je 0x1fa32
xorl %ebp, %ebp
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
leaq 0xaf(%rsp), %r15
movsbl 0x1(%r15), %r12d
incq %r15
movl %r12d, %edi
callq 0xe080
testl %eax, %eax
jne 0x1f6ba
movzbl %r12b, %eax
incl %ebp
movl $0x2, %r14d
testl %eax, %eax
je 0x1f9e5
cmpl $0x23, %eax
je 0x1f9e5
movl (%rsp), %eax
testl %eax, %eax
leaq 0x58(%rsp), %r14
je 0x1f75e
cmpl %eax, 0x24(%rsp)
jge 0x1f7ae
movq %r15, %rdi
leaq 0x82fd8(%rip), %rsi     # 0xa26e3
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0xe420
movl %eax, %r15d
cmpl $0x3, %eax
jne 0x1f903
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movq %r14, %rdi
callq 0x3ca26
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x20(%rax)
incl 0x24(%rsp)
jmp 0x1f920
movq %r15, %rdi
leaq 0x82cf8(%rip), %rsi     # 0xa2460
callq 0xe100
movq %r15, %rdi
testq %rax, %rax
je 0x1f955
leaq 0x82ce4(%rip), %rsi     # 0xa2464
leaq 0x70(%rsp), %rdx
movq %r14, %rcx
leaq 0x4(%rsp), %r8
leaq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0xe420
cmpl $0x4, %eax
jne 0x1f9e2
movl 0x58(%rsp), %eax
movl %eax, (%rsp)
jmp 0x1f9e2
movl 0x20(%rsp), %eax
cmpl 0x4(%rsp), %eax
jge 0x1f935
movq %r15, %rdi
leaq 0x82d16(%rip), %rsi     # 0xa24dc
callq 0xe710
testq %rax, %rax
je 0x1f9a0
movq %rax, %rdi
callq 0xe700
testl %eax, %eax
setle %r15b
jle 0x1f9c5
movl %eax, %r14d
xorl %edi, %edi
leaq 0x82ce8(%rip), %rsi     # 0xa24dc
callq 0xe710
testq %rax, %rax
je 0x1f8db
movq %r13, 0x30(%rsp)
movq %r14, 0x38(%rsp)
leal -0x1(%r14), %ecx
movl %ecx, 0x2c(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rax, %rdi
callq 0xe700
movq 0x8(%rbx), %rcx
cltq
movq (%rcx,%rax,8), %r12
testq %r13, %r13
cmoveq %r12, %r13
cmoveq %r15, %r12
testq %r13, %r13
je 0x1f8a5
cmpq %r15, %r13
je 0x1f8a5
testq %r15, %r15
je 0x1f8a5
testq %r12, %r12
je 0x1f8a5
cmpq %r12, %r15
je 0x1f8a5
cmpq %r12, %r13
je 0x1f8a5
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 0x1f8a5
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x70682
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x70682
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
cmpl %r14d, 0x2c(%rsp)
je 0x1f9ca
xorl %edi, %edi
leaq 0x82c23(%rip), %rsi     # 0xa24dc
callq 0xe710
incl %r14d
movq %r12, %r15
testq %rax, %rax
jne 0x1f81d
cmpl 0x38(%rsp), %r14d
setge %r15b
movq 0x30(%rsp), %r13
leaq 0x82bfd(%rip), %rdi     # 0xa24df
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
movq %r13, %rdi
callq 0xe430
movl $0x1, %r14d
jmp 0x1f9d5
leaq 0x82b96(%rip), %rdi     # 0xa24a0
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
movq %r13, %rdi
callq 0xe430
movl $0x1, %r14d
cmpl $0x3, %r15d
je 0x1f9e2
jmp 0x1f9e5
movl $0x3, %r14d
leaq 0x82bcb(%rip), %rdi     # 0xa250d
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x1f9e5
leaq 0x82d9a(%rip), %rsi     # 0xa26f6
movq %rsp, %rdx
leaq 0x4(%rsp), %rcx
leaq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x1f97b
cmpl $0x0, (%rsp)
jne 0x1f9e2
leaq 0x82aeb(%rip), %rdi     # 0xa246d
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
movq %r13, %rdi
callq 0xe430
movl $0x1, %r14d
jmp 0x1f9e5
leaq 0x82b38(%rip), %rdi     # 0xa24df
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
movq %r13, %rdi
callq 0xe430
movl $0x1, %r14d
jmp 0x1f9dd
xorl %r14d, %r14d
jmp 0x1f9d5
xorl %r14d, %r14d
movb $0x1, %r15b
movq 0x30(%rsp), %r13
movzbl %r15b, %eax
addl %eax, 0x20(%rsp)
testl %r14d, %r14d
jne 0x1f9e5
xorl %r14d, %r14d
testl %r14d, %r14d
je 0x1f9f0
cmpl $0x2, %r14d
jne 0x1fa28
leaq 0xb0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r13, %rdx
callq 0xe730
testq %rax, %rax
jne 0x1f6b2
jmp 0x1fa32
leaq 0x82a31(%rip), %rdi     # 0xa2448
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x1fb08
cmpl $0x3, %r14d
jne 0x1fb1c
movq %r13, %r14
cmpl $0x3, 0x1c(%rsp)
jl 0x1faf1
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 0x1fadb
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 0x1fadb
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 0x1fa42
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
leaq 0x10(%rsp), %rdi
callq 0x70656
movl %ebx, %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x1fafe
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
jmp 0x1fb32
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0
nop