#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__ RAND_MAX;
int closest_centroid(double *, double *, int, int);
double rand();
int generate_random_indicies_in_cluster(double *data, double *centroids,
                                        int *sample_indicies, int cluster,
                                        int n, int max_iter, int k, int N,
                                        int D) {
  int index, cur_c;
  int iter = 0, num_found = 0;
  bool not_unique;
  while (num_found < n) {
    iter = 0;
    not_unique = true;
    cur_c = -1;
    while (iter < max_iter && not_unique && cur_c != cluster) {
      index = (int)(rand() / (double)RAND_MAX * N);
      not_unique = false;
      for (int i = 0; i < num_found; i++) {
        if (index == sample_indicies[i]) {
          not_unique = true;
          break;
        }
      }
      if (!not_unique) {
        cur_c = closest_centroid(data + index * D, centroids, k, D);
      }
      iter++;
    }
    if (!not_unique) {
      sample_indicies[num_found] = index;
      num_found++;
    }
    if (iter >= max_iter) {
      break;
    }
  }
  return num_found;
}


x86 assembly (clang, -O0):
<generate_random_indicies_in_cluster>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
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   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    129c <generate_random_indicies_in_cluster+0x17c>
movl   $0x0,-0x30(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x3d(%rbp)
jge    11a3 <generate_random_indicies_in_cluster+0x83>
xor    %eax,%eax
cmpl   $0x0,-0x38(%rbp)
mov    %al,-0x3d(%rbp)
je     11a3 <generate_random_indicies_in_cluster+0x83>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11b3 <generate_random_indicies_in_cluster+0x93>
jmp    1265 <generate_random_indicies_in_cluster+0x145>
mov    $0x0,%al
call   1040 <rand@plt>
mov    0x2e27(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
cvtsi2sdl 0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1227 <generate_random_indicies_in_cluster+0x107>
mov    -0x28(%rbp),%eax
mov    -0x18(%rbp),%rcx
movslq -0x3c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    1214 <generate_random_indicies_in_cluster+0xf4>
movl   $0x1,-0x38(%rbp)
jmp    1227 <generate_random_indicies_in_cluster+0x107>
jmp    1219 <generate_random_indicies_in_cluster+0xf9>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11e8 <generate_random_indicies_in_cluster+0xc8>
cmpl   $0x0,-0x38(%rbp)
jne    1257 <generate_random_indicies_in_cluster+0x137>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%eax
imul   0x20(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    0x10(%rbp),%edx
mov    0x20(%rbp),%ecx
call   1030 <closest_centroid@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1177 <generate_random_indicies_in_cluster+0x57>
cmpl   $0x0,-0x38(%rbp)
jne    1286 <generate_random_indicies_in_cluster+0x166>
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     1297 <generate_random_indicies_in_cluster+0x177>
jmp    129c <generate_random_indicies_in_cluster+0x17c>
jmp    1156 <generate_random_indicies_in_cluster+0x36>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_random_indicies_in_cluster>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,0x8(%rsp)
mov    %rdx,%rbx
mov    %rsi,0x18(%rsp)
mov    %rdi,0x10(%rsp)
mov    %r9d,0x4(%rsp)
test   %r9d,%r9d
setg   %al
mov    %ecx,0xc(%rsp)
cmp    $0xffffffff,%ecx
setne  %r13b
cvtsi2sdl 0x68(%rsp),%xmm0
movsd  %xmm0,0x20(%rsp)
mov    %al,0x3(%rsp)
and    %al,%r13b
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    1188 <generate_random_indicies_in_cluster+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %al,%al
je     1259 <generate_random_indicies_in_cluster+0x139>
cmp    0x8(%rsp),%r15d
jge    1259 <generate_random_indicies_in_cluster+0x139>
mov    0x3(%rsp),%al
test   %r13b,%r13b
je     1180 <generate_random_indicies_in_cluster+0x60>
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <rand@plt>
mov    0x2e3c(%rip),%rax
xorps  %xmm1,%xmm1
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
mulsd  0x20(%rsp),%xmm0
cvttsd2si %xmm0,%r12d
test   %r15d,%r15d
je     1210 <generate_random_indicies_in_cluster+0xf0>
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
cmp    %r12d,(%rbx,%rax,1)
je     11f0 <generate_random_indicies_in_cluster+0xd0>
add    $0x4,%rax
cmp    %rax,%r14
jne    11d0 <generate_random_indicies_in_cluster+0xb0>
jmp    1210 <generate_random_indicies_in_cluster+0xf0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    0x4(%rsp),%ebp
setl   %al
jge    1180 <generate_random_indicies_in_cluster+0x60>
cmpl   $0xffffffff,0xc(%rsp)
jne    119e <generate_random_indicies_in_cluster+0x7e>
jmp    1180 <generate_random_indicies_in_cluster+0x60>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%eax
mov    0x70(%rsp),%r8d
imul   %r8d,%eax
cltq
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,8),%rdi
mov    0x18(%rsp),%rsi
mov    0x60(%rsp),%edx
mov    %r8d,%ecx
call   1030 <closest_centroid@plt>
add    $0x1,%ebp
lea    0x1(%r15),%eax
add    $0x4,%r14
mov    %r15d,%ecx
mov    %r12d,(%rbx,%rcx,4)
mov    %eax,%r15d
cmp    0x4(%rsp),%ebp
jl     1188 <generate_random_indicies_in_cluster+0x68>
jmp    125c <generate_random_indicies_in_cluster+0x13c>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i32 @generate_random_indicies_in_cluster(double* %0, double* %1, i32* nocapture %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7, i32 %8) {
%10 = icmp sgt i32 %5, 0
%11 = icmp ne i32 %3, -1
%12 = and i1 %10, %11
%13 = sitofp i32 %7 to double
%14 = icmp ne i32 %3, -1
br label %15
15: ; preds = %41, %9
%16 = phi i32 [ %50, %41 ], [ 0, %9 ]
%17 = icmp slt i32 %16, %4
%18 = icmp eq i32 %16, 0
%19 = zext i32 %16 to i64
br i1 %17, label %20, label %54
20: ; preds = %15, %51
br i1 %12, label %21, label %51
21: ; preds = %20, %37
%22 = phi i32 [ %38, %37 ], [ 0, %20 ]
%23 = tail call double (...) @rand()
%24 = load i64, i64* @RAND_MAX
%25 = sitofp i64 %24 to double
%26 = fdiv double %23, %25
%27 = fmul double %26, %13
%28 = fptosi double %27 to i32
br i1 %18, label %41, label %31
29: ; preds = %31
%30 = icmp eq i64 %36, %19
br i1 %30, label %41, label %31
31: ; preds = %21, %29
%32 = phi i64 [ %36, %29 ], [ 0, %21 ]
%33 = getelementptr inbounds i32, i32* %2, i64 %32
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, %28
%36 = add nuw nsw i64 %32, 1
br i1 %35, label %37, label %29
37: ; preds = %31
%38 = add nuw nsw i32 %22, 1
%39 = icmp slt i32 %38, %5
%40 = and i1 %39, %14
br i1 %40, label %21, label %51
41: ; preds = %21, %29
%42 = mul nsw i32 %28, %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double, double* %0, i64 %43
%45 = tail call i32 @closest_centroid(double* %44, double* %1, i32 %6, i32 %8)
%46 = add nuw nsw i32 %22, 1
%47 = icmp slt i32 %46, %5
%48 = zext i32 %16 to i64
%49 = getelementptr inbounds i32, i32* %2, i64 %48
store i32 %28, i32* %49
%50 = add nuw nsw i32 %16, 1
br i1 %47, label %15, label %54
51: ; preds = %37, %20
%52 = phi i1 [ %10, %20 ], [ %39, %37 ]
%53 = select i1 %52, i1 %17, i1 false
br i1 %53, label %20, label %54
54: ; preds = %41, %15, %51
%55 = phi i32 [ %16, %51 ], [ %16, %15 ], [ %50, %41 ]
ret i32 %55
}