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

  // Check if have texcoords
  RNBoolean has_texcoords = FALSE;
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R2Point& t = VertexTextureCoords(vertex);
    if (!t.IsZero()) { has_texcoords = TRUE; break; }
  }

  // Write vertices
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R3Point& p = VertexPosition(vertex);
    const R2Point& t = VertexTextureCoords(vertex);
    if (has_texcoords) fprintf(fp, "vt %g %g\n", t.X(), t.Y());
    fprintf(fp, "v %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);
    if (has_texcoords) {
      fprintf(fp, "f %d/%d %d/%d %d/%d\n",
        v0->id + 1, v0->id + 1,
        v1->id + 1, v1->id + 1,
        v2->id + 1, v2->id + 1);
    }
    else {
      fprintf(fp, "f %d %d %d\n",
        v0->id + 1, v1->id + 1, v2->id + 1);
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8491b(%rip), %rsi     # 0xa77bd
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x22f0f
movq %rax, %r14
movslq 0x14(%rbx), %rax
testq %rax, %rax
jle 0x22f27
movq 0x8(%rbx), %rcx
decq %rax
xorl %edx, %edx
xorpd %xmm0, %xmm0
movl $0x1, %esi
xorl %edi, %edi
movq (%rcx,%rdx,8), %r8
movsd 0x50(%r8), %xmm1
movsd 0x48(%r8), %xmm2
cmpneqpd %xmm0, %xmm2
cmpneqpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movd %xmm1, %r8d
testb $0x1, %r8b
cmovnel %esi, %edi
jne 0x22f07
leaq 0x1(%rdx), %r8
cmpq %rdx, %rax
movq %r8, %rdx
jne 0x22ecf
testl %edi, %edi
sete %bpl
jmp 0x22f2a
leaq 0x7f4a7(%rip), %rdi     # 0xa23bd
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x2301c
movb $0x1, %bpl
cmpl $0x0, 0x14(%rbx)
jle 0x22f94
xorl %r13d, %r13d
leaq 0x7fac9(%rip), %r15     # 0xa2a03
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r12
testb %bpl, %bpl
jne 0x22f66
movsd 0x48(%r12), %xmm0
movsd 0x50(%r12), %xmm1
movq %r14, %rdi
leaq 0x7fa9a(%rip), %rsi     # 0xa29f9
movb $0x2, %al
callq 0xe670
movsd 0x18(%r12), %xmm0
movsd 0x20(%r12), %xmm1
movsd 0x28(%r12), %xmm2
movq %r14, %rdi
movq %r15, %rsi
movb $0x3, %al
callq 0xe670
incq %r13
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jl 0x22f3a
cmpl $0x0, 0x34(%rbx)
jle 0x2300f
leaq 0x7fa83(%rip), %r15     # 0xa2a24
leaq 0x7fa67(%rip), %r12     # 0xa2a0f
xorl %r13d, %r13d
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rax
movl 0x70(%rcx), %edx
incl %edx
movl 0x70(%rsi), %r8d
incl %r8d
movl 0x70(%rax), %r10d
incl %r10d
movq %r14, %rdi
testb %bpl, %bpl
je 0x22fec
movq %r15, %rsi
movl %r8d, %ecx
movl %r10d, %r8d
xorl %eax, %eax
callq 0xe670
jmp 0x23003
movq %r12, %rsi
movl %edx, %ecx
movl %r8d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r10
callq 0xe670
addq $0x10, %rsp
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x22fab
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop