// File: /workspace/github_cpp_makefile_star2_O1/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
movl %r8d, (%rsp)
jne 0x41e91
leaq 0x9db26(%rip), %r12     # 0xdf963
xorl %eax, %eax
movl %eax, %r14d
movl %ebp, %eax
subl %r14d, %eax
jle 0x42239
movslq %eax, %rdx
movl $0x30, %esi
movq %rbx, %rdi
movq %r15, %rcx
callq 0x13790
movq %rax, %r13
testq %rax, %rax
je 0x41e6e
leal (%r14,%r13), %eax
jmp 0x41e7b
movq %r12, %rdi
xorl %eax, %eax
callq 0xaddb2
movl %r14d, %eax
testq %r13, %r13
jne 0x41e3f
xorl %r15d, %r15d
cmpl %r14d, %ebp
jle 0x42239
jmp 0x4230b
cmpl $0x5, %r9d
je 0x4203f
cmpl $0x4, %r9d
je 0x41f4c
cmpl $0x3, %r9d
jne 0x421c0
testl %ebp, %ebp
jle 0x42239
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 0x41ebe
jmp 0x42239
testl %ebp, %ebp
jle 0x42239
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 0x41f61
jmp 0x42239
testl %ebp, %ebp
jle 0x42239
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 0x421b1
movl %eax, %ecx
shrl $0x10, %ecx
addl $0xffff8000, %ecx       # imm = 0xFFFF8000
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x9cc81(%rip), %xmm1    # 0xdede0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
addsd 0x9bfc9(%rip), %xmm0    # 0xde140
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss 0x9cc66(%rip), %xmm0    # 0xdedec
ucomiss %xmm1, %xmm0
ja 0x4219f
ucomiss 0x9cc5e(%rip), %xmm1    # 0xdedf0
movaps %xmm1, %xmm0
jbe 0x4219f
movss 0x9cc51(%rip), %xmm0    # 0xdedf0
cvttss2si %xmm0, %ecx
movw %cx, 0x2a(%rbx,%r14)
movzwl %ax, %eax
movl %eax, 0x24(%rbx,%r14)
addq $0x30, %r14
cmpq %r14, %r13
jne 0x42059
jmp 0x42239
cmpl $0x2, %r9d
setae %al
testl %ebp, %ebp
setle %cl
orb %al, %cl
jne 0x42239
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 0x421e2
cmpl $0x0, (%rsp)
sete %al
testl %ebp, %ebp
setle %cl
movl $0x1, %r15d
orb %al, %cl
jne 0x4230b
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
xorl %r12d, %r12d
leaq (%rbx,%r12), %rdi
movl $0x3, %esi
callq 0xad34c
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl $0x3, %esi
callq 0xad255
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl $0x3, %esi
callq 0xad255
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
callq 0xad255
leaq (%rbx,%r12), %rdi
addq $0x28, %rdi
movl $0x1, %esi
callq 0xad255
leaq (%rbx,%r12), %rdi
addq $0x2a, %rdi
movl $0x1, %esi
callq 0xad255
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movl $0x1, %esi
callq 0xad34c
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0xad34c
leaq (%rbx,%r12), %rdi
addq $0x24, %rdi
movl $0x1, %esi
callq 0xad34c
addq $0x30, %r12
cmpq %r12, %r14
jne 0x42260
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop