// File: /workspace/github_cpp_makefile_star2_O2/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 0x21440
pushq $0x2f
popq %rsi
movq %r15, %rdi
callq 0x219c0
testq %rax, %rax
jne 0x3127b
leaq 0x410(%rsp), %rdi
pushq $0x5c
popq %rsi
callq 0x219c0
testq %rax, %rax
je 0x313d6
movb $0x0, (%rax)
pushq $0x2f
popq %rsi
movq %r12, %rdi
callq 0x219c0
leaq 0x1(%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
leaq 0x10(%rsp), %r13
movl $0x3ff, %edx            # imm = 0x3FF
movq %r13, %rdi
callq 0x21440
movq %r13, %rdi
callq 0x21370
cmpl $0x5, %eax
jl 0x312bb
addl $-0x4, %eax
movb $0x0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x21370
movl $0x400, %edx            # imm = 0x400
subq %rax, %rdx
leaq 0x944b9(%rip), %rsi     # 0xc5790
movq %r13, %rdi
callq 0x21610
testq %r15, %r15
je 0x31308
leaq 0x944aa(%rip), %rsi     # 0xc5795
leaq 0x810(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x21140
movq %r13, %rdi
callq 0x21770
leaq 0x97ab5(%rip), %rsi     # 0xc8dc4
movq %r12, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x31392
movq %rax, %r13
leaq 0x9447b(%rip), %rsi     # 0xc57a1
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21da0
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x307e2
testl %eax, %eax
je 0x313a7
leaq 0x810(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0xc(%rsp), %r8
andl $0x0, (%r8)
leaq 0x8(%rsp), %r9
andl $0x0, (%r9)
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
leaq 0x58(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
pushq %r13
pushq %rax
callq 0x30c6e
popq %rcx
popq %rdx
movq %r13, %rdi
callq 0x218c0
pushq $0x1
popq %rbp
jmp 0x313c2
leaq 0x9428d(%rip), %rdi     # 0xc5626
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0x7e83e
jmp 0x313c2
leaq 0x943fe(%rip), %rdi     # 0xc57ac
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x7e83e
movq %r13, %rdi
callq 0x218c0
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 0x3127e