int MoveAvg8(int NewValue, int bufferNum) {
  static int RunSum[32] = {[0 ...(32 - 1)] = 0};
  static int Buffer[32][8] = {[0 ...(32 - 1)] = {0, 0, 0, 0, 0, 0, 0, 0}};
  static unsigned char Newest[32] = {[0 ...(32 - 1)] = 0};
  static unsigned char Oldest[32] = {[0 ...(32 - 1)] = 1};
  RunSum[bufferNum] =
      RunSum[bufferNum] - Buffer[bufferNum][Oldest[bufferNum]] + NewValue;
  Buffer[bufferNum][Newest[bufferNum]] = NewValue;
  Newest[bufferNum] = (Newest[bufferNum] + 1) & 0x07;
  Oldest[bufferNum] = (Oldest[bufferNum] + 1) & 0x07;
  return (RunSum[bufferNum] >> 3);
}


x86 assembly (clang, -O0):
<MoveAvg8>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq -0x8(%rbp),%rcx
lea    0x2f4b(%rip),%rax
mov    (%rax,%rcx,4),%edx
movslq -0x8(%rbp),%rcx
lea    0x2fbd(%rip),%rax
shl    $0x5,%rcx
add    %rcx,%rax
movslq -0x8(%rbp),%rsi
lea    0x2efb(%rip),%rcx
movzbl (%rcx,%rsi,1),%ecx
sub    (%rax,%rcx,4),%edx
add    -0x4(%rbp),%edx
movslq -0x8(%rbp),%rcx
lea    0x2f16(%rip),%rax
mov    %edx,(%rax,%rcx,4)
mov    -0x4(%rbp),%edx
movslq -0x8(%rbp),%rcx
lea    0x2f85(%rip),%rax
shl    $0x5,%rcx
add    %rcx,%rax
movslq -0x8(%rbp),%rsi
lea    0x3373(%rip),%rcx
movzbl (%rcx,%rsi,1),%ecx
mov    %edx,(%rax,%rcx,4)
movslq -0x8(%rbp),%rcx
lea    0x3361(%rip),%rax
movzbl (%rax,%rcx,1),%eax
add    $0x1,%eax
and    $0x7,%eax
mov    %al,%dl
movslq -0x8(%rbp),%rcx
lea    0x334a(%rip),%rax
mov    %dl,(%rax,%rcx,1)
movslq -0x8(%rbp),%rcx
lea    0x2e8c(%rip),%rax
movzbl (%rax,%rcx,1),%eax
add    $0x1,%eax
and    $0x7,%eax
mov    %al,%dl
movslq -0x8(%rbp),%rcx
lea    0x2e75(%rip),%rax
mov    %dl,(%rax,%rcx,1)
movslq -0x8(%rbp),%rcx
lea    0x2e97(%rip),%rax
mov    (%rax,%rcx,4),%eax
sar    $0x3,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<MoveAvg8>:
movslq %esi,%rsi
lea    0x2f56(%rip),%r9
mov    (%r9,%rsi,4),%eax
lea    0x2f1b(%rip),%r8
movzbl (%rsi,%r8,1),%r10d
mov    %rsi,%rcx
shl    $0x5,%rcx
lea    0x2fb8(%rip),%rdx
add    %rcx,%rdx
sub    (%rdx,%r10,4),%eax
add    %edi,%eax
mov    %eax,(%r9,%rsi,4)
lea    0x33a4(%rip),%r9
movzbl (%rsi,%r9,1),%ecx
mov    %edi,(%rdx,%rcx,4)
add    $0x1,%ecx
and    $0x7,%cl
mov    %cl,(%rsi,%r9,1)
lea    0x1(%r10),%ecx
and    $0x7,%cl
mov    %cl,(%rsi,%r8,1)
sar    $0x3,%eax
ret

LLVM IR (-O0):
@MoveAvg8.RunSum = internal global [32 x i32] zeroinitializer
@MoveAvg8.Buffer = internal global [32 x [8 x i32]] zeroinitializer
@MoveAvg8.Newest = internal global [32 x i8] zeroinitializer
@MoveAvg8.Oldest = internal global [32 x i8] c"\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01"
define i32 @MoveAvg8(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [32 x i32], [32 x i32]* @MoveAvg8.RunSum, i64 0, i64 %6
%8 = load i32, i32* %7
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [32 x [8 x i32]], [32 x [8 x i32]]* @MoveAvg8.Buffer, i64 0, i64 %10
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Oldest, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = zext i8 %15 to i64
%17 = getelementptr inbounds [8 x i32], [8 x i32]* %11, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = sub nsw i32 %8, %18
%20 = load i32, i32* %3
%21 = add nsw i32 %19, %20
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [32 x i32], [32 x i32]* @MoveAvg8.RunSum, i64 0, i64 %23
store i32 %21, i32* %24
%25 = load i32, i32* %3
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [32 x [8 x i32]], [32 x [8 x i32]]* @MoveAvg8.Buffer, i64 0, i64 %27
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Newest, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i64
%34 = getelementptr inbounds [8 x i32], [8 x i32]* %28, i64 0, i64 %33
store i32 %25, i32* %34
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Newest, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = add nsw i32 %39, 1
%41 = and i32 %40, 7
%42 = trunc i32 %41 to i8
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Newest, i64 0, i64 %44
store i8 %42, i8* %45
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Oldest, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = add nsw i32 %50, 1
%52 = and i32 %51, 7
%53 = trunc i32 %52 to i8
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Oldest, i64 0, i64 %55
store i8 %53, i8* %56
%57 = load i32, i32* %4
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [32 x i32], [32 x i32]* @MoveAvg8.RunSum, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = ashr i32 %60, 3
ret i32 %61
}

LLVM IR (-O2):
@MoveAvg8.RunSum = internal unnamed_addr global [32 x i32] zeroinitializer
@MoveAvg8.Buffer = internal unnamed_addr global [32 x [8 x i32]] zeroinitializer
@MoveAvg8.Newest = internal unnamed_addr global [32 x i8] zeroinitializer
@MoveAvg8.Oldest = internal unnamed_addr global [32 x i8] c"\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01"
define i32 @MoveAvg8(i32 %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds [32 x i32], [32 x i32]* @MoveAvg8.RunSum, i64 0, i64 %3
%5 = load i32, i32* %4
%6 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Oldest, i64 0, i64 %3
%7 = load i8, i8* %6
%8 = zext i8 %7 to i64
%9 = getelementptr inbounds [32 x [8 x i32]], [32 x [8 x i32]]* @MoveAvg8.Buffer, i64 0, i64 %3, i64 %8
%10 = load i32, i32* %9
%11 = sub nsw i32 %5, %10
%12 = add nsw i32 %11, %0
store i32 %12, i32* %4
%13 = getelementptr inbounds [32 x i8], [32 x i8]* @MoveAvg8.Newest, i64 0, i64 %3
%14 = load i8, i8* %13
%15 = zext i8 %14 to i64
%16 = getelementptr inbounds [32 x [8 x i32]], [32 x [8 x i32]]* @MoveAvg8.Buffer, i64 0, i64 %3, i64 %15
store i32 %0, i32* %16
%17 = add i8 %14, 1
%18 = and i8 %17, 7
store i8 %18, i8* %13
%19 = add i8 %7, 1
%20 = and i8 %19, 7
store i8 %20, i8* %6
%21 = ashr i32 %12, 3
ret i32 %21
}