// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq (%rdi), %r12
movq 0x8(%rdi), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rdi), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x20(%rdi), %r14
pushq $0x8
popq %rdi
callq 0x32e0
movq %rax, %rbx
movq %rax, %rdi
callq 0x3fe0
leaq 0x28(%rsp), %r15
pushq $0x1
popq %r13
movq %r12, %rdi
callq 0x8cd4
testq %rax, %rax
je 0x75ad
movq %rax, %rbp
movq %r15, %rdi
movq %rax, %rsi
callq 0x88de
movq %rbp, %rdi
movq %r14, %rsi
movq 0x20(%rsp), %rdx
callq 0x7a74
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x41bc
movq %r15, %rdi
callq 0x35d8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x43a2
movq %rbp, %rdi
callq 0x88fe
movq 0x18(%rsp), %rdi
movl %eax, %esi
movl %r13d, %edx
callq 0xbda2
movq %rbp, %rdi
callq 0x87c4
movq %rbp, %rdi
pushq $0x48
popq %rsi
callq 0x32f0
jmp 0x752e
movq %rbx, %rdi
callq 0x40f2
testb %al, %al
jne 0x75cb
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
xorl %ecx, %ecx
callq 0x43a2
pushq $0x28
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x32f0
movq %rbx, %rdi
callq 0x4016
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x32f0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x32f0
jmp 0x7619
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x35d8
movq %r14, %rdi
callq 0x3550
nop