// File: 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
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 0x424d45
movb $0x0, -0x1(%rbp)
jmp 0x4250b0
jmp 0x424d47
movq -0x60(%rbp), %rax
testb $0x1, 0x18(%rax)
je 0x424d5a
movb $0x0, -0x1(%rbp)
jmp 0x4250b0
movq -0x60(%rbp), %rdx
movq 0x68(%rdx), %rax
movq 0x30(%rdx), %rcx
movq 0x40(%rdx), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jne 0x424db4
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 0x424db2
movq -0x60(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
jmp 0x424d81
jmp 0x424db4
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 0x424e2f
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 0x424e06
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 0x424ef9
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rax
cmpq 0x212308(%rip), %rax    # 0x637178
jne 0x424e7b
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 0x424ee0
movl $0x38, -0x2c(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x424ee9
movl -0x2c(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0x424eeb
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x424e4e
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rax
cmpq 0x212270(%rip), %rax    # 0x637178
jne 0x424f13
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), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x50(%rbp)
movq -0x60(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl 0x80(%rcx), %eax
jae 0x424faf
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rax
cmpq 0x21221f(%rip), %rax    # 0x637178
jne 0x424f64
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
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x50(%rbp), %ecx
shll $0x3, %ecx
shll %cl, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
orl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x424f3b
movq -0x60(%rbp), %rcx
movq 0x68(%rcx), %rax
addq $0x1, %rax
movq %rax, 0x68(%rcx)
movq 0x68(%rcx), %rax
cmpq 0x98(%rcx), %rax
jne 0x424fd4
movq -0x60(%rbp), %rax
movb $0x1, 0x18(%rax)
movq -0x60(%rbp), %rax
cmpl $0x0, 0x7c(%rax)
je 0x425071
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rsi
movl 0x80(%rax), %eax
movl %eax, %edx
leaq -0x58(%rbp), %rdi
callq 0x401080
movq -0x60(%rbp), %rax
movss -0x58(%rbp), %xmm1
movl 0x8c(%rax), %eax
cvtsi2ss %rax, %xmm0
ucomiss %xmm1, %xmm0
ja 0x42506d
movq -0x60(%rbp), %rax
movss -0x58(%rbp), %xmm0
movss %xmm0, -0x6c(%rbp)
movq 0x90(%rax), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x68(%rbp)
testq %rax, %rax
movss %xmm0, -0x64(%rbp)
js 0x42505e
movss -0x68(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
movss -0x6c(%rbp), %xmm0
movss -0x64(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x42506f
jmp 0x425073
jmp 0x4250ac
jmp 0x425073
movq -0x60(%rbp), %rdx
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movb $0x1, %al
cmpl 0x8c(%rdx), %ecx
movb %al, -0x6d(%rbp)
jb 0x4250a1
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movl (%rax), %eax
cmpq 0x90(%rcx), %rax
seta %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x424d47
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)