double mlr_get_var(unsigned long long n, double sum, double sum2) {
  double mean = sum / n;
  double numerator = sum2 - 2.0 * mean * sum + n * mean * mean;
  if (numerator < 0.0)
    numerator = 0.0;
  double denominator = n - 1LL;
  return numerator / denominator;
}


x86 assembly (clang, -O0):
<mlr_get_var>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movq   -0x8(%rbp),%xmm1
movaps 0xedd(%rip),%xmm3
punpckldq %xmm3,%xmm1
movapd 0xee1(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm4
unpckhpd %xmm1,%xmm1
addsd  %xmm4,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  -0x10(%rbp),%xmm4
mulsd  %xmm4,%xmm0
subsd  %xmm0,%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm3,%xmm0
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm2
unpckhpd %xmm0,%xmm0
addsd  %xmm2,%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    11a7 <mlr_get_var+0xa7>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
movq   %rax,%xmm0
movaps 0xe45(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xe49(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
divsd  -0x30(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<mlr_get_var>:
movq   %rdi,%xmm2
movdqa 0xef3(%rip),%xmm3
punpckldq %xmm3,%xmm2
movapd 0xef7(%rip),%xmm4
subpd  %xmm4,%xmm2
movapd %xmm2,%xmm5
unpckhpd %xmm2,%xmm5
addsd  %xmm2,%xmm5
movapd %xmm0,%xmm2
divsd  %xmm5,%xmm2
movsd  0xee7(%rip),%xmm6
mulsd  %xmm2,%xmm6
mulsd  %xmm0,%xmm6
addsd  %xmm1,%xmm6
mulsd  %xmm2,%xmm5
mulsd  %xmm2,%xmm5
addsd  %xmm6,%xmm5
xorpd  %xmm0,%xmm0
maxsd  %xmm5,%xmm0
add    $0xffffffffffffffff,%rdi
movq   %rdi,%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm4,%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @mlr_get_var(i64 %0, double %1, double %2) {
%4 = alloca i64
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
store i64 %0, i64* %4
store double %1, double* %5
store double %2, double* %6
%10 = load double, double* %5
%11 = load i64, i64* %4
%12 = uitofp i64 %11 to double
%13 = fdiv double %10, %12
store double %13, double* %7
%14 = load double, double* %6
%15 = load double, double* %7
%16 = fmul double 2.000000e+00, %15
%17 = load double, double* %5
%18 = fneg double %16
%19 = call double @llvm.fmuladd.f64(double %18, double %17, double %14)
%20 = load i64, i64* %4
%21 = uitofp i64 %20 to double
%22 = load double, double* %7
%23 = fmul double %21, %22
%24 = load double, double* %7
%25 = call double @llvm.fmuladd.f64(double %23, double %24, double %19)
store double %25, double* %8
%26 = load double, double* %8
%27 = fcmp olt double %26, 0.000000e+00
br i1 %27, label %28, label %29
28: ; preds = %3
store double 0.000000e+00, double* %8
br label %29
29: ; preds = %28, %3
%30 = load i64, i64* %4
%31 = sub i64 %30, 1
%32 = uitofp i64 %31 to double
store double %32, double* %9
%33 = load double, double* %8
%34 = load double, double* %9
%35 = fdiv double %33, %34
ret double %35
}

LLVM IR (-O2):
define double @mlr_get_var(i64 %0, double %1, double %2) {
%4 = uitofp i64 %0 to double
%5 = fdiv double %1, %4
%6 = fmul double %5, -2.000000e+00
%7 = tail call double @llvm.fmuladd.f64(double %6, double %1, double %2)
%8 = fmul double %5, %4
%9 = tail call double @llvm.fmuladd.f64(double %8, double %5, double %7)
%10 = fcmp olt double %9, 0.000000e+00
%11 = select i1 %10, double 0.000000e+00, double %9
%12 = add i64 %0, -1
%13 = uitofp i64 %12 to double
%14 = fdiv double %11, %13
ret double %14
}