// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movl %r9d, %r12d
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r14
testl %r8d, %r8d
setne %al
testl %ecx, %ecx
setg %cl
andb %al, %cl
movb %cl, 0x3(%rsp)
cmpb $0x1, %cl
jne 0x42098
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r15
xorl %r13d, %r13d
leaq (%rbx,%r13), %rdi
movl $0x3, %esi
callq 0xabe58
leaq (%rbx,%r13), %rdi
addq $0x10, %rdi
movl $0x3, %esi
callq 0xabd61
leaq (%rbx,%r13), %rdi
addq $0x16, %rdi
movl $0x3, %esi
callq 0xabd61
leaq (%rbx,%r13), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
callq 0xabd61
leaq (%rbx,%r13), %rdi
addq $0x28, %rdi
movl $0x1, %esi
callq 0xabd61
leaq (%rbx,%r13), %rdi
addq $0x2a, %rdi
movl $0x1, %esi
callq 0xabd61
leaq (%rbx,%r13), %rdi
addq $0xc, %rdi
movl $0x1, %esi
callq 0xabe58
leaq (%rbx,%r13), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0xabe58
leaq (%rbx,%r13), %rdi
addq $0x24, %rdi
movl $0x1, %esi
callq 0xabe58
addq $0x30, %r13
cmpq %r13, %r15
jne 0x41fed
movl 0x40(%rsp), %eax
testl %ebp, %ebp
jle 0x42115
movl %ebp, %ecx
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
andb $0x7f, 0x2f(%rbx,%rdx)
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0x420ac
movl %r12d, %ecx
xorl $0x6, %ecx
orl %eax, %ecx
jne 0x42129
testl %ebp, %ebp
jle 0x4240a
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x9c8b1(%rip), %r12     # 0xde98d
movl %ebp, %eax
subl %r13d, %eax
movslq %eax, %rdx
movl $0x30, %esi
movq %rbx, %rdi
movq %r14, %rcx
callq 0x139a0
testq %rax, %rax
je 0x420fe
addl %eax, %r13d
jmp 0x4210b
xorl %r15d, %r15d
movq %r12, %rdi
xorl %eax, %eax
callq 0xac81a
cmpl %ebp, %r13d
jl 0x420dc
jmp 0x42475
movl %r12d, %ecx
xorl $0x6, %ecx
movl $0x1, %r15d
orl %eax, %ecx
je 0x42538
cmpl $0x5, %r12d
je 0x422b1
cmpl $0x4, %r12d
je 0x421c8
cmpl $0x3, %r12d
jne 0x42415
testl %ebp, %ebp
jle 0x4240a
movl %ebp, %r12d
leaq 0x1c(%rbx), %r15
leaq -0x1c(%r15), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
leaq -0xc(%r15), %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x139a0
leaq 0x10(%r15), %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
movsbl 0x13(%r15), %edi
movq %r14, %rsi
callq 0x13670
addq $0x30, %r15
decq %r12
jne 0x42156
jmp 0x42402
testl %ebp, %ebp
jle 0x4240a
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r12
xorl %r13d, %r13d
leaq (%rbx,%r13), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0xc, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x10, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x16, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r13), %r15
addq $0x2c, %r15
movl $0x1, %esi
movl $0x3, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x139a0
movsbl 0x3(%r15), %edi
movq %r14, %rsi
callq 0x13670
addq $0x30, %r13
cmpq %r13, %r12
jne 0x421dd
jmp 0x42402
testl %ebp, %ebp
jle 0x4240a
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
xorl %r15d, %r15d
leaq (%rbx,%r15), %r12
addq $0xc, %r12
movswl 0x1e(%r12), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x9baed(%rip), %xmm0    # 0xdddd0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
mulss 0x9c5ea(%rip), %xmm1    # 0xde8dc
addss 0x9c5e6(%rip), %xmm1    # 0xde8e0
cvttss2si %xmm1, %rax
shll $0x10, %eax
ucomiss 0x9c2ff(%rip), %xmm0    # 0xde608
movl $0x0, %ecx
cmovnel %eax, %ecx
cmovpl %eax, %ecx
leaq (%rbx,%r15), %rdi
movzwl 0x18(%r12), %eax
orl %ecx, %eax
movl %eax, 0x4(%rsp)
movl $0x4, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r15), %rdi
addq $0x10, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r15), %rdi
addq $0x16, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r15), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r15), %rdi
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x139a0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %r14, %rcx
callq 0x139a0
leaq (%rbx,%r15), %r12
addq $0x2c, %r12
movl $0x1, %esi
movl $0x3, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x139a0
movsbl 0x3(%r12), %edi
movq %r14, %rsi
callq 0x13670
addq $0x30, %r15
cmpq %r15, %r13
jne 0x422c6
movl $0x1, %r15d
jmp 0x42475
movl $0x1, %r15d
jmp 0x42538
testl %ebp, %ebp
setle %al
cmpl $0x2, %r12d
setae %cl
movl $0x1, %r15d
orb %al, %cl
jne 0x42475
movl %ebp, %r13d
leaq 0x2c(%rbx), %r12
leaq -0x2c(%r12), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0x139a0
movl $0x1, %esi
movl $0x3, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x139a0
movsbl 0x3(%r12), %edi
movq %r14, %rsi
callq 0x13670
addq $0x30, %r12
decq %r13
jne 0x42432
cmpb $0x0, 0x3(%rsp)
je 0x42538
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
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, %r14
jne 0x4248d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq