// File: /workspace/github_cpp_makefile_star2_O3/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 0x84bc3(%rip), %rsi     # 0xa77b9
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x22cda
movq %rax, %r14
movslq 0x14(%rbx), %rax
testq %rax, %rax
jle 0x22cf2
movq 0x8(%rbx), %rcx
movq (%rcx), %rdx
movsd 0x48(%rdx), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movb $0x1, %bpl
jne 0x22c75
jp 0x22c75
movsd 0x50(%rdx), %xmm1
ucomisd %xmm0, %xmm1
jne 0x22c75
jp 0x22c75
movl $0x1, %esi
movq %rsi, %rdx
cmpq %rsi, %rax
je 0x22c6e
movq (%rcx,%rdx,8), %rsi
movsd 0x48(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x22c6e
jp 0x22c6e
movsd 0x50(%rsi), %xmm1
leaq 0x1(%rdx), %rsi
ucomisd %xmm0, %xmm1
jne 0x22c6e
jnp 0x22c44
cmpq %rax, %rdx
setb %bpl
testl %eax, %eax
jle 0x22cf4
xorl %r13d, %r13d
leaq 0x7fd7d(%rip), %r12     # 0xa2a00
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r15
testb %bpl, %bpl
je 0x22cad
movsd 0x48(%r15), %xmm0
movsd 0x50(%r15), %xmm1
movq %r14, %rdi
leaq 0x7fd50(%rip), %rsi     # 0xa29f6
movb $0x2, %al
callq 0xe670
movsd 0x18(%r15), %xmm0
movsd 0x20(%r15), %xmm1
movsd 0x28(%r15), %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0xe670
incq %r13
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jl 0x22c83
jmp 0x22cf4
leaq 0x7f6d9(%rip), %rdi     # 0xa23ba
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x22d7c
xorl %ebp, %ebp
cmpl $0x0, 0x34(%rbx)
jle 0x22d6f
leaq 0x7fd0b(%rip), %r15     # 0xa2a0c
leaq 0x7fd19(%rip), %r12     # 0xa2a21
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 0x22d53
movq %r15, %rsi
movl %edx, %ecx
movl %r8d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r10
callq 0xe670
addq $0x10, %rsp
jmp 0x22d63
movq %r12, %rsi
movl %r8d, %ecx
movl %r10d, %r8d
xorl %eax, %eax
callq 0xe670
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x22d0b
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