// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
shll %cl, %edi
cmpl $0x2, (%rbx)
jne 0x417581
movq %rdx, %r14
movq %rsi, %r15
notl %edi
movq %rdi, 0x10(%rsp)
movb 0x18(%rbx), %al
movl %eax, 0xc(%rsp)
testb %al, %al
jne 0x417579
movq 0x68(%rbx), %rdx
movq 0x30(%rbx), %rax
movq 0x40(%rbx), %rcx
cmpq 0x8(%rax,%rcx,8), %rdx
jne 0x417407
incq %rcx
movq %rcx, 0x40(%rbx)
movq (%rax,%rcx,8), %rdx
leaq 0x1(%rcx), %rsi
cmpq 0x8(%rax,%rcx,8), %rdx
movq %rsi, %rcx
je 0x4173f1
movzbl 0xc(%r15), %ebp
addl 0x84(%rbx), %ebp
addl %ebp, %ebp
movl 0x40(%rbx), %eax
andl %edi, %eax
movl %ebp, %ecx
negb %cl
shlq %cl, %rax
movq (%r15), %rdi
movq %rax, (%rdi)
movl 0x10(%r15), %edx
cmpl $0x2, %edx
jb 0x417440
addq $0x8, %rdi
decl %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x401098
cmpl $0x0, 0xa8(%rbx)
je 0x4174b5
movl $0x38, %r12d
subl %ebp, %r12d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x70(%rbx), %rax
cmpq 0x202d16(%rip), %rax    # 0x61a178
jne 0x41746c
movq %rbx, %rdi
callq 0x417332
movq 0x60(%rbx), %rax
movq 0x70(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %eax
movl %r12d, %ecx
shlq %cl, %rax
movq (%r15), %rcx
movl %r13d, %edx
orq %rax, (%rcx,%rdx,8)
xorl %eax, %eax
testl %r12d, %r12d
leal -0x8(%r12), %ecx
sete %al
movl $0x38, %edx
cmovel %edx, %ecx
addl %eax, %r13d
incl %ebp
movl %ecx, %r12d
cmpl 0xa8(%rbx), %ebp
jb 0x417457
movq 0x70(%rbx), %rax
cmpq 0x202cb8(%rip), %rax    # 0x61a178
jne 0x4174ca
movq %rbx, %rdi
callq 0x417332
movq 0x60(%rbx), %rax
movq 0x70(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %eax
movq %rax, (%r14)
cmpl $0x2, 0x80(%rbx)
jb 0x41753d
movl $0x1, %r12d
movl $0x8, %r13d
movq 0x70(%rbx), %rax
cmpq 0x202c77(%rip), %rax    # 0x61a178
jne 0x41750b
movq %rbx, %rdi
callq 0x417332
movq 0x60(%rbx), %rax
movq 0x70(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %eax
movl %r13d, %ecx
andb $-0x8, %cl
shlq %cl, %rax
orq %rax, (%r14)
incq %r12
movl 0x80(%rbx), %eax
addq $0x8, %r13
cmpq %rax, %r12
jb 0x4174f6
movq 0x68(%rbx), %rax
incq %rax
movq %rax, 0x68(%rbx)
cmpq 0x98(%rbx), %rax
jne 0x417555
movb $0x1, 0x18(%rbx)
movq (%r14), %rax
movl 0x8c(%rbx), %ecx
cmpq %rcx, %rax
movq 0x10(%rsp), %rdi
jb 0x4173cc
cmpq 0x90(%rbx), %rax
ja 0x4173cc
movl 0xc(%rsp), %eax
xorb $0x1, %al
jmp 0x417583
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq