// File: /workspace/github_cpp_makefile_star2_O1/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 0x2868
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 0x2bc8
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x1b26(%rip), %rsi      # 0x403b
leaq 0x1b29(%rip), %rdx      # 0x4045
leaq 0x10(%rsp), %rdi
callq 0x2bc8
leaq 0x78(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x29ae
addq %rbx, %rbx
leaq (%rbx,%rbx,4), %rdx
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r8
movaps %xmm0, (%r8)
leaq 0x8cf(%rip), %rax       # 0x2e1e
movq %rax, 0x18(%r8)
leaq 0x8c8(%rip), %rax       # 0x2e22
movq %rax, 0x10(%r8)
leaq 0xd8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x1, %ecx
callq 0x2a22
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 0x2ea0
xorl %ebp, %ebp
movq 0x130(%rsp), %rbx
leaq 0x78(%rsp), %r14
movq %rsp, %r15
movq %r14, %rdi
callq 0x2acc
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 0x2acc
testb %al, %al
je 0x2887
leaq 0x78(%rsp), %rdi
callq 0x2ea0
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 0x2b06
movq (%r15), %rax
incq %rax
movq %rax, (%r15)
cmpq $0xf423f, %rax          # imm = 0xF423F
ja 0x276c
movq %rsp, %r15
movq (%rbx), %rbp
movq 0x40(%rbx), %rax
cmpq %rax, %rbp
jne 0x26dd
xorl %eax, %eax
jmp 0x26f1
cmpl $0x0, 0x88(%rbx)
je 0x26e6
movq %rbx, %rdi
callq 0x3fa2
addq %rbp, %rax
testq %rax, %rax
je 0x275e
cmpq $0x0, (%rsp)
jne 0x2704
movq (%rax), %rcx
movq %rcx, (%rsp)
movq (%rax), %rax
cmpq (%rsp), %rax
jne 0x2849
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 0x282a
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
cmpq $0xf423f, %rax          # imm = 0xF423F
ja 0x275e
movl $0x8, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2b06
incq (%rsp)
cmpq $0xf4240, (%rsp)        # imm = 0xF4240
jb 0x26cd
movq 0x4855(%rip), %rdi      # 0x6fc8
leaq 0x1935(%rip), %rsi      # 0x40af
movl $0x15, %edx
callq 0x21d0
movq 0x483d(%rip), %rdi      # 0x6fc8
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x2130
movq %rax, %rbx
leaq 0x1926(%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 0x2b52
leaq 0x78(%rsp), %rdi
callq 0x2b52
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x2801
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x2818
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 0x1873(%rip), %rdi      # 0x40a4
leaq 0x17da(%rip), %rsi      # 0x4012
leaq 0x17e6(%rip), %rcx      # 0x4025
movl $0x70, %edx
callq 0x2120
leaq 0x184a(%rip), %rdi      # 0x409a
leaq 0x17bb(%rip), %rsi      # 0x4012
leaq 0x17c7(%rip), %rcx      # 0x4025
movl $0x6e, %edx
callq 0x2120
leaq 0x1795(%rip), %rdi      # 0x4004
leaq 0x179c(%rip), %rsi      # 0x4012
leaq 0x17a8(%rip), %rcx      # 0x4025
movl $0x34, %edx
callq 0x2120
movl $0x10, %edi
callq 0x20e0
movq %rax, %r14
leaq 0x17c9(%rip), %rsi      # 0x4064
movq %rax, %rdi
callq 0x20a0
movq 0x4746(%rip), %rsi      # 0x6ff0
movq 0x470f(%rip), %rdx      # 0x6fc0
movq %r14, %rdi
callq 0x2270
movq %rax, %rbx
movq %r14, %rdi
callq 0x2160
jmp 0x2905
jmp 0x28e6
movq %rax, %rbx
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x2912
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x2912
movq %rax, %rdi
callq 0x2bbd
movq %rax, %rbx
jmp 0x291c
movq %rax, %rbx
jmp 0x2933
jmp 0x2902
jmp 0x2902
jmp 0x2902
jmp 0x2902
jmp 0x2902
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x2b52
leaq 0x78(%rsp), %rdi
callq 0x2b52
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x2933
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x294a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280