// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::WriteObj(gaps::R3SceneNode*, char const*) const
int R3Scene::
WriteObj(R3SceneNode *node, const char *filename) const
{
  // Determine directory name (for texture image files)
  char *dirname = NULL;
  char buffer[1024];
  strncpy(buffer, filename, 1023);
  char *endp = strrchr(buffer, '/');
  if (!endp) endp = strrchr(buffer, '\\');
  if (!endp) strcpy(buffer, ".");
  else { *endp = '\0'; dirname = buffer; }

  // Determine material filename
  char mtl_filename[1024];
  const char *startp = strrchr(filename, '/');
  startp = (startp) ? startp+1 : filename;
  strncpy(mtl_filename, startp, 1023);
  int slen = strlen(mtl_filename);
  if (slen > 4) mtl_filename[slen-4] = '\0';
  strncat(mtl_filename, ".mtl", 1024-strlen(mtl_filename));

  // Create directory
  if (dirname) {
    char mkdir_cmd[1064];
    sprintf(mkdir_cmd, "mkdir -p %s", dirname);
    system(mkdir_cmd);
  }

  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Write material file
  fprintf(fp, "mtllib %s\n", mtl_filename);
  if (!WriteObjMtlFile(dirname, mtl_filename)) {
    RNFail("Unable to write OBJ material file %s\n", mtl_filename);
    fclose(fp);
    return 0;
  }

  // Write nodes 
  int ngroups = 0;
  int nvertices = 0;
  int nnormals = 0;
  int ntexture_coords = 0;
  if (!WriteObj(node, node->Transformation(), ngroups, nvertices, nnormals, ntexture_coords, fp)) {
    RNFail("Unable to write OBJ file %s\n", filename);
    fclose(fp);
    return 0;
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc38, %rsp            # imm = 0xC38
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x410(%rsp), %r15
movl $0x3ff, %edx            # imm = 0x3FF
movq %r15, %rdi
movq %r12, %rsi
callq 0x21430
movq %r15, %rdi
movl $0x2f, %esi
callq 0x219b0
testq %rax, %rax
jne 0x33599
leaq 0x410(%rsp), %rdi
movl $0x5c, %esi
callq 0x219b0
testq %rax, %rax
je 0x336f6
movb $0x0, (%rax)
movq %r12, %rdi
movl $0x2f, %esi
callq 0x219b0
leaq 0x1(%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
leaq 0x10(%rsp), %r13
movl $0x3ff, %edx            # imm = 0x3FF
movq %r13, %rdi
callq 0x21430
movq %r13, %rdi
callq 0x21380
cmpl $0x5, %eax
jl 0x335db
addl $-0x4, %eax
movb $0x0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x21380
movl $0x400, %edx            # imm = 0x400
subq %rax, %rdx
leaq 0xa71c9(%rip), %rsi     # 0xda7c0
movq %r13, %rdi
callq 0x21610
testq %r15, %r15
je 0x33628
leaq 0xa71ba(%rip), %rsi     # 0xda7c5
leaq 0x810(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x21140
movq %r13, %rdi
callq 0x21770
leaq 0xaa80d(%rip), %rsi     # 0xdde3c
movq %r12, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x336b2
movq %rax, %r13
leaq 0xa718b(%rip), %rsi     # 0xda7d1
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d80
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x32a7a
testl %eax, %eax
je 0x336c7
leaq 0x810(%rsp), %rcx
movl %ebp, (%rcx)
leaq 0xc(%rsp), %r8
movl %ebp, (%r8)
leaq 0x8(%rsp), %r9
movl %ebp, (%r9)
leaq 0x4(%rsp), %rax
movl %ebp, (%rax)
leaq 0x58(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
pushq %r13
pushq %rax
callq 0x32f14
addq $0x10, %rsp
movq %r13, %rdi
callq 0x218b0
movl $0x1, %ebp
jmp 0x336e2
leaq 0xa6f9d(%rip), %rdi     # 0xda656
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0x8a0fe
jmp 0x336e2
leaq 0xa710e(%rip), %rdi     # 0xda7dc
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x8a0fe
movq %r13, %rdi
callq 0x218b0
movl %ebp, %eax
addq $0xc38, %rsp            # imm = 0xC38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movw $0x2e, 0x410(%rsp)
xorl %r15d, %r15d
jmp 0x3359c