// File: /workspace/github_cpp_makefile_star2_O2/enfiskutensykkel[P]lamport/src/producer.cpp
// Function: Producer::test_queue(Queue&, unsigned int, unsigned int)
bool Producer::test_queue(Queue& queue, unsigned repetitions, unsigned num_threads)
{
	std::vector<TProducer> tests;

	fprintf(stderr, "Testing %s...\n", queue.type().c_str());

	// Create a barrier to synchronize threads
	pthread_barrier_t barrier;
	if (pthread_barrier_init(&barrier, nullptr, num_threads + 1) != 0)
	{
		assert(false);
		return false;
	}

	// Create and start threads
	fprintf(stderr, "%-25s", "Creating producers");
	for (unsigned i = 0; i < num_threads; ++i)
	{
		try
		{
			tests.push_back(std::make_pair(std::shared_ptr<Producer>( new Producer(barrier, i, queue, repetitions) ), true));
		}
		catch (const std::runtime_error& e)
		{
			fprintf(stderr, "FAIL\n");
			return false;
		}
	}
	fprintf(stderr, "DONE\n");

	// Synchronize with threads
	fprintf(stderr, "%-25s", "Synchronizing threads");
	int ret = pthread_barrier_wait(&barrier);
	if (ret != PTHREAD_BARRIER_SERIAL_THREAD && ret != 0)
	{
		tests.clear(); // this invokes the destructor on all producers
		pthread_barrier_destroy(&barrier);
		fprintf(stderr, "FAIL\n");
		return false;
	}
	fprintf(stderr, "DONE\n");

	// Run tests
	uint64_t duration;
	fprintf(stderr, "%-25s", "Running test");
	try
	{
		duration = test_dequeuing(queue, tests, repetitions);
		fprintf(stderr, "%s\n", "DONE");
	}
	catch (const std::runtime_error& e)
	{
		pthread_barrier_destroy(&barrier);
		fprintf(stderr, "%s\n", "FAIL");
		return false;
	}
	pthread_barrier_destroy(&barrier);
	fprintf(stderr, "\n");

	// Write summary
	fprintf(stdout, "\033[1;96mTest result\033[0m\n");
	fprintf(stdout, "Queue type    : %s\n", queue.type().c_str());
	fprintf(stdout, "Queue capacity: %u slots\n", queue.capacity);
	fprintf(stdout, "Queue size    : %u elements left (%s)\n", queue.size(), queue.empty() && queue.size() == 0 ? "\033[0;92mPASS\033[0m" : "\033[0;91mFAIL\033[0m");
	fprintf(stdout, "Queue elements: %u elements per thread\n", repetitions);
	fprintf(stdout, "Producers     : %u threads\n", num_threads);
	fprintf(stdout, "Total duration: %.5f seconds\n", duration / (1000000000.0));

	unsigned failed = 0;
	for (unsigned i = 0; i < tests.size(); ++i)
	{
		fprintf(stdout, "    %2u\t", i + 1);
		if (!tests[i].second)
		{
			fprintf(stdout, "\033[0;91mFAIL\033[0m\n"); 
			++failed;
		}
		else
		{
			fprintf(stdout, "\033[0;92mPASS\033[0m\n");
		}
	}
	fprintf(stdout, "%u out of %u producer threads passed\n", (num_threads - failed), num_threads);
	fprintf(stdout, "\n");

	return failed == 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, %r13d
movl %esi, 0xc(%rsp)
movq %rdi, %r14
xorpd %xmm0, %xmm0
andq $0x0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
movq 0x458e(%rip), %rax      # 0x6fd8
movq (%rax), %rbx
movq (%rdi), %rax
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq *0x10(%rax)
leaq 0x68(%rsp), %r12
movq (%r12), %rdx
leaq 0x16ad(%rip), %rsi      # 0x4118
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2190
movq %r12, %rdi
callq 0x2090
leal 0x1(%r13), %edx
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x21e0
testl %eax, %eax
je 0x2ab3
leaq 0x20(%rsp), %rdi
callq 0x3156
movl %r15d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x38(%rsp)
movq 0x4519(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
leaq 0x165e(%rip), %rsi      # 0x4127
leaq 0x165d(%rip), %rdx      # 0x412d
xorl %eax, %eax
callq 0x2190
leaq 0x10(%rsp), %r12
leaq 0x20(%rsp), %r13
leaq 0x40(%rsp), %rbx
cmpl %r15d, 0x38(%rsp)
je 0x2b5a
pushq $0x28
popq %rdi
callq 0x2130
movq %rax, %rbp
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movl %r15d, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
callq 0x3002
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3232
movapd 0x10(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movb $0x1, 0x50(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x32e4
leaq 0x48(%rsp), %rdi
callq 0x3182
leaq 0x18(%rsp), %rdi
callq 0x3182
incl %r15d
jmp 0x2ae6
movq 0x4477(%rip), %rbx      # 0x6fd8
movq (%rbx), %rcx
leaq 0x15db(%rip), %rdi      # 0x4146
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
movq (%rbx), %rdi
leaq 0x15a7(%rip), %rsi      # 0x4127
leaq 0x15c5(%rip), %rdx      # 0x414c
xorl %eax, %eax
callq 0x2190
leaq 0x68(%rsp), %rdi
callq 0x2150
decl %eax
cmpl $-0x3, %eax
ja 0x2bd0
leaq 0x20(%rsp), %rdi
callq 0x314e
leaq 0x68(%rsp), %rdi
callq 0x2140
movq (%rbx), %rcx
leaq 0x1583(%rip), %rdi      # 0x4140
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
xorl %r15d, %r15d
jmp 0x2a94
movq (%rbx), %rcx
leaq 0x156c(%rip), %rdi      # 0x4146
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
movq (%rbx), %rdi
leaq 0x1538(%rip), %rsi      # 0x4127
leaq 0x156c(%rip), %rdx      # 0x4162
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2190
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %r12
shrq $0x3e, %rax
leaq (,%r12,4), %rbx
negq %rax
sbbq %r15, %r15
orq %rbx, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20c0
imull 0xc(%rsp), %r12d
incl %r12d
leaq 0x40(%rsp), %rsi
xorl %edi, %edi
callq 0x2110
leaq 0x10(%rsp), %rbx
decl %r12d
je 0x2c83
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
testb %al, %al
jne 0x2c78
callq 0x21b0
jmp 0x2c61
movslq 0x10(%rsp), %rax
incl (%r15,%rax,4)
jmp 0x2c5c
leaq 0x10(%rsp), %rsi
xorl %edi, %edi
callq 0x2110
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movl 0xc(%rsp), %esi
cmpq %rcx, %rax
je 0x2cb6
movq (%rax), %rdx
movslq (%rdx), %rdx
cmpl %esi, (%r15,%rdx,4)
sete 0x10(%rax)
addq $0x18, %rax
jmp 0x2c9d
movq %r15, %rdi
callq 0x2180
movq 0x10(%rsp), %r12
subq 0x40(%rsp), %r12
jl 0x2d00
setne %dl
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
cmpq %rcx, %rax
setg %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x2d00
movq %rax, %r13
subq %rcx, %r13
jge 0x2d06
subq %rcx, %rax
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
decq %r12
movq %rax, %r13
jmp 0x2d06
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x42cb(%rip), %rbx      # 0x6fd8
movq (%rbx), %rdi
leaq 0x148a(%rip), %rsi      # 0x41a1
leaq 0x1451(%rip), %rdx      # 0x416f
xorl %eax, %eax
callq 0x2190
leaq 0x68(%rsp), %rdi
callq 0x2140
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x2170
movq 0x426f(%rip), %rbp      # 0x6fb0
movq (%rbp), %rcx
leaq 0x142d(%rip), %rdi      # 0x4179
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
movq (%rbp), %rbx
movq (%r14), %rax
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq *0x10(%rax)
leaq 0x40(%rsp), %r15
movq (%r15), %rdx
leaq 0x1419(%rip), %rsi      # 0x4191
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2190
movq %r15, %rdi
callq 0x2090
movq (%rbp), %rdi
movl 0x8(%r14), %edx
leaq 0x140c(%rip), %rsi      # 0x41a5
xorl %eax, %eax
callq 0x2190
movq (%rbp), %rbx
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
movl %eax, %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x30(%rax)
testb %al, %al
je 0x2dcd
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
testl %eax, %eax
je 0x2f24
leaq 0x1422(%rip), %rcx      # 0x41f6
imulq $0x3b9aca00, %r12, %r14 # imm = 0x3B9ACA00
addq %r13, %r14
leaq 0x13da(%rip), %rsi      # 0x41bf
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2190
movq 0x41b8(%rip), %rbx      # 0x6fb0
movq (%rbx), %rdi
leaq 0x1404(%rip), %rsi      # 0x4206
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x2190
movq (%rbx), %rdi
leaq 0x1417(%rip), %rsi      # 0x422e
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x2190
movq %r14, %xmm1
punpckldq 0x12b0(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
movq (%rbx), %rdi
subpd 0x12b5(%rip), %xmm1     # 0x40f0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd 0x12c1(%rip), %xmm0     # 0x4110
leaq 0x13f4(%rip), %rsi      # 0x424a
movb $0x1, %al
callq 0x2190
leaq 0x1404(%rip), %r14      # 0x4268
pushq $0x10
popq %r12
pushq $0x1
popq %rbx
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movl %ebp, %r13d
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq 0x411f(%rip), %rbp      # 0x6fb0
movq (%rbp), %rdi
cmpq %r13, %rax
jbe 0x2ef2
movq %rbp, %r15
leal 0x1(%r13), %ebp
movq %r14, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2190
movq 0x20(%rsp), %rax
imulq $0x18, %r13, %rdx
movq (%r15), %rcx
cmpb $0x0, 0x10(%rax,%rdx)
je 0x2ed4
leaq 0x13bb(%rip), %rdi      # 0x4282
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2230
jmp 0x2e75
leaq 0x1396(%rip), %rdi      # 0x4271
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2230
incl 0xc(%rsp)
incl %r13d
movl %r13d, %ebp
jmp 0x2e75
movq 0x38(%rsp), %rcx
movl %ecx, %edx
movl 0xc(%rsp), %ebx
subl %ebx, %edx
leaq 0x138d(%rip), %rsi      # 0x4293
xorl %eax, %eax
callq 0x2190
movq (%rbp), %rsi
pushq $0xa
popq %rdi
callq 0x2170
testl %ebx, %ebx
sete %r15b
jmp 0x2a94
leaq 0x12bb(%rip), %rcx      # 0x41e6
jmp 0x2dd4
jmp 0x2f36
jmp 0x2f36
jmp 0x2fb3
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3156
movq %rbx, %rdi
callq 0x2210
movq %rdx, %r14
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3182
leaq 0x18(%rsp), %rdi
callq 0x3182
jmp 0x2f7d
movq %rdx, %r14
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2120
jmp 0x2f7d
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x2f39
movq %rbx, %rdi
callq 0x2080
movq 0x4046(%rip), %rax      # 0x6fd8
movq (%rax), %rcx
leaq 0x11a4(%rip), %rdi      # 0x4140
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
callq 0x21f0
jmp 0x2bc8
jmp 0x2f36
movq %rax, %rbx
cmpl $0x1, %edx
movq 0x4018(%rip), %r14      # 0x6fd8
jne 0x2f39
movq %rbx, %rdi
callq 0x2080
leaq 0x68(%rsp), %rdi
callq 0x2140
movq (%r14), %rdi
leaq 0x11bf(%rip), %rsi      # 0x41a1
leaq 0x118b(%rip), %rdx      # 0x4174
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2190
callq 0x21f0
jmp 0x2a94
jmp 0x2f36