// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r8, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdx, 0x70(%rsp)
movq 0x8(%rdi), %rax
movq 0x8(%rax), %rax
xorl %ebx, %ebx
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x3390d
movq %rsi, %r14
movq %rdi, %r15
testq %rsi, %rsi
jne 0x3370f
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x3390d
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0xe59c8
movl 0xc(%rbx), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe5a5e
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x338f5
leaq 0x10(%r15), %rbp
movq %r15, 0x68(%rsp)
leaq 0x18(%r15), %rbx
movq $0x0, 0x20(%rsp)
leaq 0x38(%rsp), %r12
movl $0x0, 0xc(%rsp)
movq %rbx, 0x60(%rsp)
movq 0x38(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r14
decl %esi
movq %r12, %rdi
callq 0xe5be8
cmpl $0x0, 0x3c(%r14)
je 0x337a9
jle 0x338cd
xorl %ebx, %ebx
movq 0x30(%r14), %rax
movq (%rax,%rbx,8), %rsi
movl 0x44(%rsp), %edx
movq %r12, %rdi
callq 0xe5a5e
incq %rbx
movslq 0x3c(%r14), %rax
cmpq %rax, %rbx
jl 0x33784
jmp 0x338c8
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe5a40
movq (%rbx), %rcx
movq %rax, %rbx
subq %rcx, %rbx
cmpq $0x1, %rax
movl $0x0, %r15d
sbbl %r15d, %r15d
leaq 0x10(%rsp), %rdi
callq 0xe59c8
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0xe59c8
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r13, %r8
callq *0x30(%rax)
shrq $0x3, %rbx
orl %ebx, %r15d
js 0x3386c
testl %eax, %eax
je 0x3386c
movq %r14, %rdi
xorl %esi, %esi
callq 0x407bc
movq %rbp, %rdi
movl %r15d, %esi
callq 0x4172a
cmpl $0x0, 0x1c(%rsp)
jle 0x33845
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %rbp, %rdi
callq 0x4168a
incq %rbx
movslq 0x1c(%rsp), %rax
cmpq %rax, %rbx
jl 0x33827
cmpl $0x0, 0x34(%rsp)
jle 0x3386c
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %rbp, %rdi
callq 0x4168a
incq %rbx
movslq 0x34(%rsp), %rax
cmpq %rax, %rbx
jl 0x3384e
cmpq $0x0, 0x58(%rsp)
je 0x33883
movq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe5d1a
cmpq $0x0, 0x50(%rsp)
je 0x3389a
movq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xe5d1a
movq 0x20(%rsp), %rax
addl 0x1c(%rsp), %eax
movq %rax, 0x20(%rsp)
movl 0xc(%rsp), %eax
addl 0x34(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x28(%rsp), %rdi
callq 0xe5a32
leaq 0x10(%rsp), %rdi
callq 0xe5a32
movq 0x60(%rsp), %rbx
movl 0x44(%rsp), %eax
testl %eax, %eax
jne 0x3375e
cmpl $0x0, 0x20(%rsp)
setne %al
cmpl $0x0, 0xc(%rsp)
setne %cl
andb %al, %cl
movzbl %cl, %ebx
movq 0x68(%rsp), %r15
jmp 0x338f7
xorl %ebx, %ebx
movq (%r15), %rax
movq %r15, %rdi
callq *0x378(%rax)
leaq 0x38(%rsp), %rdi
callq 0xe5a32
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x33948
jmp 0x33948
jmp 0x33948
movq %rax, %rbx
jmp 0x3393c
jmp 0x33948
jmp 0x3392f
jmp 0x3392f
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xe5a32
leaq 0x10(%rsp), %rdi
callq 0xe5a32
jmp 0x3394b
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xe5a32
movq %rbx, %rdi
callq 0x28120
nop