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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, unsigned char *, int);
int main(int argc, char **argv) {
  int fd = 0;
  fd = open("/dev/buttonsdev", 0);
  if (fd < 0) {
    perror("open device "
           "/dev/buttonsdev");
    exit(1);
  }
  int i = 0;
  unsigned char key_values[4];
  unsigned char history_values[4];
  for (i = 0; i < 4; i++)
    history_values[i] = 'r';
  while (1) {
    i = read(fd, key_values, sizeof(key_values));
    if (i < 0) {
      perror("read device");
    } else {
      printf("read key values: <%c,%c,%c,%c>\n", key_values[0], key_values[1],
             key_values[2], key_values[3]);
      for (i = 0; i < 4; i++) {
        if (history_values[i] == 'r' && key_values[i] == 'p')
          printf("\t\t\t\t\tButton %d pressed\n", i);
        else if (history_values[i] == 'p' && key_values[i] == 'r')
          printf("\t\t\t\t\tButton %d release\n\n", i);
        else
          ;
        history_values[i] = key_values[i];
      }
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe98(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119e <main+0x4e>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    11cd <main+0x7d>
movslq -0x18(%rbp),%rax
movb   $0x72,-0x20(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ac <main+0x5c>
jmp    11d2 <main+0x82>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1201 <main+0xb1>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
jmp    12d2 <main+0x182>
movzbl -0x1c(%rbp),%esi
movzbl -0x1b(%rbp),%edx
movzbl -0x1a(%rbp),%ecx
movzbl -0x19(%rbp),%r8d
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    12cd <main+0x17d>
movslq -0x18(%rbp),%rax
movzbl -0x20(%rbp,%rax,1),%eax
cmp    $0x72,%eax
jne    126b <main+0x11b>
movslq -0x18(%rbp),%rax
movzbl -0x1c(%rbp,%rax,1),%eax
cmp    $0x70,%eax
jne    126b <main+0x11b>
mov    -0x18(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12af <main+0x15f>
movslq -0x18(%rbp),%rax
movzbl -0x20(%rbp,%rax,1),%eax
cmp    $0x70,%eax
jne    12a5 <main+0x155>
movslq -0x18(%rbp),%rax
movzbl -0x1c(%rbp,%rax,1),%eax
cmp    $0x72,%eax
jne    12a5 <main+0x155>
mov    -0x18(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12aa <main+0x15a>
jmp    12aa <main+0x15a>
jmp    12af <main+0x15f>
movslq -0x18(%rbp),%rax
mov    -0x1c(%rbp,%rax,1),%cl
movslq -0x18(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1227 <main+0xd7>
jmp    12d2 <main+0x182>
jmp    11d2 <main+0x82>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xea7(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,0x14(%rsp)
test   %eax,%eax
js     1370 <main+0x220>
mov    $0x72,%r14b
mov    $0x72,%r13b
mov    $0x72,%cl
jmp    11ac <main+0x5c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xec9(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%r14d
mov    %ebp,%r13d
mov    %r15d,%ecx
mov    $0x72,%al
xchg   %ax,%ax
mov    %cl,0xe(%rsp)
mov    %al,0xf(%rsp)
mov    0x14(%rsp),%ebx
lea    0x10(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11f0 <main+0xa0>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
jmp    11c1 <main+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10(%rsp),%bl
mov    0x11(%rsp),%bpl
movzbl %bl,%esi
movzbl %bpl,%edx
mov    0x12(%rsp),%r15b
movzbl %r15b,%ecx
mov    0x13(%rsp),%r12b
movzbl %r12b,%r8d
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x70,%r14b
je     12e0 <main+0x190>
cmp    $0x72,%r14b
jne    1245 <main+0xf5>
lea    0xe11(%rip),%rdi
cmp    $0x70,%bl
jne    1245 <main+0xf5>
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x72,%r13b
je     1300 <main+0x1b0>
cmp    $0x70,%r13b
jne    126e <main+0x11e>
lea    0xe04(%rip),%rdi
cmp    $0x72,%bpl
jne    126e <main+0x11e>
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0xe(%rsp),%al
cmp    $0x72,%al
je     1320 <main+0x1d0>
cmp    $0x70,%al
jne    1297 <main+0x147>
lea    0xddb(%rip),%rdi
cmp    $0x72,%r15b
jne    1297 <main+0x147>
mov    $0x2,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0xf(%rsp),%al
mov    %ebx,%r14d
mov    %ebp,%r13d
mov    %r15d,%ecx
cmp    $0x72,%al
je     1340 <main+0x1f0>
cmp    $0x70,%al
mov    %r12d,%eax
jne    11b0 <main+0x60>
mov    %ebx,%r14d
mov    %ebp,%r13d
mov    %r15d,%ecx
mov    %r12d,%eax
cmp    $0x72,%r12b
jne    11b0 <main+0x60>
jmp    1190 <main+0x40>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xd79(%rip),%rdi
cmp    $0x72,%bl
je     123c <main+0xec>
jmp    1245 <main+0xf5>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xd41(%rip),%rdi
cmp    $0x70,%bpl
je     1262 <main+0x112>
jmp    126e <main+0x11e>
cs nopw 0x0(%rax,%rax,1)
lea    0xd21(%rip),%rdi
cmp    $0x70,%r15b
je     128b <main+0x13b>
jmp    1297 <main+0x147>
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%eax
cmp    $0x70,%r12b
jne    11b0 <main+0x60>
lea    0xcf4(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x70,%al
mov    %ebx,%r14d
mov    %ebp,%r13d
mov    %r15d,%ecx
jmp    11b0 <main+0x60>
lea    0xc89(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/buttonsdev\00"
@.str.1 = [28 x i8] c"open device /dev/buttonsdev\00"
@.str.2 = [12 x i8] c"read device\00"
@.str.3 = [32 x i8] c"read key values: <%c,%c,%c,%c>\0A\00"
@.str.4 = [24 x i8] c"\09\09\09\09\09Button %d pressed\0A\00"
@.str.5 = [25 x i8] c"\09\09\09\09\09Button %d release\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i8]
%9 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
store i32 0, i32* %7
store i32 0, i32* %7
br label %17
17: ; preds = %24, %16
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 4
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %22
store i8 114, i8* %23
br label %24
24: ; preds = %20
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %17
27: ; preds = %17
br label %28
28: ; preds = %27, %101
%29 = load i32, i32* %6
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 4)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %101
36: ; preds = %28
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 1
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 2
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 3
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %39, i32 %42, i32 %45, i32 %48)
store i32 0, i32* %7
br label %50
50: ; preds = %97, %36
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 4
br i1 %52, label %53, label %100
53: ; preds = %50
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = icmp eq i32 %58, 114
br i1 %59, label %60, label %70
60: ; preds = %53
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 %62
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = icmp eq i32 %65, 112
br i1 %66, label %67, label %70
67: ; preds = %60
%68 = load i32, i32* %7
%69 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %68)
br label %89
70: ; preds = %60, %53
%71 = load i32, i32* %7
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %72
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = icmp eq i32 %75, 112
br i1 %76, label %77, label %87
77: ; preds = %70
%78 = load i32, i32* %7
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 %79
%81 = load i8, i8* %80
%82 = zext i8 %81 to i32
%83 = icmp eq i32 %82, 114
br i1 %83, label %84, label %87
84: ; preds = %77
%85 = load i32, i32* %7
%86 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %85)
br label %88
87: ; preds = %77, %70
br label %88
88: ; preds = %87, %84
br label %89
89: ; preds = %88, %67
%90 = load i32, i32* %7
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 %91
%93 = load i8, i8* %92
%94 = load i32, i32* %7
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %95
store i8 %93, i8* %96
br label %97
97: ; preds = %89
%98 = load i32, i32* %7
%99 = add nsw i32 %98, 1
store i32 %99, i32* %7
br label %50
100: ; preds = %50
br label %101
101: ; preds = %100, %34
br label %28
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/buttonsdev\00"
@.str.1 = [28 x i8] c"open device /dev/buttonsdev\00"
@.str.2 = [12 x i8] c"read device\00"
@.str.3 = [32 x i8] c"read key values: <%c,%c,%c,%c>\0A\00"
@.str.4 = [24 x i8] c"\09\09\09\09\09Button %d pressed\0A\00"
@.str.5 = [25 x i8] c"\09\09\09\09\09Button %d release\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4 x i8]
%4 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
br label %14
14: ; preds = %66, %9
%15 = phi i8 [ 114, %9 ], [ %25, %66 ]
%16 = phi i8 [ 114, %9 ], [ %27, %66 ]
%17 = phi i8 [ 114, %9 ], [ %29, %66 ]
%18 = phi i8 [ 114, %9 ], [ %67, %66 ]
br label %19
19: ; preds = %14, %22
%20 = call i32 @read(i32 %4, i8* %10, i32 4)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %19
24: ; preds = %19
%25 = load i8, i8* %10
%26 = zext i8 %25 to i32
%27 = load i8, i8* %11
%28 = zext i8 %27 to i32
%29 = load i8, i8* %12
%30 = zext i8 %29 to i32
%31 = load i8, i8* %13
%32 = zext i8 %31 to i32
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %28, i32 %30, i32 %32)
switch i8 %15, label %41 [
i8 114, label %34
i8 112, label %36
]
34: ; preds = %24
%35 = icmp eq i8 %25, 112
br i1 %35, label %38, label %41
36: ; preds = %24
%37 = icmp eq i8 %25, 114
br i1 %37, label %38, label %41
38: ; preds = %36, %34
%39 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), %34 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), %36 ]
%40 = tail call i32 (i8*, i32, ...) @printf(i8* %39, i32 0)
br label %41
41: ; preds = %38, %24, %34, %36
switch i8 %16, label %49 [
i8 114, label %44
i8 112, label %42
]
42: ; preds = %41
%43 = icmp eq i8 %27, 114
br i1 %43, label %46, label %49
44: ; preds = %41
%45 = icmp eq i8 %27, 112
br i1 %45, label %46, label %49
46: ; preds = %44, %42
%47 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), %42 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), %44 ]
%48 = tail call i32 (i8*, i32, ...) @printf(i8* %47, i32 1)
br label %49
49: ; preds = %46, %44, %42, %41
switch i8 %17, label %57 [
i8 114, label %52
i8 112, label %50
]
50: ; preds = %49
%51 = icmp eq i8 %29, 114
br i1 %51, label %54, label %57
52: ; preds = %49
%53 = icmp eq i8 %29, 112
br i1 %53, label %54, label %57
54: ; preds = %52, %50
%55 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), %50 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), %52 ]
%56 = tail call i32 (i8*, i32, ...) @printf(i8* %55, i32 2)
br label %57
57: ; preds = %54, %52, %50, %49
switch i8 %18, label %66 [
i8 114, label %62
i8 112, label %58
]
58: ; preds = %57
%59 = icmp eq i8 %31, 114
br i1 %59, label %60, label %66
60: ; preds = %58
%61 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 3)
br label %66
62: ; preds = %57
%63 = icmp eq i8 %31, 112
br i1 %63, label %64, label %66
64: ; preds = %62
%65 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 3)
br label %66
66: ; preds = %64, %62, %60, %58, %57
%67 = phi i8 [ %31, %57 ], [ %31, %58 ], [ 114, %60 ], [ %31, %62 ], [ 112, %64 ]
br label %14
}