// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::SplitLeafNodes(gaps::R3SurfelNode*, gaps::R3SurfelConstraint const&, gaps::RNArray<gaps::R3SurfelNode*>*, gaps::RNArray<gaps::R3SurfelNode*>*)
int R3SurfelViewer::
SplitLeafNodes(R3SurfelNode *source_node, const R3SurfelConstraint& constraint, 
  RNArray<R3SurfelNode *> *nodesA, RNArray<R3SurfelNode *> *nodesB)
{
  // Get convenient variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return 0;
  if (!source_node) source_node = tree->RootNode();
  if (!source_node) return 0;

  // Split leaf nodes, WHILE UPDATING NODES IN VIEWER'S RESIDENT SET
  int countA = 0;
  int countB = 0;
  RNArray<R3SurfelNode *> stack;
  stack.Insert(source_node);
  while (!stack.IsEmpty()) {
    R3SurfelNode *node = stack.Tail();
    stack.RemoveTail();
    if (node->NParts() == 0) {
      // Check if node is resident in working set
      int resident_index = resident_nodes.NodeIndex(node);
    
      // Split leaf node
      RNArray<R3SurfelNode *> partsA, partsB;
      if (tree->SplitLeafNodes(node, constraint, &partsA, &partsB)) {
        // Update resident set
        if (resident_index >= 0) {
          node->ReleaseBlocks(); // ???
          resident_nodes.RemoveNode(resident_index);
          for (int j = 0; j < partsA.NEntries(); j++) {
            R3SurfelNode *partA = partsA.Kth(j);
            resident_nodes.InsertNode(partA);
          }
          for (int j = 0; j < partsB.NEntries(); j++) {
            R3SurfelNode *partB = partsB.Kth(j);
            resident_nodes.InsertNode(partB);
          }
        }
      }

      // Insert parts into result
      if (nodesA) nodesA->Append(partsA);
      if (nodesB) nodesB->Append(partsB);
      countA += partsA.NEntries();
      countB += partsB.NEntries();
    }
    else {
      for (int i = 0; i < node->NParts(); i++) {
        R3SurfelNode *part = node->Part(i);
        stack.Insert(part);
      }
    } 
  }

  // Invalidate VBO buffers
  InvalidateVBO();

  // Return status
  if (countA == 0) return 0;
  if (countB == 0) return 0;
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x60(%rsp)
movq 0x8(%rdi), %rax
movq 0x8(%rax), %rcx
xorl %ebp, %ebp
testq %rcx, %rcx
je 0x31ded
movq %rsi, %r14
movq %rdi, %rbx
testq %rsi, %rsi
jne 0x31c3b
movq 0x18(%rcx), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x31ded
movq %rcx, 0x58(%rsp)
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0xd0d9a
movq %r15, %rdi
movq %r14, %rsi
callq 0x3344c
leaq 0x10(%rbx), %rbp
movq $0x0, 0x10(%rsp)
leaq 0x48(%rsp), %r12
movl $0x0, 0xc(%rsp)
movslq 0x54(%rsp), %rax
testq %rax, %rax
je 0x31dc2
movq 0x48(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r14
movq %r12, %rdi
callq 0x32ce0
movl 0x3c(%r14), %eax
testl %eax, %eax
je 0x31cbd
xorl %r15d, %r15d
cltq
cmpq %rax, %r15
jge 0x31c72
movq 0x30(%r14), %rax
movq (%rax,%r15,8), %rsi
movq %r12, %rdi
callq 0x3344c
incq %r15
movl 0x3c(%r14), %eax
jmp 0x31c9d
movq %rbp, %rdi
movq %r14, %rsi
callq 0x32d0e
movl %eax, %r15d
leaq 0x18(%rsp), %rdi
callq 0xd0d9a
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0xd0d9a
movq 0x58(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq 0x60(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r13, %r8
callq *0x30(%rax)
testl %r15d, %r15d
js 0x31d61
testl %eax, %eax
je 0x31d61
movq %r14, %rdi
xorl %esi, %esi
callq 0x3d7b4
movq %rbp, %rdi
movl %r15d, %esi
callq 0x3e570
xorl %r14d, %r14d
movslq 0x24(%rsp), %rax
cmpq %rax, %r14
jge 0x31d3e
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbp, %rdi
callq 0x3e4ea
incq %r14
jmp 0x31d1e
xorl %r14d, %r14d
movslq 0x34(%rsp), %rax
cmpq %rax, %r14
jge 0x31d61
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbp, %rdi
callq 0x3e4ea
incq %r14
jmp 0x31d41
cmpq $0x0, 0x40(%rsp)
je 0x31d78
movq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xd1062
cmpq $0x0, 0x38(%rsp)
je 0x31d8f
movq 0x38(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xd1062
movl 0xc(%rsp), %eax
addl 0x24(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rax
addl 0x34(%rsp), %eax
movq %rax, 0x10(%rsp)
leaq 0x28(%rsp), %rdi
callq 0xd0e08
leaq 0x18(%rsp), %rdi
callq 0xd0e08
jmp 0x31c72
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x378(%rax)
cmpl $0x0, 0xc(%rsp)
setne %al
cmpl $0x0, 0x10(%rsp)
setne %cl
andb %al, %cl
movzbl %cl, %ebp
leaq 0x48(%rsp), %rdi
callq 0xd0e08
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x31e1a
jmp 0x31e28
jmp 0x31e28
jmp 0x31e28
jmp 0x31e0d
jmp 0x31e0d
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xd0e08
leaq 0x18(%rsp), %rdi
callq 0xd0e08
jmp 0x31e2b
jmp 0x31e28
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xd0e08
movq %rbx, %rdi
callq 0x28140
nop