#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 size_t uint32_t;

/* Variables and functions */
double mean(double *A, uint32_t nel) {
  double mean = 0;
  for (uint32_t i = 0; i < nel; i++) {
    mean = (A[i] + (mean * i)) / (double)(i + 1);
  }
  return mean;
}


x86 assembly (clang, -O0):
<mean>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a1 <mean+0xa1>
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
movsd  (%rcx,%rax,8),%xmm1
movsd  -0x18(%rbp),%xmm0
movq   %rax,%xmm4
movaps 0xeb8(%rip),%xmm3
punpckldq %xmm3,%xmm4
movapd 0xebc(%rip),%xmm2
subpd  %xmm2,%xmm4
movaps %xmm4,%xmm5
unpckhpd %xmm4,%xmm4
addsd  %xmm5,%xmm4
mulsd  %xmm4,%xmm0
addsd  %xmm1,%xmm0
add    $0x1,%rax
movq   %rax,%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    111c <mean+0x1c>
movsd  -0x18(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<mean>:
test   %rsi,%rsi
je     1116 <mean+0x16>
cmp    $0x1,%rsi
jne    111a <mean+0x1a>
xorpd  %xmm0,%xmm0
xor    %eax,%eax
jmp    11b7 <mean+0xb7>
xorps  %xmm0,%xmm0
ret
mov    %rsi,%rcx
and    $0xfffffffffffffffe,%rcx
xorpd  %xmm0,%xmm0
xor    %eax,%eax
movdqa 0xed1(%rip),%xmm1
movapd 0xed9(%rip),%xmm2
nopw   0x0(%rax,%rax,1)
movq   %rax,%xmm4
punpckldq %xmm1,%xmm4
subpd  %xmm2,%xmm4
movapd %xmm4,%xmm3
unpckhpd %xmm4,%xmm3
addsd  %xmm4,%xmm3
mulsd  %xmm0,%xmm3
addsd  (%rdi,%rax,8),%xmm3
lea    0x1(%rax),%rdx
movq   %rdx,%xmm0
punpckldq %xmm1,%xmm0
subpd  %xmm2,%xmm0
movapd %xmm0,%xmm4
unpckhpd %xmm0,%xmm4
addsd  %xmm0,%xmm4
divsd  %xmm4,%xmm3
mulsd  %xmm4,%xmm3
addsd  0x8(%rdi,%rax,8),%xmm3
add    $0x2,%rax
movq   %rax,%xmm0
punpckldq %xmm1,%xmm0
subpd  %xmm2,%xmm0
movapd %xmm0,%xmm4
unpckhpd %xmm0,%xmm4
addsd  %xmm0,%xmm4
movapd %xmm3,%xmm0
divsd  %xmm4,%xmm0
cmp    %rcx,%rax
jne    1140 <mean+0x40>
test   $0x1,%sil
je     1210 <mean+0x110>
movq   %rax,%xmm1
movdqa 0xe36(%rip),%xmm2
punpckldq %xmm2,%xmm1
movapd 0xe3a(%rip),%xmm3
subpd  %xmm3,%xmm1
movapd %xmm1,%xmm4
unpckhpd %xmm1,%xmm4
addsd  %xmm1,%xmm4
mulsd  %xmm4,%xmm0
addsd  (%rdi,%rax,8),%xmm0
add    $0x1,%rax
movq   %rax,%xmm1
punpckldq %xmm2,%xmm1
subpd  %xmm3,%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @mean(double* %0, i64 %1) {
%3 = alloca double*
%4 = alloca i64
%5 = alloca double
%6 = alloca i64
store double* %0, double** %3
store i64 %1, i64* %4
store double 0.000000e+00, double* %5
store i64 0, i64* %6
br label %7
7: ; preds = %24, %2
%8 = load i64, i64* %6
%9 = load i64, i64* %4
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = load double*, double** %3
%13 = load i64, i64* %6
%14 = getelementptr inbounds double, double* %12, i64 %13
%15 = load double, double* %14
%16 = load double, double* %5
%17 = load i64, i64* %6
%18 = uitofp i64 %17 to double
%19 = call double @llvm.fmuladd.f64(double %16, double %18, double %15)
%20 = load i64, i64* %6
%21 = add i64 %20, 1
%22 = uitofp i64 %21 to double
%23 = fdiv double %19, %22
store double %23, double* %5
br label %24
24: ; preds = %11
%25 = load i64, i64* %6
%26 = add i64 %25, 1
store i64 %26, i64* %6
br label %7
27: ; preds = %7
%28 = load double, double* %5
ret double %28
}

LLVM IR (-O2):
define double @mean(double* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %22, label %4
4: ; preds = %2
%5 = and i64 %1, 1
%6 = icmp eq i64 %1, 1
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = and i64 %1, -2
br label %24
9: ; preds = %24, %4
%10 = phi double [ undef, %4 ], [ %41, %24 ]
%11 = phi i64 [ 0, %4 ], [ %39, %24 ]
%12 = phi double [ 0.000000e+00, %4 ], [ %41, %24 ]
%13 = icmp eq i64 %5, 0
br i1 %13, label %22, label %14
14: ; preds = %9
%15 = getelementptr inbounds double, double* %0, i64 %11
%16 = load double, double* %15
%17 = uitofp i64 %11 to double
%18 = tail call double @llvm.fmuladd.f64(double %12, double %17, double %16)
%19 = add nuw i64 %11, 1
%20 = uitofp i64 %19 to double
%21 = fdiv double %18, %20
br label %22
22: ; preds = %14, %9, %2
%23 = phi double [ 0.000000e+00, %2 ], [ %10, %9 ], [ %21, %14 ]
ret double %23
24: ; preds = %24, %7
%25 = phi i64 [ 0, %7 ], [ %39, %24 ]
%26 = phi double [ 0.000000e+00, %7 ], [ %41, %24 ]
%27 = phi i64 [ 0, %7 ], [ %42, %24 ]
%28 = getelementptr inbounds double, double* %0, i64 %25
%29 = load double, double* %28
%30 = uitofp i64 %25 to double
%31 = tail call double @llvm.fmuladd.f64(double %26, double %30, double %29)
%32 = or i64 %25, 1
%33 = uitofp i64 %32 to double
%34 = fdiv double %31, %33
%35 = getelementptr inbounds double, double* %0, i64 %32
%36 = load double, double* %35
%37 = uitofp i64 %32 to double
%38 = tail call double @llvm.fmuladd.f64(double %34, double %37, double %36)
%39 = add nuw i64 %25, 2
%40 = uitofp i64 %39 to double
%41 = fdiv double %38, %40
%42 = add i64 %27, 2
%43 = icmp eq i64 %42, %8
br i1 %43, label %9, label %24
}