#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 **clusters_destroy(int **, int);
int **clusters_init(int *, int, int, int, int);
int kmeans_it(int *, int, int, int, int, int **, int, int *);
void kmeans(int *image, int *width, int *height, int *depth, int *k, int *maxit,
            int *mean_colors, int *ret) {
  int **clusters;
  int i = 0;
  int changed = 1;
  clusters = clusters_init(image, *width, *height, *depth, *k);
  i = 0;
  while (i < *maxit && changed) {
    changed = kmeans_it(image, *width, *height, *depth, *k, clusters,
                        *mean_colors, ret);
    i++;
  }
  clusters = clusters_destroy(clusters, *k);
}


x86 assembly (clang, -O0):
<kmeans>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8d
call   1040 <clusters_init@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
mov    %al,-0x41(%rbp)
jge    11b1 <kmeans+0x81>
cmpl   $0x0,-0x40(%rbp)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11c1 <kmeans+0x91>
jmp    120c <kmeans+0xdc>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8d
mov    -0x38(%rbp),%r9
mov    0x10(%rbp),%rax
mov    (%rax),%r10d
mov    0x18(%rbp),%rax
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
call   1050 <kmeans_it@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1193 <kmeans+0x63>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1030 <clusters_destroy@plt>
mov    %rax,-0x38(%rbp)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmeans>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r15
mov    0x68(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    0x60(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %rsi,0x20(%rsp)
mov    (%rsi),%esi
mov    %rdx,0x18(%rsp)
mov    (%rdx),%edx
mov    %rcx,%r13
mov    (%rcx),%ecx
mov    %r8,%rbx
mov    (%r8),%r8d
mov    %rdi,%rbp
call   1040 <clusters_init@plt>
mov    %rax,%r14
cmpl   $0x0,(%r15)
jle    11ce <kmeans+0x9e>
mov    %r15,%r12
mov    $0x1,%r15d
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rax
mov    (%rax),%esi
mov    0x18(%rsp),%rax
mov    (%rax),%edx
mov    0x0(%r13),%ecx
mov    (%rbx),%r8d
mov    0x8(%rsp),%rax
mov    (%rax),%eax
mov    %rbp,%rdi
mov    %r14,%r9
push   0x10(%rsp)
push   %rax
call   1050 <kmeans_it@plt>
add    $0x10,%rsp
cmp    (%r12),%r15d
jge    11ce <kmeans+0x9e>
add    $0x1,%r15d
test   %eax,%eax
jne    1190 <kmeans+0x60>
mov    (%rbx),%esi
mov    %r14,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <clusters_destroy@plt>

LLVM IR (-O0):
define void @kmeans(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4, i32* %5, i32* %6, i32* %7) {
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32*
%17 = alloca i32**
%18 = alloca i32
%19 = alloca i32
store i32* %0, i32** %9
store i32* %1, i32** %10
store i32* %2, i32** %11
store i32* %3, i32** %12
store i32* %4, i32** %13
store i32* %5, i32** %14
store i32* %6, i32** %15
store i32* %7, i32** %16
store i32 0, i32* %18
store i32 1, i32* %19
%20 = load i32*, i32** %9
%21 = load i32*, i32** %10
%22 = load i32, i32* %21
%23 = load i32*, i32** %11
%24 = load i32, i32* %23
%25 = load i32*, i32** %12
%26 = load i32, i32* %25
%27 = load i32*, i32** %13
%28 = load i32, i32* %27
%29 = call i32** @clusters_init(i32* %20, i32 %22, i32 %24, i32 %26, i32 %28)
store i32** %29, i32*** %17
store i32 0, i32* %18
br label %30
30: ; preds = %40, %8
%31 = load i32, i32* %18
%32 = load i32*, i32** %14
%33 = load i32, i32* %32
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* %19
%37 = icmp ne i32 %36, 0
br label %38
38: ; preds = %35, %30
%39 = phi i1 [ false, %30 ], [ %37, %35 ]
br i1 %39, label %40, label %57
40: ; preds = %38
%41 = load i32*, i32** %9
%42 = load i32*, i32** %10
%43 = load i32, i32* %42
%44 = load i32*, i32** %11
%45 = load i32, i32* %44
%46 = load i32*, i32** %12
%47 = load i32, i32* %46
%48 = load i32*, i32** %13
%49 = load i32, i32* %48
%50 = load i32**, i32*** %17
%51 = load i32*, i32** %15
%52 = load i32, i32* %51
%53 = load i32*, i32** %16
%54 = call i32 @kmeans_it(i32* %41, i32 %43, i32 %45, i32 %47, i32 %49, i32** %50, i32 %52, i32* %53)
store i32 %54, i32* %19
%55 = load i32, i32* %18
%56 = add nsw i32 %55, 1
store i32 %56, i32* %18
br label %30
57: ; preds = %38
%58 = load i32**, i32*** %17
%59 = load i32*, i32** %13
%60 = load i32, i32* %59
%61 = call i32** @clusters_destroy(i32** %58, i32 %60)
store i32** %61, i32*** %17
ret void
}

LLVM IR (-O2):
define void @kmeans(i32* %0, i32* nocapture %1, i32* nocapture %2, i32* nocapture %3, i32* nocapture %4, i32* nocapture %5, i32* nocapture %6, i32* %7) {
%9 = load i32, i32* %1
%10 = load i32, i32* %2
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = tail call i32** @clusters_init(i32* %0, i32 %9, i32 %10, i32 %11, i32 %12)
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %8, %16
%17 = phi i32 [ %24, %16 ], [ 0, %8 ]
%18 = load i32, i32* %1
%19 = load i32, i32* %2
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = load i32, i32* %6
%23 = tail call i32 @kmeans_it(i32* %0, i32 %18, i32 %19, i32 %20, i32 %21, i32** %13, i32 %22, i32* %7)
%24 = add nuw nsw i32 %17, 1
%25 = load i32, i32* %5
%26 = icmp slt i32 %24, %25
%27 = icmp ne i32 %23, 0
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %16, label %29
29: ; preds = %16, %8
%30 = load i32, i32* %4
%31 = tail call i32** @clusters_destroy(i32** %13, i32 %30)
ret void
}