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

  // Read file
  char buffer[1024];
  RNBoolean coordinate3_active = FALSE;
  RNBoolean coordinate3_point_active = FALSE;
  RNBoolean indexedfaceset_active = FALSE;
  RNBoolean indexedfaceset_coordindex_active = FALSE;
  RNArray<R3MeshVertex *> vertices;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fgets(buffer, 1023, fp)) {
    // Skip white space
    char *bufferp = buffer;
    while (isspace(*bufferp)) bufferp++;

    // Skip blank lines and comments
    if (*bufferp == '#') continue;
    if (*bufferp == '\0') continue;

    // Get first token
    bufferp = strtok(bufferp, " ,");
    if (!bufferp) break;

    // Check node being parsed
    if (coordinate3_point_active) {
      if (*bufferp == ']') {
        coordinate3_point_active = FALSE;
      } 
      else {
        do {
          if (*bufferp && !isspace(*bufferp)) {
            // Read next coordinate
            static int coordinate_index = 0;
            static RNScalar coordinates[3];
            coordinates[coordinate_index] = atof(bufferp);
            coordinate_index++;

            // Create vertex if have three coordinates
            if (coordinate_index == 3) {
              R3Point position(coordinates[0], coordinates[1], coordinates[2]);
              R3MeshVertex *vertex = CreateVertex(position);
              vertices.Insert(vertex);
              coordinate_index = 0;
            }
          }
          bufferp = strtok(NULL, " ,");
        } while (bufferp);
      }
    }
    else if (indexedfaceset_coordindex_active) {
      if (*bufferp == ']') {
        indexedfaceset_active = FALSE;
        indexedfaceset_coordindex_active = FALSE;
      } 
      else {
        do {
          // Read next coordinate index
          if (*bufferp && !isspace(*bufferp)) {
            static RNArray<R3MeshVertex *> face_vertices;
            int vertex_index = atoi(bufferp);
            if (vertex_index >= 0) {
              R3MeshVertex *vertex = vertices[vertex_index];
              face_vertices.Insert(vertex);
            }
            else {
              // Create face(s)
              R3MeshVertex *v1 = face_vertices[0];
              for (int k = 2; k < face_vertices.NEntries(); k++) {
                // Get vertices
                R3MeshVertex *v2 = face_vertices[k-1];
                R3MeshVertex *v3 = face_vertices[k];

                // Check vertices
                if ((v1 == v2) || (v2 == v3) || (v1 == v3)) continue;
 
                // Create face
                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);
                }
              }
              face_vertices.Empty();
            }
          }
          bufferp = strtok(NULL, " ,");
        } while (bufferp);
      }
    }
    else {
      if (!strcmp(bufferp, "Coordinate3")) coordinate3_active = TRUE;
      else if (!strcmp(bufferp, "IndexedFaceSet")) indexedfaceset_active = TRUE;
      else if ((coordinate3_active) && !strcmp(bufferp, "point")) coordinate3_point_active = TRUE;
      else if ((indexedfaceset_active) && !strcmp(bufferp, "coordIndex")) indexedfaceset_coordindex_active = TRUE;
    }
  }

  // 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 $0x458, %rsp            # imm = 0x458
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x89bd7(%rip), %rsi     # 0xabd93
movq %r15, %rdi
callq 0xe530
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x2266a
leaq 0x28(%rsp), %rdi
callq 0x705ec
leaq 0x10(%rsp), %rdi
callq 0x705ec
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
leaq 0x50(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
je 0x22588
leaq 0x4f(%rsp), %r15
movsbl 0x1(%r15), %ebp
incq %r15
movl %ebp, %edi
callq 0xe080
testl %eax, %eax
jne 0x2221e
movzbl %bpl, %eax
xorl %r12d, %r12d
testl %eax, %eax
je 0x2256a
cmpl $0x23, %eax
je 0x2256a
movq %r15, %rdi
leaq 0x806e2(%rip), %r15     # 0xa2935
movq %r15, %rsi
callq 0xe710
testq %rax, %rax
je 0x222f8
movq %rax, %rbp
testl %r14d, %r14d
jne 0x22300
testl %r13d, %r13d
jne 0x223b9
movq %rbp, %rdi
leaq 0x806b5(%rip), %rsi     # 0xa2938
callq 0xe640
testl %eax, %eax
je 0x2253f
movq %rbp, %rdi
leaq 0x806aa(%rip), %rsi     # 0xa2944
callq 0xe640
testl %eax, %eax
je 0x2254c
cmpl $0x0, 0xc(%rsp)
je 0x222c5
movq %rbp, %rdi
leaq 0x89eda(%rip), %rsi     # 0xac192
callq 0xe640
testl %eax, %eax
je 0x22575
cmpl $0x0, 0x8(%rsp)
je 0x22559
movq %rbp, %rdi
leaq 0x80679(%rip), %rsi     # 0xa2953
callq 0xe640
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
xorl %r14d, %r14d
movl $0x1, 0x8(%rsp)
jmp 0x22567
movb $0x1, %r12b
jmp 0x2256a
xorl %r14d, %r14d
cmpb $0x5d, (%rbp)
je 0x22567
leaq 0xc39ec(%rip), %r14     # 0xe5d00
movsbl (%rbp), %edi
testl %edi, %edi
je 0x22395
callq 0xe080
testl %eax, %eax
jne 0x22395
movq %rbp, %rdi
callq 0xe580
movslq 0xc39c4(%rip), %rax     # 0xe5cf8
movsd %xmm0, (%r14,%rax,8)
incl %eax
movl %eax, 0xc39b6(%rip)     # 0xe5cf8
cmpl $0x3, %eax
jne 0x22395
movsd 0xc39b1(%rip), %xmm0    # 0xe5d00
movsd 0xc39b1(%rip), %xmm1    # 0xe5d08
movsd 0xc39b1(%rip), %xmm2    # 0xe5d10
leaq 0x38(%rsp), %r12
movq %r12, %rdi
callq 0x3ca26
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movl 0x34(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x70682
movl $0x0, 0xc3963(%rip)     # 0xe5cf8
xorl %r12d, %r12d
xorl %edi, %edi
movq %r15, %rsi
callq 0xe710
movq %rax, %rbp
testq %rax, %rax
jne 0x22314
movl $0x1, %r14d
jmp 0x2256a
xorl %r13d, %r13d
cmpb $0x5d, (%rbp)
je 0x2255c
leaq 0x10(%rsp), %r12
movsbl (%rbp), %edi
testl %edi, %edi
je 0x224c7
callq 0xe080
testl %eax, %eax
jne 0x224c7
movb 0xc393e(%rip), %al      # 0xe5d28
testb %al, %al
je 0x224e8
movq %rbp, %rdi
callq 0xe700
testl %eax, %eax
js 0x22420
movq 0x28(%rsp), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rsi
movl 0xc3915(%rip), %edx     # 0xe5d24
leaq 0xc3902(%rip), %rdi     # 0xe5d18
callq 0x70682
jmp 0x224c7
cmpl $0x3, 0xc38fd(%rip)     # 0xe5d24
jl 0x224b2
movq 0xc38e4(%rip), %rax     # 0xe5d18
movq (%rax), %rbp
movl $0x2, %r14d
movq 0xc38d4(%rip), %rax     # 0xe5d18
movq -0x8(%rax,%r14,8), %r15
cmpq %r15, %rbp
je 0x224a3
movq (%rax,%r14,8), %r13
cmpq %r13, %r15
je 0x224a3
cmpq %r13, %rbp
je 0x224a3
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x224a3
movl 0x1c(%rsp), %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x70682
movl 0x1c(%rsp), %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x70682
movl 0x1c(%rsp), %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x70682
incq %r14
movslq 0xc3877(%rip), %rax     # 0xe5d24
cmpq %rax, %r14
jl 0x2243d
leaq 0xc385f(%rip), %rdi     # 0xe5d18
xorl %esi, %esi
callq 0x7082c
leaq 0x8046e(%rip), %r15     # 0xa2935
xorl %r14d, %r14d
xorl %edi, %edi
movq %r15, %rsi
callq 0xe710
movq %rax, %rbp
movl $0x1, %r13d
testq %rax, %rax
jne 0x223cb
jmp 0x22567
leaq 0xc3839(%rip), %rdi     # 0xe5d28
callq 0xe810
testl %eax, %eax
je 0x223f2
leaq 0xc3815(%rip), %rdi     # 0xe5d18
callq 0x705ec
leaq 0x4e147(%rip), %rdi     # 0x70656
leaq 0xc3802(%rip), %rsi     # 0xe5d18
leaq 0xc2f6b(%rip), %rdx     # 0xe5488
callq 0xe3b0
leaq 0xc37ff(%rip), %rdi     # 0xe5d28
callq 0xe2e0
leaq 0x80400(%rip), %r15     # 0xa2935
leaq 0x10(%rsp), %r12
jmp 0x223f2
movl $0x1, 0xc(%rsp)
xorl %r13d, %r13d
jmp 0x22564
xorl %r13d, %r13d
movl $0x1, 0x8(%rsp)
jmp 0x22564
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
testb %r12b, %r12b
je 0x221fc
jmp 0x22588
movl $0x1, %r14d
xorl %r13d, %r13d
movl $0x1, 0xc(%rsp)
jmp 0x22567
cmpl $0x3, 0x1c(%rsp)
jl 0x22645
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %r13
movq 0x8(%rax,%r14,8), %r12
movq 0x10(%rax,%r14,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 0x2262f
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 0x2262f
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, %r14
movslq 0x1c(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %r14
jle 0x22596
movq 0x20(%rsp), %rdi
callq 0xe430
leaq 0x10(%rsp), %rdi
callq 0x70656
leaq 0x28(%rsp), %rdi
callq 0x70656
movl $0x1, %ebx
jmp 0x2267d
leaq 0x7fdd7(%rip), %rdi     # 0xa2448
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc368d(%rip), %rdi     # 0xe5d28
callq 0xe2d0
jmp 0x226ba
movq %rax, %rbx
jmp 0x226c4
jmp 0x226b7
jmp 0x226b7
jmp 0x226b7
jmp 0x226b7
jmp 0x226b7
jmp 0x226b7
jmp 0x226b7
jmp 0x226b7
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70656
leaq 0x28(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0