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


x86 assembly (clang, -O0):
<closest_centroid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a6 <closest_centroid+0x96>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <distance_metric@plt>
movsd  %xmm0,-0x30(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1183 <closest_centroid+0x73>
movsd  -0x30(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1193 <closest_centroid+0x83>
mov    -0x34(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1198 <closest_centroid+0x88>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1134 <closest_centroid+0x24>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closest_centroid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x8(%rsp)
test   %edx,%edx
jle    118a <closest_centroid+0x7a>
mov    %rsi,%rbx
movslq %ecx,%r12
mov    %edx,%r13d
shl    $0x3,%r12
mov    $0xffffffff,%ebp
xor    %r14d,%r14d
jmp    1159 <closest_centroid+0x49>
xchg   %ax,%ax
cmovne %r14d,%ebp
add    $0x1,%r14
add    %r12,%rbx
movsd  %xmm0,0x10(%rsp)
cmp    %r14,%r13
mov    %r15d,%ecx
je     118f <closest_centroid+0x7f>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    %ecx,%r15d
mov    %ecx,%edx
call   1030 <distance_metric@plt>
cmp    $0xffffffff,%ebp
sete   %al
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
seta   %cl
or     %al,%cl
test   %cl,%cl
jne    1140 <closest_centroid+0x30>
movapd %xmm1,%xmm0
jmp    1140 <closest_centroid+0x30>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @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 i32 [ -1, %4 ], [ %23, %11 ]
ret i32 %10
11: ; preds = %6, %11
%12 = phi i64 [ 0, %6 ], [ %24, %11 ]
%13 = phi i32 [ -1, %6 ], [ %23, %11 ]
%14 = phi double [ undef, %6 ], [ %21, %11 ]
%15 = mul nsw i64 %12, %7
%16 = getelementptr inbounds double, double* %1, i64 %15
%17 = tail call double @distance_metric(double* %0, double* %16, i32 %3)
%18 = icmp eq i32 %13, -1
%19 = fcmp olt double %17, %14
%20 = select i1 %18, i1 true, i1 %19
%21 = select i1 %20, double %17, double %14
%22 = trunc i64 %12 to i32
%23 = select i1 %20, i32 %22, i32 %13
%24 = add nuw nsw i64 %12, 1
%25 = icmp eq i64 %24, %8
br i1 %25, label %9, label %11
}