// File: ridgelab[P]Kmer-SSR/src/FindSSRs.cpp
// Function: consume(void*)
static
void* consume(void* consumer_args_vptr)
{
	// cast void pointer to correct pointer type
	ConsumerArguments* consumer_args = (ConsumerArguments*) consumer_args_vptr;

	// Extract the arguments out of the struct
	TaskQueue* tasks = consumer_args->tasks_ptr;
	ProgressMeter* progress_bar = consumer_args->progress_bar_ptr;
	OutputFile* ofd = consumer_args->ofd_ptr;
	AtomicityChecker* atomicity_checker = consumer_args->atomicity_checker_ptr;
	Arguments* args = consumer_args->arguments_ptr;

	// create a container for ssrs
	SSRcontainer* ssrs = new SSRcontainer();
	Task* task = nullptr;

	// while we have work to do
	while (true)
	{
		// get a task to perform
		task = tasks->get();
	
		// if the task is NULL, it's a signal that we're all done here
		if (task == nullptr)
		{
			break; // stop working because there isn't any work left to do
		}

		// add the ssrs to the container and *attempt* to write all ssrs in the container to file
		ssrs->add(task->getID(), findSSRs(task, args, atomicity_checker));
		ssrs->writeToFile(ofd, false, true); // false means it won't block if it can't write, true means clear the SSRcontainer (if it wrote)

		// update the progress bar with the work done
		progress_bar->updateProgress(task->size(), true);

		delete task;
	}

	// if we haven't printed out all the ssrs yet, do it
	if (!ssrs->empty())
	{
		ssrs->writeToFile(ofd, true, false); // true means it will block until it can write, false don't means clear the SSRcontainer (because we're about to delete it anyway)
	}

	// delete the arguments pointer and the SSRcontainer
	delete consumer_args;
	delete ssrs;

	return nullptr;
	//pthread_exit(NULL);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movl $0x8, %edi
callq 0x43a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
callq 0x5ac0
jmp 0x11c6d
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16af0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x11cb5
jmp 0x11d91
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x8, %esi
callq 0x43b0
jmp 0x11e19
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x58(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x16370
movq -0x58(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x12750
movq %rax, -0x88(%rbp)
jmp 0x11ce7
movq -0x88(%rbp), %rdx
movq -0x90(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x5e60
jmp 0x11d00
leaq -0x78(%rbp), %rdi
callq 0x4788
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x61c0
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x58(%rbp), %rdi
callq 0x163d0
movq -0xa0(%rbp), %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x207e0
movq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x11d72
movq -0x98(%rbp), %rdi
callq 0x16110
movq -0x98(%rbp), %rdi
movl $0x48, %esi
callq 0x43b0
jmp 0x11c7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4788
jmp 0x11e19
movq -0x40(%rbp), %rdi
callq 0x5cb0
testb $0x1, %al
jne 0x11db2
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x61c0
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x11de0
movq -0xa8(%rbp), %rdi
callq 0x137a0
movq -0xa8(%rbp), %rdi
movl $0x28, %esi
callq 0x43b0
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x11e0e
movq -0xb0(%rbp), %rdi
callq 0x5b10
movq -0xb0(%rbp), %rdi
movl $0x8, %esi
callq 0x43b0
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x46e0
nopw %cs:(%rax,%rax)