#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 selection_algorithm(int /*<<< orphan*/, int, int, int *);
int moving_median(int sensor, int current, int *values) {
  static short count[10] = {0};
  static int dupArray[5] = {0};
  if (count[sensor] < 5) {
    values[count[sensor]] = current;
    count[sensor]++;
    return current;
  } else {
    if (count[sensor] != 5 - 1) {
      for (int i = 0; i < (5 - 1); i++) {
        values[i] = values[i + 1];
      }
    } else {
      count[sensor] = 5 + 1;
    }
    values[5 - 1] = current;
    for (int i = 0; i < 5; i++) {
      dupArray[i] = values[i];
    }
    return selection_algorithm(0, 5 - 1, 3, dupArray);
  }
  return 0;
}


x86 assembly (clang, -O0):
<moving_median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0x8(%rbp),%rcx
lea    0x2f13(%rip),%rax
movswl (%rax,%rcx,2),%eax
cmp    $0x5,%eax
jge    117d <moving_median+0x6d>
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rsi
lea    0x2ef4(%rip),%rcx
movswq (%rcx,%rsi,2),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x8(%rbp),%rcx
lea    0x2ee1(%rip),%rax
mov    (%rax,%rcx,2),%dx
add    $0x1,%dx
lea    0x2ed2(%rip),%rax
mov    %dx,(%rax,%rcx,2)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1242 <moving_median+0x132>
movslq -0x8(%rbp),%rcx
lea    0x2eb8(%rip),%rax
movswl (%rax,%rcx,2),%eax
cmp    $0x4,%eax
je     11d4 <moving_median+0xc4>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    11cf <moving_median+0xbf>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119c <moving_median+0x8c>
jmp    11e5 <moving_median+0xd5>
movslq -0x8(%rbp),%rcx
lea    0x2e61(%rip),%rax
movw   $0x6,(%rax,%rcx,2)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
movl   $0x0,-0x20(%rbp)
cmpl   $0x5,-0x20(%rbp)
jge    1227 <moving_median+0x117>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
movslq -0x20(%rbp),%rcx
lea    0x2e4a(%rip),%rax
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11f6 <moving_median+0xe6>
xor    %edi,%edi
mov    $0x4,%esi
mov    $0x3,%edx
lea    0x2e26(%rip),%rcx
call   1030 <selection_algorithm@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<moving_median>:
movslq %edi,%rax
lea    0x2f26(%rip),%rcx
movswq (%rcx,%rax,2),%rdi
cmp    $0x5,%rdi
jge    1132 <moving_median+0x22>
mov    %esi,(%rdx,%rdi,4)
lea    0x1(%rdi),%edx
mov    %dx,(%rcx,%rax,2)
mov    %esi,%eax
ret
movups 0x4(%rdx),%xmm0
movups %xmm0,(%rdx)
mov    %esi,0x10(%rdx)
mov    (%rdx),%eax
mov    %eax,0x2f1c(%rip)
lea    0x2f15(%rip),%rcx
mov    0x4(%rdx),%eax
mov    %eax,0x2f10(%rip)
mov    0x8(%rdx),%eax
mov    %eax,0x2f0b(%rip)
mov    0xc(%rdx),%eax
mov    %eax,0x2f06(%rip)
mov    %esi,0x2f04(%rip)
xor    %edi,%edi
mov    $0x4,%esi
mov    $0x3,%edx
jmp    1030 <selection_algorithm@plt>

LLVM IR (-O0):
@moving_median.count = internal global [10 x i16] zeroinitializer
@moving_median.dupArray = internal global [5 x i32] zeroinitializer
define i32 @moving_median(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [10 x i16], [10 x i16]* @moving_median.count, i64 0, i64 %11
%13 = load i16, i16* %12
%14 = sext i16 %13 to i32
%15 = icmp slt i32 %14, 5
br i1 %15, label %16, label %31
16: ; preds = %3
%17 = load i32, i32* %6
%18 = load i32*, i32** %7
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [10 x i16], [10 x i16]* @moving_median.count, i64 0, i64 %20
%22 = load i16, i16* %21
%23 = sext i16 %22 to i64
%24 = getelementptr inbounds i32, i32* %18, i64 %23
store i32 %17, i32* %24
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [10 x i16], [10 x i16]* @moving_median.count, i64 0, i64 %26
%28 = load i16, i16* %27
%29 = add i16 %28, 1
store i16 %29, i16* %27
%30 = load i32, i32* %6
store i32 %30, i32* %4
br label %82
31: ; preds = %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [10 x i16], [10 x i16]* @moving_median.count, i64 0, i64 %33
%35 = load i16, i16* %34
%36 = sext i16 %35 to i32
%37 = icmp ne i32 %36, 4
br i1 %37, label %38, label %57
38: ; preds = %31
store i32 0, i32* %8
br label %39
39: ; preds = %53, %38
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 4
br i1 %41, label %42, label %56
42: ; preds = %39
%43 = load i32*, i32** %7
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
%48 = load i32, i32* %47
%49 = load i32*, i32** %7
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
store i32 %48, i32* %52
br label %53
53: ; preds = %42
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %39
56: ; preds = %39
br label %61
57: ; preds = %31
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [10 x i16], [10 x i16]* @moving_median.count, i64 0, i64 %59
store i16 6, i16* %60
br label %61
61: ; preds = %57, %56
%62 = load i32, i32* %6
%63 = load i32*, i32** %7
%64 = getelementptr inbounds i32, i32* %63, i64 4
store i32 %62, i32* %64
store i32 0, i32* %9
br label %65
65: ; preds = %77, %61
%66 = load i32, i32* %9
%67 = icmp slt i32 %66, 5
br i1 %67, label %68, label %80
68: ; preds = %65
%69 = load i32*, i32** %7
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i32, i32* %69, i64 %71
%73 = load i32, i32* %72
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 %75
store i32 %73, i32* %76
br label %77
77: ; preds = %68
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1
store i32 %79, i32* %9
br label %65
80: ; preds = %65
%81 = call i32 @selection_algorithm(i32 0, i32 4, i32 3, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 0))
store i32 %81, i32* %4
br label %82
82: ; preds = %80, %16
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
@moving_median.count = internal unnamed_addr global [10 x i16] zeroinitializer
@moving_median.dupArray = internal global [5 x i32] zeroinitializer
define i32 @moving_median(i32 %0, i32 %1, i32* nocapture %2) {
%4 = sext i32 %0 to i64
%5 = getelementptr inbounds [10 x i16], [10 x i16]* @moving_median.count, i64 0, i64 %4
%6 = load i16, i16* %5
%7 = icmp slt i16 %6, 5
br i1 %7, label %20, label %8
8: ; preds = %3
%9 = bitcast i32* %2 to i8*
%10 = getelementptr i32, i32* %2, i64 1
%11 = bitcast i32* %10 to i8*
%12 = getelementptr inbounds i32, i32* %2, i64 4
store i32 %1, i32* %12
%13 = load i32, i32* %2
store i32 %13, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 0)
%14 = load i32, i32* %10
store i32 %14, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 1)
%15 = getelementptr inbounds i32, i32* %2, i64 2
%16 = load i32, i32* %15
store i32 %16, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 2)
%17 = getelementptr inbounds i32, i32* %2, i64 3
%18 = load i32, i32* %17
store i32 %18, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 3)
store i32 %1, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 4)
%19 = tail call i32 @selection_algorithm(i32 0, i32 4, i32 3, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @moving_median.dupArray, i64 0, i64 0))
br label %24
20: ; preds = %3
%21 = sext i16 %6 to i64
%22 = getelementptr inbounds i32, i32* %2, i64 %21
store i32 %1, i32* %22
%23 = add nsw i16 %6, 1
store i16 %23, i16* %5
br label %24
24: ; preds = %8, %20
%25 = phi i32 [ %1, %20 ], [ %19, %8 ]
ret i32 %25
}