#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 */
int /*<<< orphan*/ find_closest(double *, double *, int, int, double *);
int /*<<< orphan*/ memcpy(double *, double *, int);
int rand();
void kmpp_init_centroids(double *centroids, double *samples, int dims,
                         int n_centroids, int n_samples) {
  int s = rand() % n_samples;
  memcpy(centroids, &samples[s * dims], sizeof(double) * dims);
  int i;
  for (i = 1; i < n_centroids; i++) {
    double dist = 0;
    int j;
    for (j = 0; j < n_samples; j++) {
      double d = 0;
      find_closest(centroids, &samples[j * dims], dims, i, &d);
      dist += d * d;
    }
    double p = rand() % (int)dist;
    for (j = 0; j < n_samples; j++) {
      double d = 0;
      find_closest(centroids, &samples[j * dims], dims, i, &d);
      p -= (d * d);
      if (p <= 0) {
        break;
      }
    }
    memcpy(&centroids[i * dims], &samples[j * dims], sizeof(double) * dims);
  }
}


x86 assembly (clang, -O0):
<kmpp_init_centroids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12fe <kmpp_init_centroids+0x1ce>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1206 <kmpp_init_centroids+0xd6>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    -0x40(%rbp),%r8
call   1030 <find_closest@plt>
movsd  -0x40(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a1 <kmpp_init_centroids+0x71>
mov    $0x0,%al
call   1050 <rand@plt>
cvttsd2si -0x30(%rbp),%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12b9 <kmpp_init_centroids+0x189>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    -0x50(%rbp),%r8
call   1030 <find_closest@plt>
movsd  -0x50(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x48(%rbp),%xmm0
jb     12a6 <kmpp_init_centroids+0x176>
jmp    12b9 <kmpp_init_centroids+0x189>
jmp    12ab <kmpp_init_centroids+0x17b>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1225 <kmpp_init_centroids+0xf5>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1186 <kmpp_init_centroids+0x56>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmpp_init_centroids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r8d,%ebx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r12
xor    %eax,%eax
call   1050 <rand@plt>
cltd
mov    %ebx,0x4(%rsp)
idiv   %ebx
imul   %ebp,%edx
movslq %edx,%rax
lea    (%r15,%rax,8),%rsi
mov    %rbp,0x18(%rsp)
lea    0x0(,%rbp,8),%edx
mov    %r12,0x38(%rsp)
mov    %r12,%rdi
mov    %edx,0x34(%rsp)
call   1040 <memcpy@plt>
cmp    $0x2,%r14d
jl     132b <kmpp_init_centroids+0x1fb>
mov    0x4(%rsp),%eax
mov    %rax,0x28(%rsp)
movslq 0x18(%rsp),%rax
mov    %r14d,%ecx
mov    %rcx,0x48(%rsp)
mov    %rax,0x50(%rsp)
lea    0x0(,%rax,8),%r12
mov    $0x1,%r13d
lea    0x10(%rsp),%r14
mov    %r15,0x20(%rsp)
jmp    1207 <kmpp_init_centroids+0xd7>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <rand@plt>
mov    0x18(%rsp),%rbx
mov    0x38(%rsp),%r14
mov    %r13,%rax
imul   0x50(%rsp),%rax
lea    (%r14,%rax,8),%rdi
imul   %ebx,%ebp
movslq %ebp,%rax
lea    (%r15,%rax,8),%rsi
mov    0x34(%rsp),%edx
call   1040 <memcpy@plt>
add    $0x1,%r13
cmp    0x48(%rsp),%r13
lea    0x10(%rsp),%r14
je     132b <kmpp_init_centroids+0x1fb>
cmpl   $0x0,0x4(%rsp)
jle    11c0 <kmpp_init_centroids+0x90>
xorps  %xmm0,%xmm0
mov    0x28(%rsp),%rbx
mov    %r15,%rbp
mov    0x18(%rsp),%r15
mov    %r13,0x40(%rsp)
mov    %r12,%r13
mov    0x38(%rsp),%r12
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    0x40(%rsp),%rcx
mov    %r14,%r8
call   1030 <find_closest@plt>
movsd  0x10(%rsp),%xmm0
mulsd  %xmm0,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    %r13,%rbp
add    $0xffffffffffffffff,%rbx
jne    1230 <kmpp_init_centroids+0x100>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <rand@plt>
cmpl   $0x0,0x4(%rsp)
mov    %r15,%rbx
mov    0x20(%rsp),%r15
mov    %r12,%r14
mov    %r13,%r12
mov    0x40(%rsp),%r13
jle    11d3 <kmpp_init_centroids+0xa3>
cvttsd2si 0x8(%rsp),%ecx
cltd
idiv   %ecx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
movsd  %xmm1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %r13d,%ecx
lea    0x10(%rsp),%r8
call   1030 <find_closest@plt>
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm0
mulsd  %xmm0,%xmm0
subsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jae    131c <kmpp_init_centroids+0x1ec>
add    $0x1,%rbp
add    %r12,%r15
cmp    %rbp,0x28(%rsp)
mov    0x18(%rsp),%rbx
jne    12c0 <kmpp_init_centroids+0x190>
mov    0x28(%rsp),%rbp
jmp    1321 <kmpp_init_centroids+0x1f1>
mov    0x18(%rsp),%rbx
mov    0x20(%rsp),%r15
jmp    11d3 <kmpp_init_centroids+0xa3>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @kmpp_init_centroids(double* %0, double* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca double
%7 = alloca double
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, %4
%10 = mul nsw i32 %9, %2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds double, double* %1, i64 %11
%13 = shl i32 %2, 3
%14 = tail call i32 @memcpy(double* %0, double* %12, i32 %13)
%15 = icmp sgt i32 %3, 1
br i1 %15, label %16, label %72
16: ; preds = %5
%17 = icmp sgt i32 %4, 0
%18 = bitcast double* %6 to i8*
%19 = bitcast double* %7 to i8*
%20 = sext i32 %2 to i64
%21 = zext i32 %3 to i64
%22 = zext i32 %4 to i64
%23 = zext i32 %4 to i64
br label %24
24: ; preds = %16, %62
%25 = phi i64 [ 1, %16 ], [ %70, %62 ]
br i1 %17, label %28, label %26
26: ; preds = %24
%27 = call i32 (...) @rand()
br label %62
28: ; preds = %24
%29 = trunc i64 %25 to i32
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 0, %28 ], [ %38, %30 ]
%32 = phi double [ 0.000000e+00, %28 ], [ %37, %30 ]
store double 0.000000e+00, double* %6
%33 = mul nsw i64 %31, %20
%34 = getelementptr inbounds double, double* %1, i64 %33
%35 = call i32 @find_closest(double* %0, double* %34, i32 %2, i32 %29, double* %6)
%36 = load double, double* %6
%37 = call double @llvm.fmuladd.f64(double %36, double %36, double %32)
%38 = add nuw nsw i64 %31, 1
%39 = icmp eq i64 %38, %22
br i1 %39, label %40, label %30
40: ; preds = %30
%41 = call i32 (...) @rand()
br i1 %17, label %42, label %62
42: ; preds = %40
%43 = fptosi double %37 to i32
%44 = srem i32 %41, %43
%45 = sitofp i32 %44 to double
%46 = trunc i64 %25 to i32
br label %47
47: ; preds = %42, %57
%48 = phi i64 [ 0, %42 ], [ %58, %57 ]
%49 = phi double [ %45, %42 ], [ %55, %57 ]
store double 0.000000e+00, double* %7
%50 = mul nsw i64 %48, %20
%51 = getelementptr inbounds double, double* %1, i64 %50
%52 = call i32 @find_closest(double* %0, double* %51, i32 %2, i32 %46, double* %7)
%53 = load double, double* %7
%54 = fneg double %53
%55 = call double @llvm.fmuladd.f64(double %54, double %53, double %49)
%56 = fcmp ugt double %55, 0.000000e+00
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = add nuw nsw i64 %48, 1
%59 = icmp eq i64 %58, %23
br i1 %59, label %62, label %47
60: ; preds = %47
%61 = trunc i64 %48 to i32
br label %62
62: ; preds = %57, %60, %26, %40
%63 = phi i32 [ 0, %40 ], [ 0, %26 ], [ %61, %60 ], [ %4, %57 ]
%64 = mul nsw i64 %25, %20
%65 = getelementptr inbounds double, double* %0, i64 %64
%66 = mul nsw i32 %63, %2
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds double, double* %1, i64 %67
%69 = call i32 @memcpy(double* %65, double* %68, i32 %13)
%70 = add nuw nsw i64 %25, 1
%71 = icmp eq i64 %70, %21
br i1 %71, label %72, label %24
72: ; preds = %62, %5
ret void
}