#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 analog10(int);
scalar_t__ b_button();
scalar_t__ c_button();
int /*<<< orphan*/ display_clear();
int /*<<< orphan*/ display_printf(int, int, char *, ...);
int /*<<< orphan*/ msleep(int);
void light_start(int sensor) {
  ;
  while (!(!(b_button() || c_button()))) {
    msleep(1);
  };
  int max = 0, min = 9999, curr = 0, avg = 0;
  display_clear();
  display_printf(0, 0, "Max:");
  display_printf(0, 1, "Min:");
  display_printf(0, 2, "Curr:");
  display_printf(0, 3, "avg:");
  while (!b_button() && !c_button()) {
    curr = analog10(sensor);
    if (curr > max) {
      max = curr;
    }
    if (curr < min) {
      min = curr;
    }
    avg = (max + min) / 2;
    display_printf(5, 0, "%d   ", max);
    display_printf(5, 1, "%d   ", min);
    display_printf(6, 2, "%d   ", curr);
    display_printf(5, 3, "%d   ", avg);
    if (curr > avg) {
      display_printf(10, 5, "XX");
    } else {
      display_printf(10, 5, "OO");
    }
    msleep(50);
  }
  display_clear();
  display_printf(0, 4, "Prepared to begin: left to skip");
  while (analog10(sensor) > avg && !(c_button())) {
    msleep(50);
  }
}


x86 assembly (clang, -O0):
<light_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1070 <b_button@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x15(%rbp)
jne    1195 <light_start+0x35>
mov    $0x0,%al
call   1040 <c_button@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
xor    $0xff,%al
xor    $0xff,%al
test   $0x1,%al
jne    11a9 <light_start+0x49>
jmp    11b8 <light_start+0x58>
mov    $0x1,%edi
call   1050 <msleep@plt>
jmp    116b <light_start+0xb>
movl   $0x0,-0x8(%rbp)
movl   $0x270f,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1080 <display_clear@plt>
xor    %esi,%esi
lea    0xe1c(%rip),%rdx
mov    %esi,%edi
mov    $0x0,%al
call   1060 <display_printf@plt>
xor    %edi,%edi
mov    $0x1,%esi
lea    0xe0a(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
xor    %edi,%edi
mov    $0x2,%esi
lea    0xdfa(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
xor    %edi,%edi
mov    $0x3,%esi
lea    0xdeb(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    $0x0,%al
call   1070 <b_button@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x16(%rbp)
jne    1258 <light_start+0xf8>
mov    $0x0,%al
call   1040 <c_button@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    1268 <light_start+0x108>
jmp    1361 <light_start+0x201>
mov    -0x4(%rbp),%edi
call   1030 <analog10@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    1285 <light_start+0x125>
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1297 <light_start+0x137>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%ecx
mov    $0x5,%edi
xor    %esi,%esi
lea    0xd5c(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    -0xc(%rbp),%ecx
mov    $0x5,%edi
mov    $0x1,%esi
lea    0xd41(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    -0x10(%rbp),%ecx
mov    $0x6,%edi
mov    $0x2,%esi
lea    0xd26(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    -0x14(%rbp),%ecx
mov    $0x5,%edi
mov    $0x3,%esi
lea    0xd0b(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    133a <light_start+0x1da>
mov    $0xa,%edi
mov    $0x5,%esi
lea    0xced(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
jmp    1352 <light_start+0x1f2>
mov    $0xa,%edi
mov    $0x5,%esi
lea    0xcd3(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    $0x32,%edi
call   1050 <msleep@plt>
jmp    122c <light_start+0xcc>
mov    $0x0,%al
call   1080 <display_clear@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    0xcab(%rip),%rdx
mov    $0x0,%al
call   1060 <display_printf@plt>
mov    -0x4(%rbp),%edi
call   1030 <analog10@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x17(%rbp)
jle    13a8 <light_start+0x248>
mov    $0x0,%al
call   1040 <c_button@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x17(%rbp)
mov    -0x17(%rbp),%al
test   $0x1,%al
jne    13b8 <light_start+0x258>
jmp    13c7 <light_start+0x267>
mov    $0x32,%edi
call   1050 <msleep@plt>
jmp    137d <light_start+0x21d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<light_start>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
jmp    117a <light_start+0x1a>
mov    $0x1,%edi
call   1050 <msleep@plt>
xor    %eax,%eax
call   1070 <b_button@plt>
test   %rax,%rax
jne    1170 <light_start+0x10>
xor    %eax,%eax
call   1040 <c_button@plt>
test   %rax,%rax
jne    1170 <light_start+0x10>
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <display_clear@plt>
lea    0xe5e(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <display_printf@plt>
lea    0xe51(%rip),%rdx
xor    %edi,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <display_printf@plt>
lea    0xe41(%rip),%rdx
xor    %edi,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1060 <display_printf@plt>
lea    0xe32(%rip),%rdx
xor    %edi,%edi
mov    $0x3,%esi
xor    %eax,%eax
call   1060 <display_printf@plt>
xor    %eax,%eax
call   1070 <b_button@plt>
test   %rax,%rax
jne    12e7 <light_start+0x187>
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <c_button@plt>
test   %rax,%rax
jne    12e7 <light_start+0x187>
mov    $0x270f,%r12d
lea    0xdfa(%rip),%r15
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %r14d,%edi
call   1030 <analog10@plt>
mov    %eax,%ebx
cmp    %r13d,%eax
cmovg  %eax,%r13d
cmp    %r12d,%eax
cmovl  %eax,%r12d
lea    (%r12,%r13,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    $0x5,%edi
xor    %esi,%esi
mov    %r15,%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <display_printf@plt>
mov    $0x5,%edi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <display_printf@plt>
mov    $0x6,%edi
mov    $0x2,%esi
mov    %r15,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <display_printf@plt>
mov    $0x5,%edi
mov    $0x3,%esi
mov    %r15,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <display_printf@plt>
cmp    %ebp,%ebx
lea    0xd79(%rip),%rdx
lea    0xd6f(%rip),%rax
cmovg  %rax,%rdx
mov    $0xa,%edi
mov    $0x5,%esi
xor    %eax,%eax
call   1060 <display_printf@plt>
mov    $0x32,%edi
call   1050 <msleep@plt>
xor    %eax,%eax
call   1070 <b_button@plt>
test   %rax,%rax
jne    12e7 <light_start+0x187>
xor    %eax,%eax
call   1040 <c_button@plt>
test   %rax,%rax
je     1220 <light_start+0xc0>
xor    %eax,%eax
call   1080 <display_clear@plt>
lea    0xd2c(%rip),%rdx
xor    %edi,%edi
mov    $0x4,%esi
xor    %eax,%eax
call   1060 <display_printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%edi
call   1030 <analog10@plt>
cmp    %ebp,%eax
jle    1334 <light_start+0x1d4>
xor    %eax,%eax
call   1040 <c_button@plt>
test   %rax,%rax
jne    1334 <light_start+0x1d4>
mov    $0x32,%edi
call   1050 <msleep@plt>
jmp    1310 <light_start+0x1b0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"Max:\00"
@.str.1 = [5 x i8] c"Min:\00"
@.str.2 = [6 x i8] c"Curr:\00"
@.str.3 = [5 x i8] c"avg:\00"
@.str.4 = [6 x i8] c"%d \00"
@.str.5 = [3 x i8] c"XX\00"
@.str.6 = [3 x i8] c"OO\00"
@.str.7 = [32 x i8] c"Prepared to begin: left to skip\00"
define void @light_start(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
br label %7
7: ; preds = %17, %1
%8 = call i64 (...) @b_button()
%9 = icmp ne i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i64 (...) @c_button()
%12 = icmp ne i64 %11, 0
br label %13
13: ; preds = %10, %7
%14 = phi i1 [ true, %7 ], [ %12, %10 ]
%15 = xor i1 %14, true
%16 = xor i1 %15, true
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @msleep(i32 1)
br label %7
19: ; preds = %13
store i32 0, i32* %3
store i32 9999, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%20 = call i32 (...) @display_clear()
%21 = call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %68, %19
%26 = call i64 (...) @b_button()
%27 = icmp ne i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = call i64 (...) @c_button()
%30 = icmp ne i64 %29, 0
%31 = xor i1 %30, true
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %34, label %70
34: ; preds = %32
%35 = load i32, i32* %2
%36 = call i32 @analog10(i32 %35)
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = load i32, i32* %3
%39 = icmp sgt i32 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = load i32, i32* %5
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %34
%43 = load i32, i32* %5
%44 = load i32, i32* %4
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = load i32, i32* %5
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %42
%49 = load i32, i32* %3
%50 = load i32, i32* %4
%51 = add nsw i32 %49, %50
%52 = sdiv i32 %51, 2
store i32 %52, i32* %6
%53 = load i32, i32* %3
%54 = call i32 (i32, i32, i8*, ...) @display_printf(i32 5, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %4
%56 = call i32 (i32, i32, i8*, ...) @display_printf(i32 5, i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %5
%58 = call i32 (i32, i32, i8*, ...) @display_printf(i32 6, i32 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %6
%60 = call i32 (i32, i32, i8*, ...) @display_printf(i32 5, i32 3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %5
%62 = load i32, i32* %6
%63 = icmp sgt i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %48
%65 = call i32 (i32, i32, i8*, ...) @display_printf(i32 10, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %68
66: ; preds = %48
%67 = call i32 (i32, i32, i8*, ...) @display_printf(i32 10, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %66, %64
%69 = call i32 @msleep(i32 50)
br label %25
70: ; preds = %32
%71 = call i32 (...) @display_clear()
%72 = call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
br label %73
73: ; preds = %84, %70
%74 = load i32, i32* %2
%75 = call i32 @analog10(i32 %74)
%76 = load i32, i32* %6
%77 = icmp sgt i32 %75, %76
br i1 %77, label %78, label %82
78: ; preds = %73
%79 = call i64 (...) @c_button()
%80 = icmp ne i64 %79, 0
%81 = xor i1 %80, true
br label %82
82: ; preds = %78, %73
%83 = phi i1 [ false, %73 ], [ %81, %78 ]
br i1 %83, label %84, label %86
84: ; preds = %82
%85 = call i32 @msleep(i32 50)
br label %73
86: ; preds = %82
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"Max:\00"
@.str.1 = [5 x i8] c"Min:\00"
@.str.2 = [6 x i8] c"Curr:\00"
@.str.3 = [5 x i8] c"avg:\00"
@.str.4 = [6 x i8] c"%d \00"
@.str.5 = [3 x i8] c"XX\00"
@.str.6 = [3 x i8] c"OO\00"
@.str.7 = [32 x i8] c"Prepared to begin: left to skip\00"
define void @light_start(i32 %0) {
br label %2
2: ; preds = %8, %1
%3 = tail call i64 (...) @b_button()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i64 (...) @c_button()
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2, %5
%9 = tail call i32 @msleep(i32 1)
br label %2
10: ; preds = %5
%11 = tail call i32 (...) @display_clear()
%12 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i64 (...) @b_button()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %10
%19 = tail call i64 (...) @c_button()
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %44
21: ; preds = %24
%22 = tail call i64 (...) @c_button()
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %18, %21
%25 = phi i32 [ %29, %21 ], [ 0, %18 ]
%26 = phi i32 [ %31, %21 ], [ 9999, %18 ]
%27 = tail call i32 @analog10(i32 %0)
%28 = icmp sgt i32 %27, %25
%29 = select i1 %28, i32 %27, i32 %25
%30 = icmp slt i32 %27, %26
%31 = select i1 %30, i32 %27, i32 %26
%32 = add nsw i32 %29, %31
%33 = sdiv i32 %32, 2
%34 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 5, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%35 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 5, i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%36 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 6, i32 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%37 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 5, i32 3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%38 = icmp sgt i32 %27, %33
%39 = select i1 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0)
%40 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 10, i32 5, i8* %39)
%41 = tail call i32 @msleep(i32 50)
%42 = tail call i64 (...) @b_button()
%43 = icmp eq i64 %42, 0
br i1 %43, label %21, label %44
44: ; preds = %24, %21, %18, %10
%45 = phi i32 [ 0, %10 ], [ 0, %18 ], [ %33, %21 ], [ %33, %24 ]
%46 = tail call i32 (...) @display_clear()
%47 = tail call i32 (i32, i32, i8*, ...) @display_printf(i32 0, i32 4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
%48 = tail call i32 @analog10(i32 %0)
%49 = icmp sgt i32 %48, %45
br i1 %49, label %50, label %57
50: ; preds = %44, %53
%51 = tail call i64 (...) @c_button()
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = tail call i32 @msleep(i32 50)
%55 = tail call i32 @analog10(i32 %0)
%56 = icmp sgt i32 %55, %45
br i1 %56, label %50, label %57
57: ; preds = %50, %53, %44
ret void
}