// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::SplitNodes(gaps::R3SurfelPointSet&, gaps::RNArray<gaps::R3SurfelNode*>*, gaps::RNArray<gaps::R3SurfelNode*>*)
int R3SurfelTree::
SplitNodes(R3SurfelPointSet& pointset,
  RNArray<R3SurfelNode *> *nodesA, RNArray<R3SurfelNode *> *nodesB)
{
  // Initialize return value (whether any node was split)
  int status = 0;

  // Create array of nodes containing points in point set
  RNArray<R3SurfelNode *> *pointset_nodes = pointset.Nodes();
  if (!pointset_nodes) return 0;

  // Split each node
  for (int i = 0; i < pointset_nodes->NEntries(); i++) {
    R3SurfelNode *node = pointset_nodes->Kth(i);

    // Split blocks in leaf node
    RNArray<R3SurfelBlock *> blocksA, blocksB;
    if (SplitBlocks(node, pointset, &blocksA, &blocksB)) {
      // Create new nodes
      R3SurfelNode *nodeA = new R3SurfelNode();
      R3SurfelNode *nodeB = new R3SurfelNode();
      
      // Move blocks from node to nodeA
      for (int i = 0; i < blocksA.NEntries(); i++) {
        R3SurfelBlock *block = blocksA.Kth(i);
        node->RemoveBlock(block);
        nodeA->InsertBlock(block);
      }
      
      // Move blocks from node to nodeB
      for (int i = 0; i < blocksB.NEntries(); i++) {
        R3SurfelBlock *block = blocksB.Kth(i);
        node->RemoveBlock(block);
        nodeB->InsertBlock(block);
      }
      
      // Insert new nodes into tree
      InsertNode(nodeA, node);
      InsertNode(nodeB, node);
      
      // Update return values
      if (nodesA) nodesA->Insert(nodeA);
      if (nodesB) nodesB->Insert(nodeB);
      status = 1;
    }
    else {
      if (blocksA.NEntries() > 0) {
        // Update return value
        if (nodesA) nodesA->Insert(node);
      }
      else {
        // Update return value
        if (nodesB) nodesB->Insert(node);
      }
    }
  }

  // Delete array of nodes containing points in point set
  delete pointset_nodes;

  // Return whether any nodes were split
  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, (%rsp)
movq %rdx, 0x8(%rsp)
movq %rdi, %r13
movq %rsi, 0x38(%rsp)
movq %rsi, %rdi
callq 0x31248
testq %rax, %rax
je 0x36207
movq %rax, %r12
xorl %ecx, %ecx
leaq 0x10(%rsp), %r14
xorl %r15d, %r15d
movq %rax, 0x30(%rsp)
movslq 0xc(%r12), %rax
cmpq %rax, %rcx
jge 0x361f2
movq (%r12), %rax
movq %rcx, 0x40(%rsp)
movq (%rax,%rcx,8), %rbx
movq %r14, %rdi
callq 0x9a7ae
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
callq 0x9a7ae
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
movq %rbp, %r8
callq *0x48(%rax)
testl %eax, %eax
je 0x361ac
movl $0xd0, %edi
callq 0x134f0
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
callq 0x33394
movl $0xd0, %edi
callq 0x134f0
movq %rax, %rbp
movq %r13, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x33394
xorl %r13d, %r13d
movslq 0x1c(%rsp), %rax
cmpq %rax, %r13
jge 0x36122
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %r12
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x30(%rax)
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x28(%rax)
incq %r13
jmp 0x360f2
xorl %r13d, %r13d
movslq 0x2c(%rsp), %rax
cmpq %rax, %r13
jge 0x36156
movq 0x20(%rsp), %rax
movq (%rax,%r13,8), %r12
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x30(%rax)
movq (%rbp), %rax
movq %rbp, %rdi
movq %r12, %rsi
callq *0x28(%rax)
incq %r13
jmp 0x36125
movq %r14, %r13
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x34e72
movq 0x30(%rsp), %r12
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x34e72
cmpq $0x0, 0x8(%rsp)
je 0x3618f
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x1784c
pushq $0x1
popq %r15
cmpq $0x0, (%rsp)
je 0x361ce
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x1784c
pushq $0x1
popq %r15
jmp 0x361ce
cmpl $0x0, 0x1c(%rsp)
jle 0x361ba
movq 0x8(%rsp), %rax
jmp 0x361be
movq (%rsp), %rax
movq %rax, %rdi
testq %rax, %rax
je 0x361ce
movq %rbx, %rsi
callq 0x1784c
leaq 0x20(%rsp), %rdi
callq 0x9a81c
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x9a81c
movq 0x40(%rsp), %rcx
incq %rcx
jmp 0x3606e
movq %r12, %rdi
callq 0x9a81c
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0x13500
jmp 0x3620a
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x36248
movq %rax, %rbx
movl $0xd0, %esi
movq %rbp, %rdi
jmp 0x36236
movq %rax, %rbx
movl $0xd0, %esi
movq %r15, %rdi
callq 0x13500
jmp 0x3624b
movq %rax, %rbx
jmp 0x36255
jmp 0x36248
jmp 0x36248
jmp 0x36248
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x9a81c
leaq 0x10(%rsp), %rdi
callq 0x9a81c
movq %rbx, %rdi
callq 0x138f0
nop