// File: /workspace/github_cpp_makefile_star2_O2/ChnMasterOG[P]KmerGO2/kmc_api/kmc_file.cpp
// Function: CKMCFile::GetCountersForRead_kmc1_both_strands(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned int, std::allocator<unsigned int>>&)
bool CKMCFile::GetCountersForRead_kmc1_both_strands(const std::string& read, std::vector<uint32>& counters)
{
	uint32 read_len = static_cast<uint32>(read.length());
	counters.resize(read.length() - kmer_length + 1);
	std::string transformed_read = read;
	for (char& c : transformed_read)
		c = CKmerAPI::num_codes[(uchar)c];

	uint32 i = 0;
	CKmerAPI kmer(kmer_length), kmer_rev(kmer_length);
	uint32 pos = 0;
	uint32 rev_pos = kmer_length - 1;

	uint32 counters_pos = 0;

	while (i + kmer_length - 1 < read_len)
	{
		bool contains_N = false;
		while (i < read_len && pos < kmer_length)
		{
			if (CKmerAPI::num_codes[(uchar)read[i]] < 0)
			{
				pos = 0;
				rev_pos = kmer_length - 1;
				kmer.clear();
				kmer_rev.clear();
				++i;
				uint32 wrong_kmers = MIN(i - counters_pos, static_cast<uint32>(counters.size()) - counters_pos);
				fill_n(counters.begin() + counters_pos, wrong_kmers, 0);
				counters_pos += wrong_kmers;
				contains_N = true;
				break;
			}
			else
			{
				kmer_rev.insert2bits(rev_pos--, 3 - CKmerAPI::num_codes[(uchar)read[i]]);
				kmer.insert2bits(pos++, CKmerAPI::num_codes[(uchar)read[i++]]);
				
			}
		}
		if (contains_N)
			continue;
		if (pos == kmer_length)
		{
			if(kmer < kmer_rev)
				counters[counters_pos++] = count_for_kmer_kmc1(kmer);
			else
				counters[counters_pos++] = count_for_kmer_kmc1(kmer_rev);
		}
		else
			break;

		while (i < read_len)
		{
			if (CKmerAPI::num_codes[(uchar)read[i]] < 0)
			{
				pos = 0;
				break;
			}
			kmer_rev.SHR_insert2bits(3 - CKmerAPI::num_codes[(uchar)read[i]]);
			kmer.SHL_insert2bits(CKmerAPI::num_codes[(uchar)read[i++]]);
			if(kmer < kmer_rev)
				counters[counters_pos++] = count_for_kmer_kmc1(kmer);
			else
				counters[counters_pos++] = count_for_kmer_kmc1(kmer_rev);
		}
	}
	if (counters_pos < counters.size())
	{
		fill_n(counters.begin() + counters_pos, counters.size() - counters_pos, 0);
		counters_pos = static_cast<uint32>(counters.size());
	}
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r13
movq %rdi, %r12
movq 0x8(%rsi), %rsi
movl 0x78(%rdi), %eax
movq %rsi, 0x28(%rsp)
subq %rax, %rsi
incq %rsi
movq %rdx, 0x18(%rsp)
movq %rdx, %rdi
callq 0x4123ea
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x486820
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
xorl %edx, %edx
movq $0x1b6b940, %rdi        # imm = 0x1B6B940
cmpq %rdx, %rcx
je 0x411901
movzbl (%rax,%rdx), %esi
movb (%rdi,%rsi), %sil
movb %sil, (%rax,%rdx)
incq %rdx
jmp 0x4118eb
movl 0x78(%r12), %esi
leaq 0x58(%rsp), %rdi
callq 0x407f72
movl 0x78(%r12), %esi
leaq 0x70(%rsp), %rdi
callq 0x407f72
movl 0x78(%r12), %ecx
leal -0x1(%rcx), %esi
movl 0x28(%rsp), %eax
movq %rax, 0x50(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq %r13, 0x48(%rsp)
movq %r12, 0x20(%rsp)
leal (%r14,%rcx), %eax
decl %eax
movq %rbx, (%rsp)
movl %ebx, %r15d
cmpl 0x28(%rsp), %eax
jae 0x411bc8
movq %r15, 0x40(%rsp)
movq %r14, 0x30(%rsp)
movl %r14d, %r13d
movl 0x8(%rsp), %eax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
leaq (%r15,%r13), %r14
cmpq 0x50(%rsp), %r14
jae 0x4119f5
movq 0x38(%rsp), %rax
leaq (%rax,%r15), %rbx
cmpl %ecx, %ebx
jae 0x4119f5
movq 0x48(%rsp), %r12
movq (%r12), %rax
addq %r13, %rax
movzbl (%r15,%rax), %eax
movq $0x1b6b940, %rbp        # imm = 0x1B6B940
movb (%rbp,%rax), %al
testb %al, %al
js 0x411b14
leal -0x1(%rsi), %r14d
movb $0x3, %cl
subb %al, %cl
movzbl %cl, %edx
leaq 0x70(%rsp), %rdi
callq 0x41241c
movq (%r12), %rax
addq %r13, %rax
movzbl (%r15,%rax), %eax
movzbl (%rbp,%rax), %edx
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
callq 0x41241c
movq 0x20(%rsp), %rax
movl 0x78(%rax), %ecx
incq %r15
movl %r14d, %esi
jmp 0x411977
movl 0x8(%rsp), %eax
addl %r15d, %eax
cmpl %ecx, %eax
movq 0x48(%rsp), %r13
movq 0x20(%rsp), %r12
movq $0x1b6b940, %rbx        # imm = 0x1B6B940
movq 0x40(%rsp), %r15
jne 0x411bc8
movq %rsi, 0x38(%rsp)
movq %rcx, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x70(%rsp), %rbp
movq %rbp, %rsi
callq 0x408018
testb %al, %al
leaq 0x58(%rsp), %rax
cmovneq %rax, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x411ff0
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r15,4)
movq 0x28(%rsp), %rax
cmpl %eax, %r14d
cmoval %r14d, %eax
movq %rax, 0x30(%rsp)
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpq 0x50(%rsp), %r14
movq 0x38(%rsp), %rsi
jae 0x411ba4
movq (%r13), %rax
movzbl (%rax,%r14), %eax
movb (%rbx,%rax), %al
testb %al, %al
js 0x411bb4
movb $0x3, %cl
subb %al, %cl
movzbl %cl, %esi
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x41243a
movq (%r13), %rax
movzbl (%rax,%r14), %eax
incq %r14
movzbl (%rbx,%rax), %esi
leaq 0x58(%rsp), %rbp
movq %rbp, %rdi
callq 0x4124a2
movq %rbp, %rdi
movq %r12, %rsi
callq 0x408018
movq %r13, %r15
movq (%rsp), %r13
movl %r13d, %ebx
testb %al, %al
movq %r12, %rsi
movq 0x20(%rsp), %r12
cmovneq %rbp, %rsi
movq %r12, %rdi
callq 0x411ff0
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%rbx,4)
movq $0x1b6b940, %rbx        # imm = 0x1B6B940
incl %r13d
movq %r13, (%rsp)
movq %r15, %r13
jmp 0x411a73
movq %rcx, %r12
movq 0x58(%rsp), %rdi
movl 0x68(%rsp), %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x401090
movq 0x70(%rsp), %rdi
movl 0x80(%rsp), %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x401090
movq (%rsp), %r14
movq 0x30(%rsp), %rax
subl %r14d, %eax
addl %r15d, %eax
incl %eax
movq 0x18(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rbx
subq %rcx, %rbx
shrq $0x2, %rbx
subl %r14d, %ebx
cmpl %ebx, %eax
cmovbl %eax, %ebx
movq 0x40(%rsp), %rax
leaq (%rcx,%rax,4), %rdi
andl $0x0, 0x14(%rsp)
movl %ebx, %esi
leaq 0x14(%rsp), %rdx
callq 0x412a67
leaq (%r15,%r13), %rax
incq %rax
decl %r12d
addl %r14d, %ebx
movq %rax, %r14
movl %r12d, %esi
xorl %eax, %eax
movq 0x20(%rsp), %r12
jmp 0x411bba
movq (%rsp), %rbx
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %r14
jmp 0x411bba
xorl %eax, %eax
movq (%rsp), %rbx
movl %eax, 0x8(%rsp)
movl 0x78(%r12), %ecx
jmp 0x411947
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rsi
subq %rax, %rsi
sarq $0x2, %rsi
subq %r15, %rsi
jbe 0x411bf1
leaq (%rax,%r15,4), %rdi
leaq 0x14(%rsp), %rdx
andl $0x0, (%rdx)
callq 0x412a8e
leaq 0x70(%rsp), %rdi
callq 0x40825c
leaq 0x58(%rsp), %rdi
callq 0x40825c
leaq 0x88(%rsp), %rdi
callq 0x484210
movb $0x1, %al
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x411c32
movq %rax, %rbx
jmp 0x411c3f
movq %rax, %rbx
jmp 0x411c49
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x40825c
leaq 0x58(%rsp), %rdi
callq 0x40825c
leaq 0x88(%rsp), %rdi
callq 0x484210
movq %rbx, %rdi
callq 0x4b8e10