// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelBlock.cpp
// Function: gaps::R3SurfelBlock::ReadBinary(_IO_FILE*)
int R3SurfelBlock::
ReadBinary(FILE *fp)
{
  // Read number of surfels
  if (fread(&nsurfels, sizeof(int), 1, fp) != (size_t) 1) {
    RNFail("Unable to read number of surfels\n");
    return 0;
  }

  // Read bounding box
  if (fread(&bbox, sizeof(R3Box), 1, fp) != (size_t) 1) {
    RNFail("Unable to read bounding box of surfel block\n");
    return 0;
  }

  // Read resolution
  if (fread(&resolution, sizeof(RNLength), 1, fp) != (size_t) 1) {
    RNFail("Unable to read resolution of surfel block\n");
    return 0;
  }

  // Read flags
  if (fread(&flags, sizeof(RNFlags), 1, fp) != (size_t) 1) {
    RNFail("Unable to read flags of surfel block\n");
    return 0;
  }

  // Allocate memory for surfels
  surfels = new R3Surfel [ nsurfels ];
  if (!surfels) {
    RNFail("Unable to allocate surfel block\n");
    return 0;
  }

  // Read surfels
  int count = 0;
  while (count < nsurfels) {
    int status = fread(&surfels[count], sizeof(R3Surfel), nsurfels - count, fp);
    if (status <= 0) {
      RNFail("Unable to read surfel block\n");
      delete [] surfels;
      surfels = NULL;
      nsurfels = 0;
      return 0;
    }
    count += status;
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rdi
movq %rdi, 0x40(%rsp)
addq $0x8, %rdi
movq 0x60(%rsp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x13860
cmpq $0x1, %rax
je 0x4eaf1
leaq 0x15665d(%rip), %rdi    # 0x1a513a
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x74(%rsp)
jmp 0x4ed42
movq 0x40(%rsp), %rdi
addq $0x28, %rdi
movq 0x60(%rsp), %rcx
movl $0x38, %esi
movl $0x1, %edx
callq 0x13860
cmpq $0x1, %rax
je 0x4eb2f
leaq 0x156641(%rip), %rdi    # 0x1a515c
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x74(%rsp)
jmp 0x4ed42
movq 0x40(%rsp), %rdi
addq $0x80, %rdi
movq 0x60(%rsp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x13860
cmpq $0x1, %rax
je 0x4eb70
leaq 0x15662d(%rip), %rdi    # 0x1a5189
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x74(%rsp)
jmp 0x4ed42
movq 0x40(%rsp), %rdi
addq $0x88, %rdi
movq 0x60(%rsp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x13860
cmpq $0x1, %rax
je 0x4ebb1
leaq 0x156617(%rip), %rdi    # 0x1a51b4
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x74(%rsp)
jmp 0x4ed42
movq 0x40(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
movl $0x30, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x28(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x38(%rsp)
cmpq $0x0, %rax
je 0x4ec3f
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
imulq $0x30, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x47310
jmp 0x4ec27
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0x20(%rsp)
jne 0x4ec16
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x4ec8f
leaq 0x156385(%rip), %rdi    # 0x1a4fde
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x74(%rsp)
jmp 0x4ed42
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
callq 0x13a90
jmp 0x4ed4b
movl $0x0, 0x50(%rsp)
movq 0x40(%rsp), %rcx
movl 0x50(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x4ed3a
movq 0x40(%rsp), %rax
movq (%rax), %rdi
movslq 0x50(%rsp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rdi
movl 0x8(%rax), %eax
subl 0x50(%rsp), %eax
movslq %eax, %rdx
movq 0x60(%rsp), %rcx
movl $0x30, %esi
callq 0x13860
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x4c(%rsp)
jg 0x4ed29
leaq 0x1564f2(%rip), %rdi    # 0x1a51da
movb $0x0, %al
callq 0x1464b0
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x4ed0c
movq 0x8(%rsp), %rdi
callq 0x13750
movq 0x40(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
movl $0x0, 0x74(%rsp)
jmp 0x4ed42
movl 0x4c(%rsp), %eax
addl 0x50(%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0x4ec97
movl $0x1, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)