// File: /workspace/github_cpp_makefile_star2_O2/ridgelab[P]Kmer-SSR/src/FindSSRs.cpp
// Function: FindSSRs::processInput()
void FindSSRs::processInput() // produce
{
	ifstream fasta_file;
	fasta_file.open(this->args->getInFileName().c_str());

	// init (set the data_size of) the progress bar
	uint32_t init_size = this->args->getInFileName() == "/dev/stdin" ? 0 : calculateDataSizeFromFasta(fasta_file);
	this->progress_bar->initialize(init_size);

	string header = "";
	string sequence = "";
	string line = "";
	while (getline(fasta_file, line))
	{
		if (line.size())
		{
			if (line[0] != '>') // line is a sequence
			{
				this->progress_bar->updateProgress(1, false); // +1 for the '\n'

				for (uint32_t i = 0; i < line.size(); ++i) // -1 because we don't want to add the '\n' to the sequence
				{
					//sequence = sequence + (char) toupper(line[i]);
					sequence += (char) toupper(line[i]);
					//if (!isspace(line[i])) // TODO
					//{
					//	sequence += (char) toupper(line[i]);
					//}
					//else
					//{
					//	// update the progress bar based on the space
					//}
				}
			}
			else // line is a header
			{
				this->processSequence(header,sequence);

				header = line.substr(1, string::npos);
				sequence = "";
				
				this->progress_bar->updateProgress(header.size() + 2, false); // +1 for the '\n', +1 for the '>'
			}
		}
		else // empty line
		{
			this->progress_bar->updateProgress(1, false); // +1 for the '\n'
		}
		
	}
	
	this->processSequence(header,sequence); // catch the last sequence

	fasta_file.close(); // close the input file

	// put some NULL tasks in the TaskQueue, signalling the threads they can quit when they encounter one
	switch (this->args->getNumThreads())
	{
		case 1:
			break;
		default:
			for (uint32_t i = 1; i < this->args->getNumThreads(); ++i)
			{
				this->tasks->add(nullptr);
			}
			break;
	}

	// if there are plenty of tasks left (more than 2 tasks, excluding the dummy tasks, per thread)
	if (this->tasks->size() > (this->args->getNumThreads() * 3))
	{
		// the main thread becomes a consumer for a while
		SSRcontainer* ssrs = new SSRcontainer();
		Task* task = nullptr;

		// but only until each thread will have 2 tasks left (excluding the dummy task)
		while (this->tasks->size() > (this->args->getNumThreads() * 3))
		{
			// get a task
			task = this->tasks->get(); // get a task
		
			// fulfill the task, add the results to the SSRcontainer, attempt to write them to file
			ssrs->add(task->getID(), findSSRs(task, this->args, this->atomicity_checker));
			ssrs->writeToFile(this->out_file, false, true); // false means it won't block if it can't write, true means it will clear the SSRcontainer if it wrote
		
			// let the user know we've made some progress
			progress_bar->updateProgress(task->size(), true);

			// delete the ask now that we've completed it
			delete task;
		}

		// if we didn't write everything, let's do it now
		if (!ssrs->empty())
		{
			ssrs->writeToFile(this->out_file, true, false); // true means it will block until it can write, false means it won't clear the SSRcontainer when finished (because we're about to delete the thing anyway right after this)
		}

		delete ssrs;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdi, %rbx
leaq 0x80(%rsp), %rdi
callq 0x3330
movq (%rbx), %rsi
movq %rsp, %rdi
callq 0xa25c
movq (%rsp), %rsi
leaq 0x80(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x31d0
movq %rsp, %rdi
callq 0x35d8
movq (%rbx), %rsi
movq %rsp, %rdi
callq 0xa25c
leaq 0x516c(%rip), %rsi      # 0xc22c
movq %rsp, %rdi
callq 0x8006
xorl %r14d, %r14d
testb %al, %al
jne 0x711b
leaq 0x80(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x33e0
leaq 0x80(%rsp), %rdi
callq 0x3540
movq %rax, %r14
leaq 0x80(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x33e0
movq 0x80(%rsp), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x3500
movq %rsp, %rdi
callq 0x35d8
movq 0x28(%rbx), %rdi
movl %r14d, %esi
callq 0xbd5a
leaq 0x4f56(%rip), %rsi      # 0xc08c
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
callq 0x47b0
leaq 0x4f42(%rip), %rsi      # 0xc08c
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x47b0
leaq 0x4f2c(%rip), %rsi      # 0xc08c
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x47b0
leaq 0x80(%rsp), %r14
leaq 0x20(%rsp), %r15
pushq $0x1
popq %rbp
leaq 0x40(%rsp), %r13
movq %r14, %rdi
movq %r15, %rsi
callq 0x3070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x724f
cmpq $0x0, 0x28(%rsp)
movl %ebp, %esi
je 0x7202
movq 0x20(%rsp), %rax
cmpb $0x3e, (%rax)
jne 0x7212
movq %rbx, %rdi
movq %rsp, %rsi
movq %r13, %rdx
callq 0x7724
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x3300
movq %rsp, %rdi
movq %r12, %rsi
callq 0x3390
movq %r12, %rdi
callq 0x35d8
movq %r13, %rdi
leaq 0x4e96(%rip), %rsi      # 0xc08c
callq 0x35b0
movl 0x8(%rsp), %esi
addl $0x2, %esi
movq 0x28(%rbx), %rdi
xorl %edx, %edx
callq 0xbda2
jmp 0x7184
movq 0x28(%rbx), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xbda2
xorl %r12d, %r12d
movl %r12d, %r12d
cmpq %r12, 0x28(%rsp)
jbe 0x7184
movq 0x20(%rsp), %rax
movsbl (%rax,%r12), %edi
callq 0x3590
movsbl %al, %esi
movq %r13, %rdi
callq 0x3280
incl %r12d
jmp 0x7222
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
movq %rbx, %rdi
callq 0x7724
leaq 0x80(%rsp), %rdi
callq 0x3090
movq (%rbx), %rdi
callq 0xa18e
cmpl $0x1, %eax
jne 0x7352
movq 0x38(%rbx), %rdi
callq 0x8c6a
movl %eax, %ebp
movq (%rbx), %rdi
callq 0xa18e
leal (%rax,%rax,2), %eax
cmpl %eax, %ebp
jbe 0x73a4
pushq $0x8
popq %rdi
callq 0x32e0
movq %rax, %r14
movq %rax, %rdi
callq 0x3fe0
leaq 0x60(%rsp), %r15
pushq $0x1
popq %rbp
movq 0x38(%rbx), %rdi
callq 0x8c6a
movl %eax, %r13d
movq (%rbx), %rdi
callq 0xa18e
leal (%rax,%rax,2), %eax
cmpl %eax, %r13d
jbe 0x7374
movq 0x38(%rbx), %rdi
callq 0x8cd4
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0x88de
movq (%rbx), %rsi
movq 0x30(%rbx), %rdx
movq %r13, %rdi
callq 0x7a74
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x41bc
movq %r15, %rdi
callq 0x35d8
movq 0x20(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
movl %ebp, %ecx
callq 0x43a2
movq 0x28(%rbx), %r12
movq %r13, %rdi
callq 0x88fe
movq %r12, %rdi
movl %eax, %esi
movl %ebp, %edx
callq 0xbda2
movq %r13, %rdi
callq 0x87c4
movq %r13, %rdi
pushq $0x48
popq %rsi
callq 0x32f0
jmp 0x72b6
pushq $0x1
popq %rbp
movq (%rbx), %rdi
callq 0xa18e
cmpl %eax, %ebp
jae 0x727d
movq 0x38(%rbx), %rdi
xorl %esi, %esi
callq 0x8c82
incl %ebp
jmp 0x7355
movq %r14, %rdi
callq 0x40f2
testb %al, %al
jne 0x7391
movq 0x20(%rbx), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x43a2
movq %r14, %rdi
callq 0x4016
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x32f0
leaq 0x20(%rsp), %rdi
callq 0x35d8
leaq 0x40(%rsp), %rdi
callq 0x35d8
movq %rsp, %rdi
callq 0x35d8
leaq 0x80(%rsp), %rdi
callq 0x30d0
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x32f0
jmp 0x742a
jmp 0x7427
jmp 0x7427
movq %rax, %rbx
jmp 0x7434
jmp 0x7407
jmp 0x7402
jmp 0x7402
jmp 0x7402
jmp 0x7407
movq %rax, %rbx
jmp 0x7446
movq %rax, %rbx
jmp 0x743e
jmp 0x7427
jmp 0x7427
jmp 0x7427
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x35d8
jmp 0x742a
jmp 0x7427
jmp 0x7427
jmp 0x7427
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35d8
leaq 0x40(%rsp), %rdi
callq 0x35d8
movq %rsp, %rdi
callq 0x35d8
leaq 0x80(%rsp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x3550
nop