// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::WritePrincetonNode(gaps::R3Scene const*, gaps::R3SceneNode const*, gaps::R3Transformation const&, _IO_FILE*, char const*)
static int
WritePrincetonNode(const R3Scene *scene, const R3SceneNode *node,
  const R3Transformation& parent_transformation,
  FILE *fp, const char *indent)
{
  // Compute indent
  int nindent = strlen(indent);
  char *child_indent = new char [nindent + 3];
  strcpy(child_indent, indent);
  child_indent[nindent] = ' ';
  child_indent[nindent+1] = ' ';
  child_indent[nindent+2] = '\0';

  // Compute transformation
  R3Affine transformation = R3identity_affine;
  transformation.Transform(parent_transformation);
  transformation.Transform(node->Transformation());

  // Don't write root separately, unless transformed
  RNBoolean print_node = (node != scene->Root()) || (!node->Transformation().IsIdentity());
  if (print_node) {
    // Write group start
    fprintf(fp, "\n");
    if (!node->Name()) fprintf(fp, "%sbegin -1\n", indent);
    else fprintf(fp, "%sgroup %s -1\n", indent, node->Name());

    // Write node transformation
    const R4Matrix& m = node->Transformation().Matrix();
    fprintf(fp, "%s%g %g %g %g\n", child_indent, m[0][0], m[0][1], m[0][2], m[0][3]);
    fprintf(fp, "%s%g %g %g %g\n", child_indent, m[1][0], m[1][1], m[1][2], m[1][3]);
    fprintf(fp, "%s%g %g %g %g\n", child_indent, m[2][0], m[2][1], m[2][2], m[2][3]);
    fprintf(fp, "%s%g %g %g %g\n", child_indent, m[3][0], m[3][1], m[3][2], m[3][3]);
  }
  
  // Write elements
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *element = node->Element(i);
    if (!WritePrincetonElement(scene, element, transformation, fp, child_indent)) return 0;
  }

  // Write references
  for (int i = 0; i < node->NReferences(); i++) {
    R3SceneReference *reference = node->Reference(i);
    if (!WritePrincetonReference(scene, reference, transformation, fp, child_indent)) return 0;
  }

  // Write children
  for (int i = 0; i < node->NChildren(); i++) {
    R3SceneNode *child = node->Child(i);
    if (!WritePrincetonNode(scene, child, transformation, fp, child_indent)) return 0;
  }

  // Write end
  if (print_node) fprintf(fp, "%send\n", indent);

  // Delete child indent buffer
  delete [] child_indent;

  // Return success
  return 1;
}

// Assembly:
subq $0x338, %rsp            # imm = 0x338
movq %rdi, 0x328(%rsp)
movq %rsi, 0x320(%rsp)
movq %rdx, 0x318(%rsp)
movq %rcx, 0x310(%rsp)
movq %r8, 0x308(%rsp)
movq 0x308(%rsp), %rdi
callq 0x223d0
movl %eax, 0x304(%rsp)
movl 0x304(%rsp), %eax
addl $0x3, %eax
movslq %eax, %rdi
callq 0x22040
movq %rax, 0x2f8(%rsp)
movq 0x2f8(%rsp), %rdi
movq 0x308(%rsp), %rsi
callq 0x22900
movq 0x2f8(%rsp), %rax
movslq 0x304(%rsp), %rcx
movb $0x20, (%rax,%rcx)
movq 0x2f8(%rsp), %rcx
movslq 0x304(%rsp), %rax
movb $0x20, 0x1(%rax,%rcx)
movq 0x2f8(%rsp), %rcx
movslq 0x304(%rsp), %rax
movb $0x0, 0x2(%rax,%rcx)
leaq 0x1c0bc9(%rip), %rsi    # 0x1fe9d0
leaq 0x1e8(%rsp), %rdi
movq %rdi, 0x198(%rsp)
callq 0x48c80
movq 0x198(%rsp), %rdi
movq 0x318(%rsp), %rsi
callq 0xc27f0
jmp 0x3de33
movq 0x320(%rsp), %rdi
callq 0x2bae0
movq %rax, %rsi
leaq 0x1e8(%rsp), %rdi
callq 0xc1dd0
jmp 0x3de52
movq 0x320(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x328(%rsp), %rdi
callq 0x2b510
movq %rax, 0x190(%rsp)
jmp 0x3de79
movq 0x188(%rsp), %rcx
movq 0x190(%rsp), %rdx
movb $0x1, %al
cmpq %rdx, %rcx
movb %al, 0x187(%rsp)
jne 0x3decf
movq 0x320(%rsp), %rdi
callq 0x2bae0
movq %rax, %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movl %eax, 0x180(%rsp)
jmp 0x3deb9
movl 0x180(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x187(%rsp)
movb 0x187(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x1d8(%rsp)
cmpl $0x0, 0x1d8(%rsp)
je 0x3e402
movq 0x310(%rsp), %rdi
leaq 0x157743(%rip), %rsi    # 0x195642
movb $0x0, %al
callq 0x22f30
movq 0x320(%rsp), %rdi
callq 0x2b800
cmpq $0x0, %rax
jne 0x3df5f
movq 0x310(%rsp), %rdi
movq 0x308(%rsp), %rdx
leaq 0x151928(%rip), %rsi    # 0x18f858
movb $0x0, %al
callq 0x22f30
jmp 0x3dfad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0xc1b00
jmp 0x3e6d4
movq 0x310(%rsp), %rax
movq %rax, 0x170(%rsp)
movq 0x308(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x320(%rsp), %rdi
callq 0x2b800
movq 0x170(%rsp), %rdi
movq 0x178(%rsp), %rdx
movq %rax, %rcx
leaq 0x1518be(%rip), %rsi    # 0x18f864
movb $0x0, %al
callq 0x22f30
movq 0x320(%rsp), %rdi
callq 0x2bae0
movq %rax, %rdi
callq 0x4e7f0
movq %rax, 0x168(%rsp)
jmp 0x3dfcc
movq 0x168(%rsp), %rax
movq %rax, 0x1d0(%rsp)
movq 0x310(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x2f8(%rsp), %rax
movq %rax, 0x158(%rsp)
movq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x4e800
movq %rax, 0x160(%rsp)
jmp 0x3e015
movq 0x160(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x140(%rsp)
movq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x4e800
movq %rax, 0x148(%rsp)
jmp 0x3e043
movq 0x148(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x130(%rsp)
movq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x4e800
movq %rax, 0x138(%rsp)
jmp 0x3e072
movq 0x138(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x120(%rsp)
movq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x4e800
movq %rax, 0x128(%rsp)
jmp 0x3e0a1
movsd 0x120(%rsp), %xmm2
movsd 0x130(%rsp), %xmm1
movsd 0x140(%rsp), %xmm0
movq 0x158(%rsp), %rdx
movq 0x150(%rsp), %rdi
movq 0x128(%rsp), %rax
movsd 0x18(%rax), %xmm3
leaq 0x151793(%rip), %rsi    # 0x18f873
movb $0x4, %al
callq 0x22f30
movq 0x310(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x2f8(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x1, %esi
callq 0x4e800
movq %rax, 0x118(%rsp)
jmp 0x3e123
movq 0x118(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xf8(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x1, %esi
callq 0x4e800
movq %rax, 0x100(%rsp)
jmp 0x3e154
movq 0x100(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x1, %esi
callq 0x4e800
movq %rax, 0xf0(%rsp)
jmp 0x3e186
movq 0xf0(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0xd8(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x1, %esi
callq 0x4e800
movq %rax, 0xe0(%rsp)
jmp 0x3e1b8
movsd 0xd8(%rsp), %xmm2
movsd 0xe8(%rsp), %xmm1
movsd 0xf8(%rsp), %xmm0
movq 0x110(%rsp), %rdx
movq 0x108(%rsp), %rdi
movq 0xe0(%rsp), %rax
movsd 0x18(%rax), %xmm3
leaq 0x15167c(%rip), %rsi    # 0x18f873
movb $0x4, %al
callq 0x22f30
movq 0x310(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x2f8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x2, %esi
callq 0x4e800
movq %rax, 0xd0(%rsp)
jmp 0x3e23a
movq 0xd0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x2, %esi
callq 0x4e800
movq %rax, 0xb8(%rsp)
jmp 0x3e26b
movq 0xb8(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x2, %esi
callq 0x4e800
movq %rax, 0xa8(%rsp)
jmp 0x3e29d
movq 0xa8(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x2, %esi
callq 0x4e800
movq %rax, 0x98(%rsp)
jmp 0x3e2cf
movsd 0x90(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm1
movsd 0xb0(%rsp), %xmm0
movq 0xc8(%rsp), %rdx
movq 0xc0(%rsp), %rdi
movq 0x98(%rsp), %rax
movsd 0x18(%rax), %xmm3
leaq 0x151565(%rip), %rsi    # 0x18f873
movb $0x4, %al
callq 0x22f30
movq 0x310(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x2f8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x3, %esi
callq 0x4e800
movq %rax, 0x88(%rsp)
jmp 0x3e34e
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x3, %esi
callq 0x4e800
movq %rax, 0x70(%rsp)
jmp 0x3e379
movq 0x70(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x3, %esi
callq 0x4e800
movq %rax, 0x60(%rsp)
jmp 0x3e3a2
movq 0x60(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x1d0(%rsp), %rdi
movl $0x3, %esi
callq 0x4e800
movq %rax, 0x50(%rsp)
jmp 0x3e3cb
movsd 0x48(%rsp), %xmm2
movsd 0x58(%rsp), %xmm1
movsd 0x68(%rsp), %xmm0
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rdi
movq 0x50(%rsp), %rax
movsd 0x18(%rax), %xmm3
leaq 0x151478(%rip), %rsi    # 0x18f873
movb $0x4, %al
callq 0x22f30
movl $0x0, 0x1cc(%rsp)
movl 0x1cc(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x320(%rsp), %rdi
callq 0x2bba0
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x3e4ca
movq 0x320(%rsp), %rdi
movl 0x1cc(%rsp), %esi
callq 0x2bbc0
movq %rax, 0x38(%rsp)
jmp 0x3e44e
movq 0x38(%rsp), %rax
movq %rax, 0x1c0(%rsp)
movq 0x328(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
movq 0x310(%rsp), %rcx
movq 0x2f8(%rsp), %r8
leaq 0x1e8(%rsp), %rdx
callq 0x46b60
movl %eax, 0x34(%rsp)
jmp 0x3e48e
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
jne 0x3e4b2
movl $0x0, 0x334(%rsp)
movl $0x1, 0x1bc(%rsp)
jmp 0x3e6b8
jmp 0x3e4b4
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0x3e40d
movl $0x0, 0x1b8(%rsp)
movl 0x1b8(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x320(%rsp), %rdi
callq 0x2bb40
movl %eax, 0x30(%rsp)
jmp 0x3e4f3
movl 0x2c(%rsp), %eax
movl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3e59a
movq 0x320(%rsp), %rdi
movl 0x1b8(%rsp), %esi
callq 0x2bb60
movq %rax, 0x20(%rsp)
jmp 0x3e51e
movq 0x20(%rsp), %rax
movq %rax, 0x1b0(%rsp)
movq 0x328(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
movq 0x310(%rsp), %rcx
movq 0x2f8(%rsp), %r8
leaq 0x1e8(%rsp), %rdx
callq 0x47fb0
movl %eax, 0x1c(%rsp)
jmp 0x3e55e
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jne 0x3e582
movl $0x0, 0x334(%rsp)
movl $0x1, 0x1bc(%rsp)
jmp 0x3e6b8
jmp 0x3e584
movl 0x1b8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1b8(%rsp)
jmp 0x3e4d5
movl $0x0, 0x1ac(%rsp)
movl 0x1ac(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x320(%rsp), %rdi
callq 0x2baf0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x3e65f
movq 0x320(%rsp), %rdi
movl 0x1ac(%rsp), %esi
callq 0x2bb10
movq %rax, 0x10(%rsp)
jmp 0x3e5e6
movq 0x10(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x328(%rsp), %rdi
movq 0x1a0(%rsp), %rsi
movq 0x310(%rsp), %rcx
movq 0x2f8(%rsp), %r8
leaq 0x1e8(%rsp), %rdx
callq 0x3dd50
movl %eax, 0xc(%rsp)
jmp 0x3e626
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x3e647
movl $0x0, 0x334(%rsp)
movl $0x1, 0x1bc(%rsp)
jmp 0x3e6b8
jmp 0x3e649
movl 0x1ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ac(%rsp)
jmp 0x3e5a5
cmpl $0x0, 0x1d8(%rsp)
je 0x3e687
movq 0x310(%rsp), %rdi
movq 0x308(%rsp), %rdx
leaq 0x151202(%rip), %rsi    # 0x18f882
movb $0x0, %al
callq 0x22f30
movq 0x2f8(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x3e6a2
movq (%rsp), %rdi
callq 0x22da0
movl $0x1, 0x334(%rsp)
movl $0x1, 0x1bc(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0xc1b00
movl 0x334(%rsp), %eax
addq $0x338, %rsp            # imm = 0x338
retq
movq 0x1e0(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)