#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*/ memcpy(double *, double *, int);
int rand();
void lloyd_init_centroids(double *centroids, double *samples, int dims,
                          int n_centroids, int n_samples) {
  if (!centroids || !samples || dims < 1 || n_centroids < 1 || n_samples < 1) {
    return;
  }
  int i;
  for (i = 0; i < n_centroids; i++) {
    int s = rand() % n_samples;
    memcpy(&centroids[i * dims], &samples[s * dims], sizeof(double) * dims);
  }
}


x86 assembly (clang, -O0):
<lloyd_init_centroids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     116e <lloyd_init_centroids+0x4e>
cmpq   $0x0,-0x10(%rbp)
je     116e <lloyd_init_centroids+0x4e>
cmpl   $0x1,-0x14(%rbp)
jl     116e <lloyd_init_centroids+0x4e>
cmpl   $0x1,-0x18(%rbp)
jl     116e <lloyd_init_centroids+0x4e>
cmpl   $0x1,-0x1c(%rbp)
jge    1173 <lloyd_init_centroids+0x53>
jmp    11d9 <lloyd_init_centroids+0xb9>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11d9 <lloyd_init_centroids+0xb9>
mov    $0x0,%al
call   1040 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    117a <lloyd_init_centroids+0x5a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lloyd_init_centroids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,0x4(%rsp)
test   %rdi,%rdi
je     119b <lloyd_init_centroids+0x7b>
mov    %rsi,%r15
test   %rsi,%rsi
je     119b <lloyd_init_centroids+0x7b>
mov    %edx,%r12d
test   %edx,%edx
jle    119b <lloyd_init_centroids+0x7b>
test   %ecx,%ecx
jle    119b <lloyd_init_centroids+0x7b>
cmpl   $0x0,0x4(%rsp)
jle    119b <lloyd_init_centroids+0x7b>
mov    %rdi,%rbp
lea    0x0(,%r12,8),%r13d
mov    %r12d,%ebx
mov    %ecx,%r14d
shl    $0x3,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rand@plt>
cltd
idivl  0x4(%rsp)
imul   %r12d,%edx
movslq %edx,%rax
lea    (%r15,%rax,8),%rsi
mov    %rbp,%rdi
mov    %r13d,%edx
call   1030 <memcpy@plt>
add    %rbx,%rbp
add    $0xffffffffffffffff,%r14
jne    1170 <lloyd_init_centroids+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @lloyd_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
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
%13 = load double*, double** %6
%14 = icmp ne double* %13, null
br i1 %14, label %15, label %27
15: ; preds = %5
%16 = load double*, double** %7
%17 = icmp ne double* %16, null
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 1
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %24, %21, %18, %15, %5
br label %57
28: ; preds = %24
store i32 0, i32* %11
br label %29
29: ; preds = %54, %28
%30 = load i32, i32* %11
%31 = load i32, i32* %9
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %57
33: ; preds = %29
%34 = call i32 (...) @rand()
%35 = load i32, i32* %10
%36 = srem i32 %34, %35
store i32 %36, i32* %12
%37 = load double*, double** %6
%38 = load i32, i32* %11
%39 = load i32, i32* %8
%40 = mul nsw i32 %38, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %37, i64 %41
%43 = load double*, double** %7
%44 = load i32, i32* %12
%45 = load i32, i32* %8
%46 = mul nsw i32 %44, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %43, i64 %47
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = mul i64 8, %50
%52 = trunc i64 %51 to i32
%53 = call i32 @memcpy(double* %42, double* %48, i32 %52)
br label %54
54: ; preds = %33
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %29
57: ; preds = %27, %29
ret void
}

LLVM IR (-O2):
define void @lloyd_init_centroids(double* %0, double* %1, i32 %2, i32 %3, i32 %4) {
%6 = icmp eq double* %0, null
%7 = icmp eq double* %1, null
%8 = or i1 %6, %7
%9 = icmp slt i32 %2, 1
%10 = or i1 %8, %9
%11 = icmp slt i32 %3, 1
%12 = or i1 %10, %11
%13 = icmp slt i32 %4, 1
%14 = or i1 %12, %13
br i1 %14, label %31, label %15
15: ; preds = %5
%16 = shl i32 %2, 3
%17 = zext i32 %2 to i64
%18 = zext i32 %3 to i64
br label %19
19: ; preds = %15, %19
%20 = phi i64 [ 0, %15 ], [ %29, %19 ]
%21 = tail call i32 (...) @rand()
%22 = srem i32 %21, %4
%23 = mul nsw i64 %20, %17
%24 = getelementptr inbounds double, double* %0, i64 %23
%25 = mul nsw i32 %22, %2
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds double, double* %1, i64 %26
%28 = tail call i32 @memcpy(double* %24, double* %27, i32 %16)
%29 = add nuw nsw i64 %20, 1
%30 = icmp eq i64 %29, %18
br i1 %30, label %31, label %19
31: ; preds = %19, %5
ret void
}