#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 */

/* Variables and functions */
int getHC_SR04Measure();
int getHC_SR04Measure_smooth() {
  int tooHigh = 0;
  int tooLow = 0;
  long sum = 0;
  int measures[30];
  int ValidSamples = 0;
  for (int i = 0; i < 30; i++) {
    measures[i] = getHC_SR04Measure();
    if (measures[i] == -1) {
      tooLow++;
    } else if (measures[i] == 999) {
      tooHigh++;
    } else {
      ValidSamples++;
      sum = sum + measures[i];
    }
  }
  if (ValidSamples > (30 % 5)) {
    return sum / ValidSamples;
  } else if (tooLow > tooHigh) {
    return -1;
  } else {
    return 999;
  }
}


x86 assembly (clang, -O0):
<getHC_SR04Measure_smooth>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
cmpl   $0x1e,-0x98(%rbp)
jge    11f9 <getHC_SR04Measure_smooth+0xe9>
mov    $0x0,%al
call   1030 <getHC_SR04Measure@plt>
mov    %eax,%ecx
movslq -0x98(%rbp),%rax
mov    %ecx,-0x90(%rbp,%rax,4)
movslq -0x98(%rbp),%rax
cmpl   $0xffffffff,-0x90(%rbp,%rax,4)
ff
jne    118c <getHC_SR04Measure_smooth+0x7c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e0 <getHC_SR04Measure_smooth+0xd0>
movslq -0x98(%rbp),%rax
cmpl   $0x3e7,-0x90(%rbp,%rax,4)
jne    11b2 <getHC_SR04Measure_smooth+0xa2>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11db <getHC_SR04Measure_smooth+0xcb>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x98(%rbp),%rcx
movslq -0x90(%rbp,%rcx,4),%rcx
ff
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11e0 <getHC_SR04Measure_smooth+0xd0>
jmp    11e5 <getHC_SR04Measure_smooth+0xd5>
mov    -0x98(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x98(%rbp)
jmp    1145 <getHC_SR04Measure_smooth+0x35>
cmpl   $0x0,-0x94(%rbp)
jle    121e <getHC_SR04Measure_smooth+0x10e>
mov    -0x18(%rbp),%rax
movslq -0x94(%rbp),%rcx
cqto
idiv   %rcx
mov    %eax,-0x4(%rbp)
jmp    123d <getHC_SR04Measure_smooth+0x12d>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    1236 <getHC_SR04Measure_smooth+0x126>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <getHC_SR04Measure_smooth+0x12d>
movl   $0x3e7,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHC_SR04Measure_smooth>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x1e,%ebx
xor    %ebp,%ebp
xor    %r14d,%r14d
xor    %r15d,%r15d
xor    %r12d,%r12d
jmp    113d <getHC_SR04Measure_smooth+0x2d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cltq
add    %rax,%r14
add    $0xffffffff,%ebx
je     1166 <getHC_SR04Measure_smooth+0x56>
xor    %eax,%eax
call   1030 <getHC_SR04Measure@plt>
cmp    $0x3e7,%eax
je     1160 <getHC_SR04Measure_smooth+0x50>
cmp    $0xffffffff,%eax
jne    1130 <getHC_SR04Measure_smooth+0x20>
add    $0x1,%r15d
jmp    1138 <getHC_SR04Measure_smooth+0x28>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r12d
jmp    1138 <getHC_SR04Measure_smooth+0x28>
test   %ebp,%ebp
jle    117f <getHC_SR04Measure_smooth+0x6f>
mov    %ebp,%ecx
mov    %r14,%rax
shr    $0x20,%rax
je     1191 <getHC_SR04Measure_smooth+0x81>
mov    %r14,%rax
cqto
idiv   %rcx
jmp    1198 <getHC_SR04Measure_smooth+0x88>
cmp    %r12d,%r15d
mov    $0xffffffff,%ecx
mov    $0x3e7,%eax
cmovg  %ecx,%eax
jmp    1198 <getHC_SR04Measure_smooth+0x88>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getHC_SR04Measure_smooth() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca [30 x i32]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %2
store i32 0, i32* %3
store i64 0, i64* %4
store i32 0, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %45, %0
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 30
br i1 %10, label %11, label %48
11: ; preds = %8
%12 = call i32 (...) @getHC_SR04Measure()
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [30 x i32], [30 x i32]* %5, i64 0, i64 %14
store i32 %12, i32* %15
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [30 x i32], [30 x i32]* %5, i64 0, i64 %17
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %44
24: ; preds = %11
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [30 x i32], [30 x i32]* %5, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 999
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %43
33: ; preds = %24
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
%36 = load i64, i64* %4
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [30 x i32], [30 x i32]* %5, i64 0, i64 %38
%40 = load i32, i32* %39
%41 = sext i32 %40 to i64
%42 = add nsw i64 %36, %41
store i64 %42, i64* %4
br label %43
43: ; preds = %33, %30
br label %44
44: ; preds = %43, %21
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %8
48: ; preds = %8
%49 = load i32, i32* %6
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i64, i64* %4
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = sdiv i64 %52, %54
%56 = trunc i64 %55 to i32
store i32 %56, i32* %1
br label %63
57: ; preds = %48
%58 = load i32, i32* %3
%59 = load i32, i32* %2
%60 = icmp sgt i32 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %57
store i32 -1, i32* %1
br label %63
62: ; preds = %57
store i32 999, i32* %1
br label %63
63: ; preds = %62, %61, %51
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
define i32 @getHC_SR04Measure_smooth() {
br label %3
1: ; preds = %18
%2 = icmp sgt i32 %22, 0
br i1 %2, label %25, label %29
3: ; preds = %0, %18
%4 = phi i32 [ 0, %0 ], [ %23, %18 ]
%5 = phi i32 [ 0, %0 ], [ %22, %18 ]
%6 = phi i64 [ 0, %0 ], [ %21, %18 ]
%7 = phi i32 [ 0, %0 ], [ %20, %18 ]
%8 = phi i32 [ 0, %0 ], [ %19, %18 ]
%9 = tail call i32 (...) @getHC_SR04Measure()
switch i32 %9, label %14 [
i32 -1, label %10
i32 999, label %12
]
10: ; preds = %3
%11 = add nsw i32 %7, 1
br label %18
12: ; preds = %3
%13 = add nsw i32 %8, 1
br label %18
14: ; preds = %3
%15 = add nsw i32 %5, 1
%16 = sext i32 %9 to i64
%17 = add nsw i64 %6, %16
br label %18
18: ; preds = %10, %14, %12
%19 = phi i32 [ %8, %10 ], [ %13, %12 ], [ %8, %14 ]
%20 = phi i32 [ %11, %10 ], [ %7, %12 ], [ %7, %14 ]
%21 = phi i64 [ %6, %10 ], [ %6, %12 ], [ %17, %14 ]
%22 = phi i32 [ %5, %10 ], [ %5, %12 ], [ %15, %14 ]
%23 = add nuw nsw i32 %4, 1
%24 = icmp eq i32 %23, 30
br i1 %24, label %1, label %3
25: ; preds = %1
%26 = zext i32 %22 to i64
%27 = sdiv i64 %21, %26
%28 = trunc i64 %27 to i32
br label %32
29: ; preds = %1
%30 = icmp sgt i32 %20, %19
%31 = select i1 %30, i32 -1, i32 999
br label %32
32: ; preds = %29, %25
%33 = phi i32 [ %28, %25 ], [ %31, %29 ]
ret i32 %33
}