#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 *);
int bufferSum(struct CircularBuffer *, int);
double bufferAverage(struct CircularBuffer *buf, int n) {
  int l = bufferLength(buf);
  if (n > l)
    n = l;
  return (n == 0) ? n : bufferSum(buf, n) / n;
}


x86 assembly (clang, -O0):
<bufferAverage>:
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    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    114d <bufferAverage+0x2d>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1162 <bufferAverage+0x42>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1175 <bufferAverage+0x55>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <bufferSum@plt>
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufferAverage>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <bufferLength@plt>
cmp    %ebx,%eax
cmovl  %eax,%ebx
test   %ebx,%ebx
je     114a <bufferAverage+0x2a>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <bufferSum@plt>
cltd
idiv   %ebx
cvtsi2sd %eax,%xmm0
jmp    114e <bufferAverage+0x2e>
xorpd  %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.CircularBuffer = type { i32 }
define double @bufferAverage(%struct.CircularBuffer* %0, i32 %1) {
%3 = alloca %struct.CircularBuffer*
%4 = alloca i32
%5 = alloca i32
store %struct.CircularBuffer* %0, %struct.CircularBuffer** %3
store i32 %1, i32* %4
%6 = load %struct.CircularBuffer*, %struct.CircularBuffer** %3
%7 = call i32 @bufferLength(%struct.CircularBuffer* %6)
store i32 %7, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = icmp sgt i32 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* %5
store i32 %12, i32* %4
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i32, i32* %4
br label %24
18: ; preds = %13
%19 = load %struct.CircularBuffer*, %struct.CircularBuffer** %3
%20 = load i32, i32* %4
%21 = call i32 @bufferSum(%struct.CircularBuffer* %19, i32 %20)
%22 = load i32, i32* %4
%23 = sdiv i32 %21, %22
br label %24
24: ; preds = %18, %16
%25 = phi i32 [ %17, %16 ], [ %23, %18 ]
%26 = sitofp i32 %25 to double
ret double %26
}

LLVM IR (-O2):
%struct.CircularBuffer = type { i32 }
define double @bufferAverage(%struct.CircularBuffer* %0, i32 %1) {
%3 = tail call i32 @bufferLength(%struct.CircularBuffer* %0)
%4 = icmp slt i32 %3, %1
%5 = select i1 %4, i32 %3, i32 %1
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = tail call i32 @bufferSum(%struct.CircularBuffer* %0, i32 %5)
%9 = sdiv i32 %8, %5
%10 = sitofp i32 %9 to double
br label %11
11: ; preds = %2, %7
%12 = phi double [ %10, %7 ], [ 0.000000e+00, %2 ]
ret double %12
}