#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 sumSq(double *, double *, int, int, int, int);
void selectCluster(double *x, double *c, int *assign, int N, int K, int D,
                   int *conv) {
  double min;
  int min_idx;
  int convCheck = 1;
  double dist;
  for (int n = 0; n < N; ++n) {
    min = 1.0 / 0.0;
    min_idx = -1;
    for (int k = 0; k < K; ++k) {
      dist = sumSq(x, c, N, D, n, k);
      if (min > dist) {
        min = dist;
        min_idx = k;
      }
    }
    if (convCheck) {
      (*conv) = (min_idx == assign[n] ? 1 : 0);
      if (!conv)
        convCheck = 0;
    }
    assign[n] = min_idx;
  }
}


x86 assembly (clang, -O0):
<selectCluster>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1224 <selectCluster+0x114>
movsd  0xeab(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11c7 <selectCluster+0xb7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    -0x48(%rbp),%r9d
call   1030 <sumSq@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x40(%rbp),%xmm0
jbe    11b4 <selectCluster+0xa4>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11b9 <selectCluster+0xa9>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1168 <selectCluster+0x58>
cmpl   $0x0,-0x38(%rbp)
je     1208 <selectCluster+0xf8>
mov    -0x34(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
xor    %ecx,%ecx
mov    $0x1,%eax
cmp    %esi,%edx
cmove  %eax,%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,0x10(%rbp)
jne    1203 <selectCluster+0xf3>
movl   $0x0,-0x38(%rbp)
jmp    1208 <selectCluster+0xf8>
mov    -0x34(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1141 <selectCluster+0x31>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<selectCluster>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x18(%rsp)
test   %ecx,%ecx
jle    11db <selectCluster+0xcb>
mov    %r9d,%r13d
mov    %ecx,%eax
mov    %rax,0x10(%rsp)
xor    %r15d,%r15d
mov    %ecx,0xc(%rsp)
mov    %r8d,0x8(%rsp)
jmp    1175 <selectCluster+0x65>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ebx,%ebx
mov    0x18(%rsp),%rdx
cmp    (%rdx,%r15,4),%r12d
sete   %bl
mov    0x60(%rsp),%rbp
mov    %ebx,0x0(%rbp)
mov    %r12d,(%rdx,%r15,4)
add    $0x1,%r15
cmp    0x10(%rsp),%r15
je     11db <selectCluster+0xcb>
mov    $0xffffffff,%r12d
test   %r8d,%r8d
jle    1150 <selectCluster+0x40>
xor    %r14d,%r14d
movsd  0xe75(%rip),%xmm0
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x20(%rsp)
mov    %rdi,%rbx
mov    %rsi,%rbp
mov    %ecx,%edx
mov    %r13d,%ecx
mov    %r15d,%r8d
mov    %r14d,%r9d
call   1030 <sumSq@plt>
mov    %rbx,%rdi
mov    0x8(%rsp),%r8d
mov    %rbp,%rsi
mov    0xc(%rsp),%ecx
movsd  0x20(%rsp),%xmm1
ucomisd %xmm0,%xmm1
cmova  %r14d,%r12d
minsd  %xmm1,%xmm0
add    $0x1,%r14d
cmp    %r14d,%r8d
jne    1190 <selectCluster+0x80>
jmp    1150 <selectCluster+0x40>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @selectCluster(double* %0, double* %1, i32* %2, i32 %3, i32 %4, i32 %5, i32* %6) {
%8 = alloca double*
%9 = alloca double*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca double
%16 = alloca i32
%17 = alloca i32
%18 = alloca double
%19 = alloca i32
%20 = alloca i32
store double* %0, double** %8
store double* %1, double** %9
store i32* %2, i32** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32* %6, i32** %14
store i32 1, i32* %17
store i32 0, i32* %19
br label %21
21: ; preds = %72, %7
%22 = load i32, i32* %19
%23 = load i32, i32* %11
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %75
25: ; preds = %21
store double 0x7FF0000000000000, double* %15
store i32 -1, i32* %16
store i32 0, i32* %20
br label %26
26: ; preds = %45, %25
%27 = load i32, i32* %20
%28 = load i32, i32* %12
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %48
30: ; preds = %26
%31 = load double*, double** %8
%32 = load double*, double** %9
%33 = load i32, i32* %11
%34 = load i32, i32* %13
%35 = load i32, i32* %19
%36 = load i32, i32* %20
%37 = call double @sumSq(double* %31, double* %32, i32 %33, i32 %34, i32 %35, i32 %36)
store double %37, double* %18
%38 = load double, double* %15
%39 = load double, double* %18
%40 = fcmp ogt double %38, %39
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load double, double* %18
store double %42, double* %15
%43 = load i32, i32* %20
store i32 %43, i32* %16
br label %44
44: ; preds = %41, %30
br label %45
45: ; preds = %44
%46 = load i32, i32* %20
%47 = add nsw i32 %46, 1
store i32 %47, i32* %20
br label %26
48: ; preds = %26
%49 = load i32, i32* %17
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %66
51: ; preds = %48
%52 = load i32, i32* %16
%53 = load i32*, i32** %10
%54 = load i32, i32* %19
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
%57 = load i32, i32* %56
%58 = icmp eq i32 %52, %57
%59 = zext i1 %58 to i64
%60 = select i1 %58, i32 1, i32 0
%61 = load i32*, i32** %14
store i32 %60, i32* %61
%62 = load i32*, i32** %14
%63 = icmp ne i32* %62, null
br i1 %63, label %65, label %64
64: ; preds = %51
store i32 0, i32* %17
br label %65
65: ; preds = %64, %51
br label %66
66: ; preds = %65, %48
%67 = load i32, i32* %16
%68 = load i32*, i32** %10
%69 = load i32, i32* %19
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %68, i64 %70
store i32 %67, i32* %71
br label %72
72: ; preds = %66
%73 = load i32, i32* %19
%74 = add nsw i32 %73, 1
store i32 %74, i32* %19
br label %21
75: ; preds = %21
ret void
}

LLVM IR (-O2):
define void @selectCluster(double* %0, double* %1, i32* nocapture %2, i32 %3, i32 %4, i32 %5, i32* nocapture writeonly %6) {
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %16
9: ; preds = %7
%10 = icmp sgt i32 %4, 0
%11 = zext i32 %3 to i64
br label %12
12: ; preds = %9, %27
%13 = phi i64 [ 0, %9 ], [ %33, %27 ]
br i1 %10, label %14, label %27
14: ; preds = %12
%15 = trunc i64 %13 to i32
br label %17
16: ; preds = %27, %7
ret void
17: ; preds = %14, %17
%18 = phi i32 [ %25, %17 ], [ 0, %14 ]
%19 = phi double [ %24, %17 ], [ 0x7FF0000000000000, %14 ]
%20 = phi i32 [ %23, %17 ], [ -1, %14 ]
%21 = tail call double @sumSq(double* %0, double* %1, i32 %3, i32 %5, i32 %15, i32 %18)
%22 = fcmp ogt double %19, %21
%23 = select i1 %22, i32 %18, i32 %20
%24 = select i1 %22, double %21, double %19
%25 = add nuw nsw i32 %18, 1
%26 = icmp eq i32 %25, %4
br i1 %26, label %27, label %17
27: ; preds = %17, %12
%28 = phi i32 [ -1, %12 ], [ %23, %17 ]
%29 = getelementptr inbounds i32, i32* %2, i64 %13
%30 = load i32, i32* %29
%31 = icmp eq i32 %28, %30
%32 = zext i1 %31 to i32
store i32 %32, i32* %6
store i32 %28, i32* %29
%33 = add nuw nsw i64 %13, 1
%34 = icmp eq i64 %33, %11
br i1 %34, label %16, label %12
}