// File: /workspace/github_cpp_makefile_star2_O2/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
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x94996
xorl %ebp, %ebp
movslq 0x2c(%rbx), %rax
cmpq %rax, %rbp
jge 0x26679
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x26c38
incq %rbp
jmp 0x2665b
xorl %r13d, %r13d
movslq 0x1c(%rsp), %rax
cmpq %rax, %r13
jge 0x266a2
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq %r12, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x2662f
incq %r13
jmp 0x2667c
cmpq %rbx, (%r12)
je 0x267f4
movl 0x2c(%rbx), %eax
testl %eax, %eax
jg 0x267f4
xorl %r12d, %r12d
testl %eax, %eax
jne 0x266cf
cmpl $0x0, 0x4c(%rbx)
jne 0x266cf
xorl %r12d, %r12d
cmpl $0x0, 0x3c(%rbx)
sete %r12b
leaq 0x132122(%rip), %rdi    # 0x1587f8
callq 0x26a40
pushq $0x1
popq %r13
testl %eax, %eax
jne 0x26715
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x37f36
leaq 0x132101(%rip), %rsi    # 0x1587f8
leaq 0x38(%rsp), %rdi
xorl %edx, %edx
callq 0x44d34
movl %eax, %ebp
leaq 0x38(%rsp), %rdi
callq 0x64132
testl %ebp, %ebp
cmovel %r13d, %r12d
testq %r15, %r15
sete %al
cmpq %r15, %rbx
sete %cl
orb %al, %cl
jne 0x26745
movq %r15, %rdi
movq %rbx, %rsi
callq 0x38088
testl %eax, %eax
jne 0x26745
movq %rbx, %rdi
movq %r15, %rsi
callq 0x38088
testl %eax, %eax
cmovel %r13d, %r12d
testq %r14, %r14
je 0x267da
andq $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x3813a
leaq 0x38(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %rsp, %r8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2684b
movsd (%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x12d8e8(%rip), %rax    # 0x154078
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x65fee
movsd 0x30(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x267df
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd 0xb783c(%rip), %xmm0    # 0xde010
ucomisd %xmm1, %xmm0
ja 0x267df
testl %r12d, %r12d
je 0x267f4
movq %rbx, %rdi
callq 0x37510
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x228a0
leaq 0x10(%rsp), %rdi
callq 0x94a04
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 0x64132
jmp 0x26839
jmp 0x26836
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x65fee
jmp 0x26839
jmp 0x26836
jmp 0x26836
jmp 0x26836
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x94a04
movq %rbx, %rdi
callq 0x23070