// File: jingdao[P]PointCloudApp/kdtree.cpp
// Function: KdTree::findMedian(int*, int, int, int)
void KdTree::findMedian(int* pointList, int n, int target, int dimension) {
	if (n < 2) return;
	int pivot_index = rand() % n;
	int pivot = pointList[pivot_index];
	float pivotValue = float_data[pivot * 4 + dimension];
	pointList[pivot_index] = pointList[n-1];
	int i=-1,j=-1,tmp;
	do {
		j++;
		if (float_data[pointList[j] * 4 + dimension] < pivotValue || 
			(float_data[pointList[j] * 4 + dimension] == pivotValue && rand()%2 == 0)) {
			i++;
			tmp = pointList[i];
			pointList[i] = pointList[j];
			pointList[j] = tmp;
		}
	} while (j < n - 2);
	i++;
	pointList[n-1] = pointList[i];
	pointList[i] = pivot;
	if (i == target) return;
	else if (i > target) findMedian(pointList, i , target, dimension);
	else findMedian(pointList + i + 1, n - i - 1, target - i - 1, dimension);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpl $0x2, -0x14(%rbp)
jge 0xe21d
jmp 0xe3cc
callq 0x2080
cltd
idivl -0x14(%rbp)
movq -0x40(%rbp), %rax
movl %edx, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movslq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x24(%rbp)
movq 0x30(%rax), %rax
movl -0x24(%rbp), %ecx
shll $0x2, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30(%rbp)
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shll $0x2, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm1
movss -0x28(%rbp), %xmm0
ucomiss %xmm1, %xmm0
ja 0xe2ed
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shll $0x2, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
ucomiss -0x28(%rbp), %xmm0
jne 0xe328
jp 0xe328
callq 0x2080
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xe328
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xe32a
movl -0x30(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jl 0xe27e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0xe377
jmp 0xe3cc
movl -0x2c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jle 0xe398
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
callq 0xe1f0
jmp 0xe3ca
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x2c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
addq $0x4, %rsi
movl -0x14(%rbp), %edx
subl -0x2c(%rbp), %edx
subl $0x1, %edx
movl -0x18(%rbp), %ecx
subl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movl -0x1c(%rbp), %r8d
callq 0xe1f0
jmp 0xe3cc
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)