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

  // Read body
  char cmd[128];
  int command_number = 1;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fscanf(fp, "%s", cmd) == 1) {
    if (!strcmp(cmd, "#vertex")) {
      // Read data
      double px, py, pz;
      double nx, ny, nz;
      double ts, tt;
      if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf", &px, &py, &pz, &nx, &ny, &nz, &ts, &tt) != 8) {
        RNFail("Unable to read vertex at command %d in file %s", command_number, filename);
        return 0;
      }

      // Create vertex
      R3Point point(px, py, pz);
      R3Vector normal(nx, ny, nz);
      if (normal.IsZero()) CreateVertex(point);
      else CreateVertex(point, normal);
    }
    else if (!strcmp(cmd, "#shape_triangle")) {
      // Read data
      int m;
      int i1, i2, i3;
      if (fscanf(fp, "%d%d%d%d", &m, &i1, &i2, &i3) != 4) {
        RNFail("Unable to read triangle at command %d in file %s", command_number, filename);
        return 0;
      }

      // Get vertices
      R3MeshVertex *v1 = vertices.Kth(i1);
      R3MeshVertex *v2 = vertices.Kth(i2);
      R3MeshVertex *v3 = vertices.Kth(i3);

      // Check vertices
      if ((v1 == v2) || (v2 == v3) || (v1 == v3)) continue;

      // Create face
      R3MeshFace *face = CreateFace(v1, v2, v3);
      if (!face) {
        // 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);
      }

      // Set face material
      if (face) SetFaceMaterial(face, m);
    }
	
    // Increment command number
    command_number++;
  }

  // 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 $0x118, %rsp            # imm = 0x118
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8c22d(%rip), %rsi     # 0xabd93
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x1fde0
movq %rax, %r14
movq %r15, 0x28(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x705ec
movl $0x1, 0x4(%rsp)
movq %r14, %rdi
leaq 0x88319(%rip), %rsi     # 0xa7eb4
leaq 0x90(%rsp), %rdx
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x1fdf8
movabsq $0x78657472657623, %rax # imm = 0x78657472657623
cmpq %rax, 0x90(%rsp)
je 0x1fcd9
movdqa 0x90(%rsp), %xmm0
pcmpeqb 0x826d4(%rip), %xmm0    # 0xa22b0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x1fdd7
movq %r14, %rdi
leaq 0x82aff(%rip), %rsi     # 0xa26f4
leaq 0x68(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0xe840
cmpl $0x4, %eax
jne 0x1fc70
movslq 0x30(%rsp), %rcx
movq 0x8(%rbx), %rax
movq (%rax,%rcx,8), %r15
movslq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %r13
movl $0x2, %ebp
cmpq %r13, %r15
je 0x1fc8c
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %r12
cmpq %r12, %r15
je 0x1fc8c
cmpq %r13, %r12
je 0x1fc8c
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
je 0x1fca2
movl 0x68(%rsp), %ecx
movl %ecx, 0x98(%rax)
xorl %ebp, %ebp
jmp 0x1fc8c
movl $0x1, %ebp
leaq 0x82901(%rip), %rdi     # 0xa257d
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x72a1e
cmpl $0x2, %ebp
je 0x1fb91
testl %ebp, %ebp
jne 0x1fec3
jmp 0x1fdd7
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x70682
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
xorl %ebp, %ebp
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x70682
jmp 0x1fc8c
movq %r14, %rdi
leaq 0x8894e(%rip), %rsi     # 0xa8631
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
xorl %eax, %eax
leaq 0x80(%rsp), %r10
pushq %r10
leaq 0x90(%rsp), %r10
pushq %r10
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
callq 0xe840
addq $0x20, %rsp
movl %eax, %ebp
cmpl $0x8, %eax
movq 0x28(%rsp), %rdx
jne 0x1fda8
movq 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
leaq 0x68(%rsp), %rdi
callq 0x3ca26
movsd 0x58(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
leaq 0x30(%rsp), %rdi
callq 0x3d4be
movsd 0x30(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq (%rbx), %rax
jne 0x1fdbc
jp 0x1fdbc
movsd 0x38(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x1fdbc
jp 0x1fdbc
movsd 0x40(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x1fdbc
jp 0x1fdbc
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
xorl %edx, %edx
callq *0x20(%rax)
jmp 0x1fdce
leaq 0x8278f(%rip), %rdi     # 0xa253e
movl 0x4(%rsp), %esi
xorl %eax, %eax
callq 0x72a1e
jmp 0x1fdce
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
cmpl $0x8, %ebp
jne 0x1fec3
incl 0x4(%rsp)
jmp 0x1fb91
leaq 0x825d6(%rip), %rdi     # 0xa23bd
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x1fecf
cmpl $0x3, 0x14(%rsp)
jl 0x1feb4
xorl %ebp, %ebp
movq 0x8(%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 0x1fe9e
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 0x1fe9e
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 0x14(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jle 0x1fe05
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x1fec5
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
callq 0x70656
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
jmp 0x1fef5
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0