#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 DBL_MAX;
double common_euclidDist2(double *, double *, int);
int common_findNearestPoint(double *pt, int nfeatures, double **pts, int npts) {
  int index = -1;
  int i;
  double max_dist = DBL_MAX;
  const double limit = 0.99999;
  for (i = 0; i < npts; i++) {
    double dist;
    dist = common_euclidDist2(pt, pts[i], nfeatures);
    if ((dist / max_dist) < limit) {
      max_dist = dist;
      index = i;
      if (max_dist == 0) {
        break;
      }
    }
  }
  return index;
}


x86 assembly (clang, -O0):
<common_findNearestPoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2eac(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xebb(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11db <common_findNearestPoint+0xcb>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0xc(%rbp),%edx
call   1030 <common_euclidDist2@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
divsd  -0x30(%rbp),%xmm1
movsd  0xe74(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11c8 <common_findNearestPoint+0xb8>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11c3 <common_findNearestPoint+0xb3>
jp     11c3 <common_findNearestPoint+0xb3>
jmp    11db <common_findNearestPoint+0xcb>
jmp    11c8 <common_findNearestPoint+0xb8>
jmp    11cd <common_findNearestPoint+0xbd>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1151 <common_findNearestPoint+0x41>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<common_findNearestPoint>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
jle    1195 <common_findNearestPoint+0x85>
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r13
mov    0x2eb1(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,(%rsp)
mov    %ecx,%ebp
mov    $0xffffffff,%r15d
xor    %ebx,%ebx
jmp    1159 <common_findNearestPoint+0x49>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11a0 <common_findNearestPoint+0x90>
mov    (%r14,%rbx,8),%rsi
mov    %r13,%rdi
mov    %r12d,%edx
call   1030 <common_euclidDist2@plt>
movapd %xmm0,%xmm1
divsd  (%rsp),%xmm1
movsd  0xe87(%rip),%xmm2
ucomisd %xmm1,%xmm2
jbe    1150 <common_findNearestPoint+0x40>
ucomisd 0xe81(%rip),%xmm0
jne    118b <common_findNearestPoint+0x7b>
jnp    119d <common_findNearestPoint+0x8d>
movsd  %xmm0,(%rsp)
mov    %ebx,%r15d
jmp    1150 <common_findNearestPoint+0x40>
mov    $0xffffffff,%r15d
jmp    11a0 <common_findNearestPoint+0x90>
mov    %ebx,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@DBL_MAX = global double 0.000000e+00
define i32 @common_findNearestPoint(double* %0, i32 %1, double** nocapture %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %28
6: ; preds = %4
%7 = load double, double* @DBL_MAX
%8 = zext i32 %3 to i64
br label %9
9: ; preds = %6, %21
%10 = phi i64 [ 0, %6 ], [ %24, %21 ]
%11 = phi i32 [ -1, %6 ], [ %23, %21 ]
%12 = phi double [ %7, %6 ], [ %22, %21 ]
%13 = getelementptr inbounds double*, double** %2, i64 %10
%14 = load double*, double** %13
%15 = tail call double @common_euclidDist2(double* %0, double* %14, i32 %1)
%16 = fdiv double %15, %12
%17 = fcmp olt double %16, 9.999900e-01
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = fcmp oeq double %15, 0.000000e+00
%20 = trunc i64 %10 to i32
br i1 %19, label %26, label %21
21: ; preds = %9, %18
%22 = phi double [ %15, %18 ], [ %12, %9 ]
%23 = phi i32 [ %20, %18 ], [ %11, %9 ]
%24 = add nuw nsw i64 %10, 1
%25 = icmp eq i64 %24, %8
br i1 %25, label %28, label %9
26: ; preds = %18
%27 = trunc i64 %10 to i32
br label %28
28: ; preds = %21, %26, %4
%29 = phi i32 [ -1, %4 ], [ %27, %26 ], [ %23, %21 ]
ret i32 %29
}