#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 compute_distance(double const *, double const *, unsigned int);
double sqrt(double);
double compute_rms2(const double *CX, unsigned int dim, unsigned int ncls) {
  int cnt = 0;
  double rms = 0.0;
  for (int i = ncls - 1; i > 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      const double *pc1 = CX + i * dim;
      const double *pc2 = CX + j * dim;
      double d = compute_distance(pc1, pc2, dim);
      rms += d * d;
      cnt++;
    }
  }
  rms /= cnt;
  rms = sqrt(rms);
  return (rms);
}


x86 assembly (clang, -O0):
<compute_rms2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11f2 <compute_rms2+0xd2>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     11df <compute_rms2+0xbf>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0xc(%rbp),%ecx
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0xc(%rbp),%ecx
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <compute_distance@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    115d <compute_rms2+0x3d>
jmp    11e4 <compute_rms2+0xc4>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    114a <compute_rms2+0x2a>
cvtsi2sdl -0x14(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_rms2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    -0x1(%rdx),%eax
test   %eax,%eax
jle    11c5 <compute_rms2+0xa5>
mov    %esi,%ebp
mov    %rdi,%r15
add    $0xfffffffe,%edx
mov    %eax,%r12d
imul   %esi,%edx
xorpd  %xmm1,%xmm1
xor    %eax,%eax
nopl   (%rax)
mov    %eax,0x4(%rsp)
mov    %r12d,%eax
imul   %ebp,%eax
lea    (%r15,%rax,8),%rbx
mov    %rdx,0x8(%rsp)
mov    %edx,%r13d
mov    %r12d,%r14d
nopl   0x0(%rax)
movsd  %xmm1,0x10(%rsp)
mov    %r13d,%eax
lea    (%r15,%rax,8),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <compute_distance@plt>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm0,%xmm1
add    $0xffffffff,%r14d
sub    %ebp,%r13d
test   %r14d,%r14d
jg     1170 <compute_rms2+0x50>
mov    0x4(%rsp),%eax
add    %r12d,%eax
add    $0xffffffffffffffff,%r12
mov    0x8(%rsp),%rdx
sub    %ebp,%edx
test   %r12d,%r12d
jg     1150 <compute_rms2+0x30>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  %xmm0,%xmm1
jmp    11cd <compute_rms2+0xad>
movsd  0xe33(%rip),%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jb     11ed <compute_rms2+0xcd>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movapd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <sqrt@plt>

LLVM IR (-O0):
define double @compute_rms2(double* %0, i32 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca double*
%12 = alloca double*
%13 = alloca double
store double* %0, double** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store double 0.000000e+00, double* %8
%14 = load i32, i32* %6
%15 = sub i32 %14, 1
store i32 %15, i32* %9
br label %16
16: ; preds = %52, %3
%17 = load i32, i32* %9
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %55
19: ; preds = %16
%20 = load i32, i32* %9
%21 = sub nsw i32 %20, 1
store i32 %21, i32* %10
br label %22
22: ; preds = %48, %19
%23 = load i32, i32* %10
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %51
25: ; preds = %22
%26 = load double*, double** %4
%27 = load i32, i32* %9
%28 = load i32, i32* %5
%29 = mul i32 %27, %28
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds double, double* %26, i64 %30
store double* %31, double** %11
%32 = load double*, double** %4
%33 = load i32, i32* %10
%34 = load i32, i32* %5
%35 = mul i32 %33, %34
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds double, double* %32, i64 %36
store double* %37, double** %12
%38 = load double*, double** %11
%39 = load double*, double** %12
%40 = load i32, i32* %5
%41 = call double @compute_distance(double* %38, double* %39, i32 %40)
store double %41, double* %13
%42 = load double, double* %13
%43 = load double, double* %13
%44 = load double, double* %8
%45 = call double @llvm.fmuladd.f64(double %42, double %43, double %44)
store double %45, double* %8
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %48
48: ; preds = %25
%49 = load i32, i32* %10
%50 = add nsw i32 %49, -1
store i32 %50, i32* %10
br label %22
51: ; preds = %22
br label %52
52: ; preds = %51
%53 = load i32, i32* %9
%54 = add nsw i32 %53, -1
store i32 %54, i32* %9
br label %16
55: ; preds = %16
%56 = load i32, i32* %7
%57 = sitofp i32 %56 to double
%58 = load double, double* %8
%59 = fdiv double %58, %57
store double %59, double* %8
%60 = load double, double* %8
%61 = call double @sqrt(double %60)
store double %61, double* %8
%62 = load double, double* %8
ret double %62
}

LLVM IR (-O2):
define double @compute_rms2(double* %0, i32 %1, i32 %2) {
%4 = add i32 %2, -1
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = add i32 %2, -2
%8 = zext i32 %7 to i64
%9 = zext i32 %4 to i64
br label %22
10: ; preds = %31
%11 = add i32 %25, %27
%12 = add nsw i64 %23, -1
%13 = trunc i64 %12 to i32
%14 = icmp sgt i32 %13, 0
%15 = add nsw i64 %24, -1
br i1 %14, label %22, label %16
16: ; preds = %10
%17 = sitofp i32 %11 to double
%18 = fdiv double %39, %17
br label %19
19: ; preds = %16, %3
%20 = phi double [ %18, %16 ], [ 0x7FF8000000000000, %3 ]
%21 = tail call double @sqrt(double %20)
ret double %21
22: ; preds = %10, %6
%23 = phi i64 [ %9, %6 ], [ %12, %10 ]
%24 = phi i64 [ %8, %6 ], [ %15, %10 ]
%25 = phi i32 [ 0, %6 ], [ %11, %10 ]
%26 = phi double [ 0.000000e+00, %6 ], [ %39, %10 ]
%27 = trunc i64 %23 to i32
%28 = mul i32 %27, %1
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds double, double* %0, i64 %29
br label %31
31: ; preds = %22, %31
%32 = phi i64 [ %24, %22 ], [ %40, %31 ]
%33 = phi double [ %26, %22 ], [ %39, %31 ]
%34 = trunc i64 %32 to i32
%35 = mul i32 %34, %1
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds double, double* %0, i64 %36
%38 = tail call double @compute_distance(double* %30, double* %37, i32 %1)
%39 = tail call double @llvm.fmuladd.f64(double %38, double %38, double %33)
%40 = add nsw i64 %32, -1
%41 = icmp sgt i32 %34, 0
br i1 %41, label %31, label %10
}