// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::WriteIfsFile(char const*) const
int R3Mesh::
WriteIfsFile(const char *filename) const
{
  float version = 1.0;
  unsigned int nverts = NVertices();
  unsigned int nfaces = NFaces();

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

  // Write header
  WriteIfsString(fp, "IFS");
  fwrite(&version, sizeof(float), 1, fp);
  WriteIfsString(fp, "filname");

  // Write vertices
  WriteIfsString(fp, "VERTICES");
  fwrite(&nverts, sizeof(unsigned int), 1, fp);
  for (unsigned int i = 0; i < nverts; i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R3Point& position = VertexPosition(vertex);
    float p[3];
    p[0] = position.X();
    p[1] = position.Y();
    p[2] = position.Z();
    fwrite(&p, sizeof(float), 3, fp);
  }

  // Write faces
  WriteIfsString(fp, "TRIANGLES");
  fwrite(&nfaces, sizeof(unsigned int), 1, fp);
  for (int i = 0; i < NFaces(); i++) {
    R3MeshFace *face = Face(i);
    R3MeshVertex *v0 = VertexOnFace(face, 0);
    R3MeshVertex *v1 = VertexOnFace(face, 1);
    R3MeshVertex *v2 = VertexOnFace(face, 2);
    unsigned int v[3];
    v[0] = v0->id;
    v[1] = v1->id;
    v[2] = v2->id;
    fwrite(&v, sizeof(unsigned int), 3, fp);
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq 0x88(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movss 0xfe00c(%rip), %xmm0    # 0x133268
movss %xmm0, 0x7c(%rsp)
callq 0x135e0
movq 0x8(%rsp), %rdi
movl %eax, 0x78(%rsp)
callq 0x135a0
movl %eax, 0x74(%rsp)
movq 0x80(%rsp), %rdi
leaq 0xfe832(%rip), %rsi     # 0x133aba
callq 0xf5e0
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
jne 0x352be
movq 0x80(%rsp), %rsi
leaq 0xfe139(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0x354d4
movq 0x68(%rsp), %rdi
leaq 0xfe4b0(%rip), %rsi     # 0x13377a
callq 0x354f0
movq 0x68(%rsp), %rcx
leaq 0x7c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xf980
movq 0x68(%rsp), %rdi
leaq 0xfe7c9(%rip), %rsi     # 0x133abd
callq 0x354f0
movq 0x68(%rsp), %rdi
leaq 0xfe52b(%rip), %rsi     # 0x133830
callq 0x354f0
movq 0x68(%rsp), %rcx
leaq 0x78(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xf980
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x78(%rsp), %eax
jae 0x353c5
movq 0x8(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x13630
movq 0x8(%rsp), %rdi
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rsi
callq 0x13660
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x37820
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x44(%rsp)
movq 0x50(%rsp), %rdi
callq 0x37870
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x48(%rsp)
movq 0x50(%rsp), %rdi
callq 0x378c0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4c(%rsp)
movq 0x68(%rsp), %rcx
leaq 0x44(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
callq 0xf980
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x3532b
movq 0x68(%rsp), %rdi
leaq 0xfe4e8(%rip), %rsi     # 0x1338b9
callq 0x354f0
movq 0x68(%rsp), %rcx
leaq 0x74(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xf980
movl $0x0, 0x40(%rsp)
movq 0x8(%rsp), %rdi
movl 0x40(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x354bf
movq 0x8(%rsp), %rdi
movl 0x40(%rsp), %esi
callq 0x136d0
movq 0x8(%rsp), %rdi
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq 0x8(%rsp), %rdi
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x13700
movq 0x8(%rsp), %rdi
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rsi
movl $0x2, %edx
callq 0x13700
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movl 0x70(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
movl 0x70(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movl 0x70(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x68(%rsp), %rcx
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
callq 0xf980
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x353f7
movq 0x68(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)