// File: EnderGed[P]fast-random-walk/HelperFunctions.cpp
// Function: countSort2D(std::vector<unsigned int*, std::allocator<unsigned int*>>, unsigned long)
void countSort2D(std::vector<uint *> const lists, size_t const rows)
{
	// Determine maximum index
	uint const maxIndex = maxValue(lists[0], rows);

	// Allocate adjacency list
	read_t * const adj = prepareAdjList(lists[0], rows, maxIndex);

	uint * const arrCpy = (uint*)malloc(rows * sizeof(*arrCpy));
	if(!arrCpy) printError("Memory allocation failed");

	// Sort all lists (except the first one) with respect to values in first list
	for(size_t i = 1; i < lists.size(); ++i)
	{
		// Copy arr
		memcpy(arrCpy, lists[i], rows * sizeof(*arrCpy));

		// Move rows back to arr but to the correct position
		for(register size_t pos = 0; pos < rows; ++pos)
		{
			uint const targetRow = adj[lists[0][pos]];
			lists[i][targetRow] = arrCpy[pos];
			++adj[lists[0][pos]];
		}

		memmove(adj + 1, adj, (maxIndex + 1) * sizeof(*adj));
		adj[0] = 0;
	}

	// Sort the first list
	memcpy(arrCpy, lists[0], rows * sizeof(*arrCpy));

	for(register size_t pos = 0; pos < rows; ++pos)
	{
		uint const targetRow = adj[arrCpy[pos]];
		lists[0][targetRow] = arrCpy[pos];
		++adj[arrCpy[pos]];
	}

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

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x40(%rsp)
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x72c0
movq (%rax), %rdi
movq 0x88(%rsp), %rsi
callq 0x69c0
movq 0x40(%rsp), %rdi
movl %eax, 0x84(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x72c0
movq (%rax), %rdi
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
callq 0x6a30
movq %rax, 0x78(%rsp)
movq 0x88(%rsp), %rdi
shlq $0x2, %rdi
callq 0x2240
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x7030
leaq 0x513b(%rip), %rdi      # 0xc166
callq 0x6380
movq $0x1, 0x68(%rsp)
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rax
movq %rax, 0x38(%rsp)
callq 0x72e0
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0x719b
movq 0x40(%rsp), %rdi
movq 0x70(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x68(%rsp), %rsi
callq 0x72c0
movq 0x30(%rsp), %rdi
movq (%rax), %rsi
movq 0x88(%rsp), %rdx
shlq $0x2, %rdx
callq 0x2170
movq $0x0, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq 0x88(%rsp), %rax
jae 0x7157
movq 0x40(%rsp), %rdi
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x72c0
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq (%rcx), %rcx
movq 0x60(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, 0x5c(%rsp)
movq 0x70(%rsp), %rax
movq 0x60(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x24(%rsp)
movq 0x68(%rsp), %rsi
callq 0x72c0
movl 0x24(%rsp), %edx
movq 0x40(%rsp), %rdi
movq (%rax), %rax
movl 0x5c(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x72c0
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq (%rcx), %rcx
movq 0x60(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0x7099
movq 0x78(%rsp), %rdi
addq $0x8, %rdi
movq 0x78(%rsp), %rsi
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
shlq $0x3, %rdx
callq 0x22b0
movq 0x78(%rsp), %rax
movq $0x0, (%rax)
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x7039
movq 0x40(%rsp), %rdi
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x72c0
movq 0x10(%rsp), %rdi
movq (%rax), %rsi
movq 0x88(%rsp), %rdx
shlq $0x2, %rdx
callq 0x2170
movq $0x0, 0x50(%rsp)
movq 0x50(%rsp), %rax
cmpq 0x88(%rsp), %rax
jae 0x725c
movq 0x40(%rsp), %rdi
movq 0x78(%rsp), %rax
movq 0x70(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, 0x4c(%rsp)
movq 0x70(%rsp), %rax
movq 0x50(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x72c0
movl 0xc(%rsp), %edx
movq (%rax), %rax
movl 0x4c(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movq 0x78(%rsp), %rax
movq 0x70(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
jmp 0x71d5
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
addq $0x98, %rsp
retq
nopl (%rax,%rax)