#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*/ v;

/* Variables and functions */
double arithmetic(double *, int);
double arithmetic_incremental(double, double, size_t);
int /*<<< orphan*/ assert(int);
int fabs(double);
int /*<<< orphan*/ randv(double *, int, size_t);
void test_arithmetic_incremental(void) {
  double v[1000];
  double x, y;
  size_t i, n;
  for (n = 0; n < 10000; n++) {
    randv(v, (sizeof(v) / sizeof(v[0])), n);
    x = arithmetic(v, (sizeof(v) / sizeof(v[0])));
    y = v[0];
    for (i = 1; i < (sizeof(v) / sizeof(v[0])); i++)
      y = arithmetic_incremental(y, v[i], i + 1);
    assert(fabs(x - y) < 1e-10);
  }
}


x86 assembly (clang, -O0):
<test_arithmetic_incremental>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f60,%rsp
movq   $0x0,-0x1f60(%rbp)
cmpq   $0x2710,-0x1f60(%rbp)
jae    1264 <test_arithmetic_incremental+0x114>
lea    -0x1f40(%rbp),%rdi
mov    -0x1f60(%rbp),%rdx
mov    $0x3e8,%esi
call   1030 <randv@plt>
lea    -0x1f40(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <arithmetic@plt>
movsd  %xmm0,-0x1f48(%rbp)
ff
movsd  -0x1f40(%rbp),%xmm0
ff
movsd  %xmm0,-0x1f50(%rbp)
ff
movq   $0x1,-0x1f58(%rbp)
cmpq   $0x3e8,-0x1f58(%rbp)
jae    121b <test_arithmetic_incremental+0xcb>
movsd  -0x1f50(%rbp),%xmm0
ff
mov    -0x1f58(%rbp),%rax
movsd  -0x1f40(%rbp,%rax,8),%xmm1
ff ff
mov    -0x1f58(%rbp),%rdi
add    $0x1,%rdi
call   1070 <arithmetic_incremental@plt>
movsd  %xmm0,-0x1f50(%rbp)
ff
mov    -0x1f58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1f58(%rbp)
jmp    11c3 <test_arithmetic_incremental+0x73>
movsd  -0x1f48(%rbp),%xmm0
ff
subsd  -0x1f50(%rbp),%xmm0
ff
call   1050 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xdc4(%rip),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x1f60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1f60(%rbp)
jmp    1166 <test_arithmetic_incremental+0x16>
add    $0x1f60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_arithmetic_incremental>:
push   %r15
push   %r14
push   %rbx
sub    $0x1f50,%rsp
xor    %r15d,%r15d
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    $0x3e8,%esi
mov    %r15,%rdx
call   1030 <randv@plt>
mov    %r14,%rdi
mov    $0x3e8,%esi
call   1040 <arithmetic@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x2,%ebx
movsd  0x10(%rsp),%xmm0
xchg   %ax,%ax
movsd  0x8(%rsp,%rbx,8),%xmm1
mov    %rbx,%rdi
call   1070 <arithmetic_incremental@plt>
add    $0x1,%rbx
cmp    $0x3e9,%rbx
jne    11a0 <test_arithmetic_incremental+0x50>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <fabs@plt>
xor    %edi,%edi
test   %eax,%eax
setle  %dil
call   1060 <assert@plt>
add    $0x1,%r15
cmp    $0x2710,%r15
jne    1170 <test_arithmetic_incremental+0x20>
add    $0x1f50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @test_arithmetic_incremental() {
%1 = alloca [1000 x double]
%2 = alloca double
%3 = alloca double
%4 = alloca i64
%5 = alloca i64
store i64 0, i64* %5
br label %6
6: ; preds = %40, %0
%7 = load i64, i64* %5
%8 = icmp ult i64 %7, 10000
br i1 %8, label %9, label %43
9: ; preds = %6
%10 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
%11 = load i64, i64* %5
%12 = call i32 @randv(double* %10, i32 1000, i64 %11)
%13 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
%14 = call double @arithmetic(double* %13, i32 1000)
store double %14, double* %2
%15 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
%16 = load double, double* %15
store double %16, double* %3
store i64 1, i64* %4
br label %17
17: ; preds = %28, %9
%18 = load i64, i64* %4
%19 = icmp ult i64 %18, 1000
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load double, double* %3
%22 = load i64, i64* %4
%23 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 %22
%24 = load double, double* %23
%25 = load i64, i64* %4
%26 = add i64 %25, 1
%27 = call double @arithmetic_incremental(double %21, double %24, i64 %26)
store double %27, double* %3
br label %28
28: ; preds = %20
%29 = load i64, i64* %4
%30 = add i64 %29, 1
store i64 %30, i64* %4
br label %17
31: ; preds = %17
%32 = load double, double* %2
%33 = load double, double* %3
%34 = fsub double %32, %33
%35 = call i32 @fabs(double %34)
%36 = sitofp i32 %35 to double
%37 = fcmp olt double %36, 1.000000e-10
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
br label %40
40: ; preds = %31
%41 = load i64, i64* %5
%42 = add i64 %41, 1
store i64 %42, i64* %5
br label %6
43: ; preds = %6
ret void
}

LLVM IR (-O2):
define void @test_arithmetic_incremental() {
%1 = alloca [1000 x double]
%2 = bitcast [1000 x double]* %1 to i8*
%3 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 0
br label %4
4: ; preds = %0, %17
%5 = phi i64 [ 0, %0 ], [ %23, %17 ]
%6 = call i32 @randv(double* %3, i32 1000, i64 %5)
%7 = call double @arithmetic(double* %3, i32 1000)
%8 = load double, double* %3
br label %9
9: ; preds = %4, %9
%10 = phi i64 [ 1, %4 ], [ %14, %9 ]
%11 = phi double [ %8, %4 ], [ %15, %9 ]
%12 = getelementptr inbounds [1000 x double], [1000 x double]* %1, i64 0, i64 %10
%13 = load double, double* %12
%14 = add nuw nsw i64 %10, 1
%15 = call double @arithmetic_incremental(double %11, double %13, i64 %14)
%16 = icmp eq i64 %14, 1000
br i1 %16, label %17, label %9
17: ; preds = %9
%18 = fsub double %7, %15
%19 = call i32 @fabs(double %18)
%20 = icmp slt i32 %19, 1
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = add nuw nsw i64 %5, 1
%24 = icmp eq i64 %23, 10000
br i1 %24, label %25, label %4
25: ; preds = %17
ret void
}