// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movb 0x84(%rdi), %cl
addb %cl, %cl
pushq $-0x1
popq %rdi
shll %cl, %edi
cmpl $0x2, (%rbx)
jne 0x410e61
movq %rdx, %r14
movq %rsi, %r15
notl %edi
pushq $0x1
popq %rbp
movq %rdi, 0x10(%rsp)
movb 0x18(%rbx), %al
movl %eax, 0xc(%rsp)
testb %al, %al
jne 0x410e59
movq 0x68(%rbx), %rdx
movq 0x30(%rbx), %rax
movq 0x40(%rbx), %rcx
cmpq 0x8(%rax,%rcx,8), %rdx
jne 0x410d02
leaq 0x1(%rcx), %rsi
movq %rsi, 0x40(%rbx)
movq 0x8(%rax,%rcx,8), %rdx
cmpq 0x10(%rax,%rcx,8), %rdx
movq %rsi, %rcx
je 0x410ce9
jmp 0x410d05
movq %rcx, %rsi
movzbl 0xc(%r15), %edx
addl 0x84(%rbx), %edx
addl %edx, %edx
andl %edi, %esi
movl %edx, %ecx
negb %cl
shlq %cl, %rsi
movq (%r15), %rax
movq %rsi, (%rax)
movl 0x10(%r15), %ecx
movq %rbp, %rsi
cmpq %rcx, %rsi
jae 0x410d37
andq $0x0, (%rax,%rsi,8)
incq %rsi
jmp 0x410d28
pushq $0x38
popq %rcx
movl %ecx, %r12d
subl %edx, %r12d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x70(%rbx), %rcx
movq 0x203428(%rip), %rdx    # 0x614178
cmpl 0xa8(%rbx), %r13d
jae 0x410da6
cmpq %rdx, %rcx
jne 0x410d6d
movq %rbx, %rdi
callq 0x410c32
movq 0x70(%rbx), %rcx
movq (%r15), %rax
movq 0x60(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x70(%rbx)
movzbl (%rdx,%rcx), %edx
movl %r12d, %ecx
shlq %cl, %rdx
movl %ebp, %ecx
orq %rdx, (%rax,%rcx,8)
xorl %ecx, %ecx
testl %r12d, %r12d
leal -0x8(%r12), %edx
pushq $0x38
popq %rsi
cmovel %esi, %edx
sete %cl
addl %ecx, %ebp
incl %r13d
movl %edx, %r12d
jmp 0x410d45
cmpq %rdx, %rcx
jne 0x410db7
movq %rbx, %rdi
callq 0x410c32
movq 0x70(%rbx), %rcx
movq 0x60(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %ecx
movq %rcx, (%r14)
pushq $0x8
popq %r12
pushq $0x1
popq %rbp
movq %rbp, %r13
movl 0x80(%rbx), %ecx
cmpq %rcx, %r13
jae 0x410e1d
movq 0x70(%rbx), %rcx
cmpq 0x20338e(%rip), %rcx    # 0x614178
jne 0x410dfc
movq %rbx, %rdi
callq 0x410c32
movq 0x60(%rbx), %rax
movq 0x70(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %edx
movl %r12d, %ecx
andb $-0x8, %cl
shlq %cl, %rdx
orq %rdx, (%r14)
incq %r13
addq $0x8, %r12
jmp 0x410dd4
movq 0x68(%rbx), %rax
incq %rax
movq %rax, 0x68(%rbx)
cmpq 0x98(%rbx), %rax
jne 0x410e35
movb $0x1, 0x18(%rbx)
movq (%r14), %rax
movl 0x8c(%rbx), %ecx
cmpq %rcx, %rax
movq 0x10(%rsp), %rdi
jb 0x410cc7
cmpq 0x90(%rbx), %rax
ja 0x410cc7
movl 0xc(%rsp), %eax
xorb $0x1, %al
jmp 0x410e63
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq