// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::InsertSubsetBlocks(gaps::R3SurfelBlock*, gaps::RNArray<gaps::R3Surfel const*> const&, gaps::RNArray<gaps::R3Surfel const*> const&, gaps::R3SurfelBlock**, gaps::R3SurfelBlock**)
int R3SurfelDatabase::
InsertSubsetBlocks(R3SurfelBlock *block, 
  const RNArray<const R3Surfel *>& subset1, const RNArray<const R3Surfel *>& subset2, 
  R3SurfelBlock **blockA, R3SurfelBlock **blockB)
{
  // Check subset sizes
  assert(subset1.NEntries() + subset2.NEntries() <= block->NSurfels());
  
  // Check subset1
  if (subset1.IsEmpty()) {
    if (blockA) *blockA = NULL;
    if (blockB) *blockB = block;
    return 0;
  }

  // Check subset2
  if (subset2.IsEmpty()) {
    if (blockA) *blockA = block;
    if (blockB) *blockB = NULL;
    return 0;
  }

  // Create new blocks
  R3SurfelBlock *block1 = new R3SurfelBlock(subset1, block->PositionOrigin(), block->TimestampOrigin());
  R3SurfelBlock *block2 = new R3SurfelBlock(subset2, block->PositionOrigin(), block->TimestampOrigin());
    
  // Insert new blocks 
  InsertBlock(block1);
  InsertBlock(block2);

  // Update file offsets
  if ((block->file_surfels_offset > 0) && (block->file_surfels_count > 0)) {
    block1->file_surfels_offset = block->file_surfels_offset;
    block1->file_surfels_count = block1->NSurfels();
    block2->file_surfels_offset = block->file_surfels_offset + block1->NSurfels() * NBytesPerSurfel();
    block2->file_surfels_count = block2->NSurfels();
    block->file_surfels_offset = 0;
    block->file_surfels_count = 0;
  }

  // Update file read counts ???
  if (block->file_read_count > 0) {
    block1->file_read_count = block->file_read_count;
    block2->file_read_count = block->file_read_count;
  }
    
  // Update block properties
  block1->UpdateProperties();
  block2->UpdateProperties();

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

  // Return success
  return 1;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x98(%rsp), %rdi
callq 0x31da0
cmpl $0x0, %eax
je 0x725ae
cmpq $0x0, 0x88(%rsp)
je 0x72580
movq 0x88(%rsp), %rax
movq $0x0, (%rax)
cmpq $0x0, 0x80(%rsp)
je 0x7259e
movq 0xa0(%rsp), %rcx
movq 0x80(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0xb4(%rsp)
jmp 0x728de
movq 0x90(%rsp), %rdi
callq 0x31da0
cmpl $0x0, %eax
je 0x72608
cmpq $0x0, 0x88(%rsp)
je 0x725de
movq 0xa0(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x80(%rsp)
je 0x725f8
movq 0x80(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0xb4(%rsp)
jmp 0x728de
movl $0xc8, %edi
callq 0x13570
movq %rax, 0x40(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x189c0
movq %rax, 0x50(%rsp)
jmp 0x72638
movq 0xa0(%rsp), %rdi
callq 0x50320
movsd %xmm0, 0x38(%rsp)
jmp 0x7264d
movsd 0x38(%rsp), %xmm0
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x49580
jmp 0x72669
movq 0x40(%rsp), %rax
movq %rax, 0x78(%rsp)
movl $0xc8, %edi
callq 0x13570
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x189c0
movq %rax, 0x30(%rsp)
jmp 0x726a3
movq 0xa0(%rsp), %rdi
callq 0x50320
movsd %xmm0, 0x18(%rsp)
jmp 0x726b8
movsd 0x18(%rsp), %xmm0
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x49580
jmp 0x726d4
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x78(%rsp), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0xa0(%rsp), %rax
cmpq $0x0, 0xa8(%rax)
jbe 0x72828
movq 0xa0(%rsp), %rax
cmpl $0x0, 0xb0(%rax)
jbe 0x72828
movq 0xa0(%rsp), %rax
movq 0xa8(%rax), %rcx
movq 0x78(%rsp), %rax
movq %rcx, 0xa8(%rax)
movq 0x78(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0xb0(%rax)
movq 0xa0(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x78(%rsp), %rdi
callq 0x1c0e0
movq 0x58(%rsp), %rdi
movl %eax, 0x14(%rsp)
callq 0x72900
movq 0x8(%rsp), %rcx
movl %eax, %edx
movl 0x14(%rsp), %eax
imull %edx, %eax
cltq
addq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0xa8(%rax)
movq 0x60(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0xb0(%rax)
movq 0xa0(%rsp), %rax
movq $0x0, 0xa8(%rax)
movq 0xa0(%rsp), %rax
movl $0x0, 0xb0(%rax)
jmp 0x72828
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0xc8, %esi
callq 0x13580
jmp 0x728ed
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0xc8, %esi
callq 0x13580
jmp 0x728ed
movq 0xa0(%rsp), %rax
cmpl $0x0, 0xb4(%rax)
jbe 0x7286b
movq 0xa0(%rsp), %rax
movl 0xb4(%rax), %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0xb4(%rax)
movq 0xa0(%rsp), %rax
movl 0xb4(%rax), %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0xb4(%rax)
movq 0x78(%rsp), %rdi
callq 0x4c380
movq 0x60(%rsp), %rdi
callq 0x4c380
movq 0x58(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x311c0
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x311c0
cmpq $0x0, 0x88(%rsp)
je 0x728b8
movq 0x78(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x80(%rsp)
je 0x728d3
movq 0x60(%rsp), %rcx
movq 0x80(%rsp), %rax
movq %rcx, (%rax)
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
movq 0x70(%rsp), %rdi
callq 0x13970
nopw (%rax,%rax)