// File: /workspace/github_cpp_makefile_star2_O1/splatlab[P]debgr/main.cc
// Function: reads_to_kmers(kmercounting::chunk&, flush_object*)
void reads_to_kmers(chunk &c, flush_object *obj)
{
	auto fs = c.get_reads();
	auto fe = c.get_reads();
	auto end = fs + c.get_size();
	while (fs && fs!=end) {
		fs = static_cast<char*>(memchr(fs, '\n', end-fs)); // ignore the first line
		fs++; // increment the pointer

		fe = static_cast<char*>(memchr(fs, '\n', end-fs)); // read the read
		string read(fs, fe-fs);
		/*cout << read << endl;*/

start_read:
		if (read.length() < K) // start with the next read if length is smaller than K
			goto next_read;
		{
			uint64_t first = 0;
			uint64_t first_rev = 0;
			uint64_t first_can = 0;
			uint64_t item = 0;
			//cout << "K " << read.substr(0,K) << endl;
			for(int i=0; i<K; i++) { //First kmer
				uint8_t curr = kmer::map_base(read[i]);
				if (curr > DNA_MAP::G) { // 'N' is encountered
					read = read.substr(i+1, read.length());
					goto start_read;
				}
				first = first | curr;
				first = first << 2;
			}
			first = first >> 2;
			first_rev = kmer::reverse_complement(first);

			//cout << "kmer: "; cout << int_to_str(first);
			//cout << " reverse-comp: "; cout << int_to_str(first_rev) << endl;

			if (kmer::compare_kmers(first, first_rev))
				first_can = first;
			else
				first_can = first_rev;

			// create a set of true k-mers
			uint64_t exact_hash = HashUtil::hash_64(first_can, BITMASK(2*K));
			qf_insert(obj->exact_qf, exact_hash, 0, 1, true, true);
			//true_kmers.insert(first_can);

			// this is the first kmer of the read, hash using invertible hash and insert
			// in the first QF
			// No need to use the range, because by default the QF will use the
			// lower order 56 (q+r) bits.
			uint64_t node = kmer::prefix(first, K-1);
			node = canonicalize_node(node);
			record_end(node, first_can, obj);

			// hash the kmer using murmurhash/xxHash before adding to the list
			item = HashUtil::MurmurHash64A(((void*)&first_can), sizeof(first_can),
																		 obj->local_qf->metadata->seed);
			/*
			 * first try and insert in the main QF.
			 * If lock can't be accuired in the first attempt then
			 * insert the item in the local QF.
			 */
			if (!qf_insert(obj->main_qf, item%obj->main_qf->metadata->range, 0, 1,
										 true, false)) {
				qf_insert(obj->local_qf, item%obj->local_qf->metadata->range, 0, 1,
									false, false);
				obj->count++;
				// check of the load factor of the local QF is more than 50%
				if (obj->count > 1ULL<<(QBITS_LOCAL_QF-1)) {
					dump_local_qf_to_main(obj);
					obj->count = 0;
				}
			}
			//cout<< "X " << bitset<64>(first)<<endl;

			uint64_t next = first;
			uint64_t next_rev = first_rev;
			uint64_t next_can = first_can;

			for(uint32_t i=K; i<read.length(); i++) { //next kmers
				uint64_t nextn = kmer::suffix(next, K-1);
				nextn = canonicalize_node(nextn);
				if (can_have_duplex_edges(nextn))
					record_end(nextn, next_can, obj);
				//cout << "K: " << read.substr(i-K+1,K) << endl;
				uint8_t curr = kmer::map_base(read[i]);
				if (curr > DNA_MAP::G) { // 'N' is encountered
					read = read.substr(i+1, read.length());
					// don't insert if i = K and node is a duplex node.
					if (i > K || !can_have_duplex_edges(nextn)) {
						nextn = kmer::suffix(next, K-1);
						nextn = canonicalize_node(nextn);
						record_end(nextn, next_can, obj);
					}
					goto start_read;
				}
				next = (next << 2) & BITMASK(2*K);
				next_rev = next_rev >> 2;
				
				next |= curr;
				uint64_t tmp = kmer::reverse_complement_base(curr);
				tmp <<= (K*2-2);
				next_rev = next_rev | tmp;
				if (kmer::compare_kmers(next, next_rev))
					next_can = next;
				else
					next_can = next_rev;

				uint64_t exact_hash = HashUtil::hash_64(next_can, BITMASK(2*K));
				qf_insert(obj->exact_qf, exact_hash, 0, 1, true, true);
				//true_kmers.insert(next_can);

				// hash the kmer using murmurhash/xxHash before adding to the list
				item = HashUtil::MurmurHash64A(((void*)&next_can), sizeof(next_can),
																			 obj->local_qf->metadata->seed);
				//item = XXH63 (((void*)&item), sizeof(item), seed);

				/*
				 * first try and insert in the main QF.
				 * If lock can't be accuired in the first attempt then
				 * insert the item in the local QF.
				 */
				if (!qf_insert(obj->main_qf, item%obj->main_qf->metadata->range, 0, 1, true,
											 false)) {
					qf_insert(obj->local_qf, item%obj->local_qf->metadata->range, 0, 1, false,
										false);
					obj->count++;
					// check of the load factor of the local QF is more than 50%
					if (obj->count > 1ULL<<(QBITS_LOCAL_QF-1)) {
						dump_local_qf_to_main(obj);
						obj->count = 0;
					}
				}

				//cout<<bitset<64>(next)<<endl;
				//assert(next == str_to_int(read.substr(i-K+1,K)));

				if (can_have_duplex_edges(nextn))
					record_end(nextn, next_can, obj);
			}
			node = kmer::suffix(next, K-1);
			node = canonicalize_node(node);
			record_end(node, next_can, obj);
		}

next_read:
		fs = ++fe;		// increment the pointer
		fs = static_cast<char*>(memchr(fs, '\n', end-fs)); // ignore one line
		fs++; // increment the pointer
		fs = static_cast<char*>(memchr(fs, '\n', end-fs)); // ignore one more line
		fs++; // increment the pointer
	}
	free(c.get_reads());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, 0x8(%rsp)
movq (%rdi), %r12
movq %rdi, 0x60(%rsp)
movl 0x8(%rdi), %eax
testq %r12, %r12
setne %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x6851
movl %eax, %r14d
addq %r12, %r14
leaq 0x40(%rsp), %rbx
leaq 0x88(%rsp), %r15
movq %r14, 0x68(%rsp)
movq %r14, %rdx
subq %r12, %rdx
movq %r12, %rdi
movl $0xa, %esi
callq 0x53e0
leaq 0x1(%rax), %r12
movq %r14, %rdx
subq %r12, %rdx
movq %r12, %rdi
movl $0xa, %esi
callq 0x53e0
movq %rbx, 0x30(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rax, 0x70(%rsp)
movq %rax, %rdx
callq 0xa2b2
cmpq $0x1c, 0x38(%rsp)
jb 0x67fd
movq $0x0, 0x28(%rsp)
movl $0x1, %r12d
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
movzbl -0x1(%rax,%r12), %eax
movb $0x1, %bl
cmpl $0x46, %eax
jg 0x6295
cmpl $0x41, %eax
je 0x62af
cmpl $0x43, %eax
jne 0x62a6
xorl %eax, %eax
jmp 0x62bb
cmpl $0x47, %eax
je 0x62b6
cmpl $0x54, %eax
jne 0x62a6
movl $0x2, %eax
jmp 0x62bb
movl $0x4, %eax
xorl %ebx, %ebx
jmp 0x62bb
movl $0x1, %eax
jmp 0x62bb
movl $0x3, %eax
testb %bl, %bl
je 0x62cd
orq %r13, %rax
shlq $0x2, %rax
xorl %ebp, %ebp
movq %rax, %r13
jmp 0x6304
movq 0x38(%rsp), %rcx
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x53b0
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5460
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x62ff
callq 0x5330
movl $0x4, %ebp
testb %bl, %bl
je 0x631a
incq %r12
cmpq $0x1d, %r12
jne 0x6275
movl $0x6, %ebp
cmpl $0x6, %ebp
jne 0x67ed
movq %r13, %r12
shrq $0x2, %r12
movl $0x1c, %eax
xorl %ebx, %ebx
movq %r12, %rcx
movl %ecx, %edx
andl $0x3, %edx
addq %rbx, %rdx
shrq $0x2, %rcx
shlq $0x2, %rdx
movq %rdx, %rbx
xorq $0xc, %rbx
decl %eax
jne 0x6334
shrq $0x2, %rbx
cmpq %rbx, %r12
movq %rbx, %rdi
cmovaq %r12, %rdi
movq %rdi, 0x28(%rsp)
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
callq 0xb11c
movq 0x8(%rsp), %r14
movq 0x20(%r14), %rdi
movl $0x1, %ecx
movq %rax, %rsi
xorl %edx, %edx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xb9f5
shrq $0x4, %r13
movq %r13, %rdi
callq 0x5ebf
movq 0x28(%rsp), %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x6147
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %edx
leaq 0x28(%rsp), %rdi
movl $0x8, %esi
callq 0xaf54
movq %rax, %r13
movq 0x18(%r14), %rbp
movq 0x8(%rbp), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %r13, %rdi
xorl %esi, %esi
callq 0x5580
movl $0x1, %ecx
movq %rbp, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0xb9f5
testb %al, %al
jne 0x645a
movq 0x10(%r14), %rbp
movq 0x8(%rbp), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %r13, %rdi
xorl %esi, %esi
callq 0x5580
movl $0x1, %ecx
movq %rbp, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb9f5
movl 0x28(%r14), %eax
incl %eax
movl %eax, 0x28(%r14)
cmpl $0x8001, %eax           # imm = 0x8001
jb 0x645a
movq %r14, %rdi
callq 0x689d
movl $0x0, 0x28(%r14)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x9, %ebp
cmpq $0x1d, 0x38(%rsp)
jb 0x67bc
movl $0x1c, %eax
movq %rax, 0x18(%rsp)
movl $0x1c, %r15d
leaq 0x78(%rsp), %r14
movq %r12, %r13
movabsq $0x3fffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFF
andq %rax, %r13
movq %r13, %rdi
callq 0x5ebf
movq %rax, %rdx
movl $0x1b, %ecx
xorl %eax, %eax
movq %rdx, 0x20(%rsp)
movl %edx, %esi
andl $0x3, %esi
addq %rax, %rsi
shrq $0x2, %rdx
shlq $0x2, %rsi
movq %rsi, %rax
xorq $0xc, %rax
decl %ecx
jne 0x64b1
shrq $0x2, %rax
movq 0x20(%rsp), %rdi
movl %edi, %edx
andl $0x3, %edx
movabsq $0x55555555555555, %rcx # imm = 0x55555555555555
imulq %rcx, %rdx
movq %rdx, 0x50(%rsp)
cmpq %rdi, %rax
je 0x64f9
cmpq %rdi, 0x50(%rsp)
jne 0x6508
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x6147
movq 0x30(%rsp), %rax
movzbl (%rax,%r15), %eax
movb $0x1, %cl
cmpl $0x46, %eax
jg 0x6527
cmpl $0x41, %eax
je 0x6541
cmpl $0x43, %eax
jne 0x6538
xorl %eax, %eax
jmp 0x654d
cmpl $0x47, %eax
je 0x6548
cmpl $0x54, %eax
jne 0x6538
movl $0x2, %eax
jmp 0x654d
movl $0x4, %eax
xorl %ecx, %ecx
jmp 0x654d
movl $0x1, %eax
jmp 0x654d
movl $0x3, %eax
movl %ecx, 0x5c(%rsp)
testb %cl, %cl
je 0x66d0
movq %rbx, %rcx
shrq $0x2, %rcx
leaq (%rax,%r13,4), %r12
shlq $0x36, %rax
movabsq $0xc0000000000000, %rbx # imm = 0xC0000000000000
subq %rax, %rbx
orq %rcx, %rbx
cmpq %rbx, %r12
movq %rbx, %rdi
cmovaq %r12, %rdi
movq %rdi, 0x10(%rsp)
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
callq 0xb11c
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0x1, %ecx
movq %rax, %rsi
xorl %edx, %edx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0xb9f5
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %edx
leaq 0x10(%rsp), %rdi
movl $0x8, %esi
callq 0xaf54
movq %rax, %r13
movq 0x8(%rsp), %rax
movq 0x18(%rax), %r15
movq 0x8(%r15), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %r13, %rdi
xorl %esi, %esi
callq 0x5580
movl $0x1, %ecx
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0xb9f5
testb %al, %al
jne 0x667b
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r15
movq 0x8(%r15), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %r13, %rdi
xorl %esi, %esi
callq 0x5580
movl $0x1, %ecx
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb9f5
movq 0x8(%rsp), %rcx
movl 0x28(%rcx), %eax
incl %eax
movl %eax, 0x28(%rcx)
cmpl $0x8001, %eax           # imm = 0x8001
jb 0x667b
movq 0x8(%rsp), %rdi
callq 0x689d
movq 0x8(%rsp), %rax
movl $0x0, 0x28(%rax)
movl $0x1b, %eax
xorl %ecx, %ecx
movq 0x20(%rsp), %rdi
movq %rdi, %rdx
movl %edx, %esi
andl $0x3, %esi
addq %rcx, %rsi
shrq $0x2, %rdx
shlq $0x2, %rsi
movq %rsi, %rcx
xorq $0xc, %rcx
decl %eax
jne 0x668a
shrq $0x2, %rcx
xorl %r15d, %r15d
cmpq %rdi, %rcx
je 0x66bc
cmpq %rdi, 0x50(%rsp)
jne 0x6789
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x6147
jmp 0x6789
movq %r12, %rbp
movq 0x18(%rsp), %rax
leal 0x1(%rax), %edx
movq 0x38(%rsp), %rcx
movq %r14, %rdi
movq %r14, %r12
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x53b0
leaq 0x88(%rsp), %r15
movq %r14, %rdi
movq %r12, %rsi
callq 0x5460
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
movq %r12, %r14
je 0x6718
callq 0x5330
cmpl $0x1c, 0x18(%rsp)
movq %rbp, %r12
movl $0x9, %ebp
ja 0x6769
movl $0x1b, %eax
xorl %ecx, %ecx
movq 0x20(%rsp), %rdx
movl %edx, %esi
andl $0x3, %esi
addq %rcx, %rsi
shrq $0x2, %rdx
shlq $0x2, %rsi
movq %rsi, %rcx
xorq $0xc, %rcx
decl %eax
jne 0x6733
shrq $0x2, %rcx
movl $0x4, %r15d
movq 0x20(%rsp), %rax
cmpq %rax, %rcx
je 0x6789
cmpq %rax, 0x50(%rsp)
je 0x6789
movq %r13, %rdi
callq 0x5ebf
movl $0x4, %r15d
movq 0x10(%rsp), %rsi
movq %rax, %rdi
movq 0x8(%rsp), %rdx
callq 0x6147
cmpb $0x0, 0x5c(%rsp)
je 0x67ac
movq 0x18(%rsp), %rax
incl %eax
movq %rax, %r15
movq %rax, 0x18(%rsp)
cmpq %rax, 0x38(%rsp)
ja 0x648a
jmp 0x67af
movl %r15d, %ebp
movq 0x8(%rsp), %r14
leaq 0x88(%rsp), %r15
cmpl $0x9, %ebp
jne 0x67e8
movabsq $0x3fffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFF
andq %rax, %r12
movq %r12, %rdi
callq 0x5ebf
xorl %ebp, %ebp
movq 0x10(%rsp), %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x6147
leaq 0x30(%rsp), %r14
testl %ebp, %ebp
je 0x67fd
cmpq $0x1b, 0x38(%rsp)
ja 0x6263
movq 0x70(%rsp), %rdi
incq %rdi
movq 0x68(%rsp), %r14
movq %r14, %rdx
subq %rdi, %rdx
movl $0xa, %esi
callq 0x53e0
leaq 0x1(%rax), %rdi
movq %r14, %rdx
subq %rdi, %rdx
movl $0xa, %esi
callq 0x53e0
movq %rax, %r12
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rbx
cmpq %rbx, %rdi
je 0x6845
callq 0x5330
incq %r12
cmpq %r14, %r12
jne 0x6210
movq 0x60(%rsp), %rax
movq (%rax), %rdi
callq 0x5430
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x687e
jmp 0x687e
jmp 0x687e
jmp 0x687e
jmp 0x687e
jmp 0x687e
jmp 0x687e
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq 0x30(%rsp), %rdi
cmpq %rax, %rdi
je 0x6895
callq 0x5330
movq %rbx, %rdi
callq 0x5620