// File: /workspace/github_cpp_makefile_star2_O1/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 0x3395a
movq %rsi, %r14
movq %rdi, %r15
testq %rsi, %rsi
jne 0x3374e
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x3395a
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0xe7204
movl 0xc(%rbx), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe729a
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x33942
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 0xe740e
cmpl $0x0, 0x3c(%r14)
je 0x337f6
cmpl $0x0, 0x3c(%r14)
jle 0x3391a
xorl %ebx, %ebx
movq 0x30(%r14), %rax
movq (%rax,%rbx,8), %rsi
movl 0x44(%rsp), %edx
movq %r12, %rdi
callq 0xe729a
incq %rbx
movslq 0x3c(%r14), %rax
cmpq %rax, %rbx
jl 0x337d1
jmp 0x33915
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe727c
movq (%rbx), %rcx
movq %rax, %rbx
subq %rcx, %rbx
cmpq $0x1, %rax
movl $0x0, %r15d
sbbl %r15d, %r15d
leaq 0x10(%rsp), %rdi
callq 0xe7204
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0xe7204
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 0x338b9
testl %eax, %eax
je 0x338b9
movq %r14, %rdi
xorl %esi, %esi
callq 0x4075e
movq %rbp, %rdi
movl %r15d, %esi
callq 0x416d2
cmpl $0x0, 0x1c(%rsp)
jle 0x33892
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %rbp, %rdi
callq 0x41632
incq %rbx
movslq 0x1c(%rsp), %rax
cmpq %rax, %rbx
jl 0x33874
cmpl $0x0, 0x34(%rsp)
jle 0x338b9
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %rbp, %rdi
callq 0x41632
incq %rbx
movslq 0x34(%rsp), %rax
cmpq %rax, %rbx
jl 0x3389b
cmpq $0x0, 0x58(%rsp)
je 0x338d0
movq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe7534
cmpq $0x0, 0x50(%rsp)
je 0x338e7
movq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xe7534
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 0xe726e
leaq 0x10(%rsp), %rdi
callq 0xe726e
movq 0x60(%rsp), %rbx
movl 0x44(%rsp), %eax
testl %eax, %eax
jne 0x337a6
cmpl $0x0, 0x20(%rsp)
setne %al
cmpl $0x0, 0xc(%rsp)
setne %cl
andb %al, %cl
movzbl %cl, %ebx
movq 0x68(%rsp), %r15
jmp 0x33944
xorl %ebx, %ebx
movq (%r15), %rax
movq %r15, %rdi
callq *0x378(%rax)
leaq 0x38(%rsp), %rdi
callq 0xe726e
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x33995
jmp 0x33995
jmp 0x33995
movq %rax, %rbx
jmp 0x33989
jmp 0x33995
jmp 0x3397c
jmp 0x3397c
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xe726e
leaq 0x10(%rsp), %rdi
callq 0xe726e
jmp 0x33998
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xe726e
movq %rbx, %rdi
callq 0x28130