// File: /workspace/github_cpp_makefile_star2_O1/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 0x4387(%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 0x14d6(%rip), %rsi      # 0x4148
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2190
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x2c8a
callq 0x2120
leal 0x1(%r12), %edx
xorl %ebx, %ebx
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x21e0
testl %eax, %eax
je 0x2cbf
leaq 0x20(%rsp), %rdi
callq 0x3352
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4312(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
leaq 0x1487(%rip), %rsi      # 0x4157
leaq 0x1486(%rip), %rdx      # 0x415d
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2190
movq %r12, 0x40(%rsp)
testl %r12d, %r12d
je 0x2d85
leaq 0x68(%rsp), %r13
leaq 0x20(%rsp), %rbx
leaq 0x48(%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 0x31fa
movq %r12, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x340e
movupd 0x10(%rsp), %xmm0
movupd %xmm0, 0x48(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movb $0x1, 0x58(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x34c0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2d68
callq 0x339e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2d77
callq 0x339e
incl %r15d
cmpl %r15d, 0x40(%rsp)
jne 0x2cfe
leaq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2262
testb $0x1, %al
jne 0x2d9f
xorl %ebx, %ebx
jmp 0x2ca1
movq 0x4232(%rip), %rbx      # 0x6fd8
movq (%rbx), %rcx
leaq 0x13c6(%rip), %rdi      # 0x4176
movl $0x5, %esi
movl $0x1, %edx
callq 0x2240
movq (%rbx), %rdi
leaq 0x138e(%rip), %rsi      # 0x4157
leaq 0x13c2(%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 0x48(%rsp), %rsi
xorl %edi, %edi
callq 0x2110
testl %r12d, %r12d
je 0x2e73
leaq 0x10(%rsp), %rbx
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
testb %al, %al
jne 0x2e65
callq 0x21b0
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
jmp 0x2e4e
movslq 0x10(%rsp), %rax
incl (%r15,%rax,4)
decl %r12d
jne 0x2e42
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 0x2ea9
movq (%rax), %rdx
movslq (%rdx), %rdx
cmpl %esi, (%r15,%rdx,4)
sete 0x10(%rax)
addq $0x18, %rax
cmpq %rcx, %rax
jne 0x2e92
movq %r15, %rdi
callq 0x2180
movq 0x10(%rsp), %r15
subq 0x48(%rsp), %r15
jge 0x2ec4
xorl %ebp, %ebp
xorl %r15d, %r15d
jmp 0x2ef3
jne 0x2ed2
movq 0x18(%rsp), %rax
cmpq 0x50(%rsp), %rax
jle 0x2ebd
movq 0x18(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rax, %rbp
subq %rcx, %rbp
jge 0x2ef3
subq %rcx, %rax
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
decq %r15
movq %rax, %rbp
movq 0x40de(%rip), %rbx      # 0x6fd8
movq (%rbx), %rdi
leaq 0x12cd(%rip), %rsi      # 0x41d1
leaq 0x1294(%rip), %rdx      # 0x419f
xorl %eax, %eax
callq 0x2190
leaq 0x68(%rsp), %rdi
callq 0x2140
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x2170
movq 0x4080(%rip), %r13      # 0x6fb0
movq (%r13), %rcx
leaq 0x126e(%rip), %rdi      # 0x41a9
movl $0x17, %esi
movl $0x1, %edx
callq 0x2240
movq (%r13), %rbx
movq (%r14), %rax
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq *0x10(%rax)
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x2312
movq (%r13), %rdi
movl 0x8(%r14), %edx
leaq 0x125d(%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 0x2fad
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
testl %eax, %eax
je 0x310d
leaq 0x1272(%rip), %rcx      # 0x4226
imulq $0x3b9aca00, %r15, %r14 # imm = 0x3B9ACA00
addq %rbp, %r14
leaq 0x122a(%rip), %rsi      # 0x41ef
xorl %ebp, %ebp
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2190
movq (%r13), %rdi
leaq 0x1257(%rip), %rsi      # 0x4236
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x2190
movq (%r13), %rdi
leaq 0x1269(%rip), %rsi      # 0x425e
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x2190
movq %r14, %xmm1
punpckldq 0x1102(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
movq (%r13), %rdi
subpd 0x1106(%rip), %xmm1     # 0x4120
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd 0x1112(%rip), %xmm0     # 0x4140
leaq 0x1245(%rip), %rsi      # 0x427a
movb $0x1, %al
callq 0x2190
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x30da
movl $0x1, %ebx
leaq 0x1240(%rip), %r14      # 0x4298
leaq 0x1242(%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 0x30a0
movl $0x10, %esi
movl $0x1, %edx
leaq 0x1219(%rip), %rdi      # 0x42b2
callq 0x2240
jmp 0x30b4
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 0x3064
movq (%r13), %rdi
movq 0x40(%rsp), %rcx
movl %ecx, %edx
subl %ebp, %edx
leaq 0x11d5(%rip), %rsi      # 0x42c3
xorl %eax, %eax
callq 0x2190
movq (%r13), %rsi
movl $0xa, %edi
callq 0x2170
testl %ebp, %ebp
sete %bl
jmp 0x2ca1
leaq 0x1102(%rip), %rcx      # 0x4216
jmp 0x2fb4
jmp 0x31a4
jmp 0x31a4
jmp 0x3127
jmp 0x3127
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x31a7
movq %rbx, %rdi
callq 0x2070
leaq 0x68(%rsp), %rdi
callq 0x2140
movq 0x3e90(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
leaq 0x107f(%rip), %rsi      # 0x41d1
leaq 0x104b(%rip), %rdx      # 0x41a4
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2190
callq 0x21f0
jmp 0x2ca1
jmp 0x31a4
movq %rdx, %r14
movq %rax, %rbx
movq %r12, %rdi
callq 0x2120
jmp 0x31bf
movq %rdx, %r14
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3193
callq 0x339e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x31bf
callq 0x339e
jmp 0x31bf
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3352
movq %rbx, %rdi
callq 0x2210
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x31a7
movq %rbx, %rdi
callq 0x2070
movq 0x3e04(%rip), %rax      # 0x6fd8
movq (%rax), %rcx
leaq 0xf92(%rip), %rdi       # 0x4170
movl $0x5, %esi
movl $0x1, %edx
callq 0x2240
callq 0x21f0
jmp 0x2d98
jmp 0x31a4
nop