// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::ReadSurfel(_IO_FILE*, gaps::R3Surfel*, int, int, unsigned int, unsigned int) const
int R3SurfelDatabase::
ReadSurfel(FILE *fp, R3Surfel *ptr, int count, int swap_endian,
  unsigned int major_version, unsigned int minor_version) const
{
  // Check database version
  if ((major_version == current_major_version) && (minor_version == current_minor_version)) {
    int sofar = 0;
    while (sofar < count) {
      size_t status = fread(ptr, sizeof(R3Surfel), count - sofar, fp);
      if (status > 0) sofar += status;
      else { RNFail("Unable to read surfel from database file\n"); return 0; }
    }
  }
  else {
    if (major_version == 5) {
      for (int i = 0; i < count; i++) {
        RNUInt32 attribute;
        fread(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fread(&ptr[i].timestamp, sizeof(RNScalar32), 1, fp);
        fread(ptr[i].normal, sizeof(RNInt16), 3, fp);
        fread(ptr[i].tangent, sizeof(RNInt16), 3, fp);
        fread(ptr[i].radius, sizeof(RNInt16), 2, fp);
        fread(&ptr[i].identifier, sizeof(RNUInt32), 1, fp);
        fread(&attribute, sizeof(RNUInt32), 1, fp);
        fread(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fread(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
        unsigned int encoded_elevation = (attribute >> 16) & 0xFFFF;
        if (encoded_elevation != 0) {
          float elevation = (encoded_elevation - 32768.0) / 400.0;
          ptr[i].SetElevation(elevation);
          ptr[i].SetAttribute(attribute & 0x0000FFFF);
        }
      }
    }
    else if (major_version == 4) {
      for (int i = 0; i < count; i++) {
        fread(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fread(&ptr[i].timestamp, sizeof(RNScalar32), 1, fp);
        fread(ptr[i].normal, sizeof(RNInt16), 3, fp);
        fread(ptr[i].tangent, sizeof(RNInt16), 3, fp);
        fread(ptr[i].radius, sizeof(RNInt16), 2, fp);
        fread(&ptr[i].identifier, sizeof(RNUInt32), 1, fp);
        fread(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fread(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
      }
    }
    else if (major_version == 3) {
      for (int i = 0; i < count; i++) {
        fread(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fread(ptr[i].normal, sizeof(RNInt16), 3, fp);
        fread(ptr[i].radius, sizeof(RNInt16), 1, fp);
        fread(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fread(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
        ptr[i].radius[1] = ptr[i].radius[0];
      }
    }
    else if (major_version < 2) {
      for (int i = 0; i < count; i++) {
        float position[3];
        unsigned char color_and_flags[4];
        fread(position, sizeof(RNScalar32), 3, fp);
        fread(color_and_flags, sizeof(RNUChar8), 4, fp);
        ptr[i].SetPosition(position);
        ptr[i].SetColor(color_and_flags);
        ptr[i].SetFlags(color_and_flags[3]);
      }
    }
  }

  // Swap endian
  if (swap_endian) {
    for (int i = 0; i < count; i++) {
      RNSwap4(ptr[i].position, 3);
      RNSwap2(ptr[i].normal, 3);
      RNSwap2(ptr[i].tangent, 3);
      RNSwap2(ptr[i].radius, 2);
      RNSwap2(&ptr[i].depth, 1);
      RNSwap2(&ptr[i].elevation, 1);
      RNSwap4(&ptr[i].timestamp, 1);
      RNSwap4(&ptr[i].identifier, 1);
      RNSwap4(&ptr[i].attribute, 1);
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r15
movl %r9d, %eax
xorl $0x6, %eax
orl 0x50(%rsp), %eax
jne 0x41af3
testl %ebp, %ebp
jle 0x41e47
movl %r8d, (%rsp)
xorl %r14d, %r14d
movl %ebp, %eax
subl %r14d, %eax
movslq %eax, %rdx
movl $0x30, %esi
movq %rbx, %rdi
movq %r15, %rcx
callq 0x13790
testq %rax, %rax
je 0x41bb2
addl %eax, %r14d
cmpl %ebp, %r14d
jl 0x41ac5
jmp 0x41ecb
cmpl $0x5, %r9d
je 0x41cbf
cmpl $0x4, %r9d
je 0x41bc8
movl %r8d, (%rsp)
cmpl $0x3, %r9d
jne 0x41e52
testl %ebp, %ebp
jle 0x41e47
movl %ebp, %r14d
leaq 0x1c(%rbx), %r12
leaq -0x1c(%r12), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq -0xc(%r12), %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
movl $0x2, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x13790
leaq 0x10(%r12), %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq 0x13(%r12), %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
movzwl (%r12), %eax
movw %ax, 0x2(%r12)
addq $0x30, %r12
decq %r14
jne 0x41b24
jmp 0x41ecb
leaq 0x9cdaa(%rip), %rdi     # 0xde963
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xac81a
jmp 0x41f9d
testl %ebp, %ebp
jle 0x41e47
movl %r8d, (%rsp)
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
xorl %r12d, %r12d
leaq (%rbx,%r12), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
movl $0x2, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0x2c, %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r12), %rdi
addq $0x2f, %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
addq $0x30, %r12
cmpq %r12, %r14
jne 0x41be1
jmp 0x41ecb
testl %ebp, %ebp
jle 0x41e47
movl %r8d, (%rsp)
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
xorl %r14d, %r14d
leaq 0x8(%rsp), %r12
leaq (%rbx,%r14), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0xc, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0x10, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0x16, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
movl $0x2, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0x2c, %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x13790
leaq (%rbx,%r14), %rdi
addq $0x2f, %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x13790
movl 0x8(%rsp), %eax
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x41e35
movl %eax, %ecx
shrl $0x10, %ecx
addl $0xffff8000, %ecx       # imm = 0xFFFF8000
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x9bfed(%rip), %xmm1    # 0xdddd0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
addsd 0x9b33d(%rip), %xmm0    # 0xdd138
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss 0x9bfd2(%rip), %xmm0    # 0xddddc
ucomiss %xmm1, %xmm0
ja 0x41e23
ucomiss 0x9bfca(%rip), %xmm1    # 0xddde0
movaps %xmm1, %xmm0
jbe 0x41e23
movss 0x9bfbd(%rip), %xmm0    # 0xddde0
cvttss2si %xmm0, %ecx
movw %cx, 0x2a(%rbx,%r14)
movzwl %ax, %eax
movl %eax, 0x24(%rbx,%r14)
addq $0x30, %r14
cmpq %r14, %r13
jne 0x41cdd
jmp 0x41ecb
movl $0x1, %r14d
jmp 0x41f9d
cmpl $0x2, %r9d
setae %al
testl %ebp, %ebp
setle %cl
orb %al, %cl
jne 0x41ecb
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
xorl %r12d, %r12d
leaq 0x4(%rsp), %r13
movl $0x4, %esi
movl $0x3, %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rcx
callq 0x13790
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x13790
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rbx,%r12)
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x8(%rbx,%r12)
movl 0x4(%rsp), %eax
movl %eax, 0x2c(%rbx,%r12)
addq $0x30, %r12
cmpq %r12, %r14
jne 0x41e74
cmpl $0x0, (%rsp)
sete %al
testl %ebp, %ebp
setle %cl
movl $0x1, %r14d
orb %al, %cl
jne 0x41f9d
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r15
xorl %r12d, %r12d
leaq (%rbx,%r12), %rdi
movl $0x3, %esi
callq 0xabe58
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl $0x3, %esi
callq 0xabd61
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl $0x3, %esi
callq 0xabd61
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
callq 0xabd61
leaq (%rbx,%r12), %rdi
addq $0x28, %rdi
movl $0x1, %esi
callq 0xabd61
leaq (%rbx,%r12), %rdi
addq $0x2a, %rdi
movl $0x1, %esi
callq 0xabd61
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movl $0x1, %esi
callq 0xabe58
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0xabe58
leaq (%rbx,%r12), %rdi
addq $0x24, %rdi
movl $0x1, %esi
callq 0xabe58
addq $0x30, %r12
cmpq %r12, %r15
jne 0x41ef2
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop