// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::SplitBlock(gaps::R3SurfelNode*, gaps::R3SurfelBlock*, gaps::R3SurfelConstraint const&, gaps::R3SurfelBlock**, gaps::R3SurfelBlock**)
int R3SurfelTree::
SplitBlock(R3SurfelNode *node, R3SurfelBlock *block, const R3SurfelConstraint& constraint, 
  R3SurfelBlock **blockA, R3SurfelBlock **blockB)
{
  // Just checking
  assert(node && block);
  assert(block->Node() == node);

  // Check if block is fully outside constraint
  int check = constraint.Check(block);
  if (check == R3_SURFEL_CONSTRAINT_FAIL) { 
    if (blockA) *blockA = NULL;
    if (blockB) *blockB = block;
    return 0;
  }


  // Check if block is fully inside constraint
  if (check == R3_SURFEL_CONSTRAINT_PASS) { 
    if (blockA) *blockA = block;
    if (blockB) *blockB = NULL;
    return 0;
  }

  // Read block
  database->ReadBlock(block);

  // Partition surfels according to constraint
  RNArray<const R3Surfel *> subset1, subset2;
  for (int i = 0; i < block->NSurfels(); i++) {
    const R3Surfel *surfel = block->Surfel(i);
    if (constraint.Check(block, surfel)) subset1.Insert(surfel);
    else subset2.Insert(surfel);
  }

  // Create subset blocks 
  R3SurfelBlock *block1 = NULL;
  R3SurfelBlock *block2 = NULL;
  if (!database->InsertSubsetBlocks(block, subset1, subset2, &block1, &block2) || !block1 || !block2) {
    database->ReleaseBlock(block);
    if (blockA) *blockA = block1;
    if (blockB) *blockB = block2;
    return 0;
  }

  // Update node
  node->InsertBlock(block1); 
  node->InsertBlock(block2); 
  node->RemoveBlock(block);

  // Remove old block
  block->SetDirty(FALSE);
  database->ReleaseBlock(block);
  database->RemoveAndDeleteBlock(block);

  // Return new blocks
  if (blockA) *blockA = block1;
  if (blockB) *blockB = block2;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r14
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movq (%rcx), %rax
movq %rcx, %rdi
movq %rdx, %rsi
callq *0x20(%rax)
cmpl $0x2, %eax
je 0x366df
testl %eax, %eax
jne 0x366fb
testq %rbx, %rbx
je 0x366cc
andq $0x0, (%rbx)
xorl %ebx, %ebx
testq %r14, %r14
je 0x36862
movq %r15, (%r14)
jmp 0x36862
testq %rbx, %rbx
je 0x366e7
movq %r15, (%rbx)
xorl %ebx, %ebx
testq %r14, %r14
je 0x36862
andq $0x0, (%r14)
jmp 0x36862
movq %r13, 0x28(%rsp)
movq %rbx, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %r12, 0x30(%rsp)
movq 0x10(%r12), %rdi
movq %r15, %rsi
callq 0x20a64
leaq 0x18(%rsp), %rdi
callq 0x9a7ae
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x9a7ae
xorl %r13d, %r13d
xorl %ebx, %ebx
movslq 0x8(%r15), %rax
cmpq %rax, %rbx
jge 0x36774
movq (%r15), %r12
addq %r13, %r12
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x28(%rax)
movq %r14, %rdi
testl %eax, %eax
je 0x36763
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x1784c
incq %rbx
addq $0x30, %r13
jmp 0x36738
leaq 0x10(%rsp), %r8
andq $0x0, (%r8)
leaq 0x8(%rsp), %r9
andq $0x0, (%r9)
movq 0x30(%rsp), %r12
movq 0x10(%r12), %rdi
movq (%rdi), %rax
leaq 0x18(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq %r15, %rsi
callq *0x28(%rax)
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %rbx
movq 0x28(%rsp), %r13
testl %eax, %eax
je 0x36825
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x36825
cmpq $0x0, 0x8(%rsp)
je 0x36825
movq (%r13), %rax
movq %r13, %rdi
callq *0x28(%rax)
movq 0x8(%rsp), %rsi
movq (%r13), %rax
movq %r13, %rdi
callq *0x28(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2ac70
movq 0x10(%r12), %rdi
movq %r15, %rsi
callq 0x20aa0
movq 0x10(%r12), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x20(%rax)
testq %rbx, %rbx
je 0x36820
movq 0x10(%rsp), %rax
movq %rax, (%rbx)
pushq $0x1
popq %rbx
jmp 0x36841
movq 0x10(%r12), %rdi
movq %r15, %rsi
callq 0x20aa0
testq %rbx, %rbx
je 0x3683f
movq 0x10(%rsp), %rax
movq %rax, (%rbx)
xorl %ebx, %ebx
testq %r14, %r14
je 0x3684e
movq 0x8(%rsp), %rax
movq %rax, (%r14)
leaq 0x48(%rsp), %rdi
callq 0x9a81c
leaq 0x18(%rsp), %rdi
callq 0x9a81c
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x36887
jmp 0x3687a
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x9a81c
leaq 0x18(%rsp), %rdi
callq 0x9a81c
movq %rbx, %rdi
callq 0x138f0
nop