// File: ChnMasterOG[P]KmerGO2/kmc_api/kmc_file.cpp
// Function: CKMCFile::ReadNextKmer(CKmerAPI&, unsigned long long&)
bool CKMCFile::ReadNextKmer(CKmerAPI &kmer, uint64 &count)
{
	uint64 prefix_mask = (1 << 2 * lut_prefix_length) - 1; //for kmc2 db

	if (is_opened != opened_for_listing)
		return false;
	do
	{
		if (end_of_file)
			return false;

		if (sufix_number == prefix_file_buf[prefix_index + 1])
		{
			prefix_index++;

			while (prefix_file_buf[prefix_index] == prefix_file_buf[prefix_index + 1])
				prefix_index++;
		}

		uint32 off = (sizeof(prefix_index)* 8) - (lut_prefix_length * 2) - kmer.byte_alignment * 2;

		uint64 temp_prefix = (prefix_index & prefix_mask) << off;	// shift prefix towards MSD. "& prefix_mask" necessary for kmc2 db format

		kmer.kmer_data[0] = temp_prefix;			// store prefix in an object CKmerAPI

		for (uint32 i = 1; i < kmer.no_of_rows; i++)
			kmer.kmer_data[i] = 0;

		//read sufix:
		uint32 row_index = 0;
		uint64 suf = 0;

		off = off - 8;

		for (uint32 a = 0; a < sufix_size; a++)
		{
			if (index_in_partial_buf == part_size)
				Reload_sufix_file_buf();

			suf = sufix_file_buf[index_in_partial_buf++];
			suf = suf << off;
			kmer.kmer_data[row_index] = kmer.kmer_data[row_index] | suf;

			if (off == 0)				//the end of a word in kmer_data
			{
				off = 56;
				row_index++;
			}
			else
				off -= 8;
		}

		//read counter:
		if (index_in_partial_buf == part_size)
			Reload_sufix_file_buf();

		count = sufix_file_buf[index_in_partial_buf++];

		for (uint32 b = 1; b < counter_size; b++)
		{
			if (index_in_partial_buf == part_size)
				Reload_sufix_file_buf();

			uint64 aux = 0x000000ff & sufix_file_buf[index_in_partial_buf++];
			aux = aux << 8 * (b);
			count = aux | count;
		}

		sufix_number++;

		if (sufix_number == total_kmers)
			end_of_file = true;

	} while ((count < min_count) || (count > max_count));

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movl 0x84(%rax), %ecx
shll %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rcx, -0x28(%rbp)
cmpl $0x2, (%rax)
je 0x4251b5
movb $0x0, -0x1(%rbp)
jmp 0x425498
jmp 0x4251b7
movq -0x60(%rbp), %rax
testb $0x1, 0x18(%rax)
je 0x4251ca
movb $0x0, -0x1(%rbp)
jmp 0x425498
movq -0x60(%rbp), %rdx
movq 0x68(%rdx), %rax
movq 0x30(%rdx), %rcx
movq 0x40(%rdx), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jne 0x425224
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
movq -0x60(%rbp), %rdx
movq 0x30(%rdx), %rax
movq 0x40(%rdx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x30(%rdx), %rcx
movq 0x40(%rdx), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jne 0x425222
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
jmp 0x4251f1
jmp 0x425224
movq -0x60(%rbp), %rax
movl 0x84(%rax), %ecx
shll %ecx
movl %ecx, %ecx
movl %ecx, %edx
movl $0x40, %ecx
subq %rdx, %rcx
movq -0x18(%rbp), %rdx
movzbl 0xc(%rdx), %edx
shll %edx
movslq %edx, %rdx
subq %rdx, %rcx
movl %ecx, -0x2c(%rbp)
movq 0x40(%rax), %rax
andq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq %cl, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x42529f
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x425276
movl $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl -0x2c(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x60(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0xa8(%rcx), %eax
jae 0x425369
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rax
cmpq 0x211e98(%rip), %rax    # 0x637178
jne 0x4252eb
movq -0x60(%rbp), %rdi
callq 0x4250c0
movq -0x60(%rbp), %rdx
movq 0x60(%rdx), %rax
movq 0x70(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x70(%rdx)
movzbl (%rax,%rcx), %eax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq %cl, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
orq -0x48(%rbp), %rdx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
cmpl $0x0, -0x2c(%rbp)
jne 0x425350
movl $0x38, -0x2c(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x425359
movl -0x2c(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0x42535b
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4252be
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rax
cmpq 0x211e00(%rip), %rax    # 0x637178
jne 0x425383
movq -0x60(%rbp), %rdi
callq 0x4250c0
movq -0x60(%rbp), %rdx
movq 0x60(%rdx), %rax
movq 0x70(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x70(%rdx)
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x50(%rbp)
movq -0x60(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl 0x80(%rcx), %eax
jae 0x42542d
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rax
cmpq 0x211dac(%rip), %rax    # 0x637178
jne 0x4253d7
movq -0x60(%rbp), %rdi
callq 0x4250c0
movq -0x60(%rbp), %rdx
movq 0x60(%rdx), %rax
movq 0x70(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x70(%rdx)
movzbl (%rax,%rcx), %eax
andl $0xff, %eax
cltq
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl -0x50(%rbp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rcx
movq -0x20(%rbp), %rax
orq (%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x4253ae
movq -0x60(%rbp), %rcx
movq 0x68(%rcx), %rax
addq $0x1, %rax
movq %rax, 0x68(%rcx)
movq 0x68(%rcx), %rax
cmpq 0x98(%rcx), %rax
jne 0x425452
movq -0x60(%rbp), %rax
movb $0x1, 0x18(%rax)
jmp 0x425454
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movl 0x8c(%rax), %eax
movl %eax, %edx
movb $0x1, %al
cmpq %rdx, %rcx
movb %al, -0x61(%rbp)
jb 0x425489
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpq 0x90(%rcx), %rax
seta %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x4251b7
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)