// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movl $0x3f800000, 0x1c(%rsp) # imm = 0x3F800000
movl 0x14(%rdi), %eax
movl %eax, 0x18(%rsp)
movl 0x34(%rdi), %eax
movl %eax, 0x14(%rsp)
leaq 0x7fa8a(%rip), %rsi     # 0xa2a81
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x2314a
movq %rax, %r14
leaq 0x7f737(%rip), %rsi     # 0xa2749
movq %rax, %rdi
callq 0x2316c
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe8b0
leaq 0x7fa4c(%rip), %rsi     # 0xa2a84
movq %r14, %rdi
callq 0x2316c
leaq 0x7f7b8(%rip), %rsi     # 0xa27ff
movq %r14, %rdi
callq 0x2316c
leaq 0x18(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe8b0
movl (%r15), %ebp
testl %ebp, %ebp
je 0x230bd
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x8(%rbx), %rax
movslq %r12d, %r12
movq (%rax,%r12,8), %rax
movupd 0x18(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsd2ss 0x28(%rax), %xmm0
movss %xmm0, 0x10(%rsp)
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe8b0
incl %r12d
cmpl %r12d, %ebp
jne 0x23078
leaq 0x7f7c4(%rip), %rsi     # 0xa2888
movq %r14, %rdi
callq 0x2316c
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0xe8b0
cmpl $0x0, 0x34(%rbx)
jle 0x2313b
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rax
movl 0x70(%rcx), %ecx
movl %ecx, 0x8(%rsp)
movl 0x70(%rdx), %ecx
movl %ecx, 0xc(%rsp)
movl 0x70(%rax), %eax
movl %eax, 0x10(%rsp)
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe8b0
incq %r12
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jl 0x230f1
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x2315d
leaq 0x7f269(%rip), %rdi     # 0xa23ba
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq