// File: /workspace/github_cpp_makefile_star2_O2/dadocolussi[P]gdc-circular_queue/tests/pingpong.cpp
// Function: run()
void
run()
{
	long page_size = ::sysconf(_SC_PAGESIZE);
	assert(page_size > 0);
	std::string ping_name("/gdcq.ping");
	std::string pong_name("/gdcq.pong");

#if USE_C_API
	F rqf(ping_name, 10 * page_size);
	F wqf(pong_name);
	bool seed = false;
#else
	F wqf(ping_name);
	F rqf(pong_name, 10 * page_size);
	bool seed = true;
#endif

	std::cout << "Trying to resolve write queue" << std::endl;
	Q& rq = rqf.get();
	for (int i = 0; i < 10; ++i)
	{
		if (wqf.can_get())
		{
			break;
		}

		std::this_thread::sleep_for(std::chrono::seconds(1));
	}
	
	if (!wqf.can_get())
	{
		throw std::runtime_error("Failed to resolve write queue");
	}
	
	Q& wq = wqf.get();
	std::cout << "Did resolve write queue" << std::endl;

	std::size_t seq = 0;
	std::size_t n = 1000000;

	if (seed)
	{
		wq.push(&seq, sizeof seq);
		++seq;
	}

	while (seq < n)
	{
		auto i = rq.peek();

		if (i == nullptr)
		{
			continue;
		}

		if (seq == 0)
		{
			// Initialize sequence.
			seq = *i;
		}

		assert(*i == seq);
		rq.pop(sizeof seq);
		assert(rq.empty());

		if (++seq < n)
		{
			wq.push(&seq, sizeof seq);
			++seq;
		}
	}

	std::cout << "Did send and receive " << n << " messages" << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x130, %rsp            # imm = 0x130
pushq $0x1e
popq %rdi
callq 0x2110
testq %rax, %rax
jle 0x26a1
movq %rax, %rbx
leaq 0x1bfa(%rip), %rsi      # 0x4030
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x27aa
leaq 0x1bef(%rip), %rsi      # 0x403b
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x27aa
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x27e6
imulq $0xa, %rbx, %rdx
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r8
movaps %xmm0, (%r8)
leaq 0x761(%rip), %rax       # 0x2be2
movq %rax, 0x18(%r8)
leaq 0x75a(%rip), %rax       # 0x2be6
movq %rax, 0x10(%r8)
leaq 0xd0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x1
popq %rcx
callq 0x284c
leaq 0x50(%rsp), %rdi
callq 0x28ce
movq 0x4b12(%rip), %rdi      # 0x6fc8
leaq 0x1b89(%rip), %rsi      # 0x4046
callq 0x2180
movq %rax, %rdi
callq 0x20d0
leaq 0xd0(%rsp), %rdi
callq 0x28ea
movq %rax, %rbx
pushq $0xa
popq %rbp
leaq 0x70(%rsp), %r14
leaq 0x8(%rsp), %r15
subl $0x1, %ebp
jb 0x250b
movq %r14, %rdi
callq 0x28fa
testb %al, %al
jne 0x250b
movq $0x1, 0x8(%rsp)
movq %r15, %rdi
callq 0x292f
jmp 0x24e7
leaq 0x70(%rsp), %rdi
callq 0x28fa
testb %al, %al
je 0x26ba
leaq 0x70(%rsp), %rdi
callq 0x28ea
movq %rax, %r14
movq 0x4a97(%rip), %rdi      # 0x6fc8
leaq 0x1b4a(%rip), %rsi      # 0x4082
callq 0x2180
movq %rax, %rdi
callq 0x20d0
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
pushq $0x8
popq %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2968
movq (%r15), %rdx
incq %rdx
movq %rdx, (%r15)
cmpq $0xf423f, %rdx          # imm = 0xF423F
ja 0x25f9
movq %rbx, %rdi
callq 0x29b4
movq 0x8(%rsp), %rdx
testq %rax, %rax
je 0x2569
movq (%rax), %rcx
testq %rdx, %rdx
je 0x259a
cmpq %rdx, %rcx
je 0x259f
jmp 0x2684
movq %rcx, 0x8(%rsp)
movq (%rbx), %rax
addq $0x8, %rax
movq 0x80(%rbx), %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, (%rbx)
movq (%rbx), %rax
movq 0x40(%rbx), %rdx
cmpq %rax, %rdx
jne 0x266b
incq %rcx
movq %rcx, 0x8(%rsp)
movq %rcx, %rdx
cmpq $0xf423f, %rcx          # imm = 0xF423F
ja 0x2569
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2968
movq 0x8(%rsp), %rdx
incq %rdx
movq %rdx, 0x8(%rsp)
jmp 0x2569
movq 0x49c8(%rip), %rdi      # 0x6fc8
leaq 0x1aa8(%rip), %rsi      # 0x40af
callq 0x2180
movl $0xf4240, %esi          # imm = 0xF4240
movq %rax, %rdi
callq 0x2130
leaq 0x1aa5(%rip), %rsi      # 0x40c5
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
leaq 0xd0(%rsp), %rdi
callq 0x29d8
leaq 0x70(%rsp), %rdi
callq 0x29d8
leaq 0x10(%rsp), %rdi
callq 0x20c0
leaq 0x30(%rsp), %rdi
callq 0x20c0
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a32(%rip), %rdi      # 0x40a4
leaq 0x1999(%rip), %rsi      # 0x4012
leaq 0x19a5(%rip), %rcx      # 0x4025
pushq $0x70
jmp 0x269b
leaq 0x1a0f(%rip), %rdi      # 0x409a
leaq 0x1980(%rip), %rsi      # 0x4012
leaq 0x198c(%rip), %rcx      # 0x4025
pushq $0x6e
popq %rdx
callq 0x2120
leaq 0x195c(%rip), %rdi      # 0x4004
leaq 0x1963(%rip), %rsi      # 0x4012
leaq 0x196f(%rip), %rcx      # 0x4025
pushq $0x34
jmp 0x269b
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r14
leaq 0x1998(%rip), %rsi      # 0x4064
movq %rax, %rdi
callq 0x2090
movq 0x4915(%rip), %rsi      # 0x6ff0
movq 0x48de(%rip), %rdx      # 0x6fc0
movq %r14, %rdi
callq 0x2250
movq %rax, %rbx
movq %r14, %rdi
callq 0x2150
jmp 0x271d
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x28ce
jmp 0x272a
movq %rax, %rbx
jmp 0x2734
movq %rax, %rbx
jmp 0x273e
jmp 0x271a
jmp 0x271a
jmp 0x271a
jmp 0x271a
jmp 0x271a
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x29d8
leaq 0x70(%rsp), %rdi
callq 0x29d8
leaq 0x10(%rsp), %rdi
callq 0x20c0
leaq 0x30(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2260