// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::WriteSTLFile(char const*) const
int R3Mesh::
WriteSTLFile(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, "solid %s\n", filename);

  // Write Faces
  for (int i = 0; i < NFaces(); i++) {
    R3MeshFace *face = Face(i);
    const R3Vector& n = FaceNormal(face);
    fprintf(fp, "facet normal %g %g %g\n", n[0], n[1], n[2]);
    fprintf(fp, "  outer loop\n");
    for (int j = 0; j < 3; j++) {
      R3MeshVertex *vertex = VertexOnFace(face, j);
      const R3Point& p = VertexPosition(vertex);
      fprintf(fp, "   vertex %g %g %g\n", p[0], p[1], p[2]);
    }
    fprintf(fp, "  endloop\n");
    fprintf(fp, "endfacet\n");
  }

  // Write trailer
  fprintf(fp, "endsolid\n");

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x80(%rsp), %rdi
leaq 0xfed20(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
jne 0x35609
movq 0x80(%rsp), %rsi
leaq 0xfddee(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0x357f7
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rdx
leaq 0xfe4a8(%rip), %rsi     # 0x133ac5
movb $0x0, %al
callq 0xf730
movl $0x0, 0x74(%rsp)
movq 0x40(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, 0x3c(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x357cf
movq 0x40(%rsp), %rdi
movl 0x74(%rsp), %esi
callq 0x136d0
movq 0x40(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
callq 0x13840
movq %rax, 0x60(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x3a4b0
movsd %xmm0, 0x30(%rsp)
movq 0x60(%rsp), %rdi
movl $0x1, %esi
callq 0x3a4b0
movsd %xmm0, 0x28(%rsp)
movq 0x60(%rsp), %rdi
movl $0x2, %esi
callq 0x3a4b0
movq 0x20(%rsp), %rdi
movsd 0x28(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x30(%rsp), %xmm0
leaq 0xfe401(%rip), %rsi     # 0x133acf
movb $0x3, %al
callq 0xf730
movq 0x78(%rsp), %rdi
leaq 0xfe405(%rip), %rsi     # 0x133ae6
movb $0x0, %al
callq 0xf730
movl $0x0, 0x5c(%rsp)
cmpl $0x3, 0x5c(%rsp)
jge 0x35799
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x13700
movq 0x40(%rsp), %rdi
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rsi
callq 0x13660
movq %rax, 0x48(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x13af0
movsd %xmm0, 0x18(%rsp)
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x13af0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rdi
movl $0x2, %esi
callq 0x13af0
movq 0x8(%rsp), %rdi
movsd 0x10(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x18(%rsp), %xmm0
leaq 0xfe372(%rip), %rsi     # 0x133af4
movb $0x3, %al
callq 0xf730
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x356f0
movq 0x78(%rsp), %rdi
leaq 0xfe363(%rip), %rsi     # 0x133b08
movb $0x0, %al
callq 0xf730
movq 0x78(%rsp), %rdi
leaq 0xfe35b(%rip), %rsi     # 0x133b13
movb $0x0, %al
callq 0xf730
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x3562c
movq 0x78(%rsp), %rdi
leaq 0xfe342(%rip), %rsi     # 0x133b1d
movb $0x0, %al
callq 0xf730
movq 0x78(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)