#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 */
typedef int uint_fast16_t;

/* Variables and functions */
double circularMvDistance(int, double *, double *);
double k2nn_dist_point(int p, double *a, int n, double *set) {
  uint_fast16_t i, j;
  double lower = -1;
  double dist = 0;
  double v;
  lower = circularMvDistance(p, a, set);
  v = circularMvDistance(p, a, set + p);
  if (v < lower) {
    dist = lower;
    lower = v;
  } else {
    dist = v;
  }
  for (i = 0, j = 0; j < 2; i++) {
    v = circularMvDistance(p, a, set + i * p);
    if (v > 0) {
      j++;
      if (v > dist) {
        lower = dist;
        dist = v;
      } else if (v > lower)
        lower = v;
    }
  }
  for (i = 2; i < n; i++) {
    v = circularMvDistance(p, a, set + i * p);
    if (v > 0) {
      if (v > lower) {
        if ((v < dist))
          dist = v;
      } else {
        dist = lower;
        lower = v;
      }
    }
  }
  return dist;
}


x86 assembly (clang, -O0):
<k2nn_dist_point>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movsd  0xed2(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <circularMvDistance@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
call   1030 <circularMvDistance@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    119d <k2nn_dist_point+0x8d>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11a7 <k2nn_dist_point+0x97>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x28(%rbp)
jge    125f <k2nn_dist_point+0x14f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdx
call   1030 <circularMvDistance@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    124c <k2nn_dist_point+0x13c>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movsd  -0x40(%rbp),%xmm0
ucomisd -0x38(%rbp),%xmm0
jbe    1228 <k2nn_dist_point+0x118>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    1247 <k2nn_dist_point+0x137>
movsd  -0x40(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    1242 <k2nn_dist_point+0x132>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1247 <k2nn_dist_point+0x137>
jmp    124c <k2nn_dist_point+0x13c>
jmp    1251 <k2nn_dist_point+0x141>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b5 <k2nn_dist_point+0xa5>
movl   $0x2,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1308 <k2nn_dist_point+0x1f8>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdx
call   1030 <circularMvDistance@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12f5 <k2nn_dist_point+0x1e5>
movsd  -0x40(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    12dc <k2nn_dist_point+0x1cc>
movsd  -0x40(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    12d7 <k2nn_dist_point+0x1c7>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    12f0 <k2nn_dist_point+0x1e0>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    12f5 <k2nn_dist_point+0x1e5>
jmp    12fa <k2nn_dist_point+0x1ea>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1266 <k2nn_dist_point+0x156>
movsd  -0x38(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<k2nn_dist_point>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbp
mov    %edx,0x1c(%rsp)
mov    %rsi,%r14
mov    %edi,%r13d
mov    %rcx,%rdx
call   1030 <circularMvDistance@plt>
movsd  %xmm0,0x10(%rsp)
movslq %r13d,%rbx
lea    0x0(,%rbx,8),%rdx
add    %rbp,%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <circularMvDistance@plt>
movsd  0x10(%rsp),%xmm2
movapd %xmm0,%xmm1
minsd  %xmm2,%xmm1
movsd  %xmm1,0x8(%rsp)
maxsd  %xmm0,%xmm2
mov    %rbx,0x20(%rsp)
lea    0x0(,%rbx,8),%r15
xor    %r12d,%r12d
mov    %rbp,%rbx
jmp    118f <k2nn_dist_point+0x7f>
xchg   %ax,%ax
movsd  0x10(%rsp),%xmm2
add    %r15,%rbx
cmp    $0x2,%r12d
jge    11e0 <k2nn_dist_point+0xd0>
movsd  %xmm2,0x10(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <circularMvDistance@plt>
ucomisd 0xe55(%rip),%xmm0
jbe    1180 <k2nn_dist_point+0x70>
add    $0x1,%r12d
movsd  0x10(%rsp),%xmm2
ucomisd %xmm2,%xmm0
jbe    11d0 <k2nn_dist_point+0xc0>
movsd  %xmm2,0x8(%rsp)
movapd %xmm0,%xmm2
jmp    1186 <k2nn_dist_point+0x76>
nopl   0x0(%rax)
ucomisd 0x8(%rsp),%xmm0
jbe    1186 <k2nn_dist_point+0x76>
movsd  %xmm0,0x8(%rsp)
jmp    1186 <k2nn_dist_point+0x76>
mov    0x1c(%rsp),%eax
cmp    $0x3,%eax
jl     1286 <k2nn_dist_point+0x176>
mov    %eax,%ebx
mov    0x20(%rsp),%rax
shl    $0x4,%rax
add    %rax,%rbp
add    $0xfffffffffffffffe,%rbx
jmp    122f <k2nn_dist_point+0x11f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm0
add    %r15,%rbp
movapd %xmm0,%xmm2
movsd  %xmm1,0x8(%rsp)
add    $0xffffffffffffffff,%rbx
je     128a <k2nn_dist_point+0x17a>
movsd  %xmm2,0x10(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1030 <circularMvDistance@plt>
ucomisd 0xdb5(%rip),%xmm0
jbe    1210 <k2nn_dist_point+0x100>
movsd  0x8(%rsp),%xmm2
ucomisd %xmm2,%xmm0
jbe    1270 <k2nn_dist_point+0x160>
movsd  0x10(%rsp),%xmm2
ucomisd %xmm0,%xmm2
jbe    127a <k2nn_dist_point+0x16a>
movsd  0x8(%rsp),%xmm1
jmp    121c <k2nn_dist_point+0x10c>
nopl   (%rax)
movapd %xmm0,%xmm1
movapd %xmm2,%xmm0
jmp    121c <k2nn_dist_point+0x10c>
movsd  0x8(%rsp),%xmm1
movapd %xmm2,%xmm0
jmp    121c <k2nn_dist_point+0x10c>
movapd %xmm2,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @k2nn_dist_point(i32 %0, double* %1, i32 %2, double* %3) {
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca double
%13 = alloca double
store i32 %0, i32* %5
store double* %1, double** %6
store i32 %2, i32* %7
store double* %3, double** %8
store double -1.000000e+00, double* %11
store double 0.000000e+00, double* %12
%14 = load i32, i32* %5
%15 = load double*, double** %6
%16 = load double*, double** %8
%17 = call double @circularMvDistance(i32 %14, double* %15, double* %16)
store double %17, double* %11
%18 = load i32, i32* %5
%19 = load double*, double** %6
%20 = load double*, double** %8
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = call double @circularMvDistance(i32 %18, double* %19, double* %23)
store double %24, double* %13
%25 = load double, double* %13
%26 = load double, double* %11
%27 = fcmp olt double %25, %26
br i1 %27, label %28, label %31
28: ; preds = %4
%29 = load double, double* %11
store double %29, double* %12
%30 = load double, double* %13
store double %30, double* %11
br label %33
31: ; preds = %4
%32 = load double, double* %13
store double %32, double* %12
br label %33
33: ; preds = %31, %28
store i32 0, i32* %9
store i32 0, i32* %10
br label %34
34: ; preds = %67, %33
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 2
br i1 %36, label %37, label %70
37: ; preds = %34
%38 = load i32, i32* %5
%39 = load double*, double** %6
%40 = load double*, double** %8
%41 = load i32, i32* %9
%42 = load i32, i32* %5
%43 = mul nsw i32 %41, %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %40, i64 %44
%46 = call double @circularMvDistance(i32 %38, double* %39, double* %45)
store double %46, double* %13
%47 = load double, double* %13
%48 = fcmp ogt double %47, 0.000000e+00
br i1 %48, label %49, label %66
49: ; preds = %37
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
%52 = load double, double* %13
%53 = load double, double* %12
%54 = fcmp ogt double %52, %53
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load double, double* %12
store double %56, double* %11
%57 = load double, double* %13
store double %57, double* %12
br label %65
58: ; preds = %49
%59 = load double, double* %13
%60 = load double, double* %11
%61 = fcmp ogt double %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = load double, double* %13
store double %63, double* %11
br label %64
64: ; preds = %62, %58
br label %65
65: ; preds = %64, %55
br label %66
66: ; preds = %65, %37
br label %67
67: ; preds = %66
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %34
70: ; preds = %34
store i32 2, i32* %9
br label %71
71: ; preds = %103, %70
%72 = load i32, i32* %9
%73 = load i32, i32* %7
%74 = icmp slt i32 %72, %73
br i1 %74, label %75, label %106
75: ; preds = %71
%76 = load i32, i32* %5
%77 = load double*, double** %6
%78 = load double*, double** %8
%79 = load i32, i32* %9
%80 = load i32, i32* %5
%81 = mul nsw i32 %79, %80
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds double, double* %78, i64 %82
%84 = call double @circularMvDistance(i32 %76, double* %77, double* %83)
store double %84, double* %13
%85 = load double, double* %13
%86 = fcmp ogt double %85, 0.000000e+00
br i1 %86, label %87, label %102
87: ; preds = %75
%88 = load double, double* %13
%89 = load double, double* %11
%90 = fcmp ogt double %88, %89
br i1 %90, label %91, label %98
91: ; preds = %87
%92 = load double, double* %13
%93 = load double, double* %12
%94 = fcmp olt double %92, %93
br i1 %94, label %95, label %97
95: ; preds = %91
%96 = load double, double* %13
store double %96, double* %12
br label %97
97: ; preds = %95, %91
br label %101
98: ; preds = %87
%99 = load double, double* %11
store double %99, double* %12
%100 = load double, double* %13
store double %100, double* %11
br label %101
101: ; preds = %98, %97
br label %102
102: ; preds = %101, %75
br label %103
103: ; preds = %102
%104 = load i32, i32* %9
%105 = add nsw i32 %104, 1
store i32 %105, i32* %9
br label %71
106: ; preds = %71
%107 = load double, double* %12
ret double %107
}

LLVM IR (-O2):
define double @k2nn_dist_point(i32 %0, double* %1, i32 %2, double* %3) {
%5 = tail call double @circularMvDistance(i32 %0, double* %1, double* %3)
%6 = sext i32 %0 to i64
%7 = getelementptr inbounds double, double* %3, i64 %6
%8 = tail call double @circularMvDistance(i32 %0, double* %1, double* %7)
%9 = fcmp olt double %8, %5
%10 = select i1 %9, double %8, double %5
%11 = select i1 %9, double %5, double %8
br label %16
12: ; preds = %31
%13 = icmp sgt i32 %2, 2
br i1 %13, label %14, label %55
14: ; preds = %12
%15 = zext i32 %2 to i64
br label %37
16: ; preds = %4, %31
%17 = phi i64 [ 0, %4 ], [ %35, %31 ]
%18 = phi double [ %11, %4 ], [ %34, %31 ]
%19 = phi double [ %10, %4 ], [ %33, %31 ]
%20 = phi i32 [ 0, %4 ], [ %32, %31 ]
%21 = mul nsw i64 %17, %6
%22 = getelementptr inbounds double, double* %3, i64 %21
%23 = tail call double @circularMvDistance(i32 %0, double* %1, double* %22)
%24 = fcmp ogt double %23, 0.000000e+00
br i1 %24, label %25, label %31
25: ; preds = %16
%26 = add nsw i32 %20, 1
%27 = fcmp ogt double %23, %18
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = fcmp ogt double %23, %19
br i1 %29, label %30, label %31
30: ; preds = %28
br label %31
31: ; preds = %25, %16, %28, %30
%32 = phi i32 [ %26, %30 ], [ %26, %28 ], [ %20, %16 ], [ %26, %25 ]
%33 = phi double [ %23, %30 ], [ %19, %28 ], [ %19, %16 ], [ %18, %25 ]
%34 = phi double [ %18, %30 ], [ %18, %28 ], [ %18, %16 ], [ %23, %25 ]
%35 = add nuw i64 %17, 1
%36 = icmp slt i32 %32, 2
br i1 %36, label %16, label %12
37: ; preds = %14, %50
%38 = phi i64 [ 2, %14 ], [ %53, %50 ]
%39 = phi double [ %34, %14 ], [ %52, %50 ]
%40 = phi double [ %33, %14 ], [ %51, %50 ]
%41 = mul nsw i64 %38, %6
%42 = getelementptr inbounds double, double* %3, i64 %41
%43 = tail call double @circularMvDistance(i32 %0, double* %1, double* %42)
%44 = fcmp ogt double %43, 0.000000e+00
br i1 %44, label %45, label %50
45: ; preds = %37
%46 = fcmp ogt double %43, %40
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = fcmp olt double %43, %39
br i1 %48, label %49, label %50
49: ; preds = %47
br label %50
50: ; preds = %45, %37, %47, %49
%51 = phi double [ %40, %49 ], [ %40, %47 ], [ %40, %37 ], [ %43, %45 ]
%52 = phi double [ %43, %49 ], [ %39, %47 ], [ %39, %37 ], [ %40, %45 ]
%53 = add nuw nsw i64 %38, 1
%54 = icmp eq i64 %53, %15
br i1 %54, label %55, label %37
55: ; preds = %50, %12
%56 = phi double [ %34, %12 ], [ %52, %50 ]
ret double %56
}