// File: /workspace/github_cpp_makefile_star2_O3/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 0x81073(%rip), %rsi     # 0xa25ab
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x2159a
movq %rax, %r14
leaq 0x70(%rsp), %rsi
movq %rax, %rdi
callq 0x21b0b
testl %eax, %eax
js 0x215a3
cmpl $0x534649, 0x70(%rsp)   # imm = 0x534649
jne 0x215ac
leaq 0x64(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x215b5
movss 0x64(%rsp), %xmm0
ucomiss 0x80d53(%rip), %xmm0    # 0xa22e0
jne 0x21591
jnp 0x215dc
leaq 0x811fc(%rip), %rdi     # 0xa2794
jmp 0x215bc
leaq 0x80ea4(%rip), %rdi     # 0xa2445
jmp 0x215bc
leaq 0x81183(%rip), %rdi     # 0xa272d
jmp 0x215bc
leaq 0x8119a(%rip), %rdi     # 0xa274d
jmp 0x215bc
leaq 0x811bb(%rip), %rdi     # 0xa2777
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
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 0x21b0b
testl %eax, %eax
js 0x21624
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x21b0b
testl %eax, %eax
js 0x217b5
movabsq $0x5345434954524556, %rax # imm = 0x5345434954524556
xorq 0x70(%rsp), %rax
movzbl 0x78(%rsp), %ecx
orq %rax, %rcx
je 0x2162d
leaq 0x811e6(%rip), %rdi     # 0xa2808
jmp 0x215bc
leaq 0x81191(%rip), %rdi     # 0xa27bc
jmp 0x215bc
leaq 0x60(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x2175a
movl 0x60(%rsp), %r13d
imulq $0x98, %r13, %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%rax), %r12
movq %r12, %rdi
callq 0xe030
leaq 0x8(%rax), %rcx
movq %r13, (%rax)
testq %r13, %r13
je 0x21766
movq %rax, 0x48(%rsp)
movq %r12, 0x10(%rsp)
movq %r13, 0x50(%rsp)
imulq $-0x98, %r13, %r13
movq %rcx, %r12
xorl %ebp, %ebp
movq %rcx, 0x28(%rsp)
movq %r12, %rdi
callq 0x239aa
addq $-0x98, %rbp
addq $0x98, %r12
cmpq %rbp, %r13
jne 0x2169b
movq 0x28(%rsp), %rax
movq %rax, 0x38(%rbx)
leaq 0x8(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq 0x50(%rsp), %r12
movl %r12d, %esi
callq 0x6f906
testq %r12, %r12
je 0x2177b
xorl %r13d, %r13d
leaq 0x30(%rsp), %r12
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x3, %edx
leaq 0x1c(%rsp), %rdi
movq %r14, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x21a0a
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x20(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x24(%rsp), %xmm2
movq %r12, %rdi
callq 0x3c532
movq 0x38(%rbx), %rdx
addq %r13, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x20(%rax)
testq %rax, %rax
je 0x21a13
incl %ebp
addq $0x98, %r13
cmpq %r13, 0x8(%rsp)
jne 0x216e8
jmp 0x2177b
leaq 0x810bf(%rip), %rdi     # 0xa2820
jmp 0x215bc
movq %rcx, 0x38(%rbx)
leaq 0x8(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movl %r13d, %esi
callq 0x6f906
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x21b0b
testl %eax, %eax
js 0x217b5
movabsq $0x454c474e41495254, %rax # imm = 0x454C474E41495254
xorq 0x70(%rsp), %rax
movzwl 0x78(%rsp), %ecx
xorq $0x53, %rcx
orq %rax, %rcx
je 0x217c1
leaq 0x810e2(%rip), %rdi     # 0xa2892
jmp 0x215bc
leaq 0x81020(%rip), %rdi     # 0xa27dc
jmp 0x215bc
leaq 0x5c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x21a2d
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 0x21848
imulq $-0xc8, 0x8(%rsp), %rbp
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x23ac2
addq $-0xc8, %r13
addq $0xc8, %r12
cmpq %r13, %rbp
jne 0x2182d
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rbx)
leaq 0x28(%rbx), %rdi
movq 0x8(%rsp), %r12
movl %r12d, %esi
callq 0x6f906
leaq 0x30(%rsp), %rdi
callq 0x6f80c
testq %r12, %r12
je 0x2193f
xorl %r12d, %r12d
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x3, %edx
leaq 0x1c(%rsp), %rdi
movq %r14, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x21a39
movslq 0x1c(%rsp), %rcx
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rcx,8), %r13
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
cmpq %rdx, %r13
je 0x2192b
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
cmpq %rcx, %rdx
je 0x2192b
cmpq %rcx, %r13
je 0x2192b
movq 0x48(%rbx), %r8
addq %r12, %r8
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq %rdx, 0x10(%rsp)
callq *0x48(%rax)
testq %rax, %rax
jne 0x2192b
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x6f8a2
movq 0x10(%rsp), %rsi
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
callq 0x6f8a2
movq 0x8(%rsp), %rsi
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
callq 0x6f8a2
incl %ebp
addq $0xc8, %r12
cmpq %r12, 0x50(%rsp)
jne 0x2187a
cmpl $0x3, 0x3c(%rsp)
jl 0x219fb
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 0x219e5
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 0x219e5
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 0x2194c
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x21a4e
leaq 0x80e37(%rip), %rdi     # 0xa2848
jmp 0x21a1a
leaq 0x80e4d(%rip), %rdi     # 0xa2867
xorl %ebx, %ebx
movl %ebp, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x215c8
leaq 0x80e78(%rip), %rdi     # 0xa28ac
jmp 0x215bc
leaq 0x80e91(%rip), %rdi     # 0xa28d1
xorl %ebx, %ebx
movl %ebp, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x719d2
leaq 0x30(%rsp), %rdi
callq 0x6f876
jmp 0x215c8
jmp 0x21a67
jmp 0x21a67
jmp 0x21a67
jmp 0x21a67
jmp 0x21a67
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x6f876
jmp 0x21b03
movq %rax, %rbx
testq %r13, %r13
movq 0x68(%rsp), %r12
je 0x21ab1
negq %r13
movq %r12, %r14
addq $-0x68, %r14
leaq 0xbfca9(%rip), %r15     # 0xe1740
movq %r15, -0xc0(%r12,%r13)
leaq (%r14,%r13), %rdi
callq 0x280f4
addq $-0xc8, %r13
jne 0x21a97
movq %r12, %rdi
movq 0x48(%rsp), %rsi
jmp 0x21afe
movq %rax, %rbx
testq %rbp, %rbp
movq 0x48(%rsp), %r12
je 0x21af6
negq %rbp
movq %r12, %r14
addq $-0x88, %r14
leaq 0xbfc24(%rip), %r15     # 0xe1700
movq %r15, -0x90(%r12,%rbp)
leaq (%r14,%rbp), %rdi
callq 0x6f876
addq $-0x98, %rbp
jne 0x21adc
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0