// File: /workspace/github_cpp_makefile_star2_O2/lluchs[P]sparsearray/main.cpp
// Function: BenchmarkResult benchmark<UnorderedLinkedListSA<C4PXS, 10000ul>>(int, unsigned long, int)
BenchmarkResult benchmark(int iterations, uint64_t seed, int addmod)
{
	uint64_t r = seed;
	auto rand = [&r]() { return r = r * 6364136223846793005 + 1442695040888963407; };
	SparseArray array;
	BenchmarkResult result = {0};

	for (int i = 0; i < iterations; i++)
	{
		// Add new PXS periodically.
		if (i % addmod == 0)
			for (int j = 0; j < 10; j++)
			{
				auto npxs = array.New();
				if (npxs)
				{
					npxs->Mat = 1;
					npxs->x = 0; npxs->y = 0;
					npxs->xdir = (int) (rand() % 100) - 50;
					npxs->ydir = (int) (rand() % 100) - 50;
				}
			}
		// walk through the array and do stuff
		for (auto& pxs : array)
		{
			pxs.x += pxs.xdir; pxs.y += pxs.ydir;
			if (std::abs(pxs.x + pxs.y) > 10000)
			{
				pxs.Mat = C4PXS::MNone;
				array.Delete(&pxs);
			}
		}
	}

	for (auto& pxs : array)
	{
		result.count++;
		result.sum += pxs.x + pxs.y;
	}
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x61aa8, %rsp          # imm = 0x61AA8
movl %edx, 0x8(%rsp)
movq %rsi, %r14
movl %edi, %ebp
movabsq $0x5851f42d4c957f2d, %r13 # imm = 0x5851F42D4C957F2D
movabsq $0x14057b7ef767814f, %rbx # imm = 0x14057B7EF767814F
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x37dc
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
movl %ebp, 0xc(%rsp)
pushq $0x64
popq %rbp
cmpl 0xc(%rsp), %eax
je 0x3780
movq %rax, 0x10(%rsp)
cltd
idivl 0x8(%rsp)
movq 0x61a98(%rsp), %r12
testl %edx, %edx
je 0x36a9
testq %r12, %r12
jne 0x376d
xorl %eax, %eax
jmp 0x371b
movq 0x61aa0(%rsp), %rsi
pushq $0xa
popq %rcx
subl $0x1, %ecx
jb 0x369c
testq %rsi, %rsi
je 0x3717
movq 0x18(%rsi), %rdi
movq %rdi, 0x61aa0(%rsp)
movb $0x1, 0x20(%rsi)
movq %r12, 0x18(%rsi)
movq %rsi, 0x61a98(%rsp)
movq $0x1, (%rsi)
andl $0x0, 0x8(%rsi)
imulq %r13, %r14
addq %rbx, %r14
movq %r14, %rax
xorl %edx, %edx
divq %rbp
addl $-0x32, %edx
movl %edx, 0xc(%rsi)
imulq %r13, %r14
addq %rbx, %r14
movq %r14, %rax
xorl %edx, %edx
divq %rbp
addl $-0x32, %edx
movl %edx, 0x10(%rsi)
movq %rsi, %r12
movq %rdi, %rsi
jmp 0x36b4
xorl %esi, %esi
jmp 0x36b4
movq %r12, %rsi
testq %rsi, %rsi
je 0x3774
movq %rax, %r12
movq 0xc(%rsi), %xmm0
movq 0x4(%rsi), %xmm1
paddd %xmm0, %xmm1
movq %xmm1, 0x4(%rsi)
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
cmpl $0x2711, %ecx           # imm = 0x2711
jb 0x3760
andl $0x0, (%rsi)
movq %r15, %rdi
callq 0x382e
movl $0x0, %eax
movq %r12, %rsi
testq %r12, %r12
je 0x371e
movq 0x18(%r12), %rax
jmp 0x371b
movq 0x10(%rsp), %rax
incl %eax
jmp 0x367c
movq 0x61a98(%rsp), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x3793
xorl %edx, %edx
jmp 0x3799
xorl %esi, %esi
xorl %edx, %edx
jmp 0x379d
movq 0x18(%rcx), %rsi
movl %edx, %edx
movq %rcx, %rdi
testq %rdi, %rdi
je 0x37c2
movq %rsi, %rcx
addl 0x4(%rdi), %eax
addl 0x8(%rdi), %eax
incq %rdx
movl $0x0, %esi
movq %rcx, %rdi
testq %rcx, %rcx
je 0x37a2
jmp 0x3799
shlq $0x20, %rax
addq %rdx, %rax
addq $0x61aa8, %rsp          # imm = 0x61AA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop