// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl $0x1e, %edi
callq 0x2110
testq %rax, %rax
jle 0x286c
movq %rax, %rbx
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1b3c(%rip), %rsi      # 0x4030
leaq 0x1b3f(%rip), %rdx      # 0x403a
leaq 0x30(%rsp), %rdi
callq 0x2bca
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x1b26(%rip), %rsi      # 0x403b
leaq 0x1b29(%rip), %rdx      # 0x4045
leaq 0x10(%rsp), %rdi
callq 0x2bca
leaq 0x78(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x29b2
addq %rbx, %rbx
leaq (%rbx,%rbx,4), %rdx
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r8
movaps %xmm0, (%r8)
leaq 0x8dd(%rip), %rax       # 0x2e2c
movq %rax, 0x18(%r8)
leaq 0x8d6(%rip), %rax       # 0x2e30
movq %rax, 0x10(%r8)
leaq 0xd8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x1, %ecx
callq 0x2a26
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x258e
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x4a33(%rip), %rbx      # 0x6fc8
leaq 0x1aaa(%rip), %rsi      # 0x4046
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x21d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x4a01(%rip), %rdi      # 0x6fc8
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0xd8(%rsp), %rdi
callq 0x2ea6
xorl %ebp, %ebp
movq 0x130(%rsp), %rbx
leaq 0x78(%rsp), %r14
movq %rsp, %r15
movq %r14, %rdi
callq 0x2ad0
testb %al, %al
jne 0x2631
movq $0x1, (%rsp)
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2070
cmpl $-0x1, %eax
jne 0x262a
callq 0x2040
cmpl $0x4, (%rax)
je 0x2610
incl %ebp
cmpl $0xa, %ebp
jne 0x25f3
leaq 0x78(%rsp), %rdi
callq 0x2ad0
testb %al, %al
je 0x288b
leaq 0x78(%rsp), %rdi
callq 0x2ea6
movq 0xd0(%rsp), %r14
movq 0x496c(%rip), %r15      # 0x6fc8
leaq 0x1a1f(%rip), %rsi      # 0x4082
movl $0x17, %edx
movq %r15, %rdi
callq 0x21d0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x493a(%rip), %rdi      # 0x6fc8
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq %rsp, %r15
movq $0x0, (%r15)
movl $0x8, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2b08
movq (%r15), %rcx
incq %rcx
movq %rcx, (%r15)
cmpq $0xf423f, %rcx          # imm = 0xF423F
ja 0x2770
movq %rsp, %r15
movq (%rbx), %rbp
movq 0x40(%rbx), %rax
cmpq %rax, %rbp
je 0x2763
cmpl $0x0, 0x88(%rbx)
je 0x26e7
movq %rbx, %rdi
callq 0x3fa8
movq (%rsp), %rcx
testq %rax, %rax
je 0x2763
movq (%rax,%rbp), %rax
testq %rcx, %rcx
je 0x270b
cmpq %rcx, %rax
je 0x270f
jmp 0x284d
movq %rax, (%rsp)
movq (%rbx), %rax
addq $0x8, %rax
movq 0x80(%rbx), %rcx
xorl %edx, %edx
divq %rcx
movq %rdx, (%rbx)
movq (%rbx), %rax
movq 0x40(%rbx), %rcx
cmpq %rax, %rcx
jne 0x282e
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
cmpq $0xf423f, %rax          # imm = 0xF423F
ja 0x2770
movl $0x8, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2b08
movq (%rsp), %rcx
incq %rcx
movq %rcx, (%rsp)
cmpq $0xf4240, %rcx          # imm = 0xF4240
jb 0x26ce
movq 0x4851(%rip), %rdi      # 0x6fc8
leaq 0x1931(%rip), %rsi      # 0x40af
movl $0x15, %edx
callq 0x21d0
movq 0x4839(%rip), %rdi      # 0x6fc8
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x2130
movq %rax, %rbx
leaq 0x1922(%rip), %rsi      # 0x40c5
movl $0x9, %edx
movq %rax, %rdi
callq 0x21d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0xd8(%rsp), %rdi
callq 0x2b54
leaq 0x78(%rsp), %rdi
callq 0x2b54
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x2805
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x281c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x186f(%rip), %rdi      # 0x40a4
leaq 0x17d6(%rip), %rsi      # 0x4012
leaq 0x17e2(%rip), %rcx      # 0x4025
movl $0x70, %edx
callq 0x2120
leaq 0x1846(%rip), %rdi      # 0x409a
leaq 0x17b7(%rip), %rsi      # 0x4012
leaq 0x17c3(%rip), %rcx      # 0x4025
movl $0x6e, %edx
callq 0x2120
leaq 0x1791(%rip), %rdi      # 0x4004
leaq 0x1798(%rip), %rsi      # 0x4012
leaq 0x17a4(%rip), %rcx      # 0x4025
movl $0x34, %edx
callq 0x2120
movl $0x10, %edi
callq 0x20e0
movq %rax, %r14
leaq 0x17c5(%rip), %rsi      # 0x4064
movq %rax, %rdi
callq 0x20a0
movq 0x4742(%rip), %rsi      # 0x6ff0
movq 0x470b(%rip), %rdx      # 0x6fc0
movq %r14, %rdi
callq 0x2270
movq %rax, %rbx
movq %r14, %rdi
callq 0x2160
jmp 0x2909
jmp 0x28ea
movq %rax, %rbx
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x2916
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x2916
movq %rax, %rdi
callq 0x2bbf
movq %rax, %rbx
jmp 0x2920
movq %rax, %rbx
jmp 0x2937
jmp 0x2906
jmp 0x2906
jmp 0x2906
jmp 0x2906
jmp 0x2906
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x2b54
leaq 0x78(%rsp), %rdi
callq 0x2b54
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x2937
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x294e
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280