// File: /workspace/github_cpp_makefile_star2_O1/ChnMasterOG[P]KmerGO2/kmc_api/kmc_file.cpp
// Function: CKMCFile::ReadNextKmer(CKmerAPI&, unsigned int&)
bool CKMCFile::ReadNextKmer(CKmerAPI &kmer, uint32 &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();
			
			uint32 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;

		if (mode != 0)
		{
			float float_counter;
			memcpy(&float_counter, &count, counter_size);
			if ((float_counter < min_count) || (float_counter > max_count))
				continue;
			else
				break;
		}

	}
	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
movb 0x84(%rdi), %cl
addb %cl, %cl
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
shll %cl, %r12d
cmpl $0x2, (%rdi)
jne 0x41731d
movq %rdi, %rbx
movq %rdx, %r14
movq %rsi, %r15
notl %r12d
movq %r12, 0x10(%rsp)
movb 0x18(%rbx), %al
movl %eax, 0x8(%rsp)
testb %al, %al
jne 0x417315
movq 0x68(%rbx), %rdx
movq 0x30(%rbx), %rax
movq 0x40(%rbx), %rcx
cmpq 0x8(%rax,%rcx,8), %rdx
jne 0x41714a
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 0x417134
movzbl 0xc(%r15), %ebp
addl 0x84(%rbx), %ebp
addl %ebp, %ebp
movl 0x40(%rbx), %eax
andl %r12d, %eax
movl %ebp, %ecx
negb %cl
shlq %cl, %rax
movq (%r15), %rdi
movq %rax, (%rdi)
movl 0x10(%r15), %edx
cmpl $0x2, %edx
jb 0x417184
addq $0x8, %rdi
decl %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x401098
cmpl $0x0, 0xa8(%rbx)
je 0x4171f8
movl $0x38, %r13d
subl %ebp, %r13d
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x70(%rbx), %rax
cmpq 0x202fd2(%rip), %rax    # 0x61a178
jne 0x4171b0
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
shlq %cl, %rax
movq (%r15), %rcx
movl %r12d, %edx
orq %rax, (%rcx,%rdx,8)
xorl %eax, %eax
testl %r13d, %r13d
leal -0x8(%r13), %ecx
sete %al
movl $0x38, %edx
cmovel %edx, %ecx
addl %eax, %r12d
incl %ebp
movl %ecx, %r13d
cmpl 0xa8(%rbx), %ebp
jb 0x41719b
movq 0x70(%rbx), %rax
cmpq 0x202f75(%rip), %rax    # 0x61a178
jne 0x41720d
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 %eax, (%r14)
movl 0x80(%rbx), %eax
cmpl $0x2, %eax
jb 0x41727b
movl $0x1, %ebp
movl $0x8, %r13d
movq 0x70(%rbx), %rax
cmpq 0x202f33(%rip), %rax    # 0x61a178
jne 0x41724f
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
shll %cl, %eax
orl %eax, (%r14)
incl %ebp
movl 0x80(%rbx), %eax
addl $0x8, %r13d
cmpl %eax, %ebp
jb 0x41723a
movq 0x68(%rbx), %rcx
incq %rcx
movq %rcx, 0x68(%rbx)
cmpq 0x98(%rbx), %rcx
movq 0x10(%rsp), %r12
jne 0x417298
movb $0x1, 0x18(%rbx)
cmpl $0x0, 0x7c(%rbx)
je 0x4172f9
movl %eax, %edx
leaq 0xc(%rsp), %rdi
movq %r14, %rsi
callq 0x401080
movl 0x8c(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
movss 0xc(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x4172f9
movq 0x90(%rbx), %rax
testq %rax, %rax
js 0x4172dc
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
jmp 0x4172f4
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x41732e
movl (%r14), %eax
cmpl 0x8c(%rbx), %eax
jb 0x41710f
cmpq %rax, 0x90(%rbx)
jb 0x41710f
movl 0x8(%rsp), %eax
xorb $0x1, %al
jmp 0x41731f
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x41731f