// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/scn2scn/scn2scn.cpp
// Function: RemoveNodes(gaps::R3Scene*, gaps::R3SceneNode*, gaps::R3SceneNode*, gaps::R3Grid*, gaps::R3Box const&)
static void
RemoveNodes(R3Scene *scene, R3SceneNode *node,
  R3SceneNode *select_subtree_node, R3Grid *select_grid, const R3Box& select_bbox)
{
  // Copy array of children -- because will be edited as iterate
  RNArray<R3SceneNode *> children;
  for (int i = 0; i < node->NChildren(); i++) {
    R3SceneNode *child = node->Child(i);
    children.Insert(child);
  }

  // Visit children recursively 
  for (int i = 0; i < children.NEntries(); i++) {
    R3SceneNode *child = children.Kth(i);
    RemoveNodes(scene, child, select_subtree_node, select_grid, select_bbox);
  }

  // Check if node must remain
  if (node == scene->Root()) return;
  if (node->NChildren() > 0) return;

  // Innocent until proven guilty
  RNBoolean remove = FALSE;

  // Check if should remove based on emptiness
  if ((node->NChildren() == 0) && (node->NReferences() == 0) && (node->NElements() == 0)) {
    remove = TRUE;
  }

  // Check if should remove based on bbox selection
  if (!select_bbox.IsEmpty()) {
    if (!R3Intersects(node->WorldBBox(), select_bbox)) {
      remove = TRUE;
    }
  }

  // Check if should remove based on subtree selection
  if (select_subtree_node) {
    if (node != select_subtree_node) {
      if (!node->IsAncestor(select_subtree_node)) { 
        if (!node->IsDecendent(select_subtree_node)) {
          remove = TRUE;
        }
      }
    }
  }

  // Check if should remove based on grid selection
  if (select_grid) {
    RNArea min_overlap = 0.5;
    RNScalar overlap = Overlap(node, select_grid);
    if (overlap < min_overlap) remove = TRUE;
    // R3Point centroid = node->WorldBBox().Centroid();
    // RNScalar grid_value = select_grid->WorldValue(centroid);
    // if (RNIsNegativeOrZero(grid_value)) remove = TRUE;
  }
  
  // Delete node 
  if (remove) delete node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
movq %rsp, %rdi
callq 0xa2750
cmpl $0x0, 0x2c(%rbx)
jle 0x27514
xorl %ebp, %ebp
movq %rsp, %r13
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
movl 0xc(%rsp), %edx
movq %r13, %rdi
callq 0xa27e6
incq %rbp
movslq 0x2c(%rbx), %rax
cmpq %rax, %rbp
jl 0x274f4
cmpl $0x0, 0xc(%rsp)
jle 0x27541
xorl %r13d, %r13d
movq (%rsp), %rax
movq (%rax,%r13,8), %rsi
movq %r12, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x274c4
incq %r13
movslq 0xc(%rsp), %rax
cmpq %rax, %r13
jl 0x2751e
cmpq %rbx, (%r12)
je 0x276c3
movl 0x2c(%rbx), %eax
testl %eax, %eax
jg 0x276c3
xorl %r12d, %r12d
testl %eax, %eax
jne 0x2756e
cmpl $0x0, 0x4c(%rbx)
jne 0x2756e
xorl %r12d, %r12d
cmpl $0x0, 0x3c(%rbx)
sete %r12b
movsd 0x14226a(%rip), %xmm0   # 0x1697e0
ucomisd 0x14227a(%rip), %xmm0   # 0x1697f8
ja 0x275db
movsd 0x142260(%rip), %xmm0   # 0x1697e8
ucomisd 0x142270(%rip), %xmm0   # 0x169800
ja 0x275db
movsd 0x142256(%rip), %xmm0   # 0x1697f0
ucomisd 0x142266(%rip), %xmm0   # 0x169808
ja 0x275db
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x3bc24
leaq 0x142220(%rip), %rsi    # 0x1697d8
leaq 0x38(%rsp), %rdi
xorl %edx, %edx
callq 0x4a0b0
movl %eax, %ebp
leaq 0x38(%rsp), %rdi
callq 0x6d220
testl %ebp, %ebp
movl $0x1, %eax
cmovel %eax, %r12d
testq %r15, %r15
sete %al
cmpq %r15, %rbx
sete %cl
orb %al, %cl
jne 0x27610
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3bdfe
testl %eax, %eax
jne 0x27610
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3bdfe
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r12d
testq %r14, %r14
je 0x276a9
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x3bf7e
leaq 0x38(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27716
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x13d9f9(%rip), %rax    # 0x165058
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x6f154
movsd 0x30(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x276ae
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0xcd96d(%rip), %xmm0    # 0xf5010
ucomisd %xmm1, %xmm0
ja 0x276ae
testl %r12d, %r12d
je 0x276c3
movq %rbx, %rdi
callq 0x3afe2
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x22890
movq %rsp, %rdi
callq 0xa27ba
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x6d220
jmp 0x27706
jmp 0x27703
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x6f154
jmp 0x27706
jmp 0x27703
jmp 0x27703
jmp 0x27703
movq %rax, %rbx
movq %rsp, %rdi
callq 0xa27ba
movq %rbx, %rdi
callq 0x23050