// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ReadIfsFile(char const*)
int R3Mesh::
ReadIfsFile(const char *filename)
{
  char buffer[1024];

  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "rb"))) {
    RNFail("Unable to open file %s\n", filename);
    return 0;
  }

  // Read magic string
  if (ReadIfsString(fp, buffer, 1024) < 0) {
    RNFail("Unable to read header of %s", filename);
    return 0;
  }
  if (strcmp(buffer, "IFS")) {
    RNFail("Bad magic string in IFS file header of %s", filename);
    return 0;
  }

  // Read version number
  float version;
  if (fread(&version, sizeof(float), 1, fp) != 1) {
    RNFail("Unable to read version of %s", filename);
    return 0;
  }
  if (version != 1.0) {
    RNFail("Bad version number in file header of %s", filename);
    return 0;
  }

  // Read model name
  if (ReadIfsString(fp, buffer, 1024) < 0) {
    RNFail("Unable to read model name of %s", filename);
    return 0;
  }

  // Read vertex header
  if (ReadIfsString(fp, buffer, 1024) < 0) {
    RNFail("Unable to read vertex header of %s", filename);
    return 0;
  }
  if (strcmp(buffer, "VERTICES")) {
    RNFail("Bad vertex header in %s", filename);
    return 0;
  }

  // Read number of vertices
  unsigned int nverts;
  if (fread(&nverts, sizeof(unsigned int), 1, fp) != 1) {
    RNFail("Unable to read number of vertices in %s", filename);
    return 0;
  }

  // Allocate block of vertices
  vertex_block = new R3MeshVertex [nverts];
  
  // Resize array of vertices
  vertices.Resize(nverts);

  // Read vertices
  for (unsigned int i = 0; i < nverts; i++) {
    float p[3];
    if (fread(p, sizeof(float), 3, fp) != 3) {
      RNFail("Unable to read vertex %d in %s", i, filename);
      return 0;
    }

    // Create mesh vertex
    if (!CreateVertex(R3Point(p[0], p[1], p[2]), &vertex_block[i])) {
      RNFail("Unable to create vertex %d in %s", i, filename);
      return 0;
    }
  }

  // Read triangle header
  if (ReadIfsString(fp, buffer, 1024) < 0) {
    RNFail("Unable to read vertex header of %s", filename);
    return 0;
  }
  if (strcmp(buffer, "TRIANGLES")) {
    RNFail("Bad triangle header in %s", filename);
    return 0;
  }

  // Read number of faces
  unsigned int nfaces;
  if (fread(&nfaces, sizeof(unsigned int), 1, fp) != 1) {
    RNFail("Unable to read number of faces in %s", filename);
    return 0;
  }

  // Allocate block of faces
  face_block = new R3MeshFace [nfaces];

  // Resize array of faces
  faces.Resize(nfaces);

  // Allocate array for degenerate triangles
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;

  // Read triangles
  for (unsigned int i = 0; i < nfaces; i++) {
    int v[3];
    if (fread(v, sizeof(unsigned int), 3, fp) != 3) {
      RNFail("Unable to read vertex index in triangle %d of %s", i, filename);
      return 0;
    }

    // Get vertices
    R3MeshVertex *v0 = vertices[v[0]];
    R3MeshVertex *v1 = vertices[v[1]];
    R3MeshVertex *v2 = vertices[v[2]];

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

    // Create mesh face
    if (!CreateFace(v0, v1, v2, &face_block[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(v0);
      degenerate_triangle_vertices.Insert(v1);
      degenerate_triangle_vertices.Insert(v2);
    }
  }

  // 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 $0x478, %rsp            # imm = 0x478
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x80de6(%rip), %rsi     # 0xa25ae
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x2182a
movq %rax, %r14
leaq 0x70(%rsp), %rsi
movq %rax, %rdi
callq 0x21da1
testl %eax, %eax
js 0x21833
cmpl $0x534649, 0x70(%rsp)   # imm = 0x534649
jne 0x2183c
leaq 0x64(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x21845
movss 0x64(%rsp), %xmm0
ucomiss 0x80ac3(%rip), %xmm0    # 0xa22e0
jne 0x21821
jnp 0x2186c
leaq 0x80f6f(%rip), %rdi     # 0xa2797
jmp 0x2184c
leaq 0x80c17(%rip), %rdi     # 0xa2448
jmp 0x2184c
leaq 0x80ef6(%rip), %rdi     # 0xa2730
jmp 0x2184c
leaq 0x80f0d(%rip), %rdi     # 0xa2750
jmp 0x2184c
leaq 0x80f2e(%rip), %rdi     # 0xa277a
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebx, %eax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x21da1
testl %eax, %eax
js 0x218b4
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x21da1
testl %eax, %eax
js 0x21a17
movabsq $0x5345434954524556, %rax # imm = 0x5345434954524556
xorq 0x70(%rsp), %rax
movzbl 0x78(%rsp), %ecx
orq %rax, %rcx
je 0x218bd
leaq 0x80f59(%rip), %rdi     # 0xa280b
jmp 0x2184c
leaq 0x80f04(%rip), %rdi     # 0xa27bf
jmp 0x2184c
leaq 0x60(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x21a23
movl 0x60(%rsp), %r12d
imulq $0x98, %r12, %rax
movq %rax, 0x50(%rsp)
leaq 0x8(%rax), %rdi
movq %rdi, 0x10(%rsp)
callq 0xe030
leaq 0x8(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x48(%rsp)
movq %r12, (%rax)
movq %r12, 0x8(%rsp)
testq %r12, %r12
je 0x21943
imulq $-0x98, 0x8(%rsp), %r13
xorl %ebp, %ebp
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x23c38
addq $-0x98, %rbp
addq $0x98, %r12
cmpq %rbp, %r13
jne 0x21928
movq 0x28(%rsp), %rax
movq %rax, 0x38(%rbx)
leaq 0x8(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq 0x8(%rsp), %r12
movl %r12d, %esi
callq 0x706e0
testq %r12, %r12
je 0x219dd
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x3, %edx
leaq 0x1c(%rsp), %rdi
movq %r14, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x21a2f
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x20(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x24(%rsp), %xmm2
movq %r13, %rdi
callq 0x3ca26
movq 0x38(%rbx), %rdx
addq %r12, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x20(%rax)
testq %rax, %rax
je 0x21a38
incl %ebp
addq $0x98, %r12
cmpq %r12, 0x50(%rsp)
jne 0x21971
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x21da1
testl %eax, %eax
js 0x21a17
movabsq $0x454c474e41495254, %rax # imm = 0x454C474E41495254
xorq 0x70(%rsp), %rax
movzwl 0x78(%rsp), %ecx
xorq $0x53, %rcx
orq %rax, %rcx
je 0x21a52
leaq 0x80e83(%rip), %rdi     # 0xa2895
jmp 0x2184c
leaq 0x80dc1(%rip), %rdi     # 0xa27df
jmp 0x2184c
leaq 0x80df9(%rip), %rdi     # 0xa2823
jmp 0x2184c
leaq 0x80e15(%rip), %rdi     # 0xa284b
jmp 0x21a3f
leaq 0x80e2b(%rip), %rdi     # 0xa286a
xorl %ebx, %ebx
movl %ebp, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x21858
leaq 0x5c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x21cd6
movl 0x5c(%rsp), %r12d
imulq $0xc8, %r12, %rax
movq %rax, 0x50(%rsp)
leaq 0x8(%rax), %rdi
movq %rdi, 0x48(%rsp)
callq 0xe030
leaq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x68(%rsp)
movq %r12, (%rax)
movq %r12, 0x8(%rsp)
testq %r12, %r12
je 0x21ad9
imulq $-0xc8, 0x8(%rsp), %rbp
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x23d50
addq $-0xc8, %r12
addq $0xc8, %r13
cmpq %r12, %rbp
jne 0x21abe
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rbx)
leaq 0x28(%rbx), %rdi
movq 0x8(%rsp), %r12
movl %r12d, %esi
callq 0x706e0
leaq 0x30(%rsp), %rdi
callq 0x705ec
testq %r12, %r12
je 0x21c0b
xorl %ebp, %ebp
xorl %r13d, %r13d
movl $0x4, %esi
movl $0x3, %edx
leaq 0x1c(%rsp), %rdi
movq %r14, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x21bcf
movslq 0x1c(%rsp), %rcx
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rcx,8), %rsi
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movl $0x7, %r12d
cmpq %rdx, %rsi
je 0x21be8
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
cmpq %rcx, %rdx
je 0x21be8
cmpq %rcx, %rsi
je 0x21be8
movq 0x48(%rbx), %r8
addq %r13, %r8
movq (%rbx), %rax
movq %rbx, %rdi
movq %rsi, %r12
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq %rdx, 0x10(%rsp)
callq *0x48(%rax)
movq %r12, %rsi
xorl %r12d, %r12d
testq %rax, %rax
jne 0x21be8
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
callq 0x70682
movq 0x10(%rsp), %rsi
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
callq 0x70682
movq 0x8(%rsp), %rsi
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
callq 0x70682
jmp 0x21be8
movl $0x1, %r12d
leaq 0x80cf8(%rip), %rdi     # 0xa28d4
movl %ebp, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x72a1e
cmpl $0x7, %r12d
je 0x21bf7
testl %r12d, %r12d
jne 0x21ce2
addq $0xc8, %r13
incl %ebp
cmpq %r13, 0x50(%rsp)
jne 0x21b0b
cmpl $0x3, 0x3c(%rsp)
jl 0x21cc7
xorl %ebp, %ebp
movq 0x30(%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 0x21cb1
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 0x21cb1
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 0x3c(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jle 0x21c18
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x21ce4
leaq 0x80bd2(%rip), %rdi     # 0xa28af
jmp 0x2184c
xorl %ebx, %ebx
leaq 0x30(%rsp), %rdi
callq 0x70656
jmp 0x21858
jmp 0x21cfd
jmp 0x21cfd
jmp 0x21cfd
jmp 0x21cfd
jmp 0x21cfd
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x70656
jmp 0x21d99
movq %rax, %rbx
testq %r12, %r12
movq 0x68(%rsp), %r13
je 0x21d47
negq %r12
movq %r13, %r14
addq $-0x68, %r14
leaq 0xbfa13(%rip), %r15     # 0xe1740
movq %r15, -0xc0(%r13,%r12)
leaq (%r14,%r12), %rdi
callq 0x28348
addq $-0xc8, %r12
jne 0x21d2d
movq %r13, %rdi
movq 0x48(%rsp), %rsi
jmp 0x21d94
movq %rax, %rbx
testq %rbp, %rbp
movq 0x48(%rsp), %r12
je 0x21d8c
negq %rbp
movq %r12, %r14
addq $-0x88, %r14
leaq 0xbf98e(%rip), %r15     # 0xe1700
movq %r15, -0x90(%r12,%rbp)
leaq (%r14,%rbp), %rdi
callq 0x70656
addq $-0x98, %rbp
jne 0x21d72
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0