// File: /workspace/github_cpp_makefile_star2_O3/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, %r12d
movl %esi, 0xc(%rsp)
movq %rdi, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x4397(%rip), %rax      # 0x6fd8
movq (%rax), %rbx
movq (%rdi), %rax
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq *0x10(%rax)
leaq 0x78(%rsp), %r15
movq -0x10(%r15), %rdx
leaq 0x14e6(%rip), %rsi      # 0x4148
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2190
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x2c7a
callq 0x2120
leal 0x1(%r12), %edx
xorl %ebx, %ebx
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x21e0
testl %eax, %eax
je 0x2caf
leaq 0x20(%rsp), %rdi
callq 0x3348
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4322(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
leaq 0x1497(%rip), %rsi      # 0x4157
leaq 0x1496(%rip), %rdx      # 0x415d
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2190
movq %r12, 0x38(%rsp)
testl %r12d, %r12d
je 0x2d75
leaq 0x68(%rsp), %r13
leaq 0x20(%rsp), %rbx
leaq 0x40(%rsp), %rbp
movl $0x28, %edi
callq 0x2130
movq %rax, %r12
movq %rax, %rdi
movq %r13, %rsi
movl %r15d, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
callq 0x31ee
movq %r12, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x340a
movapd 0x10(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movb $0x1, 0x50(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x34b4
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x2d58
callq 0x339a
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2d67
callq 0x339a
incl %r15d
cmpl %r15d, 0x38(%rsp)
jne 0x2cee
leaq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2260
testb $0x1, %al
jne 0x2d8f
xorl %ebx, %ebx
jmp 0x2c91
movq 0x4242(%rip), %rbx      # 0x6fd8
movq (%rbx), %rcx
leaq 0x13d6(%rip), %rdi      # 0x4176
movl $0x5, %esi
movl $0x1, %edx
callq 0x2240
movq (%rbx), %rdi
leaq 0x139e(%rip), %rsi      # 0x4157
leaq 0x13d2(%rip), %rdx      # 0x4192
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2190
movq 0x28(%rsp), %r12
subq 0x20(%rsp), %r12
sarq $0x3, %r12
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r12
movq %r12, %rax
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 0x20a0
imull 0xc(%rsp), %r12d
leaq 0x40(%rsp), %rsi
xorl %edi, %edi
callq 0x2110
testl %r12d, %r12d
je 0x2e63
leaq 0x10(%rsp), %rbx
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
testb %al, %al
jne 0x2e55
callq 0x21b0
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
jmp 0x2e3e
movslq 0x10(%rsp), %rax
incl (%r15,%rax,4)
decl %r12d
jne 0x2e32
leaq 0x10(%rsp), %rsi
xorl %edi, %edi
callq 0x2110
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq %rcx, %rax
movl 0xc(%rsp), %esi
je 0x2e99
movq (%rax), %rdx
movslq (%rdx), %rdx
cmpl %esi, (%r15,%rdx,4)
sete 0x10(%rax)
addq $0x18, %rax
cmpq %rcx, %rax
jne 0x2e82
movq %r15, %rdi
callq 0x2180
movq 0x10(%rsp), %r15
subq 0x40(%rsp), %r15
jl 0x2ee3
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
setne %dl
cmpq %rcx, %rax
setg %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x2ee3
movq %rax, %rbp
subq %rcx, %rbp
jge 0x2ee8
subq %rcx, %rax
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
decq %r15
movq %rax, %rbp
jmp 0x2ee8
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x40e9(%rip), %rbx      # 0x6fd8
movq (%rbx), %rdi
leaq 0x12d8(%rip), %rsi      # 0x41d1
leaq 0x129f(%rip), %rdx      # 0x419f
xorl %eax, %eax
callq 0x2190
leaq 0x68(%rsp), %rdi
callq 0x2140
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x2170
movq 0x408b(%rip), %r13      # 0x6fb0
movq (%r13), %rcx
leaq 0x1279(%rip), %rdi      # 0x41a9
movl $0x17, %esi
movl $0x1, %edx
callq 0x2240
movq (%r13), %rbx
movq (%r14), %rax
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq *0x10(%rax)
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x2310
movq (%r13), %rdi
movl 0x8(%r14), %edx
leaq 0x1268(%rip), %rsi      # 0x41d5
xorl %eax, %eax
callq 0x2190
movq (%r13), %rbx
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
movl %eax, %r12d
movq (%r14), %rax
movq %r14, %rdi
callq *0x30(%rax)
testb %al, %al
je 0x2fa2
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
testl %eax, %eax
je 0x3102
leaq 0x127d(%rip), %rcx      # 0x4226
imulq $0x3b9aca00, %r15, %r14 # imm = 0x3B9ACA00
addq %rbp, %r14
leaq 0x1235(%rip), %rsi      # 0x41ef
xorl %ebp, %ebp
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2190
movq (%r13), %rdi
leaq 0x1262(%rip), %rsi      # 0x4236
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x2190
movq (%r13), %rdi
leaq 0x1274(%rip), %rsi      # 0x425e
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x2190
movq %r14, %xmm1
punpckldq 0x110d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
movq (%r13), %rdi
subpd 0x1111(%rip), %xmm1     # 0x4120
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd 0x111d(%rip), %xmm0     # 0x4140
leaq 0x1250(%rip), %rsi      # 0x427a
movb $0x1, %al
callq 0x2190
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x30cf
movl $0x1, %ebx
leaq 0x124b(%rip), %r14      # 0x4298
leaq 0x124d(%rip), %r12      # 0x42a1
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%r13), %rdi
movq %r14, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2190
movq 0x20(%rsp), %rax
leaq (%r15,%r15,2), %rdx
movq (%r13), %rcx
cmpb $0x0, 0x10(%rax,%rdx,8)
je 0x3095
movl $0x10, %esi
movl $0x1, %edx
leaq 0x1224(%rip), %rdi      # 0x42b2
callq 0x2240
jmp 0x30a9
movl $0x10, %esi
movl $0x1, %edx
movq %r12, %rdi
callq 0x2240
incl %ebp
movl %ebx, %r15d
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
incl %ebx
cmpq %r15, %rax
ja 0x3059
movq (%r13), %rdi
movq 0x38(%rsp), %rcx
movl %ecx, %edx
subl %ebp, %edx
leaq 0x11e0(%rip), %rsi      # 0x42c3
xorl %eax, %eax
callq 0x2190
movq (%r13), %rsi
movl $0xa, %edi
callq 0x2170
testl %ebp, %ebp
sete %bl
jmp 0x2c91
leaq 0x110d(%rip), %rcx      # 0x4216
jmp 0x2fa9
jmp 0x3199
jmp 0x3199
jmp 0x311c
jmp 0x311c
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x319c
movq %rbx, %rdi
callq 0x2070
leaq 0x68(%rsp), %rdi
callq 0x2140
movq 0x3e9b(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
leaq 0x108a(%rip), %rsi      # 0x41d1
leaq 0x1056(%rip), %rdx      # 0x41a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2190
callq 0x21f0
jmp 0x2c91
jmp 0x3199
movq %rdx, %r14
movq %rax, %rbx
movq %r12, %rdi
callq 0x2120
jmp 0x31b4
movq %rdx, %r14
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x3188
callq 0x339a
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x31b4
callq 0x339a
jmp 0x31b4
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3348
movq %rbx, %rdi
callq 0x2210
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x319c
movq %rbx, %rdi
callq 0x2070
movq 0x3e0f(%rip), %rax      # 0x6fd8
movq (%rax), %rcx
leaq 0xf9d(%rip), %rdi       # 0x4170
movl $0x5, %esi
movl $0x1, %edx
callq 0x2240
callq 0x21f0
jmp 0x2d88
jmp 0x3199