// File: gjoni[P]iPot/src/kdtree.cpp
// Function: kd_nearest
struct kdres *kd_nearest(struct kdtree *kd, const double *pos)
{
	struct kdhyperrect *rect;
	struct kdnode *result;
	struct kdres *rset;
	double dist_sq;
	int i;

	if (!kd) return 0;
	if (!kd->rect) return 0;

	/* Allocate result set */
	if(!(rset = (kdres*)malloc(sizeof *rset))) {	// (kdres*) added
		return 0;
	}
	if(!(rset->rlist = (res_node*)alloc_resnode())) {	// (res_node*) added
		free(rset);
		return 0;
	}
	rset->rlist->next = 0;
	rset->tree = kd;

	/* Duplicate the bounding hyperrectangle, we will work on the copy */
	if (!(rect = hyperrect_duplicate(kd->rect))) {
		kd_res_free(rset);
		return 0;
	}

	/* Our first guesstimate is the root node */
	result = kd->root;
	dist_sq = 0;
	for (i = 0; i < kd->dim; i++)
		dist_sq += SQ(result->pos[i] - pos[i]);

	/* Search for the nearest neighbour recursively */
	kd_nearest_i(kd->root, pos, &result, &dist_sq, rect);

	/* Free the copy of the hyperrect */
	hyperrect_free(rect);

	/* Store the result */
	if (result) {
		if (rlist_insert(rset->rlist, result, -1.0) == -1) {
			kd_res_free(rset);
			return 0;
		}
		rset->size = 1;
		kd_res_rewind(rset);
		return rset;
	} else {
		kd_res_free(rset);
		return 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x31b54
movq $0x0, -0x8(%rbp)
jmp 0x31d0d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x31b6c
movq $0x0, -0x8(%rbp)
jmp 0x31d0d
movl $0x20, %edi
callq 0x3390
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x31b8d
movq $0x0, -0x8(%rbp)
jmp 0x31d0d
movl $0x18, %edi
callq 0x3390
movq -0x30(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0x31bbb
movq -0x30(%rbp), %rdi
callq 0x3320
movq $0x0, -0x8(%rbp)
jmp 0x31d0d
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x31d20
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x31c03
movq -0x30(%rbp), %rdi
callq 0x31d50
movq $0x0, -0x8(%rbp)
jmp 0x31d0d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x31c80
movq -0x28(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
movsd -0x38(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x31c1e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %r8
leaq -0x28(%rbp), %rdx
leaq -0x38(%rbp), %rcx
callq 0x31d90
movq -0x20(%rbp), %rdi
callq 0x31550
cmpq $0x0, -0x28(%rbp)
je 0x31cfc
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x28(%rbp), %rsi
movsd 0xec8f(%rip), %xmm0     # 0x40950
callq 0x31fe0
cmpl $-0x1, %eax
jne 0x31cde
movq -0x30(%rbp), %rdi
callq 0x31d50
movq $0x0, -0x8(%rbp)
jmp 0x31d0d
movq -0x30(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x30(%rbp), %rdi
callq 0x320b0
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x31d0d
movq -0x30(%rbp), %rdi
callq 0x31d50
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)