#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 */
typedef size_t uint32_t;
typedef size_t int8_t;

/* Variables and functions */
double calc_dist_double(double *, double *, size_t);
int /*<<< orphan*/ insert_point_double(size_t *, double *, size_t, double,
                                       size_t);
void search_leaf_double(double *restrict pa, uint32_t *restrict pidx,
                        int8_t no_dims, uint32_t start_idx, uint32_t n,
                        double *restrict point_coord, uint32_t k,
                        uint32_t *restrict closest_idx,
                        double *restrict closest_dist) {
  double cur_dist;
  for (uint32_t i = 0; i < n; i++) {
    cur_dist = calc_dist_double(&(pa[no_dims * pidx[start_idx + i] + 0]),
                                point_coord, no_dims);
    if (cur_dist < closest_dist[k - 1]) {
      insert_point_double(closest_idx, closest_dist, pidx[start_idx + i],
                          cur_dist, k);
    }
  }
}


x86 assembly (clang, -O0):
<search_leaf_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11f4 <search_leaf_double+0xd4>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rdx
add    -0x40(%rbp),%rdx
imul   (%rcx,%rdx,8),%rax
add    $0x0,%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <calc_dist_double@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm1
mov    0x20(%rbp),%rax
mov    0x10(%rbp),%rcx
sub    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jbe    11de <search_leaf_double+0xbe>
mov    0x18(%rbp),%rdi
mov    0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
movsd  -0x38(%rbp),%xmm0
mov    0x10(%rbp),%rcx
call   1040 <insert_point_double@plt>
jmp    11e3 <search_leaf_double+0xc3>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1154 <search_leaf_double+0x34>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_leaf_double>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,(%rsp)
test   %r8,%r8
je     11a4 <search_leaf_double+0x84>
mov    %r8,%r15
mov    %rdx,%rbx
mov    %rdi,%r12
lea    (%rsi,%rcx,8),%rbp
xor    %r14d,%r14d
jmp    1159 <search_leaf_double+0x39>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r14,%r15
je     11a4 <search_leaf_double+0x84>
mov    0x0(%rbp,%r14,8),%r13
mov    %r13,%rax
imul   %rbx,%rax
lea    (%r12,%rax,8),%rdi
mov    (%rsp),%rsi
mov    %rbx,%rdx
call   1030 <calc_dist_double@plt>
mov    0x50(%rsp),%rax
mov    0x40(%rsp),%rcx
movsd  -0x8(%rax,%rcx,8),%xmm1
ucomisd %xmm0,%xmm1
jbe    1150 <search_leaf_double+0x30>
mov    0x48(%rsp),%rdi
mov    0x50(%rsp),%rsi
mov    %r13,%rdx
mov    0x40(%rsp),%rcx
call   1040 <insert_point_double@plt>
jmp    1150 <search_leaf_double+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @search_leaf_double(double* noalias %0, i64* noalias %1, i64 %2, i64 %3, i64 %4, double* noalias %5, i64 %6, i64* noalias %7, double* noalias %8) {
%10 = alloca double*
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca double*
%16 = alloca i64
%17 = alloca i64*
%18 = alloca double*
%19 = alloca double
%20 = alloca i64
store double* %0, double** %10
store i64* %1, i64** %11
store i64 %2, i64* %12
store i64 %3, i64* %13
store i64 %4, i64* %14
store double* %5, double** %15
store i64 %6, i64* %16
store i64* %7, i64** %17
store double* %8, double** %18
store i64 0, i64* %20
br label %21
21: ; preds = %60, %9
%22 = load i64, i64* %20
%23 = load i64, i64* %14
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %63
25: ; preds = %21
%26 = load double*, double** %10
%27 = load i64, i64* %12
%28 = load i64*, i64** %11
%29 = load i64, i64* %13
%30 = load i64, i64* %20
%31 = add i64 %29, %30
%32 = getelementptr inbounds i64, i64* %28, i64 %31
%33 = load i64, i64* %32
%34 = mul i64 %27, %33
%35 = add i64 %34, 0
%36 = getelementptr inbounds double, double* %26, i64 %35
%37 = load double*, double** %15
%38 = load i64, i64* %12
%39 = call double @calc_dist_double(double* %36, double* %37, i64 %38)
store double %39, double* %19
%40 = load double, double* %19
%41 = load double*, double** %18
%42 = load i64, i64* %16
%43 = sub i64 %42, 1
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = load double, double* %44
%46 = fcmp olt double %40, %45
br i1 %46, label %47, label %59
47: ; preds = %25
%48 = load i64*, i64** %17
%49 = load double*, double** %18
%50 = load i64*, i64** %11
%51 = load i64, i64* %13
%52 = load i64, i64* %20
%53 = add i64 %51, %52
%54 = getelementptr inbounds i64, i64* %50, i64 %53
%55 = load i64, i64* %54
%56 = load double, double* %19
%57 = load i64, i64* %16
%58 = call i32 @insert_point_double(i64* %48, double* %49, i64 %55, double %56, i64 %57)
br label %59
59: ; preds = %47, %25
br label %60
60: ; preds = %59
%61 = load i64, i64* %20
%62 = add i64 %61, 1
store i64 %62, i64* %20
br label %21
63: ; preds = %21
ret void
}

LLVM IR (-O2):
define void @search_leaf_double(double* noalias %0, i64* noalias nocapture %1, i64 %2, i64 %3, i64 %4, double* noalias %5, i64 %6, i64* noalias %7, double* noalias %8) {
%10 = icmp eq i64 %4, 0
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = add i64 %6, -1
%13 = getelementptr inbounds double, double* %8, i64 %12
br label %15
14: ; preds = %27, %9
ret void
15: ; preds = %11, %27
%16 = phi i64 [ 0, %11 ], [ %28, %27 ]
%17 = add i64 %16, %3
%18 = getelementptr inbounds i64, i64* %1, i64 %17
%19 = load i64, i64* %18
%20 = mul i64 %19, %2
%21 = getelementptr inbounds double, double* %0, i64 %20
%22 = tail call double @calc_dist_double(double* %21, double* %5, i64 %2)
%23 = load double, double* %13
%24 = fcmp olt double %22, %23
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = tail call i32 @insert_point_double(i64* %7, double* %8, i64 %19, double %22, i64 %6)
br label %27
27: ; preds = %15, %25
%28 = add nuw i64 %16, 1
%29 = icmp eq i64 %28, %4
br i1 %29, label %14, label %15
}