// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::WriteOffFile(char const*) const
int R3Mesh::
WriteOffFile(const char *filename) const
{
  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "w"))) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Write header
  fprintf(fp, "OFF\n");
  fprintf(fp, "%d %d %d\n", NVertices(), NFaces(), NEdges());

  // Write vertices
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R3Point& p = VertexPosition(vertex);
    fprintf(fp, "%g %g %g\n", p.X(), p.Y(), p.Z());
  }

  // Write Faces
  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);
    fprintf(fp, "3 %d %d %d\n", v0->id, v1->id, v2->id);
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x90(%rsp), %rdi
leaq 0xff600(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0x88(%rsp)
cmpq $0x0, %rax
jne 0x34d2c
movq 0x90(%rsp), %rsi
leaq 0xfe6cb(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0xa4(%rsp)
jmp 0x34f34
movq 0x88(%rsp), %rdi
leaq 0xfed2b(%rip), %rsi     # 0x133a66
movb $0x0, %al
callq 0xf730
movq 0x40(%rsp), %rdi
movq 0x88(%rsp), %rax
movq %rax, 0x30(%rsp)
callq 0x135e0
movq 0x40(%rsp), %rdi
movl %eax, 0x38(%rsp)
callq 0x135a0
movq 0x40(%rsp), %rdi
movl %eax, 0x3c(%rsp)
callq 0x135c0
movq 0x30(%rsp), %rdi
movl 0x38(%rsp), %edx
movl 0x3c(%rsp), %ecx
movl %eax, %r8d
leaq 0xfece1(%rip), %rsi     # 0x133a6d
movb $0x0, %al
callq 0xf730
movl $0x0, 0x84(%rsp)
movq 0x40(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x34e5a
movq 0x40(%rsp), %rdi
movl 0x84(%rsp), %esi
callq 0x13630
movq 0x40(%rsp), %rdi
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rsi
callq 0x13660
movq %rax, 0x70(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x70(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x20(%rsp)
movq 0x70(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x18(%rsp)
movq 0x70(%rsp), %rdi
callq 0x378c0
movq 0x10(%rsp), %rdi
movsd 0x18(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x20(%rsp), %xmm0
leaq 0xfec49(%rip), %rsi     # 0x133a86
movb $0x3, %al
callq 0xf730
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x34d9e
movl $0x0, 0x6c(%rsp)
movq 0x40(%rsp), %rdi
movl 0x6c(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x34f1c
movq 0x40(%rsp), %rdi
movl 0x6c(%rsp), %esi
callq 0x136d0
movq 0x40(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq 0x40(%rsp), %rdi
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rsi
movl $0x1, %edx
callq 0x13700
movq 0x40(%rsp), %rdi
movq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rsi
movl $0x2, %edx
callq 0x13700
movq %rax, 0x48(%rsp)
movq 0x88(%rsp), %rdi
movq 0x58(%rsp), %rax
movl 0x70(%rax), %edx
movq 0x50(%rsp), %rax
movl 0x70(%rax), %ecx
movq 0x48(%rsp), %rax
movl 0x70(%rax), %r8d
leaq 0xfeb66(%rip), %rsi     # 0x133a6b
movb $0x0, %al
callq 0xf730
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x34e62
movq 0x88(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)