#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 */
double calc_distance(int, double *, double *);
void calc_all_distances(int dim, int n, int k, double *X, double *centroid,
                        double *distance_output) {
  for (int ii = 0; ii < n; ii++)
    for (int jj = 0; jj < k; jj++) {
      distance_output[ii * k + jj] =
          calc_distance(dim, &X[ii * dim], &centroid[jj * dim]);
    }
}


x86 assembly (clang, -O0):
<calc_all_distances>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ba <calc_all_distances+0xaa>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a7 <calc_all_distances+0x97>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdx
call   1030 <calc_distance@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
imul   -0xc(%rbp),%ecx
add    -0x30(%rbp),%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1147 <calc_all_distances+0x37>
jmp    11ac <calc_all_distances+0x9c>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1134 <calc_all_distances+0x24>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_all_distances>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8,0x28(%rsp)
mov    %rcx,0x20(%rsp)
mov    %edx,0xc(%rsp)
test   %esi,%esi
jle    11e5 <calc_all_distances+0xd5>
mov    %r9,%rbp
mov    %edi,%r12d
movslq %edi,%rax
movslq 0xc(%rsp),%rcx
mov    %esi,%edx
mov    %rdx,0x30(%rsp)
mov    %ecx,%r15d
shl    $0x3,%rcx
mov    %rcx,0x38(%rsp)
mov    %rax,0x18(%rsp)
lea    0x0(,%rax,8),%rbx
xor    %eax,%eax
mov    %rax,0x10(%rsp)
jmp    118d <calc_all_distances+0x7d>
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rcx
add    $0x1,%rcx
add    0x38(%rsp),%rbp
mov    %rcx,%rax
mov    %rcx,0x10(%rsp)
cmp    0x30(%rsp),%rcx
je     11e5 <calc_all_distances+0xd5>
cmpl   $0x0,0xc(%rsp)
jle    1170 <calc_all_distances+0x60>
mov    0x10(%rsp),%rax
imul   0x18(%rsp),%rax
mov    0x20(%rsp),%rcx
lea    (%rcx,%rax,8),%rax
mov    %rax,0x40(%rsp)
mov    0x28(%rsp),%r14
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    0x40(%rsp),%rsi
mov    %r14,%rdx
call   1030 <calc_distance@plt>
movsd  %xmm0,0x0(%rbp,%r13,8)
add    $0x1,%r13
add    %rbx,%r14
cmp    %r13,%r15
jne    11c0 <calc_all_distances+0xb0>
jmp    1170 <calc_all_distances+0x60>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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