// File: /workspace/github_cpp_makefile_star2_O1/lluchs[P]sparsearray/main.cpp
// Function: BenchmarkResult benchmark<LinkedListSA<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 $0x61a98, %rsp          # imm = 0x61A98
movl %edx, 0x4(%rsp)
movq %rsi, %r14
movl %edi, (%rsp)
movl $0x20, %eax
xorps %xmm0, %xmm0
movups %xmm0, -0x18(%rsp,%rax)
movl $0x0, -0x8(%rsp,%rax)
movb $0x0, 0x8(%rsp,%rax)
addq $0x28, %rax
cmpq $0x61aa0, %rax          # imm = 0x61AA0
jne 0x3694
leaq 0x8(%rsp), %r15
movq $0x0, 0x61a80(%r15)
movq %r15, 0x61a88(%r15)
movl $0x270f, %eax           # imm = 0x270F
leaq 0x30(%rsp), %rcx
movq %rcx, -0x10(%rcx)
addq $0x28, %rcx
decq %rax
jne 0x36d3
movq $0x0, 0x61a78(%rsp)
cmpl $0x0, (%rsp)
jle 0x387e
movabsq $0x5851f42d4c957f2d, %r10 # imm = 0x5851F42D4C957F2D
xorl %r12d, %r12d
movabsq $0x14057b7ef767814f, %rbx # imm = 0x14057B7EF767814F
movabsq $0x28f5c28f5c28f5c3, %rbp # imm = 0x28F5C28F5C28F5C3
movl %r12d, %eax
cltd
idivl 0x4(%rsp)
testl %edx, %edx
jne 0x3804
movq 0x61a88(%rsp), %rsi
movq 0x61a90(%rsp), %rdi
xorl %ecx, %ecx
testq %rdi, %rdi
je 0x3777
movq 0x18(%rdi), %r8
movq %r8, 0x61a90(%rsp)
movb $0x1, 0x20(%rdi)
testq %rsi, %rsi
je 0x377e
leaq -0x28(%rdi), %rdx
movq %rdx, %rax
cmpq %r15, %rdx
jb 0x3788
leaq -0x28(%rax), %rdx
cmpb $0x1, 0x20(%rax)
jne 0x3757
movq 0x40(%rdx), %r9
movq %r9, 0x18(%rdi)
movq %rdi, 0x40(%rdx)
jmp 0x378c
movq %rdi, %r8
xorl %edi, %edi
jmp 0x379c
movq $0x0, 0x18(%rdi)
jmp 0x3791
movq %rsi, 0x18(%rdi)
cmpq %r15, %rax
jae 0x379c
movq %rdi, 0x61a88(%rsp)
movq %rdi, %rsi
testq %rdi, %rdi
je 0x37f6
movq $0x1, (%rdi)
movl $0x0, 0x8(%rdi)
imulq %r10, %r14
addq %rbx, %r14
movq %r14, %rax
shrq $0x2, %rax
mulq %rbp
shrl $0x2, %edx
imull $0x64, %edx, %eax
movl %r14d, %r9d
subl %eax, %r9d
addl $-0x32, %r9d
imulq %r10, %r14
addq %rbx, %r14
movq %r14, %rax
shrq $0x2, %rax
mulq %rbp
movl %r9d, 0xc(%rdi)
shrl $0x2, %edx
imull $0x64, %edx, %eax
movl %r14d, %edx
subl %eax, %edx
addl $-0x32, %edx
movl %edx, 0x10(%rdi)
incl %ecx
movq %r8, %rdi
cmpl $0xa, %ecx
jne 0x3739
movq 0x61a88(%rsp), %rsi
testq %rsi, %rsi
je 0x3817
movq 0x18(%rsi), %r13
jmp 0x381a
xorl %r13d, %r13d
testq %rsi, %rsi
je 0x3867
movl 0x4(%rsi), %eax
addl 0xc(%rsi), %eax
movl 0x8(%rsi), %ecx
movl %eax, 0x4(%rsi)
addl 0x10(%rsi), %ecx
movl %ecx, 0x8(%rsi)
addl %eax, %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
cmpl $0x2711, %eax           # imm = 0x2711
jb 0x384f
movl $0x0, (%rsi)
movq %r15, %rdi
callq 0x38d6
testq %r13, %r13
je 0x385a
movq 0x18(%r13), %rax
jmp 0x385c
xorl %eax, %eax
movq %r13, %rsi
testq %r13, %r13
movq %rax, %r13
jne 0x381f
incl %r12d
cmpl (%rsp), %r12d
movabsq $0x5851f42d4c957f2d, %r10 # imm = 0x5851F42D4C957F2D
jne 0x3717
movq 0x61a88(%rsp), %rcx
testq %rcx, %rcx
je 0x3891
movq 0x18(%rcx), %rdx
jmp 0x3893
xorl %edx, %edx
xorl %eax, %eax
testq %rcx, %rcx
je 0x38c4
xorl %esi, %esi
testq %rdx, %rdx
je 0x38a7
movq 0x18(%rdx), %rdi
jmp 0x38a9
xorl %edi, %edi
addl 0x4(%rcx), %eax
addl 0x8(%rcx), %eax
incq %rsi
movq %rdx, %rcx
testq %rdx, %rdx
movq %rdi, %rdx
jne 0x389c
shlq $0x20, %rax
addq %rsi, %rax
addq $0x61a98, %rsp          # imm = 0x61A98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq