#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 */
int bufferLength(struct CircularBuffer *);
double getBufferValue(struct CircularBuffer *, int);
double bufferSum(struct CircularBuffer *buf, int n) {
  int i = bufferLength(buf);
  if (i > n)
    i = n;
  double tot = 0.0;
  while (i) {
    i--;
    tot = tot + getBufferValue(buf, i);
  }
  return tot;
}


x86 assembly (clang, -O0):
<bufferSum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <bufferLength@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    114d <bufferSum+0x2d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1194 <bufferSum+0x74>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <getBufferValue@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1155 <bufferSum+0x35>
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufferSum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1030 <bufferLength@plt>
mov    %eax,%ebp
cmp    %r14d,%eax
cmovg  %r14d,%ebp
test   %ebp,%ebp
je     117d <bufferSum+0x5d>
add    $0xffffffff,%ebp
xorpd  %xmm0,%xmm0
nopw   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <getBufferValue@plt>
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0xffffffff,%ebp
jb     1150 <bufferSum+0x30>
jmp    1181 <bufferSum+0x61>
xorpd  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.CircularBuffer = type { i32 }
define double @bufferSum(%struct.CircularBuffer* %0, i32 %1) {
%3 = alloca %struct.CircularBuffer*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store %struct.CircularBuffer* %0, %struct.CircularBuffer** %3
store i32 %1, i32* %4
%7 = load %struct.CircularBuffer*, %struct.CircularBuffer** %3
%8 = call i32 @bufferLength(%struct.CircularBuffer* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %4
store i32 %13, i32* %5
br label %14
14: ; preds = %12, %2
store double 0.000000e+00, double* %6
br label %15
15: ; preds = %18, %14
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32, i32* %5
%20 = add nsw i32 %19, -1
store i32 %20, i32* %5
%21 = load double, double* %6
%22 = load %struct.CircularBuffer*, %struct.CircularBuffer** %3
%23 = load i32, i32* %5
%24 = call double @getBufferValue(%struct.CircularBuffer* %22, i32 %23)
%25 = fadd double %21, %24
store double %25, double* %6
br label %15
26: ; preds = %15
%27 = load double, double* %6
ret double %27
}

LLVM IR (-O2):
%struct.CircularBuffer = type { i32 }
define double @bufferSum(%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 eq i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %2, %7
%8 = phi double [ %12, %7 ], [ 0.000000e+00, %2 ]
%9 = phi i32 [ %10, %7 ], [ %5, %2 ]
%10 = add nsw i32 %9, -1
%11 = tail call double @getBufferValue(%struct.CircularBuffer* %0, i32 %10)
%12 = fadd double %8, %11
%13 = icmp eq i32 %10, 0
br i1 %13, label %14, label %7
14: ; preds = %7, %2
%15 = phi double [ 0.000000e+00, %2 ], [ %12, %7 ]
ret double %15
}