#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ on_close();
int /*<<< orphan*/ on_open();
int /*<<< orphan*/ on_press();
int /*<<< orphan*/ on_release();
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int write(int, unsigned char *, int);
int brb_listen() {
  int fd, res, prev, prev_lid_up, prev_button_down, lid_up, button_down,
      first = 1;
  unsigned char buf[8];
  fd = open("/dev/big_red_button", O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    perror("Unable to open device");
    return 1;
  }
  while (1) {
    memset(buf, 0, sizeof(buf));
    buf[0] = 0x08;
    buf[7] = 0x02;
    res = write(fd, buf, 8);
    if (res < 0) {
      perror("write");
      return 1;
    }
    memset(buf, 0, sizeof(buf));
    res = read(fd, buf, 8);
    if (res >= 0) {
      if (first) {
        prev = buf[0];
        first = 0;
      }
      if (buf[0] != prev) {
        lid_up = (buf[0] & 2) >> 1;
        button_down = !(buf[0] & 1);
        prev_lid_up = (prev & 2) >> 1;
        prev_button_down = !(prev & 1);
        if (lid_up && !prev_lid_up) {
          on_open();
        } else if (!lid_up && prev_lid_up) {
          on_close();
        }
        if (button_down && !prev_button_down) {
          on_press();
        } else if (!button_down && prev_button_down) {
          on_release();
        }
        prev = buf[0];
      }
    }
    usleep(1000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<brb_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x1,-0x24(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe38(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f2 <brb_listen+0x52>
lea    0xe33(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1379 <brb_listen+0x1d9>
jmp    11f7 <brb_listen+0x57>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movb   $0x8,-0x2c(%rbp)
movb   $0x2,-0x25(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1245 <brb_listen+0xa5>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1379 <brb_listen+0x1d9>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     136a <brb_listen+0x1ca>
cmpl   $0x0,-0x24(%rbp)
je     128b <brb_listen+0xeb>
movzbl -0x2c(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movzbl -0x2c(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     1365 <brb_listen+0x1c5>
movzbl -0x2c(%rbp),%eax
and    $0x2,%eax
sar    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movzbl -0x2c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
and    $0x2,%eax
sar    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12fe <brb_listen+0x15e>
cmpl   $0x0,-0x14(%rbp)
jne    12fe <brb_listen+0x15e>
mov    $0x0,%al
call   1030 <on_open@plt>
jmp    131e <brb_listen+0x17e>
cmpl   $0x0,-0x1c(%rbp)
jne    1319 <brb_listen+0x179>
cmpl   $0x0,-0x14(%rbp)
je     1319 <brb_listen+0x179>
mov    $0x0,%al
call   10a0 <on_close@plt>
jmp    131e <brb_listen+0x17e>
cmpl   $0x0,-0x20(%rbp)
je     133e <brb_listen+0x19e>
cmpl   $0x0,-0x18(%rbp)
jne    133e <brb_listen+0x19e>
mov    $0x0,%al
call   10b0 <on_press@plt>
jmp    135e <brb_listen+0x1be>
cmpl   $0x0,-0x20(%rbp)
jne    1359 <brb_listen+0x1b9>
cmpl   $0x0,-0x18(%rbp)
je     1359 <brb_listen+0x1b9>
mov    $0x0,%al
call   1090 <on_release@plt>
jmp    135e <brb_listen+0x1be>
movzbl -0x2c(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    136a <brb_listen+0x1ca>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
jmp    11f7 <brb_listen+0x57>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brb_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e35(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11d7 <brb_listen+0x37>
lea    0xe3f(%rip),%rdi
jmp    1213 <brb_listen+0x73>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movb   $0x8,0x8(%rsp)
movb   $0x2,0xf(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    122a <brb_listen+0x8a>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r14d
lea    0x8(%rsp),%r12
jmp    1279 <brb_listen+0xd9>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ebx
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movb   $0x8,0x8(%rsp)
movb   $0x2,0xf(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
test   %eax,%eax
js     120c <brb_listen+0x6c>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1242 <brb_listen+0xa2>
test   %r14d,%r14d
movzbl 0x8(%rsp),%ebp
cmovne %ebp,%ebx
xor    %r14d,%r14d
cmp    %ebp,%ebx
je     1240 <brb_listen+0xa0>
mov    %ebx,%eax
and    $0x2,%eax
test   $0x2,%bpl
je     12c6 <brb_listen+0x126>
test   %eax,%eax
jne    12c6 <brb_listen+0x126>
xor    %eax,%eax
call   1030 <on_open@plt>
jmp    12d7 <brb_listen+0x137>
test   $0x2,%bpl
jne    12d7 <brb_listen+0x137>
test   %eax,%eax
je     12d7 <brb_listen+0x137>
xor    %eax,%eax
call   10a0 <on_close@plt>
and    $0x1,%ebx
test   $0x1,%bpl
jne    12ed <brb_listen+0x14d>
test   %ebx,%ebx
je     12ed <brb_listen+0x14d>
xor    %eax,%eax
call   10b0 <on_press@plt>
jmp    12fe <brb_listen+0x15e>
test   $0x1,%bpl
je     12fe <brb_listen+0x15e>
test   %ebx,%ebx
jne    12fe <brb_listen+0x15e>
xor    %eax,%eax
call   1090 <on_release@plt>
movzbl 0x8(%rsp),%ebx
jmp    1242 <brb_listen+0xa2>

LLVM IR (-O0):
@.str = [20 x i8] c"/dev/big_red_button\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"Unable to open device\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @brb_listen() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [8 x i8]
store i32 1, i32* %9
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %114
19: ; preds = %0
br label %20
20: ; preds = %19, %112
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 8)
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
store i8 8, i8* %23
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 7
store i8 2, i8* %24
%25 = load i32, i32* %2
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%27 = call i32 @write(i32 %25, i8* %26, i32 8)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %114
32: ; preds = %20
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 8)
%35 = load i32, i32* %2
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 8)
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = icmp sge i32 %38, 0
br i1 %39, label %40, label %112
40: ; preds = %32
%41 = load i32, i32* %9
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
store i32 %46, i32* %4
store i32 0, i32* %9
br label %47
47: ; preds = %43, %40
%48 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = load i32, i32* %4
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %111
53: ; preds = %47
%54 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = and i32 %56, 2
%58 = ashr i32 %57, 1
store i32 %58, i32* %7
%59 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = and i32 %61, 1
%63 = icmp ne i32 %62, 0
%64 = xor i1 %63, true
%65 = zext i1 %64 to i32
store i32 %65, i32* %8
%66 = load i32, i32* %4
%67 = and i32 %66, 2
%68 = ashr i32 %67, 1
store i32 %68, i32* %5
%69 = load i32, i32* %4
%70 = and i32 %69, 1
%71 = icmp ne i32 %70, 0
%72 = xor i1 %71, true
%73 = zext i1 %72 to i32
store i32 %73, i32* %6
%74 = load i32, i32* %7
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %53
%77 = load i32, i32* %5
%78 = icmp ne i32 %77, 0
br i1 %78, label %81, label %79
79: ; preds = %76
%80 = call i32 (...) @on_open()
br label %90
81: ; preds = %76, %53
%82 = load i32, i32* %7
%83 = icmp ne i32 %82, 0
br i1 %83, label %89, label %84
84: ; preds = %81
%85 = load i32, i32* %5
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %84
%88 = call i32 (...) @on_close()
br label %89
89: ; preds = %87, %84, %81
br label %90
90: ; preds = %89, %79
%91 = load i32, i32* %8
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %98
93: ; preds = %90
%94 = load i32, i32* %6
%95 = icmp ne i32 %94, 0
br i1 %95, label %98, label %96
96: ; preds = %93
%97 = call i32 (...) @on_press()
br label %107
98: ; preds = %93, %90
%99 = load i32, i32* %8
%100 = icmp ne i32 %99, 0
br i1 %100, label %106, label %101
101: ; preds = %98
%102 = load i32, i32* %6
%103 = icmp ne i32 %102, 0
br i1 %103, label %104, label %106
104: ; preds = %101
%105 = call i32 (...) @on_release()
br label %106
106: ; preds = %104, %101, %98
br label %107
107: ; preds = %106, %96
%108 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%109 = load i8, i8* %108
%110 = zext i8 %109 to i32
store i32 %110, i32* %4
br label %111
111: ; preds = %107, %47
br label %112
112: ; preds = %111, %32
%113 = call i32 @usleep(i32 1000)
br label %20
114: ; preds = %30, %17
%115 = load i32, i32* %1
ret i32 %115
}

LLVM IR (-O2):
@.str = [20 x i8] c"/dev/big_red_button\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"Unable to open device\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @brb_listen() {
%1 = alloca [8 x i8]
%2 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %0
%9 = call i32 @memset(i8* %2, i32 0, i32 8)
store i8 8, i8* %2
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 7
store i8 2, i8* %10
%11 = call i32 @write(i32 %6, i8* %2, i32 8)
%12 = icmp slt i32 %11, 0
br i1 %12, label %15, label %17
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %63
15: ; preds = %56, %8
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %63
17: ; preds = %8, %56
%18 = phi i32 [ %58, %56 ], [ 1, %8 ]
%19 = phi i32 [ %57, %56 ], [ undef, %8 ]
%20 = call i32 @memset(i8* %2, i32 0, i32 8)
%21 = call i32 @read(i32 %6, i8* %2, i32 8)
%22 = icmp sgt i32 %21, -1
br i1 %22, label %23, label %56
23: ; preds = %17
%24 = icmp eq i32 %18, 0
%25 = load i8, i8* %2
%26 = zext i8 %25 to i32
%27 = select i1 %24, i32 %19, i32 %26
%28 = icmp eq i32 %27, %26
br i1 %28, label %56, label %29
29: ; preds = %23
%30 = and i32 %26, 1
%31 = icmp ne i32 %30, 0
%32 = and i32 %27, 1
%33 = icmp eq i32 %32, 0
%34 = and i32 %26, 2
%35 = icmp eq i32 %34, 0
%36 = and i32 %27, 2
%37 = icmp ne i32 %36, 0
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %41, label %39
39: ; preds = %29
%40 = call i32 (...) @on_open()
br label %45
41: ; preds = %29
%42 = select i1 %35, i1 %37, i1 false
br i1 %42, label %43, label %45
43: ; preds = %41
%44 = call i32 (...) @on_close()
br label %45
45: ; preds = %41, %43, %39
%46 = select i1 %31, i1 true, i1 %33
br i1 %46, label %49, label %47
47: ; preds = %45
%48 = call i32 (...) @on_press()
br label %53
49: ; preds = %45
%50 = select i1 %31, i1 %33, i1 false
br i1 %50, label %51, label %53
51: ; preds = %49
%52 = call i32 (...) @on_release()
br label %53
53: ; preds = %49, %51, %47
%54 = load i8, i8* %2
%55 = zext i8 %54 to i32
br label %56
56: ; preds = %23, %53, %17
%57 = phi i32 [ %55, %53 ], [ %26, %23 ], [ %19, %17 ]
%58 = phi i32 [ 0, %53 ], [ 0, %23 ], [ %18, %17 ]
%59 = call i32 @usleep(i32 1000)
%60 = call i32 @memset(i8* %2, i32 0, i32 8)
store i8 8, i8* %2
store i8 2, i8* %10
%61 = call i32 @write(i32 %6, i8* %2, i32 8)
%62 = icmp slt i32 %61, 0
br i1 %62, label %15, label %17
63: ; preds = %15, %13
ret i32 1
}