// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::WriteSurfel(_IO_FILE*, gaps::R3Surfel*, int, int, unsigned int, unsigned int) const
int R3SurfelDatabase::
WriteSurfel(FILE *fp, R3Surfel *ptr, int count, int swap_endian, 
  unsigned int major_version, unsigned int minor_version) const
{
  // 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);
    }
  }

  // Clear surfel marks
  for (int i = 0; i < count; i++) ptr[i].SetMark(FALSE);

  // Write current version of surfel
  int status = 1;
  if ((major_version == current_major_version) && (minor_version == current_minor_version)) {
    int sofar = 0;
    while (sofar < count) {
      size_t n = fwrite(ptr, sizeof(R3Surfel), count - sofar, fp);
      if (n > 0) sofar += n;
      else { RNFail("Unable to write surfel to database file\n"); status = 0; }
    }
  }
  else {
    if (major_version == 5) {
      for (int i = 0; i < count; i++) {
        unsigned int encoded_elevation = 0;
        if (ptr[i].Elevation() != 0) encoded_elevation = 400 * ptr[i].Elevation() + 32768;
        RNUInt32 attribute = (ptr[i].Attribute() & 0x0000FFFF) | (encoded_elevation << 16);
        fwrite(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fwrite(&ptr[i].timestamp, sizeof(RNScalar32), 1, fp);
        fwrite(ptr[i].normal, sizeof(RNInt16), 3, fp);
        fwrite(ptr[i].tangent, sizeof(RNInt16), 3, fp);
        fwrite(ptr[i].radius, sizeof(RNInt16), 2, fp);
        fwrite(&ptr[i].identifier, sizeof(RNUInt32), 1, fp);
        fwrite(&attribute, sizeof(RNUInt32), 1, fp);
        fwrite(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fwrite(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
      }
    }
    else if (major_version == 4) {
      for (int i = 0; i < count; i++) {
        fwrite(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fwrite(&ptr[i].timestamp, sizeof(RNScalar32), 1, fp);
        fwrite(ptr[i].normal, sizeof(RNInt16), 3, fp);
        fwrite(ptr[i].tangent, sizeof(RNInt16), 3, fp);
        fwrite(ptr[i].radius, sizeof(RNInt16), 2, fp);
        fwrite(&ptr[i].identifier, sizeof(RNUInt32), 1, fp);
        fwrite(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fwrite(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
      }
    }
    else if (major_version == 3) {
      for (int i = 0; i < count; i++) {
        fwrite(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fwrite(ptr[i].normal, sizeof(RNInt16), 3, fp);
        fwrite(&ptr[i].radius[0], sizeof(RNInt16), 1, fp);
        fwrite(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fwrite(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
      }
    }
    else if (major_version < 2) {
      for (int i = 0; i < count; i++) {
        fwrite(ptr[i].position, sizeof(RNScalar32), 3, fp);
        fwrite(ptr[i].color, sizeof(RNUChar8), 3, fp);
        fwrite(&ptr[i].flags, sizeof(RNUChar8), 1, fp);
      }
    }
  }
  
  // Swap endian back
  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 status
  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
xorl %eax, %eax
testl %ecx, %ecx
movl %ecx, 0x18(%rsp)
cmovgl %ecx, %eax
movq %rax, 0x20(%rsp)
movl %r8d, 0x14(%rsp)
testl %r8d, %r8d
je 0x3cde8
imulq $0x30, 0x20(%rsp), %r14
xorl %r12d, %r12d
pushq $0x3
popq %rbp
pushq $0x1
popq %r13
cmpq %r12, %r14
je 0x3cde8
leaq (%rbx,%r12), %rdi
movl %ebp, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl %ebp, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl %ebp, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
pushq $0x2
popq %rsi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x28, %rdi
movl %r13d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x2a, %rdi
movl %r13d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movl %r13d, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl %r13d, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x24, %rdi
movl %r13d, %esi
callq 0x9c603
addq $0x30, %r12
jmp 0x3cd4d
movl 0x60(%rsp), %eax
pushq $0x1
popq %rdx
movq 0x20(%rsp), %r12
imulq $0x30, %r12, %rcx
movq %rcx, 0x8(%rsp)
xorl %ecx, %ecx
movl 0x10(%rsp), %esi
cmpq %rcx, 0x8(%rsp)
je 0x3ce15
andb $0x7f, 0x2f(%rbx,%rcx)
addq $0x30, %rcx
jmp 0x3ce03
movl %esi, %ecx
xorl $0x6, %ecx
orl %eax, %ecx
jne 0x3ce6f
xorl %r12d, %r12d
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
pushq $0x30
popq %r14
leaq 0x8cb1a(%rip), %rbp     # 0xc994d
movl 0x18(%rsp), %eax
subl %r12d, %eax
jle 0x3d14a
movslq %eax, %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x139e0
testq %rax, %rax
je 0x3ce5b
addl %eax, %r12d
jmp 0x3ce33
movl $0x0, 0x4(%rsp)
movq %rbp, %rdi
xorl %eax, %eax
callq 0x9ce26
jmp 0x3ce33
cmpl $0x5, %esi
movl %edx, 0x4(%rsp)
je 0x3cfc8
cmpl $0x4, %esi
je 0x3cefc
cmpl $0x3, %esi
jne 0x3d0fe
leaq 0x1c(%rbx), %r14
pushq $0x3
popq %r13
subq $0x1, %r12
jb 0x3d14a
leaq -0x1c(%r14), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
leaq -0xc(%r14), %rdi
pushq $0x2
popq %rbp
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
movq %r14, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
leaq 0x10(%r14), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
movsbl 0x13(%r14), %edi
movq %r15, %rsi
callq 0x13690
addq $0x30, %r14
jmp 0x3ce92
xorl %r12d, %r12d
pushq $0x3
popq %rbp
cmpq %r12, 0x8(%rsp)
je 0x3d14a
leaq (%rbx,%r12), %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
pushq $0x2
popq %r14
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movq %r14, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movq %r13, %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%r12), %r14
addq $0x2c, %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
movsbl 0x3(%r14), %edi
movq %r15, %rsi
callq 0x13690
addq $0x30, %r12
jmp 0x3cf02
xorl %ebp, %ebp
pushq $0x1
popq %r13
cmpq %rbp, 0x8(%rsp)
je 0x3d14a
leaq (%rbx,%rbp), %r12
addq $0xc, %r12
movswl 0x1e(%r12), %eax
cvtsi2sd %eax, %xmm0
divsd 0x8bddd(%rip), %xmm0    # 0xc8dd0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
mulss 0x8c89a(%rip), %xmm1    # 0xc989c
addss 0x8c896(%rip), %xmm1    # 0xc98a0
cvttss2si %xmm1, %rax
shll $0x10, %eax
ucomiss 0x8c5df(%rip), %xmm0    # 0xc95f8
movl $0x0, %ecx
cmovnel %eax, %ecx
cmovpl %eax, %ecx
leaq (%rbx,%rbp), %rdi
movzwl 0x18(%r12), %eax
orl %ecx, %eax
movl %eax, 0x1c(%rsp)
pushq $0x4
popq %r14
movq %r14, %rsi
pushq $0x3
popq %rdx
movq %r15, %rcx
callq 0x139e0
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%rbp), %rdi
addq $0x10, %rdi
pushq $0x2
popq %r12
movq %r12, %rsi
pushq $0x3
popq %r14
movq %r14, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%rbp), %rdi
addq $0x16, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%rbp), %rdi
addq $0x1c, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%rbp), %rdi
addq $0x20, %rdi
pushq $0x4
popq %r12
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
leaq 0x1c(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x139e0
leaq (%rbx,%rbp), %r12
addq $0x2c, %r12
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x139e0
movsbl 0x3(%r12), %edi
movq %r15, %rsi
callq 0x13690
addq $0x30, %rbp
jmp 0x3cfce
cmpl $0x1, %esi
ja 0x3d14a
leaq 0x2c(%rbx), %r14
pushq $0x4
popq %r13
pushq $0x3
popq %rbp
subq $0x1, %r12
jb 0x3d14a
leaq -0x2c(%r14), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
movq %r14, %rdi
pushq $0x1
popq %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x139e0
movsbl 0x3(%r14), %edi
movq %r15, %rsi
callq 0x13690
addq $0x30, %r14
jmp 0x3d10e
cmpl $0x0, 0x14(%rsp)
je 0x3d200
xorl %r12d, %r12d
pushq $0x3
popq %rbp
pushq $0x2
popq %r14
pushq $0x1
popq %r15
cmpq %r12, 0x8(%rsp)
je 0x3d200
leaq (%rbx,%r12), %rdi
movl %ebp, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl %ebp, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl %ebp, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movl %r14d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x28, %rdi
movl %r15d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0x2a, %rdi
movl %r15d, %esi
callq 0x9c53c
leaq (%rbx,%r12), %rdi
addq $0xc, %rdi
movl %r15d, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl %r15d, %esi
callq 0x9c603
leaq (%rbx,%r12), %rdi
addq $0x24, %rdi
movl %r15d, %esi
callq 0x9c603
addq $0x30, %r12
jmp 0x3d163
movl 0x4(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop