#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__ euclid_dist_2(float *, float *, int);
int find_nearest_point(float *, int, float **, int);
float sqrt(float);
float rms_err(float **feature, int nfeatures, int npoints,
              float **cluster_centres, int nclusters) {
  int i;
  int nearest_cluster_index;
  float sum_euclid = 0.0;
  float ret;
#pragma omp parallel for shared(feature, cluster_centres)                      \
    firstprivate(npoints, nfeatures,                                           \
                 nclusters) private(i, nearest_cluster_index) schedule(static)
  for (i = 0; i < npoints; i++) {
    nearest_cluster_index =
        find_nearest_point(feature[i], nfeatures, cluster_centres, nclusters);
    sum_euclid += euclid_dist_2(
        feature[i], cluster_centres[nearest_cluster_index], nfeatures);
  }
  ret = sqrt(sum_euclid / npoints);
  return (ret);
}


x86 assembly (clang, -O0):
<rms_err>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11c0 <rms_err+0x90>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <find_nearest_point@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0xc(%rbp),%edx
call   1040 <euclid_dist_2@plt>
cvtsi2ss %rax,%xmm0
addss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1159 <rms_err+0x29>
movss  -0x28(%rbp),%xmm0
cvtsi2ssl -0x10(%rbp),%xmm1
divss  %xmm1,%xmm0
call   1050 <sqrt@plt>
movss  %xmm0,-0x2c(%rbp)
movss  -0x2c(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rms_err>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xorps  %xmm0,%xmm0
mov    %edx,(%rsp)
test   %edx,%edx
jle    11a3 <rms_err+0x73>
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %esi,%ebp
mov    %rdi,%rbx
mov    (%rsp),%r13d
xorps  %xmm0,%xmm0
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
movss  %xmm0,0x4(%rsp)
mov    (%rbx,%r14,8),%rdi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1030 <find_nearest_point@plt>
mov    (%rbx,%r14,8),%rdi
cltq
mov    (%r12,%rax,8),%rsi
mov    %ebp,%edx
call   1040 <euclid_dist_2@plt>
movss  0x4(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2ss %rax,%xmm1
addss  %xmm1,%xmm0
add    $0x1,%r14
cmp    %r14,%r13
jne    1160 <rms_err+0x30>
xorps  %xmm1,%xmm1
cvtsi2ssl (%rsp),%xmm1
divss  %xmm1,%xmm0
ucomiss 0xe4a(%rip),%xmm0
jb     11cb <rms_err+0x9b>
sqrtss %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <sqrt@plt>

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

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