// File: /workspace/github_cpp_makefile_star2_O1/nmmmnu[P]HM3/hm3/diskfile/diskfilebuilder_impl.h
// Function: bool hm3::diskfile::DiskFileBuilder::_writeIteratorToFile<hm3::LinkList::Iterator>(hm3::LinkList::Iterator const&, hm3::LinkList::Iterator const&, std::basic_ofstream<char, std::char_traits<char>>&, std::basic_ofstream<char, std::char_traits<char>>&, std::basic_ofstream<char, std::char_traits<char>>&, bool) const
bool DiskFileBuilder::_writeIteratorToFile(const ITERATOR &begin, const ITERATOR &end,
			std::ofstream &file_meta,
			std::ofstream &file_indx,
			std::ofstream &file_data,
			bool const keepTombstones) const{
	uint64_t be;

	size_t current = 0;

	size_t datacount = 0;
	size_t tombstones = 0;
	uint64_t createdMin = 0;
	uint64_t createdMax = 0;

	bool first_created = true;

	for(ITERATOR it = begin; it != end; ++it){
		const auto &pair = *it;

		if (! pair )
			continue;

		if (pair.isTombstone()){
			if (keepTombstones == false)
					continue;

			++tombstones;
		}

		if (first_created){
			uint64_t const created = pair.getCreated();

			createdMin = created;
			createdMax = created;

			first_created = false;
		}else{
			uint64_t const created = pair.getCreated();

			if (created < createdMin)
				createdMin = created;

			if (created > createdMax)
				createdMax = created;
		}

		// write the index
		be = htobe64(current);
		file_indx.write( (const char *) & be, sizeof(uint64_t));

		// write the data
		pair.fwrite(file_data);

		current += pair.getBytes();
		++datacount;

	//	pair.print();
	}

	// now we miraculasly have the datacount.

	// write table header
	DiskFileHeader head{datacount, true, tombstones, createdMin, createdMax};
	return head.writeToFile(file_meta);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq (%rsi), %rax
leaq 0x40(%rsp), %rdi
movq %rax, (%rdi)
movq %rdx, 0x30(%rsp)
movq %rdx, %rsi
callq 0x5ce8
testb %al, %al
je 0x45c0
xorl %ebx, %ebx
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
jmp 0x4692
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r12d, %r12d
leaq 0x40(%rsp), %r14
xorl %ebp, %ebp
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x5ce4
cmpq $0x0, (%rax)
je 0x4675
movq %rax, %r15
movq %rax, %rdi
callq 0x6b48
testb %al, %al
je 0x460e
cmpb $0x0, 0xb0(%rsp)
je 0x4675
incq 0x10(%rsp)
movq %r15, %rdi
callq 0x6aba
testb $0x1, 0xc(%rsp)
je 0x462d
movl $0x0, 0xc(%rsp)
movq %rax, %rbx
movq %rax, %r13
jmp 0x463b
cmpq %rbx, %rax
cmovbq %rax, %rbx
cmpq %r13, %rax
cmovaq %rax, %r13
movq %r12, %rax
bswapq %rax
movq %rax, 0x38(%rsp)
movl $0x8, %edx
movq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2280
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x6bac
movq %r15, %rdi
callq 0x6b70
addq %rax, %r12
incq %rbp
movq %r14, %rdi
callq 0x5cd6
movq %r14, %rdi
movq 0x30(%rsp), %rsi
callq 0x5ce8
testb %al, %al
je 0x45de
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movl $0x1, %edx
movq 0x10(%rsp), %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x83ec
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x863e
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq