// File: gjoni[P]iPot/src/kdtree.cpp
// Function: find_nearest(kdnode*, double const*, double, res_node*, int, int)
static int find_nearest(struct kdnode *node, const double *pos, double range, struct res_node *list, int ordered, int dim)
{
	double dist_sq, dx;
	int i, ret, added_res = 0;

	if(!node) return 0;

	dist_sq = 0;
	for(i=0; i<dim; i++) {
		dist_sq += SQ(node->pos[i] - pos[i]);
	}
	if(dist_sq <= SQ(range)) {
		if(rlist_insert(list, node, ordered ? dist_sq : -1.0) == -1) {
			return -1;
		}
		added_res = 1;
	}

	dx = pos[node->dir] - node->pos[node->dir];

	ret = find_nearest(dx <= 0.0 ? node->left : node->right, pos, range, list, ordered, dim);
	if(ret >= 0 && fabs(dx) < range) {
		added_res += ret;
		ret = find_nearest(dx <= 0.0 ? node->right : node->left, pos, range, list, ordered, dim);
	}
	if(ret == -1) {
		return -1;
	}
	added_res += ret;

	return added_res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movl %ecx, -0x2c(%rbp)
movl %r8d, -0x30(%rbp)
movl $0x0, -0x4c(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x323bd
movl $0x0, -0x4(%rbp)
jmp 0x32595
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x3242b
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
movsd -0x38(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x323cc
movsd -0x38(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
mulsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3249b
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x32462
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
jmp 0x32471
movsd 0xe4e6(%rip), %xmm0     # 0x40950
movsd %xmm0, -0x68(%rbp)
jmp 0x32471
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdi
movsd -0x68(%rbp), %xmm0
callq 0x31fe0
cmpl $-0x1, %eax
jne 0x32494
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32595
movl $0x1, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x40(%rbp), %xmm0
jb 0x324dd
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x324e9
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movq -0x18(%rbp), %rsi
movsd -0x20(%rbp), %xmm0
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r8d
callq 0x32380
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jl 0x32577
movsd -0x40(%rbp), %xmm1
movaps 0xe445(%rip), %xmm0     # 0x40960
pand %xmm0, %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x32577
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x40(%rbp), %xmm0
jb 0x3254b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x32557
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x18(%rbp), %rsi
movsd -0x20(%rbp), %xmm0
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r8d
callq 0x32380
movl %eax, -0x48(%rbp)
cmpl $-0x1, -0x48(%rbp)
jne 0x32586
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x32595
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)