// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rcx, %r13
movq %rdx, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq %rsi, 0x50(%rsp)
movq %rsi, %rdi
callq 0x349aa
testq %rax, %rax
je 0x3a4da
movq %rax, %r15
cmpl $0x0, 0xc(%rax)
jle 0x3a4de
xorl %ebx, %ebx
leaq 0x20(%rsp), %r14
leaq 0x30(%rsp), %r12
movl $0x0, 0xc(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, 0x40(%rsp)
movq (%r15), %rax
movq (%rax,%rbx,8), %rbp
movq %r14, %rdi
callq 0xa9ca8
movq %r12, %rdi
callq 0xa9ca8
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
movq 0x50(%rsp), %rdx
movq %r14, %rcx
movq %r12, %r8
callq *0x48(%rax)
testl %eax, %eax
je 0x3a485
movl $0xd0, %edi
callq 0x134e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x3715c
movl $0xd0, %edi
callq 0x134e0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x3715c
cmpl $0x0, 0x2c(%rsp)
jle 0x3a3ec
xorl %r13d, %r13d
movq 0x20(%rsp), %rax
movq (%rax,%r13,8), %r15
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x28(%rax)
incq %r13
movslq 0x2c(%rsp), %rax
cmpq %rax, %r13
jl 0x3a3bc
cmpl $0x0, 0x3c(%rsp)
jle 0x3a425
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
movq (%rax,%r13,8), %r15
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x28(%rax)
incq %r13
movslq 0x3c(%rsp), %rax
cmpq %rax, %r13
jl 0x3a3f6
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x38f02
movq 0x48(%rsp), %r15
movq 0x40(%rsp), %r13
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x38f02
cmpq $0x0, 0x10(%rsp)
je 0x3a467
movq 0x10(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %r12, %rsi
callq 0xa9d3e
movl $0x1, 0xc(%rsp)
testq %r13, %r13
je 0x3a4ae
movl 0xc(%r13), %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0xa9d3e
jmp 0x3a4ae
cmpl $0x0, 0x2c(%rsp)
jle 0x3a49b
movq 0x10(%rsp), %rax
movq %rax, %rdi
testq %rax, %rax
jne 0x3a4a3
jmp 0x3a4ae
movq %r13, %rdi
testq %r13, %r13
je 0x3a4ae
movl 0xc(%rdi), %edx
movq %rbp, %rsi
callq 0xa9d3e
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0xa9d12
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xa9d12
incq %rbx
movslq 0xc(%r15), %rax
cmpq %rax, %rbx
jl 0x3a34c
jmp 0x3a4e6
xorl %eax, %eax
jmp 0x3a4ff
movl $0x0, 0xc(%rsp)
movq %r15, %rdi
callq 0xa9d12
movl $0x10, %esi
movq %r15, %rdi
callq 0x134f0
movl 0xc(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xd0, %esi
movq %r12, %rdi
jmp 0x3a526
movq %rax, %rbx
movl $0xd0, %esi
movq %r14, %rdi
callq 0x134f0
jmp 0x3a53d
jmp 0x3a53a
movq %rax, %rbx
jmp 0x3a547
jmp 0x3a53a
jmp 0x3a53a
jmp 0x3a53a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa9d12
leaq 0x20(%rsp), %rdi
callq 0xa9d12
movq %rbx, %rdi
callq 0x138b0
nop