#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 */

/* Variables and functions */
double euclidean_distance(double *, double *, int);
int find_closest(double *points, double *sample, int dims, int n_points,
                 double *dist_pointer) {
  if (!points || !sample || dims < 1 || n_points < 1) {
    return -1;
  }
  double dummy_dist;
  double *distance = (dist_pointer) ? dist_pointer : &dummy_dist;
  int closest = 0;
  *distance = euclidean_distance(points, sample, dims);
  int i;
  for (i = 1; i < n_points; i++) {
    double d = euclidean_distance(&points[i * dims], sample, dims);
    if (d < *distance) {
      *distance = d;
      closest = i;
    }
  }
  return closest;
}


x86 assembly (clang, -O0):
<find_closest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1154 <find_closest+0x44>
cmpq   $0x0,-0x18(%rbp)
je     1154 <find_closest+0x44>
cmpl   $0x1,-0x1c(%rbp)
jl     1154 <find_closest+0x44>
cmpl   $0x1,-0x20(%rbp)
jge    1160 <find_closest+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <find_closest+0x117>
cmpq   $0x0,-0x28(%rbp)
je     1178 <find_closest+0x68>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1185 <find_closest+0x75>
lea    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1185 <find_closest+0x75>
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <euclidean_distance@plt>
mov    -0x38(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x1,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1221 <find_closest+0x111>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <euclidean_distance@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm1
mov    -0x38(%rbp),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
jbe    120e <find_closest+0xfe>
movsd  -0x48(%rbp),%xmm0
mov    -0x38(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1213 <find_closest+0x103>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11b3 <find_closest+0xa3>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_closest>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0x8(%rsp)
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     11d7 <find_closest+0xc7>
test   %rsi,%rsi
je     11d7 <find_closest+0xc7>
cmpl   $0x0,0x8(%rsp)
jle    11d7 <find_closest+0xc7>
mov    %ecx,%r14d
test   %ecx,%ecx
jle    11d7 <find_closest+0xc7>
mov    %rdi,%rbp
test   %r8,%r8
lea    0x10(%rsp),%rbx
cmovne %r8,%rbx
mov    %rsi,%r13
mov    0x8(%rsp),%edx
call   1030 <euclidean_distance@plt>
movsd  %xmm0,(%rbx)
movl   $0x0,0xc(%rsp)
cmp    $0x2,%r14d
jb     11d3 <find_closest+0xc3>
mov    0x8(%rsp),%r12d
mov    %r14d,%r15d
lea    0x0(%rbp,%r12,8),%rbp
shl    $0x3,%r12
movl   $0x0,0xc(%rsp)
mov    $0x1,%r14d
jmp    11ac <find_closest+0x9c>
nopl   (%rax)
add    $0x1,%r14
add    %r12,%rbp
cmp    %r14,%r15
je     11d3 <find_closest+0xc3>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    0x8(%rsp),%edx
call   1030 <euclidean_distance@plt>
movsd  (%rbx),%xmm1
ucomisd %xmm0,%xmm1
jbe    11a0 <find_closest+0x90>
movsd  %xmm0,(%rbx)
mov    %r14d,%eax
mov    %r14d,0xc(%rsp)
jmp    11a0 <find_closest+0x90>
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_closest(double* %0, double* %1, i32 %2, i32 %3, double* %4) {
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
%11 = alloca double*
%12 = alloca double
%13 = alloca double*
%14 = alloca i32
%15 = alloca i32
%16 = alloca double
store double* %0, double** %7
store double* %1, double** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store double* %4, double** %11
%17 = load double*, double** %7
%18 = icmp ne double* %17, null
br i1 %18, label %19, label %28
19: ; preds = %5
%20 = load double*, double** %8
%21 = icmp ne double* %20, null
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 1
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %25, %22, %19, %5
store i32 -1, i32* %6
br label %70
29: ; preds = %25
%30 = load double*, double** %11
%31 = icmp ne double* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load double*, double** %11
br label %35
34: ; preds = %29
br label %35
35: ; preds = %34, %32
%36 = phi double* [ %33, %32 ], [ %12, %34 ]
store double* %36, double** %13
store i32 0, i32* %14
%37 = load double*, double** %7
%38 = load double*, double** %8
%39 = load i32, i32* %9
%40 = call double @euclidean_distance(double* %37, double* %38, i32 %39)
%41 = load double*, double** %13
store double %40, double* %41
store i32 1, i32* %15
br label %42
42: ; preds = %65, %35
%43 = load i32, i32* %15
%44 = load i32, i32* %10
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %68
46: ; preds = %42
%47 = load double*, double** %7
%48 = load i32, i32* %15
%49 = load i32, i32* %9
%50 = mul nsw i32 %48, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %47, i64 %51
%53 = load double*, double** %8
%54 = load i32, i32* %9
%55 = call double @euclidean_distance(double* %52, double* %53, i32 %54)
store double %55, double* %16
%56 = load double, double* %16
%57 = load double*, double** %13
%58 = load double, double* %57
%59 = fcmp olt double %56, %58
br i1 %59, label %60, label %64
60: ; preds = %46
%61 = load double, double* %16
%62 = load double*, double** %13
store double %61, double* %62
%63 = load i32, i32* %15
store i32 %63, i32* %14
br label %64
64: ; preds = %60, %46
br label %65
65: ; preds = %64
%66 = load i32, i32* %15
%67 = add nsw i32 %66, 1
store i32 %67, i32* %15
br label %42
68: ; preds = %42
%69 = load i32, i32* %14
store i32 %69, i32* %6
br label %70
70: ; preds = %68, %28
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
define i32 @find_closest(double* %0, double* %1, i32 %2, i32 %3, double* %4) {
%6 = alloca double
%7 = icmp eq double* %0, null
%8 = icmp eq double* %1, null
%9 = or i1 %7, %8
%10 = icmp slt i32 %2, 1
%11 = or i1 %9, %10
%12 = icmp slt i32 %3, 1
%13 = or i1 %11, %12
br i1 %13, label %39, label %14
14: ; preds = %5
%15 = bitcast double* %6 to i8*
%16 = icmp eq double* %4, null
%17 = select i1 %16, double* %6, double* %4
%18 = tail call double @euclidean_distance(double* %0, double* %1, i32 %2)
store double %18, double* %17
%19 = icmp ugt i32 %3, 1
br i1 %19, label %20, label %37
20: ; preds = %14
%21 = zext i32 %2 to i64
%22 = zext i32 %3 to i64
br label %23
23: ; preds = %20, %33
%24 = phi i64 [ 1, %20 ], [ %35, %33 ]
%25 = phi i32 [ 0, %20 ], [ %34, %33 ]
%26 = mul nsw i64 %24, %21
%27 = getelementptr inbounds double, double* %0, i64 %26
%28 = tail call double @euclidean_distance(double* %27, double* %1, i32 %2)
%29 = load double, double* %17
%30 = fcmp olt double %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
store double %28, double* %17
%32 = trunc i64 %24 to i32
br label %33
33: ; preds = %31, %23
%34 = phi i32 [ %32, %31 ], [ %25, %23 ]
%35 = add nuw nsw i64 %24, 1
%36 = icmp eq i64 %35, %22
br i1 %36, label %37, label %23
37: ; preds = %33, %14
%38 = phi i32 [ 0, %14 ], [ %34, %33 ]
br label %39
39: ; preds = %5, %37
%40 = phi i32 [ %38, %37 ], [ -1, %5 ]
ret i32 %40
}