#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*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
scalar_t__ reassign_clusters(double *, double *, int, int, int, int *, int *);
int /*<<< orphan*/ recompute_centroids(double *, double *, int, int, int, int *,
                                       int *);
void cluster_kmeans(double *centroids, double *samples, int dims,
                    int n_centroids, int n_samples, int *labels) {
  int *counts = malloc(sizeof(int) * n_centroids);
  memset(labels, 0, sizeof(int) * n_samples);
  memset(counts, 0, sizeof(int) * n_centroids);
  counts[0] = n_samples;
  int reassigned = n_samples;
  while (reassigned > 0) {
    reassigned = reassign_clusters(centroids, samples, dims, n_centroids,
                                   n_samples, labels, counts) == 0;
    recompute_centroids(centroids, samples, dims, n_centroids, n_samples,
                        labels, counts);
  }
  free(counts);
}


x86 assembly (clang, -O0):
<cluster_kmeans>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    121e <cluster_kmeans+0xce>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x28(%rbp),%r9
mov    -0x30(%rbp),%rax
mov    %rax,(%rsp)
call   1070 <reassign_clusters@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x28(%rbp),%r9
mov    -0x30(%rbp),%rax
mov    %rax,(%rsp)
call   1050 <recompute_centroids@plt>
jmp    11ba <cluster_kmeans+0x6a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cluster_kmeans>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%rbp
mov    %r8d,%r13d
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x0(,%rbx,4),%edi
mov    %edi,0x10(%rsp)
call   1060 <malloc@plt>
mov    %rax,0x8(%rsp)
lea    0x0(,%r13,4),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
mov    0x10(%rsp),%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%rdi
mov    %r13d,(%rdi)
test   %r13d,%r13d
jle    120b <cluster_kmeans+0xbb>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rdi,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
mov    %r13d,%r8d
mov    %rbp,%r9
call   1070 <reassign_clusters@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
mov    %r13d,%r8d
mov    %rbp,%r9
call   1050 <recompute_centroids@plt>
mov    0x8(%rsp),%rdi
cmpq   $0x0,0x10(%rsp)
je     11c0 <cluster_kmeans+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @cluster_kmeans(double* %0, double* %1, i32 %2, i32 %3, i32 %4, i32* %5) {
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
store double* %0, double** %7
store double* %1, double** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32* %5, i32** %12
%15 = load i32, i32* %10
%16 = sext i32 %15 to i64
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i32* @malloc(i32 %18)
store i32* %19, i32** %13
%20 = load i32*, i32** %12
%21 = load i32, i32* %11
%22 = sext i32 %21 to i64
%23 = mul i64 4, %22
%24 = trunc i64 %23 to i32
%25 = call i32 @memset(i32* %20, i32 0, i32 %24)
%26 = load i32*, i32** %13
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = mul i64 4, %28
%30 = trunc i64 %29 to i32
%31 = call i32 @memset(i32* %26, i32 0, i32 %30)
%32 = load i32, i32* %11
%33 = load i32*, i32** %13
%34 = getelementptr inbounds i32, i32* %33, i64 0
store i32 %32, i32* %34
%35 = load i32, i32* %11
store i32 %35, i32* %14
br label %36
36: ; preds = %39, %6
%37 = load i32, i32* %14
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %58
39: ; preds = %36
%40 = load double*, double** %7
%41 = load double*, double** %8
%42 = load i32, i32* %9
%43 = load i32, i32* %10
%44 = load i32, i32* %11
%45 = load i32*, i32** %12
%46 = load i32*, i32** %13
%47 = call i64 @reassign_clusters(double* %40, double* %41, i32 %42, i32 %43, i32 %44, i32* %45, i32* %46)
%48 = icmp eq i64 %47, 0
%49 = zext i1 %48 to i32
store i32 %49, i32* %14
%50 = load double*, double** %7
%51 = load double*, double** %8
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = load i32, i32* %11
%55 = load i32*, i32** %12
%56 = load i32*, i32** %13
%57 = call i32 @recompute_centroids(double* %50, double* %51, i32 %52, i32 %53, i32 %54, i32* %55, i32* %56)
br label %36
58: ; preds = %36
%59 = load i32*, i32** %13
%60 = call i32 @free(i32* %59)
ret void
}

LLVM IR (-O2):
define void @cluster_kmeans(double* %0, double* %1, i32 %2, i32 %3, i32 %4, i32* %5) {
%7 = shl i32 %3, 2
%8 = tail call i32* @malloc(i32 %7)
%9 = shl i32 %4, 2
%10 = tail call i32 @memset(i32* %5, i32 0, i32 %9)
%11 = tail call i32 @memset(i32* %8, i32 0, i32 %7)
store i32 %4, i32* %8
%12 = icmp sgt i32 %4, 0
br i1 %12, label %13, label %17
13: ; preds = %6, %13
%14 = tail call i64 @reassign_clusters(double* %0, double* %1, i32 %2, i32 %3, i32 %4, i32* %5, i32* %8)
%15 = icmp eq i64 %14, 0
%16 = tail call i32 @recompute_centroids(double* %0, double* %1, i32 %2, i32 %3, i32 %4, i32* %5, i32* %8)
br i1 %15, label %13, label %17
17: ; preds = %13, %6
%18 = tail call i32 @free(i32* %8)
ret void
}