// File: /workspace/github_cpp_makefile_star2_O1/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 0x338f4
leaq 0x410(%rsp), %rdi
movl $0x5c, %esi
callq 0x219b0
testq %rax, %rax
je 0x338fe
movb $0x0, (%rax)
jmp 0x3390b
movw $0x2e, 0x410(%rsp)
xorl %r15d, %r15d
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 0x3394a
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 0xa8e4a(%rip), %rsi     # 0xdc7b0
movq %r13, %rdi
callq 0x21610
testq %r15, %r15
je 0x33997
leaq 0xa8e3b(%rip), %rsi     # 0xdc7b5
leaq 0x810(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x21140
movq %r13, %rdi
callq 0x21770
leaq 0xac47e(%rip), %rsi     # 0xdfe1c
movq %r12, %rdi
callq 0x21ac0
testq %rax, %rax
je 0x33a21
movq %rax, %r13
leaq 0xa8e0c(%rip), %rsi     # 0xdc7c1
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d90
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x32dc2
testl %eax, %eax
je 0x33a36
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 0x3322e
addq $0x10, %rsp
movq %r13, %rdi
callq 0x218b0
movl $0x1, %ebp
jmp 0x33a51
leaq 0xa8c1e(%rip), %rdi     # 0xdc646
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0x8b53a
jmp 0x33a51
leaq 0xa8d8f(%rip), %rdi     # 0xdc7cc
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x8b53a
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