// File: /workspace/github_cpp_makefile_star2_O3/lluchs[P]sparsearray/main.cpp
// Function: BenchmarkResult benchmark<BitmapSA<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 $0x31258, %rsp          # imm = 0x31258
movl %edx, 0x4(%rsp)
movq %rsi, %r14
movl %edi, %ebx
leaq 0x30(%rsp), %r15
movl $0x31228, %edx          # imm = 0x31228
movq %r15, %rdi
xorl %esi, %esi
callq 0x1070
movl %ebx, (%rsp)
testl %ebx, %ebx
jle 0x2abe
movabsq $0x5851f42d4c957f2d, %r9 # imm = 0x5851F42D4C957F2D
movabsq $0x14057b7ef767814f, %r10 # imm = 0x14057B7EF767814F
leaq 0x30d70(%rsp), %rbp
movabsq $0x28f5c28f5c28f5c3, %r11 # imm = 0x28F5C28F5C28F5C3
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movl %r13d, %eax
cltd
idivl 0x4(%rsp)
testl %edx, %edx
jne 0x29f2
xorl %ecx, %ecx
xorl %edx, %edx
movq %rbp, %rax
movq (%rax), %rsi
cmpq $-0x1, %rsi
jne 0x296c
addq $0x8, %rax
addq $-0x40, %rdx
cmpq $-0x2740, %rdx          # imm = 0xD8C0
jne 0x2950
jmp 0x29e7
movq %rsi, %rdi
notq %rdi
bsfq %rdi, %r8
movq %r8, %rdi
subq %rdx, %rdi
cmpq $0x270f, %rdi           # imm = 0x270F
ja 0x29e7
btsq %r8, %rsi
movq %rsi, (%rax)
leaq (%rdi,%rdi,4), %rsi
movq $0x1, 0x30(%rsp,%rsi,4)
movl $0x0, 0x38(%rsp,%rsi,4)
imulq %r9, %r14
addq %r10, %r14
movq %r14, %rax
shrq $0x2, %rax
mulq %r11
shrl $0x2, %edx
imull $0x64, %edx, %eax
movl %r14d, %edi
subl %eax, %edi
addl $-0x32, %edi
imulq %r9, %r14
addq %r10, %r14
movq %r14, %rax
shrq $0x2, %rax
mulq %r11
movl %edi, 0x3c(%rsp,%rsi,4)
shrl $0x2, %edx
imull $0x64, %edx, %eax
movl %r14d, %edx
subl %eax, %edx
addl $-0x32, %edx
movl %edx, 0x40(%rsp,%rsi,4)
incl %ecx
cmpl $0xa, %ecx
jne 0x294b
movq %r15, 0x8(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x10(%rsp)
movq $0x40, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
movq $0x1, 0x20(%rsp)
movq %r12, %rdi
callq 0x2bc6
movq %rbx, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, %rdx
orq %rax, %rdx
je 0x2a93
cmpq $0x2710, %rcx           # imm = 0x2710
jae 0x2b51
leaq (%rcx,%rcx,4), %rcx
movq 0xc(%rax,%rcx,4), %xmm0
movq 0x4(%rax,%rcx,4), %xmm1
paddd %xmm0, %xmm1
movq %xmm1, 0x4(%rax,%rcx,4)
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %edx
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
cmpl $0x2711, %esi           # imm = 0x2711
jb 0x2a89
leaq (%rax,%rcx,4), %rsi
movl $0x0, (%rsi)
movq %r15, %rdi
callq 0x2b70
movq %r12, %rdi
callq 0x2bc6
jmp 0x2a22
incl %r13d
cmpl (%rsp), %r13d
movabsq $0x5851f42d4c957f2d, %r9 # imm = 0x5851F42D4C957F2D
movabsq $0x14057b7ef767814f, %r10 # imm = 0x14057B7EF767814F
movabsq $0x28f5c28f5c28f5c3, %r11 # imm = 0x28F5C28F5C28F5C3
jne 0x2939
leaq 0x8(%rsp), %r14
movq %r15, (%r14)
xorl %ebx, %ebx
movq %rbx, 0x8(%r14)
movq $0x40, 0x10(%r14)
movq %rbx, 0x20(%r14)
movq $0x1, 0x18(%r14)
movq %r14, %rdi
callq 0x2bc6
movq %rbx, 0x18(%r14)
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq %rax, %rdx
orq %rcx, %rdx
je 0x2b3c
leaq 0x8(%rsp), %r14
xorl %r15d, %r15d
cmpq $0x2710, %rcx           # imm = 0x2710
jae 0x2b51
leaq (%rcx,%rcx,4), %rcx
addl 0x4(%rax,%rcx,4), %ebx
addl 0x8(%rax,%rcx,4), %ebx
movq %r14, %rdi
callq 0x2bc6
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
incq %r15
movq %rcx, %rdx
orq %rax, %rdx
jne 0x2b03
shlq $0x20, %rbx
addq %r15, %rbx
movq %rbx, %rax
addq $0x31258, %rsp          # imm = 0x31258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25dd(%rip), %rdi      # 0x5135
leaq 0x25dd(%rip), %rsi      # 0x513c
leaq 0x25e6(%rip), %rcx      # 0x514c
movl $0x4d, %edx
callq 0x1080