// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rsi, 0x8(%rsp)
movq (%rdi), %rbx
movq %rdi, 0x50(%rsp)
movl 0x8(%rdi), %r14d
addq %rbx, %r14
leaq 0x30(%rsp), %r15
leaq 0x68(%rsp), %r13
movq %r14, 0x28(%rsp)
testq %rbx, %rbx
je 0x61b9
cmpq %r14, %rbx
je 0x61b9
movq %r14, %rdx
subq %rbx, %rdx
movq %rbx, %rdi
pushq $0xa
popq %rbp
movl %ebp, %esi
callq 0x53b0
leaq 0x1(%rax), %rbx
movq %r14, %rdx
subq %rbx, %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x53b0
movq %rax, %rdx
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %rdx, 0x58(%rsp)
callq 0x9258
cmpq $0x1c, 0x38(%rsp)
jb 0x617d
andq $0x0, 0x20(%rsp)
pushq $0x1
popq %rbx
xorl %r12d, %r12d
cmpq $0x1d, %rbx
je 0x5e0e
movq 0x30(%rsp), %rax
movsbl -0x1(%rax,%rbx), %edi
callq 0x74bc
cmpb $0x4, %al
jae 0x60d4
movzbl %al, %eax
orq %rax, %r12
shlq $0x2, %r12
incq %rbx
jmp 0x5de2
movq %r12, %r15
shrq $0x2, %r15
movq %r15, %rdi
callq 0x597c
movq %rax, %r13
cmpq %rax, %r15
movq %rax, %rdi
cmovaq %r15, %rdi
movq %rdi, 0x20(%rsp)
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
callq 0xa0a4
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq %rax, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0xa8b1
shrq $0x4, %r12
movq %r12, %rdi
callq 0x5b48
movq 0x20(%rsp), %rsi
movq %rax, %rdi
movq 0x8(%rsp), %rdx
callq 0x5ccd
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %edx
leaq 0x20(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x9ee8
movq %rax, %r12
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rbx
movq 0x8(%rbx), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %r12, %rdi
xorl %esi, %esi
callq 0x5550
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
pushq $0x1
popq %r8
xorl %r9d, %r9d
callq 0xa8b1
testb %al, %al
jne 0x5f2f
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
movq 0x8(%rbx), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %r12, %rdi
xorl %esi, %esi
callq 0x5550
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xa8b1
movq 0x8(%rsp), %rcx
movl 0x28(%rcx), %eax
incl %eax
movl %eax, 0x28(%rcx)
cmpl $0x8001, %eax           # imm = 0x8001
jb 0x5f2f
movq 0x8(%rsp), %rdi
callq 0x61fb
movq 0x8(%rsp), %rax
andl $0x0, 0x28(%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
pushq $0x1c
popq %rax
movl %eax, %r12d
movl %r12d, %r12d
movq 0x38(%rsp), %rbx
movabsq $0x3fffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFF
andq %rax, %r15
movq %r15, %rdi
callq 0x5b48
movq %rax, %rbp
cmpq %r12, %rbx
jbe 0x615c
movq %rbp, %rdi
callq 0x5b85
movl %eax, %ebx
testb %al, %al
je 0x5f88
movq 0x18(%rsp), %rsi
movq %rbp, %rdi
movq 0x8(%rsp), %rdx
callq 0x5ccd
movq 0x30(%rsp), %rax
movsbl (%rax,%r12), %edi
callq 0x74bc
cmpb $0x4, %al
jae 0x60ff
movb %bl, 0x17(%rsp)
movq %rbp, 0x60(%rsp)
movq %r13, %rcx
shrq $0x2, %rcx
movzbl %al, %edx
leaq (%rdx,%r15,4), %r15
movb $0x3, %dl
subb %al, %dl
movzbl %dl, %r13d
shlq $0x36, %r13
orq %rcx, %r13
cmpq %r13, %r15
movq %r13, %rdi
cmovaq %r15, %rdi
movq %rdi, 0x18(%rsp)
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
callq 0xa0a4
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq %rax, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0xa8b1
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %edx
leaq 0x18(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x9ee8
movq %rax, %rbx
movq 0x8(%rsp), %rbp
movq 0x18(%rbp), %r14
movq 0x8(%r14), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5550
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
pushq $0x1
popq %r8
xorl %r9d, %r9d
callq 0xa8b1
testb %al, %al
jne 0x60b1
movq 0x10(%rbp), %r14
movq 0x8(%r14), %rax
movq 0x40(%rax), %rdx
movq 0x48(%rax), %rcx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5550
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xa8b1
movq 0x8(%rsp), %rcx
movl 0x28(%rcx), %eax
incl %eax
movl %eax, 0x28(%rcx)
cmpl $0x8001, %eax           # imm = 0x8001
jb 0x60b1
movq 0x8(%rsp), %rdi
callq 0x61fb
movq 0x8(%rsp), %rax
andl $0x0, 0x28(%rax)
cmpb $0x0, 0x17(%rsp)
je 0x60cc
movq 0x18(%rsp), %rsi
movq 0x60(%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0x5ccd
incl %r12d
jmp 0x5f3f
movq 0x38(%rsp), %rcx
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5380
movq %r15, %rdi
movq %r13, %rsi
callq 0x5420
movq %r13, %rdi
callq 0x5130
jmp 0x5dca
leal 0x1(%r12), %edx
movq 0x38(%rsp), %rcx
leaq 0x68(%rsp), %r13
movq %r13, %rdi
leaq 0x30(%rsp), %r15
movq %r15, %rsi
callq 0x5380
movq %r15, %rdi
movq %r13, %rsi
callq 0x5420
movq %r13, %rdi
callq 0x5130
cmpl $0x1d, %r12d
setb %al
testb %bl, %al
movq 0x28(%rsp), %r14
jne 0x5dca
movq 0x18(%rsp), %rsi
movq %rbp, %rdi
movq 0x8(%rsp), %rdx
callq 0x5ccd
jmp 0x5dca
movq 0x18(%rsp), %rsi
movq %rbp, %rdi
movq 0x8(%rsp), %rdx
callq 0x5ccd
movq 0x28(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x68(%rsp), %r13
movq 0x58(%rsp), %rdi
incq %rdi
movq %r14, %rdx
subq %rdi, %rdx
pushq $0xa
popq %rbx
movl %ebx, %esi
callq 0x53b0
leaq 0x1(%rax), %rdi
movq %r14, %rdx
subq %rdi, %rdx
movl %ebx, %esi
callq 0x53b0
movq %rax, %rbx
incq %rbx
movq %r15, %rdi
callq 0x5130
jmp 0x5d74
movq 0x50(%rsp), %rax
movq (%rax), %rdi
callq 0x53f0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x61e6
jmp 0x61e6
jmp 0x61e6
jmp 0x61e6
jmp 0x61e6
jmp 0x61e6
jmp 0x61e6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5130
movq %rbx, %rdi
callq 0x5600