// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rsi, 0x8(%rsp)
cmpl $0x2, (%rbx)
jne 0x417129
movq %rdx, %r14
notl %edi
movq %rdi, 0x10(%rsp)
movb 0x18(%rbx), %al
movl %eax, 0x4(%rsp)
testb %al, %al
jne 0x417121
movq 0x68(%rbx), %rax
movq 0x30(%rbx), %rcx
movq 0x40(%rbx), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jne 0x416f97
leaq 0x1(%rdx), %rax
movq %rax, 0x40(%rbx)
movq 0x8(%rcx,%rdx,8), %rsi
cmpq 0x10(%rcx,%rdx,8), %rsi
movq %rax, %rdx
je 0x416f7e
jmp 0x416f9a
movq %rdx, %rax
movq 0x8(%rsp), %rdx
movzbl 0xc(%rdx), %r15d
addl 0x84(%rbx), %r15d
addl %r15d, %r15d
andl %edi, %eax
movl %r15d, %ecx
negb %cl
shlq %cl, %rax
movq (%rdx), %rbp
movq %rax, (%rbp)
movl 0x10(%rdx), %edx
cmpl $0x2, %edx
jb 0x416fd8
leaq 0x8(%rbp), %rdi
decl %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x401098
movl 0xa8(%rbx), %eax
testl %eax, %eax
je 0x417059
movl $0x38, %r12d
subl %r15d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x70(%rbx), %rcx
cmpq 0x20217c(%rip), %rcx    # 0x619178
jne 0x417018
movq %rbx, %rdi
callq 0x416ec0
movq 0x70(%rbx), %rcx
movq 0x8(%rsp), %rax
movq (%rax), %rbp
movl 0xa8(%rbx), %eax
movq 0x60(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x70(%rbx)
movzbl (%rdx,%rcx), %edx
movl %r12d, %ecx
shlq %cl, %rdx
movl %r15d, %ecx
orq %rdx, (%rbp,%rcx,8)
xorl %ecx, %ecx
testl %r12d, %r12d
leal -0x8(%r12), %edx
sete %cl
movl $0x38, %esi
cmovel %esi, %edx
addl %ecx, %r15d
incl %r13d
movl %edx, %r12d
cmpl %eax, %r13d
jb 0x416ff1
movq 0x70(%rbx), %rcx
cmpq 0x202114(%rip), %rcx    # 0x619178
jne 0x417072
movq %rbx, %rdi
callq 0x416ec0
movq 0x70(%rbx), %rcx
movq 0x60(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %ecx
movq %rcx, (%r14)
movl 0x80(%rbx), %edx
cmpl $0x2, %edx
jb 0x4170e5
movl $0x1, %r15d
movl $0x8, %r12d
movq 0x70(%rbx), %rcx
cmpq 0x2020d1(%rip), %rcx    # 0x619178
jne 0x4170bf
movq %rbx, %rdi
callq 0x416ec0
movq 0x60(%rbx), %rax
movq 0x70(%rbx), %rcx
movl 0x80(%rbx), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x70(%rbx)
movzbl (%rax,%rcx), %esi
movl %r12d, %ecx
andb $-0x8, %cl
shlq %cl, %rsi
orq %rsi, (%r14)
incq %r15
movl %edx, %ecx
addq $0x8, %r12
cmpq %rcx, %r15
jb 0x41709c
movq 0x68(%rbx), %rax
incq %rax
movq %rax, 0x68(%rbx)
cmpq 0x98(%rbx), %rax
jne 0x4170fd
movb $0x1, 0x18(%rbx)
movq (%r14), %rax
movl 0x8c(%rbx), %ecx
cmpq %rcx, %rax
movq 0x10(%rsp), %rdi
jb 0x416f5c
cmpq 0x90(%rbx), %rax
ja 0x416f5c
movl 0x4(%rsp), %eax
xorb $0x1, %al
jmp 0x41712b
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq