// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::InternalReadBlock(gaps::R3SurfelBlock*, _IO_FILE*, int)
int R3SurfelDatabase::
InternalReadBlock(R3SurfelBlock *block, FILE *fp, int swap_endian)
{
  // Check number of surfels
  if (block->NSurfels() == 0) return 1;

  // Check if ghosted from pending delete
  if (block->flags[R3_SURFEL_BLOCK_DELETE_PENDING_FLAG]) return 1;
  if (block->file_surfels_offset == 0) return 1;
  if (block->file_surfels_count == 0) return 1;

  // Just checking
  assert(fp);
  assert(block->database == this);
  assert(block->file_surfels_offset > 0);
  assert(block->file_surfels_count >= (unsigned int) block->nsurfels);

  // Allocate surfels
  block->surfels = new R3Surfel [ block->nsurfels ];
  if (!block->surfels) {
    RNFail("Unable to allocate surfels\n");
    return 0;
  }
  
  // Read surfels
  RNFileSeek(fp, block->file_surfels_offset, RN_FILE_SEEK_SET);
  if (!ReadSurfel(fp, block->surfels, block->nsurfels, swap_endian, major_version, minor_version)) return 0;
  
  // Update resident surfels
  resident_surfels += block->NSurfels();

#ifdef PRINT_DEBUG
  // Print debug message
  printf("Read Block     %6d : %6d %9ld : %9.3f %9.3f %9.3f\n", 
         block->database_index, block->nsurfels, resident_surfels,
         block->Centroid().X(), block->Centroid().Y(), block->Centroid().Z()); 
  fflush(stdout);
#endif

  // Return success
  return 1;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movl %ecx, 0x64(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x70(%rsp), %rdi
callq 0x1c0e0
cmpl $0x0, %eax
jne 0x741c3
movl $0x1, 0x84(%rsp)
jmp 0x743b4
movq 0x70(%rsp), %rax
addq $0x88, %rax
movq %rax, 0x38(%rsp)
leaq 0x58(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x1b970
movq 0x38(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x31a20
cmpl $0x0, %eax
je 0x74206
movl $0x1, 0x84(%rsp)
jmp 0x743b4
movq 0x70(%rsp), %rax
cmpq $0x0, 0xa8(%rax)
jne 0x74225
movl $0x1, 0x84(%rsp)
jmp 0x743b4
movq 0x70(%rsp), %rax
cmpl $0x0, 0xb0(%rax)
jne 0x74243
movl $0x1, 0x84(%rsp)
jmp 0x743b4
movq 0x70(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
movl $0x30, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x20(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x30(%rsp)
cmpq $0x0, %rax
je 0x742d1
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
imulq $0x30, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x47310
jmp 0x742b9
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jne 0x742a8
movq 0x30(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, (%rax)
movq 0x70(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x74329
leaq 0x130d60(%rip), %rdi    # 0x1a5050
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x84(%rsp)
jmp 0x743b4
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
callq 0x13a90
jmp 0x743c3
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rax
movq 0xa8(%rax), %rsi
xorl %edx, %edx
callq 0x145360
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rax
movq (%rax), %rdx
movq 0x70(%rsp), %rax
movl 0x8(%rax), %ecx
movl 0x64(%rsp), %r8d
movl 0x20(%rdi), %r9d
movl 0x24(%rdi), %r10d
movq (%rdi), %rax
movl %r10d, (%rsp)
callq *0xa0(%rax)
cmpl $0x0, %eax
jne 0x74387
movl $0x0, 0x84(%rsp)
jmp 0x743b4
movq 0x70(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movq 0x40(%rsp), %rax
movslq %ecx, %rcx
addq 0xb8(%rax), %rcx
movq %rcx, 0xb8(%rax)
movl $0x1, 0x84(%rsp)
movl 0x84(%rsp), %eax
addq $0x88, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0x13970
nopl (%rax)