// File: EnderGed[P]fast-random-walk/HelperFunctions.cpp
// Function: countSort2D(unsigned int*, unsigned long, unsigned long, unsigned long)
void countSort2D(uint * const arr, size_t const size_x, size_t const size_y, size_t column)
{
	// Prepare break condition
	uint * const arrEnd = arr + size_x * size_y;

	// Determine maximum index
	uint maxIndex = 0;
	for(uint * pos = arr + column; pos < arrEnd; pos += size_x)
		if(*pos > maxIndex) maxIndex = *pos;

	// Allocate adjacency list
	read_t * const adj = (read_t*)calloc((maxIndex + 2), sizeof(*adj));
	read_t * const counts = adj + 1;

	if(!adj) printError("Memory allocation failed");

	// Count occurences of each index
	for(uint * pos = arr + column; pos < arrEnd; pos += size_x) ++(counts[*pos]);

	// Convert occurency counts to real adjacency list
	read_t * const adj_end = adj + maxIndex + 1;
	adj[0] = 0;
	for(read_t * pos_adj = adj; pos_adj < adj_end; ++pos_adj) pos_adj[1] += pos_adj[0];

	// Copy arr
	uint * arrCpy = (uint*)malloc(size_x * size_y * sizeof(*arrCpy));
	if(!arrCpy) printError("Memory allocation failed");
	memcpy(arrCpy, arr, size_x * size_y * sizeof(*arrCpy));
	uint const * const arrCpyEnd = arrCpy + size_x * size_y;

	// Move rows back to arr but to the correct position
	for(uint const * cpyPos = arrCpy; cpyPos < arrCpyEnd; cpyPos += size_x)
	{
		for(size_t i = 0; i < size_x; ++i) arr[adj[cpyPos[column]] * size_x + i] = cpyPos[i];
		++adj[cpyPos[column]];
	}

	// Clean up
	free(arrCpy);
	free(adj);
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
imulq 0x70(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movq 0x80(%rsp), %rax
movq 0x68(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x6799
movq 0x50(%rsp), %rax
movl (%rax), %eax
cmpl 0x5c(%rsp), %eax
jbe 0x6782
movq 0x50(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x5c(%rsp)
jmp 0x6784
movq 0x78(%rsp), %rax
shlq $0x2, %rax
addq 0x50(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x675e
movl 0x5c(%rsp), %eax
addl $0x2, %eax
movl %eax, %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2120
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x67d5
leaq 0x5996(%rip), %rdi      # 0xc166
callq 0x6380
movq 0x80(%rsp), %rax
movq 0x68(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x6827
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x78(%rsp), %rax
shlq $0x2, %rax
addq 0x38(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0x67ee
movq 0x48(%rsp), %rax
movl 0x5c(%rsp), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
addq $0x8, %rax
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %rax
movq $0x0, (%rax)
movq 0x48(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x30(%rsp), %rax
jae 0x6887
movq 0x28(%rsp), %rax
movq (%rax), %rcx
movq 0x28(%rsp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x28(%rsp)
jmp 0x6856
movq 0x78(%rsp), %rdi
imulq 0x70(%rsp), %rdi
shlq $0x2, %rdi
callq 0x2240
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x68b4
leaq 0x58b7(%rip), %rdi      # 0xc166
callq 0x6380
movq 0x20(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %rdx
imulq 0x70(%rsp), %rdx
shlq $0x2, %rdx
callq 0x2170
movq 0x20(%rsp), %rax
movq 0x78(%rsp), %rcx
imulq 0x70(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
jae 0x699f
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x6969
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movq 0x80(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x10(%rsp), %rsi
movq 0x68(%rsp), %rdi
movl (%rsi,%rdi,4), %esi
movq (%rcx,%rsi,8), %rcx
imulq 0x78(%rsp), %rcx
addq 0x8(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x6914
movq 0x48(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x78(%rsp), %rax
shlq $0x2, %rax
addq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x68fb
movq 0x20(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0x2220
addq $0x88, %rsp
retq
nopl (%rax,%rax)