int cluster(int numObjects, int numAttributes, float **attributes,
            int nclusters, float threshold, float ***cluster_centres) {
  int *membership;
  float **tmp_cluster_centres;
  membership = (int *)malloc(numObjects * sizeof(int));
  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    $0x30,%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,-0x28(%rbp)
mov    $0x7,%edi
mov    $0x0,%al
call   1050 <srand@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x14(%rbp),%ecx
movss  -0x18(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0x28(%rbp),%r8
mov    $0x1,%al
call   1040 <kmeans_clustering@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11cb <cluster+0x8b>
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    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%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 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
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 4
%18 = call i8* @malloc(i64 %17)
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %13
%20 = call i32 (i32, ...) bitcast (i32 (...)* @srand to i32 (i32, ...)*)(i32 7)
%21 = load float**, float*** %9
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = load i32, i32* %10
%25 = load float, float* %11
%26 = fpext float %25 to double
%27 = load i32*, i32** %13
%28 = call i32 (float**, i32, i32, i32, double, i32*, ...) bitcast (i32 (...)* @kmeans_clustering to i32 (float**, i32, i32, i32, double, i32*, ...)*)(float** %21, i32 %22, i32 %23, i32 %24, double %26, i32* %27)
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to float**
store float** %30, float*** %14
%31 = load float***, float**** %12
%32 = load float**, float*** %31
%33 = icmp ne float** %32, null
br i1 %33, label %34, label %43
34: ; preds = %6
%35 = load float***, float**** %12
%36 = load float**, float*** %35
%37 = getelementptr inbounds float*, float** %36, i64 0
%38 = load float*, float** %37
%39 = bitcast float* %38 to i8*
%40 = load float***, float**** %12
%41 = load float**, float*** %40
%42 = bitcast float** %41 to i8*
br label %43
43: ; preds = %34, %6
%44 = load float**, float*** %14
%45 = load float***, float**** %12
store float** %44, float*** %45
%46 = load i32*, i32** %13
%47 = bitcast i32* %46 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
}