// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::SplitBlocks(gaps::R3SurfelNode*, gaps::R3SurfelConstraint const&, gaps::RNArray<gaps::R3SurfelBlock*>*, gaps::RNArray<gaps::R3SurfelBlock*>*)
int R3SurfelTree::
SplitBlocks(R3SurfelNode *node, const R3SurfelConstraint& constraint, 
  RNArray<R3SurfelBlock *> *blocksA, RNArray<R3SurfelBlock *> *blocksB)
{
  // Just checking
  assert(node);
  assert(node->Tree() == this);
  assert(node->tree_index >= 0);
  assert(nodes.Kth(node->tree_index) == node);
  int status = 0;

  // Make a temporary array of blocks
  RNArray<R3SurfelBlock *> splittable_blocks;
  for (int i = 0; i < node->NBlocks(); i++) {
    R3SurfelBlock *block = node->Block(i);
    splittable_blocks.Insert(block);
  }

  // Split blocks 
  for (int i = 0; i < splittable_blocks.NEntries(); i++) {
    R3SurfelBlock *block = splittable_blocks.Kth(i);
    R3SurfelBlock *blockA, *blockB;
    if (SplitBlock(node, block, constraint, &blockA, &blockB)) status = 1;
    if (blocksA && blockA) blocksA->Insert(blockA);
    if (blocksB && blockB) blocksB->Insert(blockB);
  }

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

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movq %r8, 0x90(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x8c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x436b0
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x186b0
movl %eax, 0x2c(%rsp)
jmp 0x65ba4
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x65c0f
movq 0xa8(%rsp), %rdi
movl 0x74(%rsp), %esi
callq 0x186d0
movq %rax, 0x20(%rsp)
jmp 0x65bc8
movq 0x20(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x436c0
jmp 0x65be3
jmp 0x65be5
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x65b89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x43730
jmp 0x65d2a
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x16d50
movl %eax, 0x1c(%rsp)
jmp 0x65c2f
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x65d09
movl 0x54(%rsp), %esi
leaq 0x78(%rsp), %rdi
callq 0x436f0
movq %rax, 0x10(%rsp)
jmp 0x65c54
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xa8(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq (%rdi), %rax
movq 0x50(%rax), %rax
leaq 0x40(%rsp), %r8
leaq 0x38(%rsp), %r9
callq *%rax
movl %eax, 0xc(%rsp)
jmp 0x65c91
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0x65ca5
movl $0x1, 0x8c(%rsp)
cmpq $0x0, 0x98(%rsp)
je 0x65cce
cmpq $0x0, 0x40(%rsp)
je 0x65cce
movq 0x98(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x436c0
jmp 0x65ccc
jmp 0x65cce
cmpq $0x0, 0x90(%rsp)
je 0x65cf7
cmpq $0x0, 0x38(%rsp)
je 0x65cf7
movq 0x90(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x436c0
jmp 0x65cf5
jmp 0x65cf7
jmp 0x65cf9
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x65c17
movl 0x8c(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x43730
movl 0x8(%rsp), %eax
addq $0xb8, %rsp
retq
movq 0x68(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)