// File: nmmmnu[P]HM3/hm3/diskfile/diskfilebuilder_impl.h
// Function: bool hm3::diskfile::DiskFileBuilder::_writeIteratorToFile<hm3::Pair const*>(hm3::Pair const* const&, hm3::Pair const* 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:
subq $0xc8, %rsp
movb 0xd0(%rsp), %al
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq %rdx, 0xb0(%rsp)
movq %rcx, 0xa8(%rsp)
movq %r8, 0xa0(%rsp)
movq %r9, 0x98(%rsp)
andb $0x1, %al
movb %al, 0x97(%rsp)
movq $0x0, 0x80(%rsp)
movq $0x0, 0x78(%rsp)
movq $0x0, 0x70(%rsp)
movq $0x0, 0x68(%rsp)
movq $0x0, 0x60(%rsp)
movb $0x1, 0x5f(%rsp)
movq 0xb8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq 0xb0(%rsp), %rcx
cmpq (%rcx), %rax
je 0x6b4d
movq 0x50(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
callq 0x47a0
testb $0x1, %al
jne 0x6a2f
jmp 0x6b3a
movq 0x48(%rsp), %rdi
callq 0xd220
testb $0x1, %al
jne 0x6a3f
jmp 0x6a63
movb 0x97(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x6a55
jmp 0x6b3a
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
testb $0x1, 0x5f(%rsp)
je 0x6a94
movq 0x48(%rsp), %rdi
callq 0xcfe0
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x60(%rsp)
movb $0x0, 0x5f(%rsp)
jmp 0x6ad1
movq 0x48(%rsp), %rdi
callq 0xcfe0
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
cmpq 0x68(%rsp), %rax
jae 0x6ab9
movq 0x38(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x38(%rsp), %rax
cmpq 0x60(%rsp), %rax
jbe 0x6acf
movq 0x38(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x6ad1
movq 0x80(%rsp), %rdi
callq 0x4130
movq %rax, 0x88(%rsp)
movq 0xa0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x8, %edx
callq 0x22f0
movq 0x48(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0xd380
movq 0x48(%rsp), %rdi
callq 0xd2d0
addq 0x80(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
movq 0x50(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x50(%rsp)
jmp 0x69fc
movq 0x78(%rsp), %rsi
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
movq %rsp, %rdi
movl $0x1, %edx
callq 0x11790
movq 0xa8(%rsp), %rsi
movq %rsp, %rdi
callq 0x11c50
andb $0x1, %al
addq $0xc8, %rsp
retq
nopl (%rax,%rax)