// File: /workspace/github_cpp_makefile_star2_O1/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 $0x168, %rsp            # imm = 0x168
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
movq %rsp, %rdi
callq 0xa3b28
cmpl $0x0, 0x2c(%rbx)
jle 0x27587
xorl %ebp, %ebp
movq %rsp, %r13
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
movl 0xc(%rsp), %edx
movq %r13, %rdi
callq 0xa3bbe
incq %rbp
movslq 0x2c(%rbx), %rax
cmpq %rax, %rbp
jl 0x27567
cmpl $0x0, 0xc(%rsp)
jle 0x275b4
xorl %r13d, %r13d
movq (%rsp), %rax
movq (%rax,%r13,8), %rsi
movq %r12, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x27537
incq %r13
movslq 0xc(%rsp), %rax
cmpq %rax, %r13
jl 0x27591
cmpq %rbx, (%r12)
je 0x27742
movl 0x2c(%rbx), %eax
testl %eax, %eax
jg 0x27742
xorl %r12d, %r12d
testl %eax, %eax
jne 0x275e1
cmpl $0x0, 0x4c(%rbx)
jne 0x275e1
xorl %r12d, %r12d
cmpl $0x0, 0x3c(%rbx)
sete %r12b
movsd 0x145207(%rip), %xmm0   # 0x16c7f0
ucomisd 0x145217(%rip), %xmm0   # 0x16c808
ja 0x2764e
movsd 0x1451fd(%rip), %xmm0   # 0x16c7f8
ucomisd 0x14520d(%rip), %xmm0   # 0x16c810
ja 0x2764e
movsd 0x1451f3(%rip), %xmm0   # 0x16c800
ucomisd 0x145203(%rip), %xmm0   # 0x16c818
ja 0x2764e
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c180
leaq 0x1451bd(%rip), %rsi    # 0x16c7e8
leaq 0x58(%rsp), %rdi
xorl %edx, %edx
callq 0x4a900
movl %eax, %ebp
leaq 0x58(%rsp), %rdi
callq 0x6e0b4
testl %ebp, %ebp
movl $0x1, %eax
cmovel %eax, %r12d
testq %r15, %r15
sete %al
cmpq %r15, %rbx
sete %cl
orb %al, %cl
jne 0x27683
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3c35a
testl %eax, %eax
jne 0x27683
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3c35a
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r12d
testq %r14, %r14
je 0x27728
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c4da
leaq 0x58(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27795
movsd 0x10(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
leaq 0x140996(%rip), %rax    # 0x168068
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movsd (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x6ffe8
movapd 0x30(%rsp), %xmm2
cmpltpd 0x20(%rsp), %xmm2
movsd 0xd0906(%rip), %xmm0    # 0xf8010
movapd 0x40(%rsp), %xmm1
cmpltpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movd %xmm1, %eax
testb $0x1, %al
movl $0x1, %eax
cmovnel %eax, %r12d
testl %r12d, %r12d
je 0x27742
movq %rbx, %rdi
callq 0x3b58c
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x22890
movq %rsp, %rdi
callq 0xa3b92
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x6e0b4
jmp 0x27785
jmp 0x27782
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x6ffe8
jmp 0x27785
jmp 0x27782
jmp 0x27782
jmp 0x27782
movq %rax, %rbx
movq %rsp, %rdi
callq 0xa3b92
movq %rbx, %rdi
callq 0x23060