#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__ calc_distance(int, double *, double *);
double calc_total_distance(int dim, int n, int k, double *X, double *centroids,
                           int *cluster_assignment_index) {
  double tot_D = 0;
  for (int ii = 0; ii < n; ii++) {
    int active_cluster = cluster_assignment_index[ii];
    if (active_cluster != -1)
      tot_D +=
          calc_distance(dim, &X[ii * dim], &centroids[active_cluster * dim]);
  }
  return tot_D;
}


x86 assembly (clang, -O0):
<calc_total_distance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11b2 <calc_total_distance+0xa2>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
je     119f <calc_total_distance+0x8f>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdx
call   1030 <calc_distance@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11a4 <calc_total_distance+0x94>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    113c <calc_total_distance+0x2c>
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_total_distance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
test   %esi,%esi
jle    11a3 <calc_total_distance+0x93>
mov    %r9,%r15
mov    %rcx,%rbx
mov    %edi,%ebp
movslq %edi,%r12
mov    %esi,%r13d
shl    $0x3,%r12
xorps  %xmm0,%xmm0
xor    %r14d,%r14d
jmp    115c <calc_total_distance+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
add    %r12,%rbx
cmp    %r14,%r13
je     11a6 <calc_total_distance+0x96>
mov    (%r15,%r14,4),%eax
cmp    $0xffffffff,%eax
je     1150 <calc_total_distance+0x40>
imul   %ebp,%eax
cltq
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,8),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
movsd  %xmm0,0x8(%rsp)
call   1030 <calc_distance@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
jmp    1150 <calc_total_distance+0x40>
xorps  %xmm0,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @calc_total_distance(i32 %0, i32 %1, i32 %2, double* %3, double* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double*
%11 = alloca double*
%12 = alloca i32*
%13 = alloca double
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store double* %3, double** %10
store double* %4, double** %11
store i32* %5, i32** %12
store double 0.000000e+00, double* %13
store i32 0, i32* %14
br label %16
16: ; preds = %47, %6
%17 = load i32, i32* %14
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %50
20: ; preds = %16
%21 = load i32*, i32** %12
%22 = load i32, i32* %14
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
store i32 %25, i32* %15
%26 = load i32, i32* %15
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %46
28: ; preds = %20
%29 = load i32, i32* %7
%30 = load double*, double** %10
%31 = load i32, i32* %14
%32 = load i32, i32* %7
%33 = mul nsw i32 %31, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %30, i64 %34
%36 = load double*, double** %11
%37 = load i32, i32* %15
%38 = load i32, i32* %7
%39 = mul nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %36, i64 %40
%42 = call i64 @calc_distance(i32 %29, double* %35, double* %41)
%43 = sitofp i64 %42 to double
%44 = load double, double* %13
%45 = fadd double %44, %43
store double %45, double* %13
br label %46
46: ; preds = %28, %20
br label %47
47: ; preds = %46
%48 = load i32, i32* %14
%49 = add nsw i32 %48, 1
store i32 %49, i32* %14
br label %16
50: ; preds = %16
%51 = load double, double* %13
ret double %51
}

LLVM IR (-O2):
define double @calc_total_distance(i32 %0, i32 %1, i32 %2, double* %3, double* %4, i32* nocapture %5) {
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = sext i32 %0 to i64
%10 = zext i32 %1 to i64
br label %13
11: ; preds = %28, %6
%12 = phi double [ 0.000000e+00, %6 ], [ %29, %28 ]
ret double %12
13: ; preds = %8, %28
%14 = phi i64 [ 0, %8 ], [ %30, %28 ]
%15 = phi double [ 0.000000e+00, %8 ], [ %29, %28 ]
%16 = getelementptr inbounds i32, i32* %5, i64 %14
%17 = load i32, i32* %16
%18 = icmp eq i32 %17, -1
br i1 %18, label %28, label %19
19: ; preds = %13
%20 = mul nsw i64 %14, %9
%21 = getelementptr inbounds double, double* %3, i64 %20
%22 = mul nsw i32 %17, %0
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %4, i64 %23
%25 = tail call i64 @calc_distance(i32 %0, double* %21, double* %24)
%26 = sitofp i64 %25 to double
%27 = fadd double %15, %26
br label %28
28: ; preds = %19, %13
%29 = phi double [ %27, %19 ], [ %15, %13 ]
%30 = add nuw nsw i64 %14, 1
%31 = icmp eq i64 %30, %10
br i1 %31, label %11, label %13
}