#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 */
float distance2_c(float *, float *, int);
int full_search_NN(float *vector, float *clusters, int num_training_vectors,
                   int dim, float *min_dist2) {
  int i;
  int min_ind;
  float min_dist;
  float dist;
  min_dist = distance2_c(vector, &clusters[0], dim);
  min_ind = 0;
  for (i = 1; i < num_training_vectors; i++) {
    dist = distance2_c(vector, &clusters[i], dim);
    if (dist < min_dist) {
      min_dist = dist;
      min_ind = i;
    }
  }
  *min_dist2 = min_dist;
  return min_ind;
}


x86 assembly (clang, -O0):
<full_search_NN>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <distance2_c@plt>
movss  %xmm0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11af <full_search_NN+0x9f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <distance2_c@plt>
movss  %xmm0,-0x30(%rbp)
movss  -0x30(%rbp),%xmm1
movss  -0x2c(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    119c <full_search_NN+0x8c>
movss  -0x30(%rbp),%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11a1 <full_search_NN+0x91>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    114d <full_search_NN+0x3d>
movss  -0x2c(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movss  %xmm0,(%rax)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_search_NN>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %ecx,%edx
call   1030 <distance2_c@plt>
movaps %xmm0,%xmm1
xor    %ebp,%ebp
cmp    $0x2,%r13d
jl     1187 <full_search_NN+0x77>
mov    %r13d,%r13d
add    $0x4,%rbx
xor    %ebp,%ebp
mov    $0x1,%r14d
movss  %xmm1,0xc(%rsp)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <distance2_c@plt>
movss  0xc(%rsp),%xmm1
ucomiss %xmm0,%xmm1
cmova  %r14d,%ebp
minss  %xmm1,%xmm0
add    $0x1,%r14
add    $0x4,%rbx
movaps %xmm0,%xmm1
cmp    %r14,%r13
jne    1150 <full_search_NN+0x40>
jmp    118a <full_search_NN+0x7a>
movaps %xmm1,%xmm0
mov    0x10(%rsp),%rax
movss  %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 @full_search_NN(float* %0, float* %1, i32 %2, i32 %3, float* %4) {
%6 = alloca float*
%7 = alloca float*
%8 = alloca i32
%9 = alloca i32
%10 = alloca float*
%11 = alloca i32
%12 = alloca i32
%13 = alloca float
%14 = alloca float
store float* %0, float** %6
store float* %1, float** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store float* %4, float** %10
%15 = load float*, float** %6
%16 = load float*, float** %7
%17 = getelementptr inbounds float, float* %16, i64 0
%18 = load i32, i32* %9
%19 = call float @distance2_c(float* %15, float* %17, i32 %18)
store float %19, float* %13
store i32 0, i32* %12
store i32 1, i32* %11
br label %20
20: ; preds = %39, %5
%21 = load i32, i32* %11
%22 = load i32, i32* %8
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %42
24: ; preds = %20
%25 = load float*, float** %6
%26 = load float*, float** %7
%27 = load i32, i32* %11
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds float, float* %26, i64 %28
%30 = load i32, i32* %9
%31 = call float @distance2_c(float* %25, float* %29, i32 %30)
store float %31, float* %14
%32 = load float, float* %14
%33 = load float, float* %13
%34 = fcmp olt float %32, %33
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = load float, float* %14
store float %36, float* %13
%37 = load i32, i32* %11
store i32 %37, i32* %12
br label %38
38: ; preds = %35, %24
br label %39
39: ; preds = %38
%40 = load i32, i32* %11
%41 = add nsw i32 %40, 1
store i32 %41, i32* %11
br label %20
42: ; preds = %20
%43 = load float, float* %13
%44 = load float*, float** %10
store float %43, float* %44
%45 = load i32, i32* %12
ret i32 %45
}

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