#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 */
scalar_t__ dist2(double, double, double, double, double, double);
double min2(double, double);
void sort_by_distance(int n, double x[], double y[], double z[], double w[]) {
  int i, j, k;
  for (i = 1; i < n; ++i) {
    double xi, yi, zi, wi;
    double d2max = 0;
    double d2maxsum = 0;
    int jmax = i;
    for (j = i; j < n; ++j) {
      double d2min = 1e20, d2sum = 0;
      for (k = 0; k < i; ++k) {
        double d2 = (float)(dist2(x[k], y[k], z[k], x[j], y[j], z[j]));
        d2min = min2(d2min, d2);
        d2sum += d2;
      }
      if (d2min > d2max || (d2min == d2max && d2sum > d2maxsum)) {
        d2max = d2min;
        d2maxsum = d2sum;
        jmax = j;
      }
    }
    xi = x[i], yi = y[i], zi = z[i], wi = w[i];
    x[i] = x[jmax];
    y[i] = y[jmax];
    z[i] = z[jmax];
    w[i] = w[jmax];
    x[jmax] = xi;
    y[jmax] = yi;
    z[jmax] = zi;
    w[jmax] = wi;
  }
}


x86 assembly (clang, -O0):
<sort_by_distance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    13a5 <sort_by_distance+0x285>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    129f <sort_by_distance+0x17f>
movsd  0xe7f(%rip),%xmm0
movsd  %xmm0,-0x78(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x80(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    123c <sort_by_distance+0x11c>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm3
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm4
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm5
call   1030 <dist2@plt>
cvtsi2ss %rax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x78(%rbp),%xmm0
movsd  -0x88(%rbp),%xmm1
ff
call   1040 <min2@plt>
movsd  %xmm0,-0x78(%rbp)
movsd  -0x88(%rbp),%xmm0
ff
addsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1195 <sort_by_distance+0x75>
movsd  -0x78(%rbp),%xmm0
ucomisd -0x60(%rbp),%xmm0
ja     1272 <sort_by_distance+0x152>
movsd  -0x78(%rbp),%xmm0
ucomisd -0x60(%rbp),%xmm0
jne    128c <sort_by_distance+0x16c>
jp     128c <sort_by_distance+0x16c>
movsd  -0x80(%rbp),%xmm0
ucomisd -0x68(%rbp),%xmm0
jbe    128c <sort_by_distance+0x16c>
movsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x6c(%rbp)
jmp    1291 <sort_by_distance+0x171>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    116d <sort_by_distance+0x4d>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x40(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x48(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x50(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x58(%rbp),%xmm0
mov    -0x28(%rbp),%rax
movslq -0x6c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1145 <sort_by_distance+0x25>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_by_distance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,0x28(%rsp)
cmp    $0x2,%edi
jl     12c2 <sort_by_distance+0x1a2>
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%r14d
jmp    11d5 <sort_by_distance+0xb5>
nopw   0x0(%rax,%rax,1)
movsd  (%rbx,%r14,8),%xmm0
movsd  (%r12,%r14,8),%xmm1
movsd  (%r15,%r14,8),%xmm2
mov    0x28(%rsp),%rcx
movsd  (%rcx,%r14,8),%xmm3
movslq 0xc(%rsp),%rax
movsd  (%rbx,%rax,8),%xmm4
movsd  %xmm4,(%rbx,%r14,8)
movsd  (%r12,%rax,8),%xmm4
movsd  %xmm4,(%r12,%r14,8)
movsd  (%r15,%rax,8),%xmm4
movsd  %xmm4,(%r15,%r14,8)
movsd  (%rcx,%rax,8),%xmm4
movsd  %xmm4,(%rcx,%r14,8)
movsd  %xmm0,(%rbx,%rax,8)
movsd  %xmm1,(%r12,%rax,8)
movsd  %xmm2,(%r15,%rax,8)
movsd  %xmm3,(%rcx,%rax,8)
add    $0x1,%r14
cmp    0x10(%rsp),%r14
je     12c2 <sort_by_distance+0x1a2>
xorps  %xmm0,%xmm0
movsd  %xmm0,0x18(%rsp)
mov    %r14,%rbp
mov    %r14d,%eax
mov    %r14d,0xc(%rsp)
xorps  %xmm0,%xmm0
movsd  %xmm0,0x20(%rsp)
jmp    1221 <sort_by_distance+0x101>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm0,0x20(%rsp)
movsd  %xmm1,0x18(%rsp)
mov    %ebp,%eax
mov    %ebp,0xc(%rsp)
add    $0x1,%rbp
cmp    0x10(%rsp),%rbp
je     1160 <sort_by_distance+0x40>
xor    %r13d,%r13d
xorps  %xmm1,%xmm1
movsd  0xdd1(%rip),%xmm0
nop
movsd  %xmm0,0x38(%rsp)
movsd  %xmm1,0x40(%rsp)
movsd  (%rbx,%r13,8),%xmm0
movsd  (%r12,%r13,8),%xmm1
movsd  (%r15,%r13,8),%xmm2
movsd  (%rbx,%rbp,8),%xmm3
movsd  (%r12,%rbp,8),%xmm4
movsd  (%r15,%rbp,8),%xmm5
call   1030 <dist2@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
xorps  %xmm1,%xmm1
cvtss2sd %xmm0,%xmm1
movsd  %xmm1,0x30(%rsp)
movsd  0x38(%rsp),%xmm0
call   1040 <min2@plt>
movsd  0x40(%rsp),%xmm1
addsd  0x30(%rsp),%xmm1
add    $0x1,%r13
cmp    %r13,%r14
jne    1230 <sort_by_distance+0x110>
ucomisd 0x20(%rsp),%xmm0
ja     1200 <sort_by_distance+0xe0>
jne    1212 <sort_by_distance+0xf2>
jp     1212 <sort_by_distance+0xf2>
ucomisd 0x18(%rsp),%xmm1
ja     1200 <sort_by_distance+0xe0>
jmp    1212 <sort_by_distance+0xf2>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @sort_by_distance(i32 %0, double* %1, double* %2, double* %3, double* %4) {
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca double
%20 = alloca i32
%21 = alloca double
%22 = alloca double
%23 = alloca double
store i32 %0, i32* %6
store double* %1, double** %7
store double* %2, double** %8
store double* %3, double** %9
store double* %4, double** %10
store i32 1, i32* %11
br label %24
24: ; preds = %180, %5
%25 = load i32, i32* %11
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %183
28: ; preds = %24
store double 0.000000e+00, double* %18
store double 0.000000e+00, double* %19
%29 = load i32, i32* %11
store i32 %29, i32* %20
%30 = load i32, i32* %11
store i32 %30, i32* %12
br label %31
31: ; preds = %100, %28
%32 = load i32, i32* %12
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %103
35: ; preds = %31
store double 1.000000e+20, double* %21
store double 0.000000e+00, double* %22
store i32 0, i32* %13
br label %36
36: ; preds = %80, %35
%37 = load i32, i32* %13
%38 = load i32, i32* %11
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %83
40: ; preds = %36
%41 = load double*, double** %7
%42 = load i32, i32* %13
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = load double, double* %44
%46 = load double*, double** %8
%47 = load i32, i32* %13
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds double, double* %46, i64 %48
%50 = load double, double* %49
%51 = load double*, double** %9
%52 = load i32, i32* %13
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds double, double* %51, i64 %53
%55 = load double, double* %54
%56 = load double*, double** %7
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %56, i64 %58
%60 = load double, double* %59
%61 = load double*, double** %8
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double, double* %61, i64 %63
%65 = load double, double* %64
%66 = load double*, double** %9
%67 = load i32, i32* %12
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds double, double* %66, i64 %68
%70 = load double, double* %69
%71 = call i64 @dist2(double %45, double %50, double %55, double %60, double %65, double %70)
%72 = sitofp i64 %71 to float
%73 = fpext float %72 to double
store double %73, double* %23
%74 = load double, double* %21
%75 = load double, double* %23
%76 = call double @min2(double %74, double %75)
store double %76, double* %21
%77 = load double, double* %23
%78 = load double, double* %22
%79 = fadd double %78, %77
store double %79, double* %22
br label %80
80: ; preds = %40
%81 = load i32, i32* %13
%82 = add nsw i32 %81, 1
store i32 %82, i32* %13
br label %36
83: ; preds = %36
%84 = load double, double* %21
%85 = load double, double* %18
%86 = fcmp ogt double %84, %85
br i1 %86, label %95, label %87
87: ; preds = %83
%88 = load double, double* %21
%89 = load double, double* %18
%90 = fcmp oeq double %88, %89
br i1 %90, label %91, label %99
91: ; preds = %87
%92 = load double, double* %22
%93 = load double, double* %19
%94 = fcmp ogt double %92, %93
br i1 %94, label %95, label %99
95: ; preds = %91, %83
%96 = load double, double* %21
store double %96, double* %18
%97 = load double, double* %22
store double %97, double* %19
%98 = load i32, i32* %12
store i32 %98, i32* %20
br label %99
99: ; preds = %95, %91, %87
br label %100
100: ; preds = %99
%101 = load i32, i32* %12
%102 = add nsw i32 %101, 1
store i32 %102, i32* %12
br label %31
103: ; preds = %31
%104 = load double*, double** %7
%105 = load i32, i32* %11
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds double, double* %104, i64 %106
%108 = load double, double* %107
store double %108, double* %14
%109 = load double*, double** %8
%110 = load i32, i32* %11
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds double, double* %109, i64 %111
%113 = load double, double* %112
store double %113, double* %15
%114 = load double*, double** %9
%115 = load i32, i32* %11
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds double, double* %114, i64 %116
%118 = load double, double* %117
store double %118, double* %16
%119 = load double*, double** %10
%120 = load i32, i32* %11
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds double, double* %119, i64 %121
%123 = load double, double* %122
store double %123, double* %17
%124 = load double*, double** %7
%125 = load i32, i32* %20
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds double, double* %124, i64 %126
%128 = load double, double* %127
%129 = load double*, double** %7
%130 = load i32, i32* %11
%131 = sext i32 %130 to i64
%132 = getelementptr inbounds double, double* %129, i64 %131
store double %128, double* %132
%133 = load double*, double** %8
%134 = load i32, i32* %20
%135 = sext i32 %134 to i64
%136 = getelementptr inbounds double, double* %133, i64 %135
%137 = load double, double* %136
%138 = load double*, double** %8
%139 = load i32, i32* %11
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds double, double* %138, i64 %140
store double %137, double* %141
%142 = load double*, double** %9
%143 = load i32, i32* %20
%144 = sext i32 %143 to i64
%145 = getelementptr inbounds double, double* %142, i64 %144
%146 = load double, double* %145
%147 = load double*, double** %9
%148 = load i32, i32* %11
%149 = sext i32 %148 to i64
%150 = getelementptr inbounds double, double* %147, i64 %149
store double %146, double* %150
%151 = load double*, double** %10
%152 = load i32, i32* %20
%153 = sext i32 %152 to i64
%154 = getelementptr inbounds double, double* %151, i64 %153
%155 = load double, double* %154
%156 = load double*, double** %10
%157 = load i32, i32* %11
%158 = sext i32 %157 to i64
%159 = getelementptr inbounds double, double* %156, i64 %158
store double %155, double* %159
%160 = load double, double* %14
%161 = load double*, double** %7
%162 = load i32, i32* %20
%163 = sext i32 %162 to i64
%164 = getelementptr inbounds double, double* %161, i64 %163
store double %160, double* %164
%165 = load double, double* %15
%166 = load double*, double** %8
%167 = load i32, i32* %20
%168 = sext i32 %167 to i64
%169 = getelementptr inbounds double, double* %166, i64 %168
store double %165, double* %169
%170 = load double, double* %16
%171 = load double*, double** %9
%172 = load i32, i32* %20
%173 = sext i32 %172 to i64
%174 = getelementptr inbounds double, double* %171, i64 %173
store double %170, double* %174
%175 = load double, double* %17
%176 = load double*, double** %10
%177 = load i32, i32* %20
%178 = sext i32 %177 to i64
%179 = getelementptr inbounds double, double* %176, i64 %178
store double %175, double* %179
br label %180
180: ; preds = %103
%181 = load i32, i32* %11
%182 = add nsw i32 %181, 1
store i32 %182, i32* %11
br label %24
183: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @sort_by_distance(i32 %0, double* nocapture %1, double* nocapture %2, double* nocapture %3, double* nocapture %4) {
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %74
7: ; preds = %5
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %54, %7
%10 = phi i64 [ 1, %7 ], [ %72, %54 ]
%11 = trunc i64 %10 to i32
br label %12
12: ; preds = %9, %48
%13 = phi i64 [ %10, %9 ], [ %52, %48 ]
%14 = phi i32 [ %11, %9 ], [ %51, %48 ]
%15 = phi double [ 0.000000e+00, %9 ], [ %50, %48 ]
%16 = phi double [ 0.000000e+00, %9 ], [ %49, %48 ]
%17 = getelementptr inbounds double, double* %1, i64 %13
%18 = getelementptr inbounds double, double* %2, i64 %13
%19 = getelementptr inbounds double, double* %3, i64 %13
br label %20
20: ; preds = %12, %20
%21 = phi i64 [ 0, %12 ], [ %38, %20 ]
%22 = phi double [ 0.000000e+00, %12 ], [ %37, %20 ]
%23 = phi double [ 1.000000e+20, %12 ], [ %36, %20 ]
%24 = getelementptr inbounds double, double* %1, i64 %21
%25 = load double, double* %24
%26 = getelementptr inbounds double, double* %2, i64 %21
%27 = load double, double* %26
%28 = getelementptr inbounds double, double* %3, i64 %21
%29 = load double, double* %28
%30 = load double, double* %17
%31 = load double, double* %18
%32 = load double, double* %19
%33 = tail call i64 @dist2(double %25, double %27, double %29, double %30, double %31, double %32)
%34 = sitofp i64 %33 to float
%35 = fpext float %34 to double
%36 = tail call double @min2(double %23, double %35)
%37 = fadd double %22, %35
%38 = add nuw nsw i64 %21, 1
%39 = icmp eq i64 %38, %10
br i1 %39, label %40, label %20
40: ; preds = %20
%41 = fcmp ogt double %36, %16
br i1 %41, label %46, label %42
42: ; preds = %40
%43 = fcmp oeq double %36, %16
%44 = fcmp ogt double %37, %15
%45 = select i1 %43, i1 %44, i1 false
br i1 %45, label %46, label %48
46: ; preds = %42, %40
%47 = trunc i64 %13 to i32
br label %48
48: ; preds = %46, %42
%49 = phi double [ %36, %46 ], [ %16, %42 ]
%50 = phi double [ %37, %46 ], [ %15, %42 ]
%51 = phi i32 [ %47, %46 ], [ %14, %42 ]
%52 = add nuw nsw i64 %13, 1
%53 = icmp eq i64 %52, %8
br i1 %53, label %54, label %12
54: ; preds = %48
%55 = getelementptr inbounds double, double* %1, i64 %10
%56 = load double, double* %55
%57 = getelementptr inbounds double, double* %2, i64 %10
%58 = load double, double* %57
%59 = getelementptr inbounds double, double* %3, i64 %10
%60 = load double, double* %59
%61 = getelementptr inbounds double, double* %4, i64 %10
%62 = load double, double* %61
%63 = sext i32 %51 to i64
%64 = getelementptr inbounds double, double* %1, i64 %63
%65 = load double, double* %64
store double %65, double* %55
%66 = getelementptr inbounds double, double* %2, i64 %63
%67 = load double, double* %66
store double %67, double* %57
%68 = getelementptr inbounds double, double* %3, i64 %63
%69 = load double, double* %68
store double %69, double* %59
%70 = getelementptr inbounds double, double* %4, i64 %63
%71 = load double, double* %70
store double %71, double* %61
store double %56, double* %64
store double %58, double* %66
store double %60, double* %68
store double %62, double* %70
%72 = add nuw nsw i64 %10, 1
%73 = icmp eq i64 %72, %8
br i1 %73, label %74, label %9
74: ; preds = %54, %5
ret void
}