#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 euclid_dist_2(float *, float *, int);
int find_nearest_point(float *pt, int nfeatures, float *pts, int npts) {
  int index, i;
  float min_dist = 3.40282347e+38;
  for (i = 0; i < npts; i++) {
    float dist;
    dist = euclid_dist_2(pt, pts + (i * nfeatures), nfeatures);
    if (dist < min_dist) {
      min_dist = dist;
      index = i;
    }
  }
  return (index);
}


x86 assembly (clang, -O0):
<find_nearest_point>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movss  0xed2(%rip),%xmm0
movss  %xmm0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a1 <find_nearest_point+0x91>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%eax
imul   -0xc(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
call   1030 <euclid_dist_2@plt>
movss  %xmm0,-0x2c(%rbp)
movss  -0x2c(%rbp),%xmm1
movss  -0x28(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    118e <find_nearest_point+0x7e>
movss  -0x2c(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1193 <find_nearest_point+0x83>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <find_nearest_point+0x2a>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_nearest_point>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,(%rsp)
test   %ecx,%ecx
jle    1173 <find_nearest_point+0x63>
mov    %rdx,%rbx
mov    %rdi,%r15
movslq (%rsp),%r12
mov    %ecx,%r13d
shl    $0x2,%r12
movss  0xec5(%rip),%xmm0
xor    %r14d,%r14d
xchg   %ax,%ax
movss  %xmm0,0x4(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    (%rsp),%edx
call   1030 <euclid_dist_2@plt>
movss  0x4(%rsp),%xmm1
ucomiss %xmm0,%xmm1
cmova  %r14d,%ebp
minss  %xmm1,%xmm0
add    $0x1,%r14
add    %r12,%rbx
cmp    %r14,%r13
jne    1140 <find_nearest_point+0x30>
jmp    1173 <find_nearest_point+0x63>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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