#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 mean(double *, double);
double samplevar(double *x, double *p, double n) {
  int i;
  double m, r, sum = 0;
  m = mean(x, n);
  for (i = 0; i < n; i++) {
    r = x[i] - m;
    sum += (r * r) * p[i];
  }
  return sum / n;
}


x86 assembly (clang, -O0):
<samplevar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
movsd  -0x18(%rbp),%xmm0
call   1030 <mean@plt>
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a9 <samplevar+0x99>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1147 <samplevar+0x37>
movsd  -0x38(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<samplevar>:
push   %r14
push   %rbx
push   %rax
movsd  %xmm0,(%rsp)
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <mean@plt>
movsd  (%rsp),%xmm3
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm3
jbe    1168 <samplevar+0x58>
xorpd  %xmm0,%xmm0
xor    %eax,%eax
nopl   (%rax)
movsd  (%rbx,%rax,8),%xmm2
subsd  %xmm1,%xmm2
mulsd  %xmm2,%xmm2
mulsd  (%r14,%rax,8),%xmm2
addsd  %xmm2,%xmm0
add    $0x1,%rax
xorps  %xmm2,%xmm2
cvtsi2sd %eax,%xmm2
ucomisd %xmm2,%xmm3
ja     1140 <samplevar+0x30>
divsd  %xmm3,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @samplevar(double* %0, double* %1, double %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca double
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double* %0, double** %4
store double* %1, double** %5
store double %2, double* %6
store double 0.000000e+00, double* %10
%11 = load double*, double** %4
%12 = load double, double* %6
%13 = call double @mean(double* %11, double %12)
store double %13, double* %8
store i32 0, i32* %7
br label %14
14: ; preds = %37, %3
%15 = load i32, i32* %7
%16 = sitofp i32 %15 to double
%17 = load double, double* %6
%18 = fcmp olt double %16, %17
br i1 %18, label %19, label %40
19: ; preds = %14
%20 = load double*, double** %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = load double, double* %23
%25 = load double, double* %8
%26 = fsub double %24, %25
store double %26, double* %9
%27 = load double, double* %9
%28 = load double, double* %9
%29 = fmul double %27, %28
%30 = load double*, double** %5
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds double, double* %30, i64 %32
%34 = load double, double* %33
%35 = load double, double* %10
%36 = call double @llvm.fmuladd.f64(double %29, double %34, double %35)
store double %36, double* %10
br label %37
37: ; preds = %19
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %14
40: ; preds = %14
%41 = load double, double* %10
%42 = load double, double* %6
%43 = fdiv double %41, %42
ret double %43
}

LLVM IR (-O2):
define double @samplevar(double* %0, double* nocapture %1, double %2) {
%4 = tail call double @mean(double* %0, double %2)
%5 = fcmp ogt double %2, 0.000000e+00
br i1 %5, label %6, label %20
6: ; preds = %3, %6
%7 = phi i64 [ %16, %6 ], [ 0, %3 ]
%8 = phi double [ %15, %6 ], [ 0.000000e+00, %3 ]
%9 = getelementptr inbounds double, double* %0, i64 %7
%10 = load double, double* %9
%11 = fsub double %10, %4
%12 = fmul double %11, %11
%13 = getelementptr inbounds double, double* %1, i64 %7
%14 = load double, double* %13
%15 = tail call double @llvm.fmuladd.f64(double %12, double %14, double %8)
%16 = add nuw i64 %7, 1
%17 = trunc i64 %16 to i32
%18 = sitofp i32 %17 to double
%19 = fcmp olt double %18, %2
br i1 %19, label %6, label %20
20: ; preds = %6, %3
%21 = phi double [ 0.000000e+00, %3 ], [ %15, %6 ]
%22 = fdiv double %21, %2
ret double %22
}