// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, 0x8(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movl %r9d, %eax
xorl $0x6, %eax
orl 0x70(%rsp), %eax
movl %r8d, 0x18(%rsp)
jne 0x3c8e2
xorl %ebp, %ebp
pushq $0x30
popq %r14
movq 0x8(%rsp), %rax
subl %ebp, %eax
jle 0x3cc30
movslq %eax, %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x137d0
addl %eax, %ebp
testq %rax, %rax
jne 0x3c8a8
leaq 0x8d04f(%rip), %rdi     # 0xc9923
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x9ce26
jmp 0x3ccf4
cmpl $0x5, %r9d
je 0x3ca78
cmpl $0x4, %r9d
je 0x3c98e
cmpl $0x3, %r9d
jne 0x3cbb9
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
testl %eax, %eax
cmovgl %eax, %ebp
leaq 0x1c(%rbx), %r14
pushq $0x3
popq %r12
subq $0x1, %rbp
jb 0x3cc30
leaq -0x1c(%r14), %rdi
pushq $0x4
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq -0xc(%r14), %rdi
pushq $0x2
popq %r13
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
movq %r14, %rdi
movq %r13, %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq %r15, %rcx
callq 0x137d0
leaq 0x10(%r14), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq 0x13(%r14), %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x137d0
movzwl (%r14), %eax
movw %ax, 0x2(%r14)
addq $0x30, %r14
jmp 0x3c914
xorl %r14d, %r14d
movq 0x8(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
imulq $0x30, %rax, %rax
movq %rax, 0x10(%rsp)
pushq $0x3
popq %rbp
pushq $0x2
popq %r13
cmpq %r14, 0x10(%rsp)
je 0x3cc30
leaq (%rbx,%r14), %rdi
pushq $0x4
popq %r12
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0xc, %rdi
movq %r12, %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0x10, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0x16, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0x1c, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0x20, %rdi
pushq $0x4
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0x2c, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r14), %rdi
addq $0x2f, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
addq $0x30, %r14
jmp 0x3c9b0
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
imulq $0x30, %rax, %rax
movq %rax, 0x30(%rsp)
cmpq %r13, 0x30(%rsp)
je 0x3cc30
leaq (%rbx,%r13), %rdi
movq %rdi, 0x10(%rsp)
pushq $0x4
popq %r12
movq %r12, %rsi
pushq $0x3
popq %r14
movq %r14, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0xc, %rdi
movq %r12, %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0x10, %rdi
pushq $0x2
popq %rbp
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0x16, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0x1c, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0x20, %rdi
pushq $0x4
popq %rbp
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0x2c, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x137d0
leaq (%rbx,%r13), %rdi
addq $0x2f, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
movl 0x20(%rsp), %r14d
cmpl $0x10000, %r14d         # imm = 0x10000
jb 0x3cbb0
movl %r14d, %eax
shrl $0x10, %eax
addl $0xffff8000, %eax       # imm = 0xFFFF8000
cvtsi2sd %eax, %xmm0
divsd 0x8c237(%rip), %xmm0    # 0xc8dd0
cvtsd2ss %xmm0, %xmm0
movq 0x10(%rsp), %rdi
callq 0x2947a
movzwl %r14w, %eax
movl %eax, 0x24(%rbx,%r13)
addq $0x30, %r13
jmp 0x3ca93
cmpl $0x1, %r9d
ja 0x3cc30
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
imulq $0x30, %rax, %rbp
pushq $0x4
popq %r12
pushq $0x1
popq %r14
cmpq %r13, %rbp
je 0x3cc30
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
pushq $0x3
popq %rdx
movq %r15, %rcx
callq 0x137d0
leaq 0x1c(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x137d0
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rbx,%r13)
movss 0x28(%rsp), %xmm0
movss %xmm0, 0x8(%rbx,%r13)
movl 0x1c(%rsp), %eax
movl %eax, 0x2c(%rbx,%r13)
addq $0x30, %r13
jmp 0x3cbdd
cmpl $0x0, 0x18(%rsp)
je 0x3ccf1
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
testl %eax, %eax
cmovlel %r12d, %eax
imulq $0x30, %rax, %r13
pushq $0x1
popq %rbp
pushq $0x3
popq %r14
pushq $0x2
popq %r15
cmpq %r12, %r13
je 0x3ccf4
leaq (%rbx,%r12), %rdi
movl %r14d, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl %r14d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl %r14d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movl %r15d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x28, %rdi
movl %ebp, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x2a, %rdi
movl %ebp, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movl %ebp, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl %ebp, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x24, %rdi
movl %ebp, %esi
callq 0x9c603
addq $0x30, %r12
jmp 0x3cc58
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop