#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct kdres {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int atoi(char *);
int /*<<< orphan*/ dist_sq(double *, double *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ isdigit(char);
void *kd_create(int);
int /*<<< orphan*/ kd_free(void *);
scalar_t__ kd_insert3(void *, int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, char *);
struct kdres *kd_nearest_range(void *, double *, double);
int /*<<< orphan*/ kd_res_end(struct kdres *);
int /*<<< orphan*/ kd_res_free(struct kdres *);
scalar_t__ kd_res_item(struct kdres *, double *);
int /*<<< orphan*/ kd_res_next(struct kdres *);
int kd_res_size(struct kdres *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, double, ...);
int /*<<< orphan*/ rd();
double sqrt(int /*<<< orphan*/);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int i, num_pts = 10;
  void *ptree;
  char *data, *pch;
  struct kdres *presults;
  double pos[3], dist;
  double pt[3] = {0, 0, 1};
  double radius = 10;
  if (argc > 1 && isdigit(argv[1][0])) {
    num_pts = atoi(argv[1]);
  }
  if (!(data = malloc(num_pts))) {
    perror("malloc failed");
    return 1;
  }
  srand(time(0));
  ptree = kd_create(3);
  for (i = 0; i < num_pts; i++) {
    data[i] = 'a' + i;
    assert(0 == kd_insert3(ptree, rd(), rd(), rd(), &data[i]));
  }
  presults = kd_nearest_range(ptree, pt, radius);
  printf("found %d results:\n", kd_res_size(presults));
  while (!kd_res_end(presults)) {
    pch = (char *)kd_res_item(presults, pos);
    dist = sqrt(dist_sq(pt, pos, 3));
    printf("node at (%.3f, %.3f, %.3f) is %.3f away and has data=%c\n", pos[0],
           pos[1], pos[2], dist, *pch);
    kd_res_next(presults);
  }
  free(data);
  kd_res_free(presults);
  kd_free(ptree);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x18(%rbp)
mov    0xd99(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0xd96(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0xd93(%rip),%rax
mov    %rax,-0x60(%rbp)
movsd  0xd67(%rip),%xmm0
movsd  %xmm0,-0x78(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    12d2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%edi
call   1050 <isdigit@plt>
cmp    $0x0,%rax
je     12d2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1150 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   10f0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1300 <main+0xb0>
lea    0xd39(%rip),%rdi
call   1140 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1481 <main+0x231>
xor    %edi,%edi
call   10d0 <time@plt>
mov    %eax,%edi
call   10a0 <srand@plt>
mov    $0x3,%edi
call   1110 <kd_create@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    13aa <main+0x15a>
mov    -0x14(%rbp),%eax
add    $0x61,%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    $0x0,%al
call   10e0 <rd@plt>
mov    %eax,-0x80(%rbp)
mov    $0x0,%al
call   10e0 <rd@plt>
mov    %eax,-0x7c(%rbp)
mov    $0x0,%al
call   10e0 <rd@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%esi
mov    -0x7c(%rbp),%edx
mov    %eax,%ecx
mov    -0x28(%rbp),%r8
movslq -0x14(%rbp),%rax
add    %rax,%r8
call   1170 <kd_insert3@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10b0 <assert@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1323 <main+0xd3>
mov    -0x20(%rbp),%rdi
lea    -0x70(%rbp),%rsi
movsd  -0x78(%rbp),%xmm0
call   1080 <kd_nearest_range@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <kd_res_size@plt>
cvtsi2sd %eax,%xmm0
lea    0xc62(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <kd_res_end@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    13f9 <main+0x1a9>
jmp    145f <main+0x20f>
mov    -0x38(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1100 <kd_res_item@plt>
mov    %rax,-0x30(%rbp)
lea    -0x70(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x3,%edx
call   1120 <dist_sq@plt>
mov    %eax,%edi
call   1160 <sqrt@plt>
movsd  %xmm0,-0x58(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm2
movsd  -0x58(%rbp),%xmm3
mov    -0x30(%rbp),%rax
movsbl (%rax),%esi
lea    0xbff(%rip),%rdi
mov    $0x4,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1130 <kd_res_next@plt>
jmp    13db <main+0x18b>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   10c0 <kd_res_free@plt>
mov    -0x20(%rbp),%rdi
call   1090 <kd_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0xdbb(%rip),%rax
mov    %rax,0x20(%rsp)
movapd 0xd9e(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
mov    $0xa,%ebx
cmp    $0x2,%edi
jl     12a1 <main+0x51>
mov    %rsi,%rbp
mov    0x8(%rsi),%rax
movsbl (%rax),%edi
call   1050 <isdigit@plt>
test   %rax,%rax
je     12a1 <main+0x51>
mov    0x8(%rbp),%rdi
call   1150 <atoi@plt>
mov    %eax,%ebx
mov    %ebx,%edi
call   10f0 <malloc@plt>
test   %rax,%rax
je     1419 <main+0x1c9>
mov    %rax,%r15
xor    %edi,%edi
call   10d0 <time@plt>
mov    %eax,%edi
call   10a0 <srand@plt>
mov    $0x3,%edi
call   1110 <kd_create@plt>
mov    %rax,(%rsp)
test   %ebx,%ebx
jle    132e <main+0xde>
mov    %ebx,%r13d
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
lea    (%r15,%r14,1),%r12
lea    0x61(%r14),%eax
mov    %al,(%r15,%r14,1)
xor    %eax,%eax
call   10e0 <rd@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <rd@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10e0 <rd@plt>
mov    (%rsp),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %r12,%r8
call   1170 <kd_insert3@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   10b0 <assert@plt>
add    $0x1,%r14
cmp    %r14,%r13
jne    12e0 <main+0x90>
mov    %r15,0x8(%rsp)
lea    0x10(%rsp),%rsi
movsd  0xcc0(%rip),%xmm0
mov    (%rsp),%r14
mov    %r14,%rdi
call   1080 <kd_nearest_range@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <kd_res_size@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
lea    0xcd1(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <kd_res_end@plt>
test   %eax,%eax
jne    13ee <main+0x19e>
lea    0x30(%rsp),%r12
lea    0x10(%rsp),%r15
lea    0xcc0(%rip),%rbp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1100 <kd_res_item@plt>
mov    %rax,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x3,%edx
call   1120 <dist_sq@plt>
mov    %eax,%edi
call   1160 <sqrt@plt>
movapd %xmm0,%xmm3
movsd  0x30(%rsp),%xmm0
movsd  0x38(%rsp),%xmm1
movsd  0x40(%rsp),%xmm2
movsbl 0x0(%r13),%esi
mov    %rbp,%rdi
mov    $0x4,%al
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1130 <kd_res_next@plt>
mov    %rbx,%rdi
call   1030 <kd_res_end@plt>
test   %eax,%eax
je     1390 <main+0x140>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
call   10c0 <kd_res_free@plt>
mov    %r14,%rdi
call   1090 <kd_free@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc08(%rip),%rdi
call   1140 <perror@plt>
mov    $0x1,%eax
jmp    140a <main+0x1ba>

LLVM IR (-O0):
%struct.kdres = type { i32 }
@__const.main.pt = [3 x double] [double 0.000000e+00, double 0.000000e+00, double 1.000000e+00]
@.str = [14 x i8] c"malloc failed\00"
@.str.1 = [19 x i8] c"found %d results:\0A\00"
@.str.2 = [57 x i8] c"node at (%.3f, %.3f, %.3f) is %.3f away and has data=%c\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.kdres*
%12 = alloca [3 x double]
%13 = alloca double
%14 = alloca [3 x double]
%15 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10, i32* %7
%16 = bitcast [3 x double]* %14 to i8*
store double 1.000000e+01, double* %15
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %32
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = call i64 @isdigit(i8 signext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %7
br label %32
32: ; preds = %27, %19, %2
%33 = load i32, i32* %7
%34 = call i8* @malloc(i32 %33)
store i8* %34, i8** %9
%35 = icmp ne i8* %34, null
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %112
38: ; preds = %32
%39 = call i32 @time(i32 0)
%40 = call i32 @srand(i32 %39)
%41 = call i8* @kd_create(i32 3)
store i8* %41, i8** %8
store i32 0, i32* %6
br label %42
42: ; preds = %66, %38
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %69
46: ; preds = %42
%47 = load i32, i32* %6
%48 = add nsw i32 97, %47
%49 = trunc i32 %48 to i8
%50 = load i8*, i8** %9
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %49, i8* %53
%54 = load i8*, i8** %8
%55 = call i32 (...) @rd()
%56 = call i32 (...) @rd()
%57 = call i32 (...) @rd()
%58 = load i8*, i8** %9
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = call i64 @kd_insert3(i8* %54, i32 %55, i32 %56, i32 %57, i8* %61)
%63 = icmp eq i64 0, %62
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
br label %66
66: ; preds = %46
%67 = load i32, i32* %6
%68 = add nsw i32 %67, 1
store i32 %68, i32* %6
br label %42
69: ; preds = %42
%70 = load i8*, i8** %8
%71 = getelementptr inbounds [3 x double], [3 x double]* %14, i64 0, i64 0
%72 = load double, double* %15
%73 = call %struct.kdres* @kd_nearest_range(i8* %70, double* %71, double %72)
store %struct.kdres* %73, %struct.kdres** %11
%74 = load %struct.kdres*, %struct.kdres** %11
%75 = call i32 @kd_res_size(%struct.kdres* %74)
%76 = sitofp i32 %75 to double
%77 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), double %76)
br label %78
78: ; preds = %83, %69
%79 = load %struct.kdres*, %struct.kdres** %11
%80 = call i32 @kd_res_end(%struct.kdres* %79)
%81 = icmp ne i32 %80, 0
%82 = xor i1 %81, true
br i1 %82, label %83, label %105
83: ; preds = %78
%84 = load %struct.kdres*, %struct.kdres** %11
%85 = getelementptr inbounds [3 x double], [3 x double]* %12, i64 0, i64 0
%86 = call i64 @kd_res_item(%struct.kdres* %84, double* %85)
%87 = inttoptr i64 %86 to i8*
store i8* %87, i8** %10
%88 = getelementptr inbounds [3 x double], [3 x double]* %14, i64 0, i64 0
%89 = getelementptr inbounds [3 x double], [3 x double]* %12, i64 0, i64 0
%90 = call i32 @dist_sq(double* %88, double* %89, i32 3)
%91 = call double @sqrt(i32 %90)
store double %91, double* %13
%92 = getelementptr inbounds [3 x double], [3 x double]* %12, i64 0, i64 0
%93 = load double, double* %92
%94 = getelementptr inbounds [3 x double], [3 x double]* %12, i64 0, i64 1
%95 = load double, double* %94
%96 = getelementptr inbounds [3 x double], [3 x double]* %12, i64 0, i64 2
%97 = load double, double* %96
%98 = load double, double* %13
%99 = load i8*, i8** %10
%100 = load i8, i8* %99
%101 = sext i8 %100 to i32
%102 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), double %93, double %95, double %97, double %98, i32 %101)
%103 = load %struct.kdres*, %struct.kdres** %11
%104 = call i32 @kd_res_next(%struct.kdres* %103)
br label %78
105: ; preds = %78
%106 = load i8*, i8** %9
%107 = call i32 @free(i8* %106)
%108 = load %struct.kdres*, %struct.kdres** %11
%109 = call i32 @kd_res_free(%struct.kdres* %108)
%110 = load i8*, i8** %8
%111 = call i32 @kd_free(i8* %110)
store i32 0, i32* %3
br label %112
112: ; preds = %105, %36
%113 = load i32, i32* %3
ret i32 %113
}

LLVM IR (-O2):
%struct.kdres = type { i32 }
@__const.main.pt = [3 x double] [double 0.000000e+00, double 0.000000e+00, double 1.000000e+00]
@.str = [14 x i8] c"malloc failed\00"
@.str.1 = [19 x i8] c"found %d results:\0A\00"
@.str.2 = [57 x i8] c"node at (%.3f, %.3f, %.3f) is %.3f away and has data=%c\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x double]
%4 = alloca [3 x double]
%5 = bitcast [3 x double]* %3 to i8*
%6 = bitcast [3 x double]* %4 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i8, i8* %10
%12 = tail call i64 @isdigit(i8 signext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = tail call i32 @atoi(i8* %15)
br label %17
17: ; preds = %14, %8, %2
%18 = phi i32 [ %16, %14 ], [ 10, %8 ], [ 10, %2 ]
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %74
23: ; preds = %17
%24 = tail call i32 @time(i32 0)
%25 = tail call i32 @srand(i32 %24)
%26 = tail call i8* @kd_create(i32 3)
%27 = icmp sgt i32 %18, 0
br i1 %27, label %28, label %44
28: ; preds = %23
%29 = zext i32 %18 to i64
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 0, %28 ], [ %42, %30 ]
%32 = trunc i64 %31 to i8
%33 = add i8 %32, 97
%34 = getelementptr inbounds i8, i8* %19, i64 %31
store i8 %33, i8* %34
%35 = tail call i32 (...) @rd()
%36 = tail call i32 (...) @rd()
%37 = tail call i32 (...) @rd()
%38 = tail call i64 @kd_insert3(i8* %26, i32 %35, i32 %36, i32 %37, i8* %34)
%39 = icmp eq i64 %38, 0
%40 = zext i1 %39 to i32
%41 = tail call i32 @assert(i32 %40)
%42 = add nuw nsw i64 %31, 1
%43 = icmp eq i64 %42, %29
br i1 %43, label %44, label %30
44: ; preds = %30, %23
%45 = getelementptr inbounds [3 x double], [3 x double]* %4, i64 0, i64 0
%46 = call %struct.kdres* @kd_nearest_range(i8* %26, double* %45, double 1.000000e+01)
%47 = call i32 @kd_res_size(%struct.kdres* %46)
%48 = sitofp i32 %47 to double
%49 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), double %48)
%50 = call i32 @kd_res_end(%struct.kdres* %46)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %70
52: ; preds = %44
%53 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%54 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 1
%55 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 2
br label %56
56: ; preds = %52, %56
%57 = call i64 @kd_res_item(%struct.kdres* %46, double* %53)
%58 = inttoptr i64 %57 to i8*
%59 = call i32 @dist_sq(double* %45, double* %53, i32 3)
%60 = call double @sqrt(i32 %59)
%61 = load double, double* %53
%62 = load double, double* %54
%63 = load double, double* %55
%64 = load i8, i8* %58
%65 = sext i8 %64 to i32
%66 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), double %61, double %62, double %63, double %60, i32 %65)
%67 = call i32 @kd_res_next(%struct.kdres* %46)
%68 = call i32 @kd_res_end(%struct.kdres* %46)
%69 = icmp eq i32 %68, 0
br i1 %69, label %56, label %70
70: ; preds = %56, %44
%71 = call i32 @free(i8* %19)
%72 = call i32 @kd_res_free(%struct.kdres* %46)
%73 = call i32 @kd_free(i8* %26)
br label %74
74: ; preds = %70, %21
%75 = phi i32 [ 0, %70 ], [ 1, %21 ]
ret i32 %75
}