#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 distance2_c(float *, float *, int);
int nn_findNN_full_search(float *query_vector, float *training_vectors,
                          int num_of_training_vectors, int dim,
                          double *min_distance) {
  int i;
  double min_d = 0;
  int min_i = -1;
  double d = 0;
  float *v = training_vectors;
  int dim2 = dim;
  min_d = distance2_c(v, query_vector, dim);
  min_i = 0;
  v += dim2;
  for (i = 1; i < num_of_training_vectors; i++) {
    d = distance2_c(v, query_vector, dim);
    if (d < min_d) {
      min_d = d;
      min_i = i;
    }
    v += dim2;
  }
  *min_distance = min_d;
  return min_i;
}


x86 assembly (clang, -O0):
<nn_findNN_full_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <distance2_c@plt>
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x4c(%rbp),%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ef <nn_findNN_full_search+0xdf>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <distance2_c@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11cc <nn_findNN_full_search+0xbc>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x4c(%rbp),%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1187 <nn_findNN_full_search+0x77>
movsd  -0x30(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nn_findNN_full_search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x8(%rsp)
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rsi,%rdi
mov    %r13,%rsi
mov    %ecx,0x4(%rsp)
mov    %ecx,%edx
call   1030 <distance2_c@plt>
movapd %xmm0,%xmm1
xor    %ebp,%ebp
cmp    $0x2,%r12d
jl     1199 <nn_findNN_full_search+0x89>
movslq 0x4(%rsp),%r14
lea    (%r15,%r14,4),%rbx
shl    $0x2,%r14
xor    %ebp,%ebp
mov    $0x1,%r15d
xchg   %ax,%ax
movsd  %xmm1,0x10(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    0x4(%rsp),%edx
call   1030 <distance2_c@plt>
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
cmova  %r15d,%ebp
minsd  %xmm1,%xmm0
add    $0x1,%r15d
add    %r14,%rbx
movapd %xmm0,%xmm1
cmp    %r15d,%r12d
jne    1160 <nn_findNN_full_search+0x50>
jmp    119d <nn_findNN_full_search+0x8d>
movapd %xmm1,%xmm0
mov    0x8(%rsp),%rax
movsd  %xmm0,(%rax)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nn_findNN_full_search(float* %0, float* %1, i32 %2, i32 %3, double* %4) {
%6 = alloca float*
%7 = alloca float*
%8 = alloca i32
%9 = alloca i32
%10 = alloca double*
%11 = alloca i32
%12 = alloca double
%13 = alloca i32
%14 = alloca double
%15 = alloca float*
%16 = alloca i32
store float* %0, float** %6
store float* %1, float** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store double* %4, double** %10
store double 0.000000e+00, double* %12
store i32 -1, i32* %13
store double 0.000000e+00, double* %14
%17 = load float*, float** %7
store float* %17, float** %15
%18 = load i32, i32* %9
store i32 %18, i32* %16
%19 = load float*, float** %15
%20 = load float*, float** %6
%21 = load i32, i32* %9
%22 = call double @distance2_c(float* %19, float* %20, i32 %21)
store double %22, double* %12
store i32 0, i32* %13
%23 = load i32, i32* %16
%24 = load float*, float** %15
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds float, float* %24, i64 %25
store float* %26, float** %15
store i32 1, i32* %11
br label %27
27: ; preds = %47, %5
%28 = load i32, i32* %11
%29 = load i32, i32* %8
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %50
31: ; preds = %27
%32 = load float*, float** %15
%33 = load float*, float** %6
%34 = load i32, i32* %9
%35 = call double @distance2_c(float* %32, float* %33, i32 %34)
store double %35, double* %14
%36 = load double, double* %14
%37 = load double, double* %12
%38 = fcmp olt double %36, %37
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = load double, double* %14
store double %40, double* %12
%41 = load i32, i32* %11
store i32 %41, i32* %13
br label %42
42: ; preds = %39, %31
%43 = load i32, i32* %16
%44 = load float*, float** %15
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds float, float* %44, i64 %45
store float* %46, float** %15
br label %47
47: ; preds = %42
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %27
50: ; preds = %27
%51 = load double, double* %12
%52 = load double*, double** %10
store double %51, double* %52
%53 = load i32, i32* %13
ret i32 %53
}

LLVM IR (-O2):
define i32 @nn_findNN_full_search(float* %0, float* %1, i32 %2, i32 %3, double* nocapture writeonly %4) {
%6 = tail call double @distance2_c(float* %1, float* %0, i32 %3)
%7 = sext i32 %3 to i64
%8 = icmp sgt i32 %2, 1
br i1 %8, label %9, label %21
9: ; preds = %5, %9
%10 = phi float* [ %14, %9 ], [ %1, %5 ]
%11 = phi i32 [ %19, %9 ], [ 1, %5 ]
%12 = phi i32 [ %18, %9 ], [ 0, %5 ]
%13 = phi double [ %17, %9 ], [ %6, %5 ]
%14 = getelementptr inbounds float, float* %10, i64 %7
%15 = tail call double @distance2_c(float* %14, float* %0, i32 %3)
%16 = fcmp olt double %15, %13
%17 = select i1 %16, double %15, double %13
%18 = select i1 %16, i32 %11, i32 %12
%19 = add nuw nsw i32 %11, 1
%20 = icmp eq i32 %19, %2
br i1 %20, label %21, label %9
21: ; preds = %9, %5
%22 = phi double [ %6, %5 ], [ %17, %9 ]
%23 = phi i32 [ 0, %5 ], [ %18, %9 ]
store double %22, double* %4
ret i32 %23
}