// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movl $0x3f800000, 0x14(%rsp) # imm = 0x3F800000
movl 0x14(%rdi), %eax
movl %eax, 0x10(%rsp)
movl 0x34(%rdi), %eax
movl %eax, 0xc(%rsp)
leaq 0x73266(%rip), %rsi     # 0x93a31
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x2090b
movq %rax, %r14
leaq 0x72f13(%rip), %rsi     # 0x936f9
movq %rax, %rdi
callq 0x2092f
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %r15
pushq $0x1
popq %r12
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe8e0
leaq 0x73224(%rip), %rsi     # 0x93a34
movq %r14, %rdi
callq 0x2092f
leaq 0x72f90(%rip), %rsi     # 0x937af
movq %r14, %rdi
callq 0x2092f
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe8e0
xorl %ebp, %ebp
movl (%r13), %r15d
movq %rsp, %r12
pushq $0x3
popq %r13
cmpl %r15d, %ebp
jae 0x20888
movq 0x8(%rbx), %rax
movslq %ebp, %rcx
movq (%rax,%rcx,8), %rax
movupd 0x18(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, (%rsp)
cvtsd2ss 0x28(%rax), %xmm0
movss %xmm0, 0x8(%rsp)
movq %r12, %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0xe8e0
incl %ebp
jmp 0x2084a
leaq 0x72fa9(%rip), %rsi     # 0x93838
movq %r14, %rdi
callq 0x2092f
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %r15
pushq $0x1
popq %rdx
movq %r15, %rsi
movq %r14, %rcx
callq 0xe8e0
xorl %ebp, %ebp
movq %rsp, %r12
pushq $0x3
popq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %rbp
jge 0x208fe
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rax
movl 0x70(%rcx), %ecx
movl %ecx, (%rsp)
movl 0x70(%rdx), %ecx
movl %ecx, 0x4(%rsp)
movl 0x70(%rax), %eax
movl %eax, 0x8(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0xe8e0
incq %rbp
jmp 0x208b7
movq %r14, %rdi
callq 0xe440
pushq $0x1
popq %rbx
jmp 0x2091e
leaq 0x72a58(%rip), %rdi     # 0x9336a
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq