int cluster(int numObjects, int numAttributes, float **attributes,
            int num_nclusters, float threshold, float ***cluster_centres) {
  int nclusters;
  int *membership;
  float **tmp_cluster_centres;
  membership = (int *)malloc(numObjects * sizeof(int));
  nclusters = num_nclusters;
  srand(7);
  tmp_cluster_centres = kmeans_clustering(attributes, numAttributes, numObjects,
                                          nclusters, threshold, membership);
  if (*cluster_centres) {
    free((*cluster_centres)[0]);
    free(*cluster_centres);
  }
  *cluster_centres = tmp_cluster_centres;
  free(membership);
  return 0;
}


x86 assembly (clang, -O0):
<cluster>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movss  %xmm0,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movslq -0x4(%rbp),%rdi
shl    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x7,%edi
mov    $0x0,%al
call   1050 <srand@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x24(%rbp),%ecx
movss  -0x18(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0x30(%rbp),%r8
mov    $0x1,%al
call   1040 <kmeans_clustering@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d1 <cluster+0x91>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cluster>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
movss  %xmm0,0x4(%rsp)
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %esi,%r12d
movslq %edi,%rbp
lea    0x0(,%rbp,4),%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x7,%edi
xor    %eax,%eax
call   1050 <srand@plt>
movss  0x4(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %r13,%rdi
mov    %r12d,%esi
mov    %ebp,%edx
mov    %r15d,%ecx
mov    %r14,%r8
mov    $0x1,%al
call   1040 <kmeans_clustering@plt>
mov    %eax,%ebp
mov    (%rbx),%rax
test   %rax,%rax
je     11b5 <cluster+0x75>
mov    (%rax),%rdi
call   1030 <free@plt>
mov    (%rbx),%rdi
call   1030 <free@plt>
movslq %ebp,%rax
mov    %rax,(%rbx)
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @cluster(i32 %0, i32 %1, float** %2, i32 %3, float %4, float*** %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca float**
%10 = alloca i32
%11 = alloca float
%12 = alloca float***
%13 = alloca i32
%14 = alloca i32*
%15 = alloca float**
store i32 %0, i32* %7
store i32 %1, i32* %8
store float** %2, float*** %9
store i32 %3, i32* %10
store float %4, float* %11
store float*** %5, float**** %12
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 4
%19 = call i8* @malloc(i64 %18)
%20 = bitcast i8* %19 to i32*
store i32* %20, i32** %14
%21 = load i32, i32* %10
store i32 %21, i32* %13
%22 = call i32 (i32, ...) bitcast (i32 (...)* @srand to i32 (i32, ...)*)(i32 7)
%23 = load float**, float*** %9
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = load i32, i32* %13
%27 = load float, float* %11
%28 = fpext float %27 to double
%29 = load i32*, i32** %14
%30 = call i32 (float**, i32, i32, i32, double, i32*, ...) bitcast (i32 (...)* @kmeans_clustering to i32 (float**, i32, i32, i32, double, i32*, ...)*)(float** %23, i32 %24, i32 %25, i32 %26, double %28, i32* %29)
%31 = sext i32 %30 to i64
%32 = inttoptr i64 %31 to float**
store float** %32, float*** %15
%33 = load float***, float**** %12
%34 = load float**, float*** %33
%35 = icmp ne float** %34, null
br i1 %35, label %36, label %45
36: ; preds = %6
%37 = load float***, float**** %12
%38 = load float**, float*** %37
%39 = getelementptr inbounds float*, float** %38, i64 0
%40 = load float*, float** %39
%41 = bitcast float* %40 to i8*
%42 = load float***, float**** %12
%43 = load float**, float*** %42
%44 = bitcast float** %43 to i8*
br label %45
45: ; preds = %36, %6
%46 = load float**, float*** %15
%47 = load float***, float**** %12
store float** %46, float*** %47
%48 = load i32*, i32** %14
%49 = bitcast i32* %48 to i8*
ret i32 0
}

LLVM IR (-O2):
define i32 @cluster(i32 %0, i32 %1, float** %2, i32 %3, float %4, float*** nocapture %5) {
%7 = sext i32 %0 to i64
%8 = shl nsw i64 %7, 2
%9 = tail call i8* @malloc(i64 %8)
%10 = bitcast i8* %9 to i32*
%11 = tail call i32 (i32, ...) bitcast (i32 (...)* @srand to i32 (i32, ...)*)(i32 7)
%12 = fpext float %4 to double
%13 = tail call i32 (float**, i32, i32, i32, double, i32*, ...) bitcast (i32 (...)* @kmeans_clustering to i32 (float**, i32, i32, i32, double, i32*, ...)*)(float** %2, i32 %1, i32 %0, i32 %3, double %12, i32* %10)
%14 = load float**, float*** %5
%15 = icmp eq float** %14, null
br i1 %15, label %21, label %16
16: ; preds = %6
%17 = bitcast float** %14 to i8**
%18 = load i8*, i8** %17
tail call void @free(i8* %18)
%19 = bitcast float*** %5 to i8**
%20 = load i8*, i8** %19
tail call void @free(i8* %20)
br label %21
21: ; preds = %16, %6
%22 = sext i32 %13 to i64
%23 = inttoptr i64 %22 to float**
store float** %23, float*** %5
tail call void @free(i8* %9)
ret i32 0
}