// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ReadCattFile(char const*)
int R3Mesh::
ReadCattFile(const char *filename)
{
  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "r"))) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Read body
  char buffer[1024];
  int line_count = 0;
  RNBoolean corners = FALSE;
  RNBoolean planes = FALSE;
  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
    if (*bufferp == '\0') continue;

    // Get keyword
    char keyword[80];
    if (sscanf(bufferp, "%s", keyword) != 1) {
      RNFail("Syntax error on line %d in file %s", line_count, filename);
      return 0;
    }

    // Check keyword
    if (keyword[0] == '%') {
      corners = planes = FALSE;
      if (!strcmp(keyword, "%CORNERS")) corners = TRUE;
      else if (!strcmp(keyword, "%PLANES")) planes = TRUE;
      else if (!strcmp(keyword, "%EOF")) break;
      continue;
    }

    // Read data
    if (corners) {
      // Read vertex coordinates
      int id;
      double x, y, z;
      if (sscanf(bufferp, "%d%lf%lf%lf", &id, &x, &y, &z) != 4) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Create vertex
      CreateVertex(R3Point(x, y, z));
    }
    else if (planes) {
      // Read plane header
      int id;
      char buffer1[256];
      char buffer2[256];
      if ((sscanf(bufferp, "%d%s%s", &id, buffer1, buffer2) != 3) ||
          (strcmp(buffer1, "/")) || (strcmp(buffer2, "/RIGID"))) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Read vertices
      RNArray<R3MeshVertex *> vertices;
      if (fgets(buffer, 1023, fp)) {
        line_count++;
        bufferp = strtok(buffer, "\t ");
        while (bufferp) {
          int id = atoi(bufferp);
          if ((id <= 0) || (id > NVertices())) {
            RNFail("Bogus vertex index on line %d in file %s", line_count, filename);
            return 0;
          }
          R3MeshVertex *v = Vertex(id-1);
          assert(v);
          vertices.Insert(v);
          bufferp = strtok(NULL, "\t ");
        }
      }

      // Create face(s)
      for (int i = 2; i < vertices.NEntries(); i++) {
        if (vertices[0] == vertices[i-1]) continue;
        if (vertices[i-1] == vertices[i]) continue;
        if (vertices[0] == vertices[i]) continue;
        if (!CreateFace(vertices[0], vertices[i-1], vertices[i])) {
          // 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(vertices[0]);
          degenerate_triangle_vertices.Insert(vertices[i-1]);
          degenerate_triangle_vertices.Insert(vertices[i]);
        }
      }
    }
  }
    
  // 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 $0x698, %rsp            # imm = 0x698
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8b18b(%rip), %rsi     # 0xabdd3
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x21070
movq %rax, %r14
movq %r15, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x6f80c
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
je 0x20fa5
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
leaq 0x28f(%rsp), %r15
movb 0x1(%r15), %r12b
incq %r15
movsbl %r12b, %edi
callq 0xe080
testl %eax, %eax
jne 0x20ca3
leal 0x1(%r13), %ebp
testb %r12b, %r12b
je 0x20f84
movq %r15, %rdi
leaq 0x871e2(%rip), %rsi     # 0xa7eb0
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x1, %eax
jne 0x21085
cmpb $0x25, 0x40(%rsp)
jne 0x20d33
movq 0x40(%rsp), %rax
movabsq $0x5352454e524f4325, %rcx # imm = 0x5352454E524F4325
xorq %rcx, %rax
movzbl 0x48(%rsp), %ecx
orq %rax, %rcx
je 0x20da3
movabsq $0x53454e414c5025, %rax # imm = 0x53454E414C5025
cmpq %rax, 0x40(%rsp)
jne 0x20f61
movl $0x0, 0x4(%rsp)
movl $0x1, (%rsp)
jmp 0x20f84
cmpl $0x0, 0x4(%rsp)
je 0x20db0
movq %r15, %rdi
leaq 0x8199a(%rip), %rsi     # 0xa26de
leaq 0x3c(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
cmpl $0x4, %eax
jne 0x210bc
movsd 0x90(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x190(%rsp), %r15
movq %r15, %rdi
callq 0x3c532
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
jmp 0x20f84
movl $0x1, 0x4(%rsp)
jmp 0x20f58
movl $0x0, 0x4(%rsp)
cmpl $0x0, (%rsp)
je 0x20f58
movq %r15, %rdi
leaq 0x818ae(%rip), %rsi     # 0xa267a
leaq 0x30(%rsp), %rdx
leaq 0x190(%rsp), %rcx
leaq 0x90(%rsp), %r8
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x210d3
cmpw $0x2f, 0x190(%rsp)
jne 0x210d3
movl 0x90(%rsp), %eax
movl $0x4749522f, %ecx       # imm = 0x4749522F
xorl %ecx, %eax
movl 0x93(%rsp), %ecx
movl $0x444947, %edx         # imm = 0x444947
xorl %edx, %ecx
orl %eax, %ecx
jne 0x210d3
leaq 0x8(%rsp), %rdi
callq 0x6f80c
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
je 0x20eb4
addl $0x2, %r13d
leaq 0x290(%rsp), %rdi
leaq 0x8182d(%rip), %rbp     # 0xa2688
movq %rbp, %rsi
callq 0xe710
testq %rax, %rax
je 0x20eb1
movq 0x28(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %rax, %rdi
callq 0xe700
testl %eax, %eax
jle 0x2109c
cmpl 0x14(%rbx), %eax
jg 0x2109c
movq 0x8(%rbx), %rcx
movl %eax, %eax
movq -0x8(%rcx,%rax,8), %rsi
movl 0x14(%rsp), %edx
movq %r12, %rdi
callq 0x6f8a2
xorl %edi, %edi
movq %rbp, %rsi
callq 0xe710
testq %rax, %rax
jne 0x20e72
movl %r13d, %ebp
cmpl $0x3, 0x14(%rsp)
jl 0x20f4c
movl $0x2, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq -0x8(%rax,%r15,8), %rdx
cmpq %rdx, %rsi
je 0x20f3b
movq (%rax,%r15,8), %rcx
cmpq %rcx, %rdx
je 0x20f3b
cmpq %rcx, %rsi
je 0x20f3b
movq (%rbx), %rax
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x20f3b
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x6f8a2
movq 0x8(%rsp), %rax
movq -0x8(%rax,%r15,8), %rsi
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x6f8a2
movq 0x8(%rsp), %rax
movq (%rax,%r15,8), %rsi
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x6f8a2
incq %r15
movslq 0x14(%rsp), %rax
cmpq %rax, %r15
jl 0x20ec5
leaq 0x8(%rsp), %rdi
callq 0x6f876
jmp 0x20f84
movl $0x0, (%rsp)
jmp 0x20f84
movl 0x40(%rsp), %eax
movl $0x464f4525, %ecx       # imm = 0x464F4525
xorl %ecx, %eax
movzbl 0x44(%rsp), %ecx
orl %eax, %ecx
je 0x20fa5
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
movl %ebp, %r13d
testq %rax, %rax
jne 0x20c9b
cmpl $0x3, 0x24(%rsp)
jl 0x21061
xorl %ebp, %ebp
movq 0x18(%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 0x2104b
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 0x2104b
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 0x24(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jle 0x20fb2
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x210ea
leaq 0x81343(%rip), %rdi     # 0xa23ba
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x210f4
leaq 0x81345(%rip), %rdi     # 0xa23d1
movl %ebp, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x210e8
leaq 0x815e8(%rip), %rdi     # 0xa268b
movl %r13d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x719d2
leaq 0x8(%rsp), %rdi
callq 0x6f876
jmp 0x210e8
leaq 0x8130e(%rip), %rdi     # 0xa23d1
movl %ebp, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x210e8
leaq 0x812f7(%rip), %rdi     # 0xa23d1
movl %ebp, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0x6f876
movl %ebx, %eax
addq $0x698, %rsp            # imm = 0x698
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2112b
jmp 0x2112b
jmp 0x2111c
jmp 0x2112b
jmp 0x2112b
jmp 0x2112b
jmp 0x2112b
jmp 0x2112b
jmp 0x2112b
jmp 0x2111c
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x6f876
jmp 0x2112e
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0