// File: /workspace/github_cpp_makefile_star2_O1/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 0x8af8f(%rip), %rsi     # 0xabd93
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x21228
movq %rax, %r14
movq %r15, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x705ec
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
je 0x2124a
xorl %ebp, %ebp
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
leaq 0x28f(%rsp), %r12
movb 0x1(%r12), %r15b
incq %r12
movsbl %r15b, %edi
callq 0xe080
testl %eax, %eax
jne 0x20e5e
leal 0x1(%rbp), %r13d
testb %r15b, %r15b
je 0x20eed
movq %r12, %rdi
leaq 0x8702e(%rip), %rsi     # 0xa7eb4
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x1, %eax
jne 0x20ef8
cmpb $0x25, 0x40(%rsp)
jne 0x20f19
movq 0x40(%rsp), %rax
movabsq $0x5352454e524f4325, %rcx # imm = 0x5352454E524F4325
xorq %rcx, %rax
movzbl 0x48(%rsp), %ecx
movl $0x2, %r12d
orq %rax, %rcx
je 0x20f8a
movabsq $0x53454e414c5025, %rax # imm = 0x53454E414C5025
cmpq %rax, 0x40(%rsp)
jne 0x21038
movl $0x0, (%rsp)
movl $0x1, 0x4(%rsp)
jmp 0x21093
movl $0x2, %r12d
jmp 0x21093
movl $0x1, %r12d
leaq 0x814cf(%rip), %rdi     # 0xa23d4
movl %r13d, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x21093
cmpl $0x0, (%rsp)
je 0x20f9e
movq %r12, %rdi
leaq 0x817b8(%rip), %rsi     # 0xa26e1
leaq 0x3c(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
movl %eax, %ebp
cmpl $0x4, %eax
jne 0x21066
movsd 0x90(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x190(%rsp), %r15
movq %r15, %rdi
callq 0x3ca26
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
jmp 0x2107c
movl $0x1, (%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x21093
cmpl $0x0, 0x4(%rsp)
je 0x21087
movq %r12, %rdi
leaq 0x816ca(%rip), %rsi     # 0xa267d
leaq 0x30(%rsp), %rdx
leaq 0x190(%rsp), %rcx
leaq 0x90(%rsp), %r8
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x21003
cmpw $0x2f, 0x190(%rsp)
jne 0x21003
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
je 0x210c8
xorl %r12d, %r12d
leaq 0x813c7(%rip), %rdi     # 0xa23d4
movl %r13d, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
testb %r12b, %r12b
jne 0x21087
movl $0x0, (%rsp)
movl $0x1, 0x4(%rsp)
movl $0x1, %r12d
jmp 0x21093
movl 0x40(%rsp), %eax
movl $0x464f4525, %ecx       # imm = 0x464F4525
xorl %ecx, %eax
movzbl 0x44(%rsp), %ecx
xorl %r12d, %r12d
orl %eax, %ecx
sete %r12b
orl $0x2, %r12d
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
jmp 0x21093
leaq 0x81367(%rip), %rdi     # 0xa23d4
movl %r13d, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
movl $0x1, %r12d
cmpl $0x4, %ebp
jne 0x2108c
xorl %r12d, %r12d
jmp 0x21093
movl $0x1, (%rsp)
movl %r13d, %ebp
testl %r12d, %r12d
je 0x210a5
cmpl $0x2, %r12d
jne 0x21240
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
jne 0x20e56
jmp 0x2124a
leaq 0x8(%rsp), %rdi
callq 0x705ec
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
je 0x21179
addl $0x2, %ebp
leaq 0x290(%rsp), %rdi
leaq 0x81589(%rip), %rsi     # 0xa268b
callq 0xe710
movq %rax, %r12
testq %r12, %r12
je 0x21176
movq %r12, %rdi
callq 0xe700
testl %eax, %eax
setg %cl
cmpl 0x14(%rbx), %eax
setle %r13b
andb %cl, %r13b
je 0x21154
movq 0x8(%rbx), %rcx
movl %eax, %eax
movq -0x8(%rcx,%rax,8), %rsi
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
xorl %edi, %edi
leaq 0x81541(%rip), %rsi     # 0xa268b
callq 0xe710
movq %rax, %r12
jmp 0x21169
leaq 0x81533(%rip), %rdi     # 0xa268e
movl %ebp, %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
testb %r13b, %r13b
jne 0x2110a
xorl %r12d, %r12d
jmp 0x21216
movl %ebp, %r13d
movb $0x1, %r12b
cmpl $0x3, 0x14(%rsp)
jl 0x21213
movl $0x2, %ebp
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq -0x8(%rax,%rbp,8), %rdx
cmpq %rdx, %rsi
je 0x21202
movq (%rax,%rbp,8), %rcx
cmpq %rcx, %rdx
je 0x21202
cmpq %rcx, %rsi
je 0x21202
movq (%rbx), %rax
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21202
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x70682
movq 0x8(%rsp), %rax
movq -0x8(%rax,%rbp,8), %rsi
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x70682
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x70682
incq %rbp
movslq 0x14(%rsp), %rax
cmpq %rax, %rbp
jl 0x2118c
movl %r13d, %ebp
leaq 0x8(%rsp), %rdi
callq 0x70656
movl %ebp, %r13d
jmp 0x2101c
leaq 0x8118e(%rip), %rdi     # 0xa23bd
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x2131d
cmpl $0x3, %r12d
jne 0x21331
cmpl $0x3, 0x24(%rsp)
jl 0x21306
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 0x212f0
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 0x212f0
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 0x21257
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
leaq 0x18(%rsp), %rdi
callq 0x70656
movl %ebx, %eax
addq $0x698, %rsp            # imm = 0x698
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x21313
jmp 0x21358
jmp 0x21358
jmp 0x2133f
jmp 0x2133f
jmp 0x21358
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x70656
jmp 0x2135b
jmp 0x21358
jmp 0x21358
jmp 0x21358
jmp 0x21358
jmp 0x21358
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0
nop