#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 dist(float *, float *, int);
int nearest(float *s, float *cents, int c, int f, float *dd) {
  int rc = -1;
  double d = 1e12;
  for (int i = 0; i < c; i++) {
    double t = dist(s, cents + i * f, f);
    if (t < d) {
      d = t;
      rc = i;
    }
  }
  if (dd)
    *dd = d;
  return rc;
}


x86 assembly (clang, -O0):
<nearest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movsd  0xec7(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ad <nearest+0x9d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <dist@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    119a <nearest+0x8a>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    119f <nearest+0x8f>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1145 <nearest+0x35>
cmpq   $0x0,-0x20(%rbp)
je     11c9 <nearest+0xb9>
movsd  -0x30(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
mov    -0x20(%rbp),%rax
movss  %xmm0,(%rax)
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nearest>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x4(%rsp)
test   %edx,%edx
jle    11b7 <nearest+0xa7>
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %r8,0x8(%rsp)
movslq 0x4(%rsp),%r13
mov    %edx,%r14d
shl    $0x2,%r13
movsd  0xebf(%rip),%xmm0
mov    $0xffffffff,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x10(%rsp)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    0x4(%rsp),%edx
call   1030 <dist@plt>
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
cmova  %r15d,%ebp
minsd  %xmm1,%xmm0
add    $0x1,%r15
add    %r13,%rbx
cmp    %r15,%r14
jne    1160 <nearest+0x50>
cvtsd2ss %xmm0,%xmm0
mov    0x8(%rsp),%r8
test   %r8,%r8
je     11a6 <nearest+0x96>
movss  %xmm0,(%r8)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
movss  0xe3c(%rip),%xmm0
test   %r8,%r8
jne    11a1 <nearest+0x91>
jmp    11a6 <nearest+0x96>

LLVM IR (-O0):
define i32 @nearest(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 double
%13 = alloca i32
%14 = alloca double
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
store i32 -1, i32* %11
store double 1.000000e+12, double* %12
store i32 0, i32* %13
br label %15
15: ; preds = %36, %5
%16 = load i32, i32* %13
%17 = load i32, i32* %8
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %39
19: ; preds = %15
%20 = load float*, float** %6
%21 = load float*, float** %7
%22 = load i32, i32* %13
%23 = load i32, i32* %9
%24 = mul nsw i32 %22, %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds float, float* %21, i64 %25
%27 = load i32, i32* %9
%28 = call double @dist(float* %20, float* %26, i32 %27)
store double %28, double* %14
%29 = load double, double* %14
%30 = load double, double* %12
%31 = fcmp olt double %29, %30
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = load double, double* %14
store double %33, double* %12
%34 = load i32, i32* %13
store i32 %34, i32* %11
br label %35
35: ; preds = %32, %19
br label %36
36: ; preds = %35
%37 = load i32, i32* %13
%38 = add nsw i32 %37, 1
store i32 %38, i32* %13
br label %15
39: ; preds = %15
%40 = load float*, float** %10
%41 = icmp ne float* %40, null
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load double, double* %12
%44 = fptrunc double %43 to float
%45 = load float*, float** %10
store float %44, float* %45
br label %46
46: ; preds = %42, %39
%47 = load i32, i32* %11
ret i32 %47
}

LLVM IR (-O2):
define i32 @nearest(float* %0, float* %1, i32 %2, i32 %3, float* writeonly %4) {
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %12
7: ; preds = %5
%8 = sext i32 %3 to i64
%9 = zext i32 %2 to i64
br label %16
10: ; preds = %16
%11 = fptrunc double %24 to float
br label %12
12: ; preds = %10, %5
%13 = phi float [ 0x426D1A94A0000000, %5 ], [ %11, %10 ]
%14 = phi i32 [ -1, %5 ], [ %26, %10 ]
%15 = icmp eq float* %4, null
br i1 %15, label %30, label %29
16: ; preds = %7, %16
%17 = phi i64 [ 0, %7 ], [ %27, %16 ]
%18 = phi i32 [ -1, %7 ], [ %26, %16 ]
%19 = phi double [ 1.000000e+12, %7 ], [ %24, %16 ]
%20 = mul nsw i64 %17, %8
%21 = getelementptr inbounds float, float* %1, i64 %20
%22 = tail call double @dist(float* %0, float* %21, i32 %3)
%23 = fcmp olt double %22, %19
%24 = select i1 %23, double %22, double %19
%25 = trunc i64 %17 to i32
%26 = select i1 %23, i32 %25, i32 %18
%27 = add nuw nsw i64 %17, 1
%28 = icmp eq i64 %27, %9
br i1 %28, label %10, label %16
29: ; preds = %12
store float %13, float* %4
br label %30
30: ; preds = %29, %12
ret i32 %14
}