// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
cmpl $0x0, 0xc(%rdx)
je 0x4150a
movq %rcx, %rbx
cmpl $0x0, 0xc(%rcx)
je 0x41524
movq %rdx, %r13
movq %rdi, %r12
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r15
leaq 0x10(%rbp), %r14
movsd 0x60(%rbp), %xmm0
movq %rax, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2c3a4
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r13
movsd 0x60(%rbp), %xmm0
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2c3a4
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x10(%rax)
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x10(%rax)
movq 0xa8(%rbp), %rax
testq %rax, %rax
je 0x413f1
cmpl $0x0, 0xb0(%rbp)
je 0x413f1
movq %rax, 0xa8(%r15)
movl 0x8(%r15), %ecx
movl %ecx, 0xb0(%r15)
movl 0x20(%r12), %esi
addl $-0x3, %esi
movl $0x10, %edx
cmpl $0x3, %esi
ja 0x413c1
movl %esi, %edx
leaq 0x9d72e(%rip), %rsi     # 0xdeaec
movl (%rsi,%rdx,4), %edx
imull %ecx, %edx
movslq %edx, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%r13)
movl 0x8(%r13), %eax
movl %eax, 0xb0(%r13)
movq $0x0, 0xa8(%rbp)
movl $0x0, 0xb0(%rbp)
movl 0xb4(%rbp), %eax
testl %eax, %eax
je 0x41409
movl %eax, 0xb4(%r15)
movl %eax, 0xb4(%r13)
movq %r15, %rdi
callq 0x2dfb8
movq %r13, %rdi
callq 0x2dfb8
movl 0xb4(%r15), %eax
cmpl $0x1, %eax
jne 0x4144a
movq (%r12), %rax
movq 0x8(%r12), %rdx
movl 0x28(%r12), %ecx
movq %r12, %rdi
movq %r15, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x41481
movl 0xb4(%r15), %eax
decl %eax
movl %eax, 0xb4(%r15)
jne 0x41481
testb $0x2, 0x89(%r15)
je 0x41481
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x18(%rax)
movq %r15, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r15, %rdi
callq 0x134f0
movl 0xb4(%r13), %eax
cmpl $0x1, %eax
jne 0x414b2
movq (%r12), %rax
movq 0x8(%r12), %rdx
movl 0x28(%r12), %ecx
movq %r12, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x414e9
movl 0xb4(%r13), %eax
decl %eax
movl %eax, 0xb4(%r13)
jne 0x414e9
testb $0x2, 0x89(%r13)
je 0x414e9
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x414f6
movq %r15, (%rax)
movl $0x1, %ecx
movq %r13, %rbp
movl $0x1, %eax
movq 0x10(%rsp), %r9
jmp 0x4151d
testq %r8, %r8
je 0x41516
movq $0x0, (%r8)
xorl %ecx, %ecx
movl $0x0, %eax
testq %r9, %r9
jne 0x41535
jmp 0x4153a
testq %r8, %r8
je 0x4152c
movq %rbp, (%r8)
xorl %ecx, %ecx
testq %r9, %r9
je 0x41549
xorl %ebp, %ebp
movq %rbp, (%r9)
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x4153a
movq %rax, %rbx
movq %r13, %r15
jmp 0x41558
movq %rax, %rbx
movl $0xc8, %esi
movq %r15, %rdi
callq 0x134f0
movq %rbx, %rdi
callq 0x138b0
nop