#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*/ assert(int);
double compute_distance(double const *, double const *, unsigned int);
double sqrt(double);
double compute_rms1(const double *CX, const double *X, const double *W,
                    const unsigned int *c, unsigned int dim,
                    unsigned int npts) {
  double sum = 0.0;
  double rms = 0.0;
  const double *px = X;
  for (unsigned int i = 0; i < npts; i++, px += dim) {
    const double *pcx = CX + c[i] * dim;
    double d = compute_distance(px, pcx, dim);
    rms += W[i] * d * d;
    sum += W[i];
  }
  rms /= sum;
  rms = sqrt(rms);
  assert(rms >= 0.0);
  return (rms);
}


x86 assembly (clang, -O0):
<compute_rms1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    120f <compute_rms1+0xdf>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x44(%rbp),%edx
mov    (%rcx,%rdx,4),%ecx
imul   -0x24(%rbp),%ecx
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <compute_distance@plt>
movsd  %xmm0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x44(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
mulsd  -0x58(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x44(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    116f <compute_rms1+0x3f>
movsd  -0x30(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1040 <sqrt@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movsd  -0x38(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_rms1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %r9d,%r9d
je     11f0 <compute_rms1+0xc0>
mov    %r8d,%ebp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r13
mov    %r8d,%eax
mov    %r9d,%esi
mov    %rsi,0x10(%rsp)
shl    $0x3,%rax
mov    %rax,0x18(%rsp)
xorpd  %xmm1,%xmm1
xor    %r15d,%r15d
xorpd  %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,0x20(%rsp)
mov    (%r12,%r15,4),%eax
imul   %ebp,%eax
lea    0x0(,%rax,8),%rsi
add    %r13,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <compute_distance@plt>
movsd  (%r14,%r15,8),%xmm1
movapd %xmm0,%xmm2
mulsd  %xmm1,%xmm2
mulsd  %xmm0,%xmm2
movsd  0x20(%rsp),%xmm0
addsd  %xmm2,%xmm0
movsd  0x8(%rsp),%xmm2
addsd  %xmm1,%xmm2
movsd  %xmm2,0x8(%rsp)
movsd  0x8(%rsp),%xmm1
add    $0x1,%r15
add    0x18(%rsp),%rbx
cmp    %r15,0x10(%rsp)
jne    1180 <compute_rms1+0x50>
divsd  %xmm1,%xmm0
jmp    11f8 <compute_rms1+0xc8>
movsd  0xe08(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1208 <compute_rms1+0xd8>
sqrtsd %xmm0,%xmm0
jmp    1211 <compute_rms1+0xe1>
call   1040 <sqrt@plt>
xorpd  %xmm1,%xmm1
movsd  %xmm0,0x8(%rsp)
xor    %edi,%edi
ucomisd %xmm1,%xmm0
setae  %dil
call   1030 <assert@plt>
movsd  0x8(%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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