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

  // Read body
  int line_number = 0;
  char buffer[1024], cmd[1024];
  RNArray<R3MeshVertex *> vertices;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fgets(buffer, 1024, fp)) {
    line_number++;
    if (sscanf(buffer, "%s", cmd) != 1) continue;
    if (!strcmp(cmd, "vertex")) {
      RNCoord x, y, z;
      if (sscanf(buffer, "%s%lf%lf%lf", cmd, &x, &y, &z) != 4) {
        RNFail("Error in vertex of STL file %s on line %d", filename, line_number);
        return 0;
      }

      // Make vertex
      const R3Point position(x, y, z);
      R3MeshVertex *vertex = CreateVertex(position);
      vertices.Insert(vertex);
    }
    else if (!strcmp(cmd, "outer")) {
      // Just checking
      assert(vertices.IsEmpty());
    }
    else if (!strcmp(cmd, "endloop")) {
      // 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]);
        }
        vertices.Empty();
      }
    }
  }

  // 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 $0x868, %rsp            # imm = 0x868
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8a22b(%rip), %rsi     # 0xabdd3
movq %r14, %rdi
callq 0xe530
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x21e82
movq %r14, 0x28(%rsp)
movq %rsp, %rdi
callq 0x6f80c
leaq 0x10(%rsp), %rdi
callq 0x6f80c
leaq 0x460(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
je 0x21da3
leaq 0x460(%rsp), %r13
movq %rsp, %r12
leaq 0x10(%rsp), %rbp
xorl %r15d, %r15d
incl %r15d
movq %r13, %rdi
leaq 0x8629b(%rip), %rsi     # 0xa7eb0
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x1, %eax
jne 0x21d88
movl 0x60(%rsp), %eax
movl $0x74726576, %ecx       # imm = 0x74726576
xorl %ecx, %eax
movl 0x63(%rsp), %ecx
movl $0x786574, %edx         # imm = 0x786574
xorl %edx, %ecx
orl %eax, %ecx
je 0x21d1e
movl 0x60(%rsp), %eax
movl $0x6574756f, %ecx       # imm = 0x6574756F
xorl %ecx, %eax
movzwl 0x64(%rsp), %ecx
xorl $0x72, %ecx
orl %eax, %ecx
setne %cl
movabsq $0x706f6f6c646e65, %rax # imm = 0x706F6F6C646E65
cmpq %rax, 0x60(%rsp)
sete %dl
movl 0xc(%rsp), %eax
cmpl $0x3, %eax
setge %sil
andb %dl, %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x21d88
movl $0x2, %r14d
movq (%rsp), %rcx
movq (%rcx), %rsi
movq -0x8(%rcx,%r14,8), %rdx
cmpq %rdx, %rsi
je 0x21d0d
movq (%rcx,%r14,8), %rcx
cmpq %rcx, %rdx
je 0x21d0d
cmpq %rcx, %rsi
je 0x21d0d
movq (%rbx), %rax
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21cff
movq (%rsp), %rax
movq (%rax), %rsi
movl 0x1c(%rsp), %edx
movq %rbp, %rdi
callq 0x6f8a2
movq (%rsp), %rax
movq -0x8(%rax,%r14,8), %rsi
movl 0x1c(%rsp), %edx
movq %rbp, %rdi
callq 0x6f8a2
movq (%rsp), %rax
movq (%rax,%r14,8), %rsi
movl 0x1c(%rsp), %edx
movq %rbp, %rdi
callq 0x6f8a2
movq %r12, %rdi
xorl %esi, %esi
callq 0x6fa6c
movl 0xc(%rsp), %eax
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0x21c93
jmp 0x21d88
movq %r13, %rdi
leaq 0x806cc(%rip), %rsi     # 0xa23f4
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
cmpl $0x4, %eax
jne 0x21ea9
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x3c532
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movl 0xc(%rsp), %edx
movq %r12, %rdi
movq %rax, %rsi
callq 0x6f8a2
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
jne 0x21c08
cmpl $0x3, 0x1c(%rsp)
jl 0x21e5f
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 0x21e49
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 0x21e49
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 0x21db0
movq 0x20(%rsp), %rdi
callq 0xe430
movl $0x1, %ebx
leaq 0x10(%rsp), %rdi
callq 0x6f876
movq %rsp, %rdi
callq 0x6f876
jmp 0x21e95
leaq 0x80531(%rip), %rdi     # 0xa23ba
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebx, %eax
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x80a52(%rip), %rdi     # 0xa2902
xorl %ebx, %ebx
movq 0x28(%rsp), %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x719d2
jmp 0x21e6e
jmp 0x21ed6
movq %rax, %rbx
jmp 0x21ee3
jmp 0x21ed6
jmp 0x21ed6
jmp 0x21ed6
jmp 0x21ed6
jmp 0x21ed6
jmp 0x21ed6
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6f876
movq %rsp, %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0
nop