// File: /workspace/github_cpp_makefile_star2_O2/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 $0x468, %rsp            # imm = 0x468
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x74697(%rip), %rsi     # 0x9355b
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x1ef22
movq %rax, %r14
leaq 0x60(%rsp), %rsi
movq %rax, %rdi
callq 0x1f40c
testl %eax, %eax
js 0x1ef2b
cmpl $0x534649, 0x60(%rsp)   # imm = 0x534649
jne 0x1ef34
leaq 0x3c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x1ef3d
movss 0x3c(%rsp), %xmm0
ucomiss 0x7437b(%rip), %xmm0    # 0x93290
jne 0x1ef19
jnp 0x1ef64
leaq 0x74824(%rip), %rdi     # 0x93744
jmp 0x1ef44
leaq 0x744cc(%rip), %rdi     # 0x933f5
jmp 0x1ef44
leaq 0x747ab(%rip), %rdi     # 0x936dd
jmp 0x1ef44
leaq 0x747c2(%rip), %rdi     # 0x936fd
jmp 0x1ef44
leaq 0x747e3(%rip), %rdi     # 0x93727
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x1f40c
testl %eax, %eax
js 0x1efab
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x1f40c
testl %eax, %eax
js 0x1f0f8
leaq 0x7481e(%rip), %rsi     # 0x937af
leaq 0x60(%rsp), %rdi
pushq $0x9
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1efb4
leaq 0x7480f(%rip), %rdi     # 0x937b8
jmp 0x1ef44
leaq 0x747ba(%rip), %rdi     # 0x9376c
jmp 0x1ef44
leaq 0x38(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x1f0b7
movl 0x38(%rsp), %r12d
imulq $0x98, %r12, %rdi
addq $0x8, %rdi
movq %rdi, 0x10(%rsp)
callq 0xe030
leaq 0x8(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x8(%rsp)
movq %r12, (%rax)
movq %r12, (%rsp)
testq %r12, %r12
je 0x1f030
imulq $-0x98, (%rsp), %r12
xorl %r13d, %r13d
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x2108c
addq $-0x98, %r13
addq $0x98, %rbp
cmpq %r13, %r12
jne 0x1f015
movq 0x28(%rsp), %rax
movq %rax, 0x38(%rbx)
leaq 0x8(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq (%rsp), %rsi
callq 0x663f0
xorl %r13d, %r13d
leaq 0x40(%rsp), %rbp
xorl %r12d, %r12d
cmpq %r12, (%rsp)
je 0x1f0c3
leaq 0x1c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x1f104
cvtss2sd 0x1c(%rsp), %xmm0
cvtss2sd 0x20(%rsp), %xmm1
cvtss2sd 0x24(%rsp), %xmm2
movq %rbp, %rdi
callq 0x37b4c
movq 0x38(%rbx), %rdx
addq %r13, %rdx
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x20(%rax)
testq %rax, %rax
je 0x1f10d
incq %r12
addq $0x98, %r13
jmp 0x1f056
leaq 0x74712(%rip), %rdi     # 0x937d0
jmp 0x1ef44
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x1f40c
testl %eax, %eax
js 0x1f0f8
leaq 0x7475d(%rip), %rsi     # 0x93838
leaq 0x60(%rsp), %rdi
pushq $0xa
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1f128
leaq 0x7474f(%rip), %rdi     # 0x93842
jmp 0x1ef44
leaq 0x7468d(%rip), %rdi     # 0x9378c
jmp 0x1ef44
leaq 0x746ed(%rip), %rdi     # 0x937f8
jmp 0x1f114
leaq 0x74703(%rip), %rdi     # 0x93817
xorl %ebx, %ebx
movl %r12d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1ef50
leaq 0x34(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x1f289
movl 0x34(%rsp), %r12d
imulq $0xc8, %r12, %rdi
addq $0x8, %rdi
movq %rdi, 0x58(%rsp)
callq 0xe030
leaq 0x8(%rax), %rcx
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
movq %r12, (%rax)
movq %r12, 0x10(%rsp)
testq %r12, %r12
je 0x1f1a4
imulq $-0xc8, 0x10(%rsp), %rbp
xorl %r12d, %r12d
movq (%rsp), %r13
movq %r13, %rdi
callq 0x21186
addq $-0xc8, %r12
addq $0xc8, %r13
cmpq %r12, %rbp
jne 0x1f189
movq (%rsp), %rax
movq %rax, 0x48(%rbx)
leaq 0x28(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x663f0
leaq 0x40(%rsp), %rdi
callq 0x6630a
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq %rax, (%rsp)
cmpq %rax, 0x10(%rsp)
je 0x1f295
leaq 0x1c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x1f358
movslq 0x1c(%rsp), %rcx
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rcx,8), %rbp
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %r13
cmpq %r13, %rbp
je 0x1f274
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %r12
cmpq %r12, %r13
je 0x1f274
cmpq %r12, %rbp
je 0x1f274
movq 0x48(%rbx), %r8
addq 0x8(%rsp), %r8
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f274
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x22bfc
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x22bfc
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x22bfc
movq (%rsp), %rax
incq %rax
addq $0xc8, 0x8(%rsp)
jmp 0x1f1cf
leaq 0x745cc(%rip), %rdi     # 0x9385c
jmp 0x1ef44
xorl %ebp, %ebp
movslq 0x4c(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jg 0x1f34b
movq 0x40(%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 0x1f342
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 0x1f342
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
jmp 0x1f297
movq %r14, %rdi
callq 0xe440
pushq $0x1
popq %rbx
jmp 0x1f36f
leaq 0x74522(%rip), %rdi     # 0x93881
xorl %ebx, %ebx
movq (%rsp), %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x681ca
leaq 0x40(%rsp), %rdi
callq 0x66378
jmp 0x1ef50
jmp 0x1f3bd
jmp 0x1f3bd
jmp 0x1f3bd
jmp 0x1f3bd
jmp 0x1f3bd
movq %rax, %rbx
testq %r12, %r12
je 0x1f3b1
negq %r12
movq 0x8(%rsp), %r14
addq $-0xc0, %r14
leaq (%r14,%r12), %rdi
callq 0x21208
addq $-0xc8, %r12
jne 0x1f39f
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %rsi
jmp 0x1f3ff
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x66378
jmp 0x1f404
movq %rax, %rbx
testq %r13, %r13
je 0x1f3f5
negq %r13
movq 0x8(%rsp), %r14
addq $-0x90, %r14
leaq (%r14,%r13), %rdi
callq 0x21124
addq $-0x98, %r13
jne 0x1f3e3
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0xe8f0
movq %rbx, %rdi
callq 0xe810