// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
cmpl $0x0, 0xc(%rdx)
je 0x3c417
movq %rcx, %rbx
cmpl $0x0, 0xc(%rcx)
je 0x3c42e
movq %rdx, %rbp
movq %rdi, %r13
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movl $0xc8, %edi
callq 0x134f0
movq %rax, %r15
leaq 0x10(%r12), %r14
movsd 0x60(%r12), %xmm0
movq %rax, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x29fb4
movl $0xc8, %edi
callq 0x134f0
movq %rax, %rbp
movsd 0x60(%r12), %xmm0
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x29fb4
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x10(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x10(%rax)
movq 0xa8(%r12), %rbx
testq %rbx, %rbx
je 0x3c3bc
cmpl $0x0, 0xb0(%r12)
je 0x3c3bc
movq %rbx, 0xa8(%r15)
movl 0x8(%r15), %r14d
movl %r14d, 0xb0(%r15)
movq %r13, %rdi
callq 0x3c478
imull %r14d, %eax
cltq
addq %rbx, %rax
movq %rax, 0xa8(%rbp)
movl 0x8(%rbp), %eax
movl %eax, 0xb0(%rbp)
andq $0x0, 0xa8(%r12)
andl $0x0, 0xb0(%r12)
movl 0xb4(%r12), %eax
testl %eax, %eax
je 0x3c3d5
movl %eax, 0xb4(%r15)
movl %eax, 0xb4(%rbp)
movq %r15, %rdi
callq 0x2b540
movq %rbp, %rdi
callq 0x2b540
movq %r13, %rdi
movq %r15, %rsi
callq 0x20aa0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x20aa0
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x3c408
movq %r15, (%rax)
pushq $0x1
popq %rcx
movq %rbp, %r12
movl %ecx, %eax
movq 0x10(%rsp), %r9
jmp 0x3c427
testq %r8, %r8
je 0x3c420
andq $0x0, (%r8)
xorl %ecx, %ecx
movl $0x0, %eax
testq %r9, %r9
jne 0x3c440
jmp 0x3c445
testq %r8, %r8
je 0x3c436
movq %r12, (%r8)
xorl %ecx, %ecx
testq %r9, %r9
je 0x3c454
xorl %r12d, %r12d
movq %r12, (%r9)
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x3c445
movq %rax, %rbx
movq %rbp, %r15
jmp 0x3c463
movq %rax, %rbx
movl $0xc8, %esi
movq %r15, %rdi
callq 0x13500
movq %rbx, %rdi
callq 0x138f0