#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 */
struct CircularBuffer {
  int dummy;
};

/* Variables and functions */
double bufferAverage(struct CircularBuffer *, int);
int bufferLength(struct CircularBuffer *);
double getBufferValue(struct CircularBuffer *, int);
double sqrt(double);
double bufferStddev(struct CircularBuffer *buf, int n) {
  double res = 0.0;
  int i = bufferLength(buf);
  if (i > n)
    i = n;
  if (n > i)
    n = i;
  if (n <= 1)
    return 0.0;
  {
    double avg = bufferAverage(buf, n);
    do {
      double val;
      i--;
      val = getBufferValue(buf, i) - avg;
      res = res + val * val;
    } while (i > 0);
  }
  return sqrt(res / (double)(n - 1));
}


x86 assembly (clang, -O0):
<bufferStddev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <bufferLength@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1175 <bufferStddev+0x35>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    1187 <bufferStddev+0x47>
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jg     119e <bufferStddev+0x5e>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1211 <bufferStddev+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <bufferAverage@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <getBufferValue@plt>
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11af <bufferStddev+0x6f>
movsd  -0x20(%rbp),%xmm0
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
call   1060 <sqrt@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufferStddev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <bufferLength@plt>
mov    %eax,%r14d
cmp    %ebp,%eax
cmovg  %ebp,%r14d
xorpd  %xmm0,%xmm0
cmp    $0x2,%r14d
jl     11cd <bufferStddev+0x8d>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <bufferAverage@plt>
movsd  %xmm0,(%rsp)
lea    0x1(%r14),%ebp
xorpd  %xmm1,%xmm1
nopl   (%rax)
movsd  %xmm1,0x8(%rsp)
lea    -0x2(%rbp),%esi
mov    %rbx,%rdi
call   1050 <getBufferValue@plt>
movsd  0x8(%rsp),%xmm1
subsd  (%rsp),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm0,%xmm1
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1180 <bufferStddev+0x40>
add    $0xffffffff,%r14d
xorps  %xmm0,%xmm0
cvtsi2sd %r14d,%xmm0
divsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jb     11d6 <bufferStddev+0x96>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <sqrt@plt>

LLVM IR (-O0):
%struct.CircularBuffer = type { i32 }
define double @bufferStddev(%struct.CircularBuffer* %0, i32 %1) {
%3 = alloca double
%4 = alloca %struct.CircularBuffer*
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
%8 = alloca double
%9 = alloca double
store %struct.CircularBuffer* %0, %struct.CircularBuffer** %4
store i32 %1, i32* %5
store double 0.000000e+00, double* %6
%10 = load %struct.CircularBuffer*, %struct.CircularBuffer** %4
%11 = call i32 @bufferLength(%struct.CircularBuffer* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = icmp sgt i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* %5
store i32 %16, i32* %7
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %5
%19 = load i32, i32* %7
%20 = icmp sgt i32 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* %7
store i32 %22, i32* %5
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* %5
%25 = icmp sle i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %23
store double 0.000000e+00, double* %3
br label %53
27: ; preds = %23
%28 = load %struct.CircularBuffer*, %struct.CircularBuffer** %4
%29 = load i32, i32* %5
%30 = call double @bufferAverage(%struct.CircularBuffer* %28, i32 %29)
store double %30, double* %8
br label %31
31: ; preds = %43, %27
%32 = load i32, i32* %7
%33 = add nsw i32 %32, -1
store i32 %33, i32* %7
%34 = load %struct.CircularBuffer*, %struct.CircularBuffer** %4
%35 = load i32, i32* %7
%36 = call double @getBufferValue(%struct.CircularBuffer* %34, i32 %35)
%37 = load double, double* %8
%38 = fsub double %36, %37
store double %38, double* %9
%39 = load double, double* %6
%40 = load double, double* %9
%41 = load double, double* %9
%42 = call double @llvm.fmuladd.f64(double %40, double %41, double %39)
store double %42, double* %6
br label %43
43: ; preds = %31
%44 = load i32, i32* %7
%45 = icmp sgt i32 %44, 0
br i1 %45, label %31, label %46
46: ; preds = %43
%47 = load double, double* %6
%48 = load i32, i32* %5
%49 = sub nsw i32 %48, 1
%50 = sitofp i32 %49 to double
%51 = fdiv double %47, %50
%52 = call double @sqrt(double %51)
store double %52, double* %3
br label %53
53: ; preds = %46, %26
%54 = load double, double* %3
ret double %54
}

LLVM IR (-O2):
%struct.CircularBuffer = type { i32 }
define double @bufferStddev(%struct.CircularBuffer* %0, i32 %1) {
%3 = tail call i32 @bufferLength(%struct.CircularBuffer* %0)
%4 = icmp sgt i32 %3, %1
%5 = select i1 %4, i32 %1, i32 %3
%6 = icmp slt i32 %5, 2
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = tail call double @bufferAverage(%struct.CircularBuffer* %0, i32 %5)
br label %9
9: ; preds = %9, %7
%10 = phi double [ 0.000000e+00, %7 ], [ %15, %9 ]
%11 = phi i32 [ %5, %7 ], [ %12, %9 ]
%12 = add nsw i32 %11, -1
%13 = tail call double @getBufferValue(%struct.CircularBuffer* %0, i32 %12)
%14 = fsub double %13, %8
%15 = tail call double @llvm.fmuladd.f64(double %14, double %14, double %10)
%16 = icmp ugt i32 %11, 1
br i1 %16, label %9, label %17
17: ; preds = %9
%18 = add nsw i32 %5, -1
%19 = sitofp i32 %18 to double
%20 = fdiv double %15, %19
%21 = tail call double @sqrt(double %20)
br label %22
22: ; preds = %2, %17
%23 = phi double [ %21, %17 ], [ 0.000000e+00, %2 ]
ret double %23
}