#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 */
typedef int /*<<< orphan*/ Y;
typedef int /*<<< orphan*/ X;

/* Variables and functions */
double arithmetic_incremental(double, double, size_t);
int /*<<< orphan*/ assert(int);
int fabs(double);
int /*<<< orphan*/ randv(double *, int, size_t);
double variance(double *, double *, size_t);
void test_variance_incremental(void) {
  double X[1000], Y[1000];
  double xi, yi, ci;
  double xn, yn;
  double u, v;
  size_t i, n;
  for (n = 2; n <= 100; n++) {
    randv(X, (sizeof(X) / sizeof(X[0])), n);
    randv(Y, (sizeof(Y) / sizeof(Y[0])), n);
    xi = yi = ci = 0;
    for (i = 0; i < n; i++) {
      xn = arithmetic_incremental(xi, X[i], i + 1);
      yn = arithmetic_incremental(yi, Y[i], i + 1);
      ci += (X[i] - xi) * (Y[i] - yn);
      xi = xn;
      yi = yn;
    }
    u = variance(X, Y, n);
    v = ci / (n - 1);
    assert(fabs(u - v) < 1e-10);
  }
}


x86 assembly (clang, -O0):
<test_variance_incremental>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3ed0,%rsp
movq   $0x2,-0x3ec8(%rbp)
cmpq   $0x64,-0x3ec8(%rbp)
64
ja     1374 <test_variance_incremental+0x224>
lea    -0x1f40(%rbp),%rdi
mov    -0x3ec8(%rbp),%rdx
mov    $0x3e8,%esi
call   1030 <randv@plt>
lea    -0x3e80(%rbp),%rdi
mov    -0x3ec8(%rbp),%rdx
mov    $0x3e8,%esi
call   1030 <randv@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x3e98(%rbp)
ff
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x3e90(%rbp)
ff
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x3e88(%rbp)
ff
movq   $0x0,-0x3ec0(%rbp)
mov    -0x3ec0(%rbp),%rax
cmp    -0x3ec8(%rbp),%rax
jae    12c3 <test_variance_incremental+0x173>
movsd  -0x3e88(%rbp),%xmm0
ff
mov    -0x3ec0(%rbp),%rax
movsd  -0x1f40(%rbp,%rax,8),%xmm1
ff ff
mov    -0x3ec0(%rbp),%rdi
add    $0x1,%rdi
call   1070 <arithmetic_incremental@plt>
movsd  %xmm0,-0x3ea0(%rbp)
ff
movsd  -0x3e90(%rbp),%xmm0
ff
mov    -0x3ec0(%rbp),%rax
movsd  -0x3e80(%rbp,%rax,8),%xmm1
ff ff
mov    -0x3ec0(%rbp),%rdi
add    $0x1,%rdi
call   1070 <arithmetic_incremental@plt>
movsd  %xmm0,-0x3ea8(%rbp)
ff
mov    -0x3ec0(%rbp),%rax
movsd  -0x1f40(%rbp,%rax,8),%xmm0
ff ff
subsd  -0x3e88(%rbp),%xmm0
ff
mov    -0x3ec0(%rbp),%rax
movsd  -0x3e80(%rbp,%rax,8),%xmm2
ff ff
subsd  -0x3ea8(%rbp),%xmm2
ff
movsd  -0x3e98(%rbp),%xmm1
ff
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x3e98(%rbp)
ff
movsd  -0x3ea0(%rbp),%xmm0
ff
movsd  %xmm0,-0x3e88(%rbp)
ff
movsd  -0x3ea8(%rbp),%xmm0
ff
movsd  %xmm0,-0x3e90(%rbp)
ff
mov    -0x3ec0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x3ec0(%rbp)
jmp    11d0 <test_variance_incremental+0x80>
mov    -0x3ec8(%rbp),%rdx
lea    -0x1f40(%rbp),%rdi
lea    -0x3e80(%rbp),%rsi
call   1040 <variance@plt>
movsd  %xmm0,-0x3eb0(%rbp)
ff
movsd  -0x3e98(%rbp),%xmm0
ff
mov    -0x3ec8(%rbp),%rax
add    $0xffffffffffffffff,%rax
movq   %rax,%xmm1
movaps 0xd0c(%rip),%xmm2
punpckldq %xmm2,%xmm1
movapd 0xd10(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x3eb8(%rbp)
ff
movsd  -0x3eb0(%rbp),%xmm0
ff
subsd  -0x3eb8(%rbp),%xmm0
ff
call   1050 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xcb4(%rip),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x3ec8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x3ec8(%rbp)
jmp    1166 <test_variance_incremental+0x16>
add    $0x3ed0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_variance_incremental>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3ea0,%rsp
mov    $0x2,%r12d
mov    $0xfffffffffffffffe,%r13
lea    0x1f60(%rsp),%r14
lea    0x20(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x3e8,%esi
mov    %r12,%rdx
call   1030 <randv@plt>
mov    %r15,%rdi
mov    $0x3e8,%esi
mov    %r12,%rdx
call   1030 <randv@plt>
xorps  %xmm3,%xmm3
mov    $0x1,%ebx
xorps  %xmm2,%xmm2
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm2,0x8(%rsp)
movsd  %xmm3,0x10(%rsp)
movsd  0x1f58(%rsp,%rbx,8),%xmm1
movaps %xmm3,%xmm0
mov    %rbx,%rdi
call   1070 <arithmetic_incremental@plt>
movsd  %xmm0,0x18(%rsp)
movsd  0x18(%rsp,%rbx,8),%xmm1
movsd  0x8(%rsp),%xmm0
mov    %rbx,%rdi
call   1070 <arithmetic_incremental@plt>
movapd %xmm0,%xmm2
movsd  0x1f58(%rsp,%rbx,8),%xmm0
subsd  0x10(%rsp),%xmm0
movsd  0x18(%rsp,%rbx,8),%xmm1
subsd  %xmm2,%xmm1
mulsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
lea    (%rbx,%r13,1),%rax
add    $0x1,%rax
add    $0x1,%rbx
movsd  0x18(%rsp),%xmm3
cmp    $0x1,%rax
jne    11c0 <test_variance_incremental+0x70>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <variance@plt>
lea    -0x1(%r12),%rax
movq   %rax,%xmm1
punpckldq 0xd9b(%rip),%xmm1
subpd  0xda3(%rip),%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
movsd  (%rsp),%xmm1
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
call   1050 <fabs@plt>
xor    %edi,%edi
test   %eax,%eax
setle  %dil
call   1060 <assert@plt>
add    $0x1,%r12
add    $0xffffffffffffffff,%r13
cmp    $0x65,%r12
jne    1180 <test_variance_incremental+0x30>
add    $0x3ea0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @test_variance_incremental() {
%1 = alloca [1000 x double]
%2 = alloca [1000 x double]
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i64
%11 = alloca i64
store i64 2, i64* %11
br label %12
12: ; preds = %76, %0
%13 = load i64, i64* %11
%14 = icmp ule i64 %13, 100
br i1 %14, label %15, label %79
15: ; preds = %12
%16 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
%17 = load i64, i64* %11
%18 = call i32 @randv(double* %16, i32 1000, i64 %17)
%19 = getelementptr inbounds [1000 x double], [1000 x double]* %2, i64 0, i64 0
%20 = load i64, i64* %11
%21 = call i32 @randv(double* %19, i32 1000, i64 %20)
store double 0.000000e+00, double* %5
store double 0.000000e+00, double* %4
store double 0.000000e+00, double* %3
store i64 0, i64* %10
br label %22
22: ; preds = %55, %15
%23 = load i64, i64* %10
%24 = load i64, i64* %11
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %58
26: ; preds = %22
%27 = load double, double* %3
%28 = load i64, i64* %10
%29 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 %28
%30 = load double, double* %29
%31 = load i64, i64* %10
%32 = add i64 %31, 1
%33 = call double @arithmetic_incremental(double %27, double %30, i64 %32)
store double %33, double* %6
%34 = load double, double* %4
%35 = load i64, i64* %10
%36 = getelementptr inbounds [1000 x double], [1000 x double]* %2, i64 0, i64 %35
%37 = load double, double* %36
%38 = load i64, i64* %10
%39 = add i64 %38, 1
%40 = call double @arithmetic_incremental(double %34, double %37, i64 %39)
store double %40, double* %7
%41 = load i64, i64* %10
%42 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 %41
%43 = load double, double* %42
%44 = load double, double* %3
%45 = fsub double %43, %44
%46 = load i64, i64* %10
%47 = getelementptr inbounds [1000 x double], [1000 x double]* %2, i64 0, i64 %46
%48 = load double, double* %47
%49 = load double, double* %7
%50 = fsub double %48, %49
%51 = load double, double* %5
%52 = call double @llvm.fmuladd.f64(double %45, double %50, double %51)
store double %52, double* %5
%53 = load double, double* %6
store double %53, double* %3
%54 = load double, double* %7
store double %54, double* %4
br label %55
55: ; preds = %26
%56 = load i64, i64* %10
%57 = add i64 %56, 1
store i64 %57, i64* %10
br label %22
58: ; preds = %22
%59 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
%60 = getelementptr inbounds [1000 x double], [1000 x double]* %2, i64 0, i64 0
%61 = load i64, i64* %11
%62 = call double @variance(double* %59, double* %60, i64 %61)
store double %62, double* %8
%63 = load double, double* %5
%64 = load i64, i64* %11
%65 = sub i64 %64, 1
%66 = uitofp i64 %65 to double
%67 = fdiv double %63, %66
store double %67, double* %9
%68 = load double, double* %8
%69 = load double, double* %9
%70 = fsub double %68, %69
%71 = call i32 @fabs(double %70)
%72 = sitofp i32 %71 to double
%73 = fcmp olt double %72, 1.000000e-10
%74 = zext i1 %73 to i32
%75 = call i32 @assert(i32 %74)
br label %76
76: ; preds = %58
%77 = load i64, i64* %11
%78 = add i64 %77, 1
store i64 %78, i64* %11
br label %12
79: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @test_variance_incremental() {
%1 = alloca [1000 x double]
%2 = alloca [1000 x double]
%3 = bitcast [1000 x double]* %1 to i8*
%4 = bitcast [1000 x double]* %2 to i8*
%5 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
%6 = getelementptr inbounds [1000 x double], [1000 x double]* %2, i64 0, i64 0
br label %7
7: ; preds = %29, %0
%8 = phi i64 [ 2, %0 ], [ %39, %29 ]
%9 = call i32 @randv(double* %5, i32 1000, i64 %8)
%10 = call i32 @randv(double* %6, i32 1000, i64 %8)
br label %11
11: ; preds = %7, %11
%12 = phi i64 [ %18, %11 ], [ 0, %7 ]
%13 = phi double [ %19, %11 ], [ 0.000000e+00, %7 ]
%14 = phi double [ %22, %11 ], [ 0.000000e+00, %7 ]
%15 = phi double [ %27, %11 ], [ 0.000000e+00, %7 ]
%16 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 %12
%17 = load double, double* %16
%18 = add nuw nsw i64 %12, 1
%19 = call double @arithmetic_incremental(double %13, double %17, i64 %18)
%20 = getelementptr inbounds [1000 x double], [1000 x double]* %2, i64 0, i64 %12
%21 = load double, double* %20
%22 = call double @arithmetic_incremental(double %14, double %21, i64 %18)
%23 = load double, double* %16
%24 = fsub double %23, %13
%25 = load double, double* %20
%26 = fsub double %25, %22
%27 = call double @llvm.fmuladd.f64(double %24, double %26, double %15)
%28 = icmp eq i64 %18, %8
br i1 %28, label %29, label %11
29: ; preds = %11
%30 = call double @variance(double* %5, double* %6, i64 %8)
%31 = add nsw i64 %8, -1
%32 = uitofp i64 %31 to double
%33 = fdiv double %27, %32
%34 = fsub double %30, %33
%35 = call i32 @fabs(double %34)
%36 = icmp slt i32 %35, 1
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = add nuw nsw i64 %8, 1
%40 = icmp eq i64 %39, 101
br i1 %40, label %41, label %7
41: ; preds = %29
ret void
}