// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::WriteBlockHeader(_IO_FILE*, int)
int R3SurfelDatabase::
WriteBlockHeader(FILE *fp, int swap_endian)
{
  // Seek to start of blocks
  RNFileSeek(fp, file_blocks_offset, RN_FILE_SEEK_SET);

  // Write blocks
  char buffer[128] = { '\0' };
  for (int i = 0; i < blocks.NEntries(); i++) {
    R3SurfelBlock *block = blocks.Kth(i);
    unsigned int block_flags = block->flags;
    if (!RNWriteUnsignedLongLong(fp, &block->file_surfels_offset, 1, swap_endian)) return 0;
    if (!RNWriteUnsignedInt(fp, &block->file_surfels_count, 1, swap_endian)) return 0;
    if (!RNWriteInt(fp, &block->nsurfels, 1, swap_endian)) return 0;
    if (!RNWriteDouble(fp, &block->position_origin[0], 3, swap_endian)) return 0;
    if (!RNWriteDouble(fp, &block->bbox[0][0], 6, swap_endian)) return 0;
    if (!RNWriteDouble(fp, &block->resolution, 1, swap_endian)) return 0;
    if (!RNWriteUnsignedInt(fp, &block_flags, 1, swap_endian)) return 0;
    if (!RNWriteDouble(fp, &block->timestamp_origin, 1, swap_endian)) return 0;
    if (!RNWriteDouble(fp, &block->timestamp_range[0], 2, swap_endian)) return 0;
    if (!RNWriteUnsignedInt(fp, &block->max_identifier, 1, swap_endian)) return 0;
    if (!RNWriteUnsignedInt(fp, &block->min_identifier, 1, swap_endian)) return 0;
    if (!RNWriteChar(fp, buffer, 32, swap_endian)) return 0;
  }

  // Return success
  return 1;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movl %edx, 0xcc(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x30(%rax), %rsi
xorl %edx, %edx
callq 0x145360
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x132c0
movl $0x0, 0x3c(%rsp)
movq 0x20(%rsp), %rdi
movl 0x3c(%rsp), %eax
movl %eax, 0x1c(%rsp)
addq $0x40, %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x751bc
movq 0x20(%rsp), %rdi
addq $0x40, %rdi
movl 0x3c(%rsp), %esi
callq 0x436f0
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
addq $0x88, %rdi
callq 0x1c310
movl %eax, 0x2c(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0xa8, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145f60
cmpl $0x0, %eax
jne 0x74f11
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0xb0, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x74f4b
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x8, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145c90
cmpl $0x0, %eax
jne 0x74f82
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1bde0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movl 0xcc(%rsp), %ecx
movl $0x3, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x74fcd
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rdi
addq $0x28, %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, %rdi
xorl %esi, %esi
callq 0x1bde0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movl 0xcc(%rsp), %ecx
movl $0x6, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x75022
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x80, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x7505c
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movl 0xcc(%rsp), %ecx
leaq 0x2c(%rsp), %rsi
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x7508f
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x60, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x750c6
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdi
addq $0x68, %rdi
xorl %esi, %esi
callq 0x1be00
movq (%rsp), %rdi
movq %rax, %rsi
movl 0xcc(%rsp), %ecx
movl $0x2, %edx
callq 0x145e40
cmpl $0x0, %eax
jne 0x7510f
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x7c, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x75146
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x78, %rsi
movl 0xcc(%rsp), %ecx
movl $0x1, %edx
callq 0x145d20
cmpl $0x0, %eax
jne 0x7517a
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
movq 0xd0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl 0xcc(%rsp), %ecx
movl $0x20, %edx
callq 0x145a90
cmpl $0x0, %eax
jne 0x751aa
movl $0x0, 0xe4(%rsp)
jmp 0x751c7
jmp 0x751ac
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x74e87
movl $0x1, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
nopw %cs:(%rax,%rax)