#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 distance_metric(double *, double *, int);
double distance_to_closest_centroid(double *vector, double *centroids, int k,
                                    int D) {
  double min_distance = -1.0;
  double cur_distance;
  for (int i = 0; i < k; i++) {
    cur_distance = distance_metric(vector, (centroids + i * D), D);
    if (min_distance < 0 || cur_distance < min_distance) {
      min_distance = cur_distance;
    }
  }
  return min_distance;
}


x86 assembly (clang, -O0):
<distance_to_closest_centroid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movsd  0xed2(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11aa <distance_to_closest_centroid+0x9a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <distance_metric@plt>
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
ja     118d <distance_to_closest_centroid+0x7d>
movsd  -0x28(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1197 <distance_to_closest_centroid+0x87>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    119c <distance_to_closest_centroid+0x8c>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113a <distance_to_closest_centroid+0x2a>
movsd  -0x20(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<distance_to_closest_centroid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
test   %edx,%edx
jle    118e <distance_to_closest_centroid+0x7e>
mov    %ecx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
movslq %ecx,%r12
mov    %edx,%ebp
shl    $0x3,%r12
movsd  0xec6(%rip),%xmm0
movapd %xmm0,%xmm2
xchg   %ax,%ax
movsd  %xmm0,0x8(%rsp)
movapd %xmm2,0x10(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <distance_metric@plt>
movapd 0x10(%rsp),%xmm2
cmpltsd 0xe9f(%rip),%xmm2
movapd %xmm2,%xmm1
andpd  %xmm0,%xmm1
minsd  0x8(%rsp),%xmm0
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
add    %r12,%rbx
movapd %xmm2,%xmm0
add    $0xffffffffffffffff,%rbp
jne    1140 <distance_to_closest_centroid+0x30>
jmp    1196 <distance_to_closest_centroid+0x86>
movsd  0xe6a(%rip),%xmm2
movapd %xmm2,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @distance_to_closest_centroid(double* %0, double* %1, i32 %2, i32 %3) {
%5 = alloca double*
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca i32
store double* %0, double** %5
store double* %1, double** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store double -1.000000e+00, double* %9
store i32 0, i32* %11
br label %12
12: ; preds = %35, %4
%13 = load i32, i32* %11
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %38
16: ; preds = %12
%17 = load double*, double** %5
%18 = load double*, double** %6
%19 = load i32, i32* %11
%20 = load i32, i32* %8
%21 = mul nsw i32 %19, %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %18, i64 %22
%24 = load i32, i32* %8
%25 = call double @distance_metric(double* %17, double* %23, i32 %24)
store double %25, double* %10
%26 = load double, double* %9
%27 = fcmp olt double %26, 0.000000e+00
br i1 %27, label %32, label %28
28: ; preds = %16
%29 = load double, double* %10
%30 = load double, double* %9
%31 = fcmp olt double %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28, %16
%33 = load double, double* %10
store double %33, double* %9
br label %34
34: ; preds = %32, %28
br label %35
35: ; preds = %34
%36 = load i32, i32* %11
%37 = add nsw i32 %36, 1
store i32 %37, i32* %11
br label %12
38: ; preds = %12
%39 = load double, double* %9
ret double %39
}

LLVM IR (-O2):
define double @distance_to_closest_centroid(double* %0, double* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = sext i32 %3 to i64
%8 = zext i32 %2 to i64
br label %11
9: ; preds = %11, %4
%10 = phi double [ -1.000000e+00, %4 ], [ %20, %11 ]
ret double %10
11: ; preds = %6, %11
%12 = phi i64 [ 0, %6 ], [ %21, %11 ]
%13 = phi double [ -1.000000e+00, %6 ], [ %20, %11 ]
%14 = mul nsw i64 %12, %7
%15 = getelementptr inbounds double, double* %1, i64 %14
%16 = tail call double @distance_metric(double* %0, double* %15, i32 %3)
%17 = fcmp olt double %13, 0.000000e+00
%18 = fcmp olt double %16, %13
%19 = select i1 %17, i1 true, i1 %18
%20 = select i1 %19, double %16, double %13
%21 = add nuw nsw i64 %12, 1
%22 = icmp eq i64 %21, %8
br i1 %22, label %9, label %11
}