// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movl %r9d, %r12d
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r15
movl %r8d, 0xc(%rsp)
testl %r8d, %r8d
sete %al
testl %ecx, %ecx
setle %cl
orb %al, %cl
jne 0x42407
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
xorl %r13d, %r13d
leaq (%rbx,%r13), %rdi
movl $0x3, %esi
callq 0xad34c
leaq (%rbx,%r13), %rdi
addq $0x10, %rdi
movl $0x3, %esi
callq 0xad255
leaq (%rbx,%r13), %rdi
addq $0x16, %rdi
movl $0x3, %esi
callq 0xad255
leaq (%rbx,%r13), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
callq 0xad255
leaq (%rbx,%r13), %rdi
addq $0x28, %rdi
movl $0x1, %esi
callq 0xad255
leaq (%rbx,%r13), %rdi
addq $0x2a, %rdi
movl $0x1, %esi
callq 0xad255
leaq (%rbx,%r13), %rdi
addq $0xc, %rdi
movl $0x1, %esi
callq 0xad34c
leaq (%rbx,%r13), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0xad34c
leaq (%rbx,%r13), %rdi
addq $0x24, %rdi
movl $0x1, %esi
callq 0xad34c
addq $0x30, %r13
cmpq %r13, %r14
jne 0x4235c
movl 0x50(%rsp), %eax
testl %ebp, %ebp
jle 0x42429
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 0x4241b
movl %r12d, %ecx
xorl $0x6, %ecx
orl %eax, %ecx
jne 0x42490
testl %ebp, %ebp
jle 0x4253c
movl $0x1, 0x10(%rsp)
xorl %r14d, %r14d
leaq 0x9d540(%rip), %r13     # 0xdf98d
movl 0xc(%rsp), %r12d
movl %ebp, %eax
subl %r14d, %eax
movslq %eax, %rdx
movl $0x30, %esi
movq %rbx, %rdi
movq %r15, %rcx
callq 0x139a0
testq %rax, %rax
je 0x42474
addl %eax, %r14d
jmp 0x42486
movl $0x0, 0x10(%rsp)
movq %r13, %rdi
xorl %eax, %eax
callq 0xaddb2
cmpl %ebp, %r14d
jl 0x42452
jmp 0x427fe
cmpl $0x5, %r12d
je 0x4263a
cmpl $0x4, %r12d
je 0x42549
cmpl $0x3, %r12d
jne 0x42792
movl $0x1, 0x10(%rsp)
testl %ebp, %ebp
jle 0x427f9
movl %ebp, %r14d
leaq 0x1c(%rbx), %r13
movl 0xc(%rsp), %r12d
leaq -0x1c(%r13), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
leaq -0xc(%r13), %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
movl $0x2, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x139a0
leaq 0x10(%r13), %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
movsbl 0x13(%r13), %edi
movq %r15, %rsi
callq 0x13670
addq $0x30, %r13
decq %r14
jne 0x424ca
jmp 0x427fe
movl $0x1, 0x10(%rsp)
jmp 0x427f9
movl $0x1, 0x10(%rsp)
testl %ebp, %ebp
jle 0x427f9
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 %r15, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0xc, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x10, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x16, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
movl $0x2, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r13), %rdi
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r13), %r14
addq $0x2c, %r14
movl $0x1, %esi
movl $0x3, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x139a0
movsbl 0x3(%r14), %edi
movq %r15, %rsi
callq 0x13670
addq $0x30, %r13
cmpq %r13, %r12
jne 0x42566
jmp 0x427f9
movl $0x1, 0x10(%rsp)
testl %ebp, %ebp
jle 0x427f9
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
xorl %r12d, %r12d
leaq (%rbx,%r12), %r14
addq $0xc, %r14
movswl 0x1e(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x9c76d(%rip), %xmm0    # 0xdede0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
mulss 0x9d25a(%rip), %xmm1    # 0xdf8dc
addss 0x9d256(%rip), %xmm1    # 0xdf8e0
cvttss2si %xmm1, %rax
shll $0x10, %eax
ucomiss 0x9cf6f(%rip), %xmm0    # 0xdf608
movl $0x0, %ecx
cmovnel %eax, %ecx
cmovpl %eax, %ecx
leaq (%rbx,%r12), %rdi
movzwl 0x18(%r14), %eax
orl %ecx, %eax
movl %eax, 0x14(%rsp)
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r12), %rdi
addq $0x10, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r12), %rdi
addq $0x16, %rdi
movl $0x2, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r12), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
movl $0x2, %edx
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x139a0
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %r15, %rcx
callq 0x139a0
leaq (%rbx,%r12), %r14
addq $0x2c, %r14
movl $0x1, %esi
movl $0x3, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x139a0
movsbl 0x3(%r14), %edi
movq %r15, %rsi
callq 0x13670
addq $0x30, %r12
cmpq %r12, %r13
jne 0x42657
jmp 0x427f9
cmpl $0x2, %r12d
setae %al
testl %ebp, %ebp
setle %cl
movl $0x1, 0x10(%rsp)
orb %al, %cl
jne 0x427f9
movl %ebp, %r14d
leaq 0x2c(%rbx), %r13
movl 0xc(%rsp), %r12d
leaq -0x2c(%r13), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x139a0
movl $0x1, %esi
movl $0x3, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x139a0
movsbl 0x3(%r13), %edi
movq %r15, %rsi
callq 0x13670
addq $0x30, %r13
decq %r14
jne 0x427b6
jmp 0x427fe
movl 0xc(%rsp), %r12d
testl %r12d, %r12d
sete %al
testl %ebp, %ebp
setle %cl
orb %al, %cl
jne 0x428c9
movl %ebp, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
xorl %r15d, %r15d
leaq (%rbx,%r15), %rdi
movl $0x3, %esi
callq 0xad34c
leaq (%rbx,%r15), %rdi
addq $0x10, %rdi
movl $0x3, %esi
callq 0xad255
leaq (%rbx,%r15), %rdi
addq $0x16, %rdi
movl $0x3, %esi
callq 0xad255
leaq (%rbx,%r15), %rdi
addq $0x1c, %rdi
movl $0x2, %esi
callq 0xad255
leaq (%rbx,%r15), %rdi
addq $0x28, %rdi
movl $0x1, %esi
callq 0xad255
leaq (%rbx,%r15), %rdi
addq $0x2a, %rdi
movl $0x1, %esi
callq 0xad255
leaq (%rbx,%r15), %rdi
addq $0xc, %rdi
movl $0x1, %esi
callq 0xad34c
leaq (%rbx,%r15), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0xad34c
leaq (%rbx,%r15), %rdi
addq $0x24, %rdi
movl $0x1, %esi
callq 0xad34c
addq $0x30, %r15
cmpq %r15, %r14
jne 0x4281e
movl 0x10(%rsp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq