// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %rbx
movb 0x84(%rdi), %cl
addb %cl, %cl
pushq $-0x1
popq %rdi
shll %cl, %edi
cmpl $0x2, (%rbx)
jne 0x410c1c
movq %rdx, %r14
movq %rsi, %r15
notl %edi
movq %rdi, 0x10(%rsp)
movb 0x18(%rbx), %al
movl %eax, 0x8(%rsp)
testb %al, %al
jne 0x410c14
movq 0x68(%rbx), %rdx
movq 0x30(%rbx), %rax
movq 0x40(%rbx), %rcx
cmpq 0x8(%rax,%rcx,8), %rdx
jne 0x410a6d
leaq 0x1(%rcx), %rsi
movq %rsi, 0x40(%rbx)
movq 0x8(%rax,%rcx,8), %rdx
cmpq 0x10(%rax,%rcx,8), %rdx
movq %rsi, %rcx
je 0x410a54
jmp 0x410a70
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
pushq $0x1
popq %rsi
cmpq %rcx, %rsi
jae 0x410aa2
andq $0x0, (%rax,%rsi,8)
incq %rsi
jmp 0x410a93
pushq $0x38
popq %rcx
movl %ecx, %r13d
subl %edx, %r13d
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x70(%rbx), %rdx
movq 0x2036bd(%rip), %rcx    # 0x614178
cmpl 0xa8(%rbx), %r12d
jae 0x410b10
cmpq %rcx, %rdx
jne 0x410ad8
movq %rbx, %rdi
callq 0x410c32
movq 0x70(%rbx), %rdx
movq (%r15), %rax
movq 0x60(%rbx), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x70(%rbx)
movzbl (%rcx,%rdx), %edx
movl %r13d, %ecx
shlq %cl, %rdx
movl %ebp, %ecx
orq %rdx, (%rax,%rcx,8)
xorl %ecx, %ecx
testl %r13d, %r13d
leal -0x8(%r13), %edx
pushq $0x38
popq %rsi
cmovel %esi, %edx
sete %cl
addl %ecx, %ebp
incl %r12d
movl %edx, %r13d
jmp 0x410ab0
cmpq %rcx, %rdx
jne 0x410b21
movq %rbx, %rdi
callq 0x410c32
movq 0x70(%rbx), %rdx
movq 0x60(%rbx), %rax
leaq 0x1(%rdx), %rcx
movq %rcx, 0x70(%rbx)
movzbl (%rax,%rdx), %ebp
movl %ebp, (%r14)
pushq $0x8
popq %r13
pushq $0x1
popq %r12
movl 0x80(%rbx), %edx
cmpl %edx, %r12d
jae 0x410b85
cmpq 0x20362a(%rip), %rcx    # 0x614178
jne 0x410b63
movq %rbx, %rdi
callq 0x410c32
movq 0x60(%rbx), %rax
movq 0x70(%rbx), %rcx
movl (%r14), %ebp
leaq 0x1(%rcx), %rdx
movq %rdx, 0x70(%rbx)
movzbl (%rax,%rcx), %esi
movl %r13d, %ecx
shll %cl, %esi
orl %esi, %ebp
movl %ebp, (%r14)
incl %r12d
addl $0x8, %r13d
movq %rdx, %rcx
jmp 0x410b3c
movq 0x68(%rbx), %rax
incq %rax
movq %rax, 0x68(%rbx)
cmpq 0x98(%rbx), %rax
jne 0x410b9d
movb $0x1, 0x18(%rbx)
cmpl $0x0, 0x7c(%rbx)
movl 0x8c(%rbx), %r12d
je 0x410bf7
leaq 0xc(%rsp), %rdi
movq %r14, %rsi
callq 0x401080
movl %r12d, %eax
cvtsi2ss %rax, %xmm1
movss 0xc(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x410bf7
movq 0x90(%rbx), %rax
testq %rax, %rax
js 0x410bdd
cvtsi2ss %rax, %xmm1
jmp 0x410bf2
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x410c2d
cmpl %r12d, %ebp
movq 0x10(%rsp), %rdi
jb 0x410a32
movl %ebp, %eax
cmpq %rax, 0x90(%rbx)
jb 0x410a32
movl 0x8(%rsp), %eax
xorb $0x1, %al
jmp 0x410c1e
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x410c1e
nop