#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;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int, int /*<<< orphan*/);
int open_dev(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, int *, int);
int main() {
  int gpio_fd = 0;
  int key = 0x00;
  gpio_fd = open_dev("/dev/key_ctl");
  if (gpio_fd < 0) {
    perror("open device key_gph");
    return gpio_fd;
  }
  ioctl(gpio_fd, 31, 0);
  ioctl(gpio_fd, 87, 0);
  while (1) {
    read(gpio_fd, &key, sizeof(key));
    switch (key & 0xF8) {
    case 0xF0:
      printf("key = 3\n");
      break;
    case 0xE8:
      printf("key = 4\n");
      break;
    case 0xD8:
      printf("key = 5\n");
      break;
    case 0xB8:
      printf("key = 6\n");
      break;
    case 0x78:
      printf("key = 7\n");
      break;
    default:
      break;
    }
  }
  close(gpio_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe8c(%rip),%rdi
call   1030 <open_dev@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119b <main+0x4b>
lea    0xe80(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    -0x8(%rbp),%edi
mov    $0x1f,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%edi
mov    $0x57,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    -0xc(%rbp),%eax
and    $0xf8,%eax
mov    %eax,-0x10(%rbp)
sub    $0x78,%eax
je     1273 <main+0x123>
jmp    11e3 <main+0x93>
mov    -0x10(%rbp),%eax
sub    $0xb8,%eax
je     1262 <main+0x112>
jmp    11f6 <main+0xa6>
mov    -0x10(%rbp),%eax
sub    $0xd8,%eax
je     1251 <main+0x101>
jmp    1209 <main+0xb9>
mov    -0x10(%rbp),%eax
sub    $0xe8,%eax
je     1240 <main+0xf0>
jmp    121c <main+0xcc>
mov    -0x10(%rbp),%eax
sub    $0xf0,%eax
jne    1284 <main+0x134>
jmp    122f <main+0xdf>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
jmp    1289 <main+0x139>
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
jmp    1289 <main+0x139>
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
jmp    1289 <main+0x139>
lea    0xdd3(%rip),%rdi
call   1040 <printf@plt>
jmp    1289 <main+0x139>
lea    0xdcb(%rip),%rdi
call   1040 <printf@plt>
jmp    1289 <main+0x139>
jmp    1289 <main+0x139>
jmp    11b9 <main+0x69>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
lea    0xe96(%rip),%rdi
call   1040 <open_dev@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1220 <main+0xd0>
mov    %ebx,%edi
mov    $0x1f,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    %ebx,%edi
mov    $0x57,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
lea    0x4(%rsp),%r14
lea    0xea0(%rip),%r15
lea    0xe89(%rip),%r13
lea    0xe72(%rip),%rbp
lea    0xe83(%rip),%r12
jmp    11c0 <main+0x70>
mov    %rbp,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
movzbl 0x4(%rsp),%eax
and    $0xf8,%al
cmp    $0xe7,%al
jle    1200 <main+0xb0>
cmp    $0x78,%al
je     11b8 <main+0x68>
mov    %r15,%rdi
cmp    $0xf0,%al
je     11bb <main+0x6b>
cmp    $0xe8,%al
jne    11c0 <main+0x70>
mov    %r12,%rdi
call   1030 <puts@plt>
jmp    11c0 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xb8,%al
je     1212 <main+0xc2>
cmp    $0xd8,%al
jne    11c0 <main+0x70>
mov    %r13,%rdi
call   1030 <puts@plt>
jmp    11c0 <main+0x70>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
jmp    11c0 <main+0x70>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/key_ctl\00"
@.str.1 = [20 x i8] c"open device key_gph\00"
@.str.2 = [9 x i8] c"key = 3\0A\00"
@.str.3 = [9 x i8] c"key = 4\0A\00"
@.str.4 = [9 x i8] c"key = 5\0A\00"
@.str.5 = [9 x i8] c"key = 6\0A\00"
@.str.6 = [9 x i8] c"key = 7\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%4 = call i32 @open_dev(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* %2
ret i32 %9
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i32 @ioctl(i32 %11, i32 31, i32 0)
%13 = load i32, i32* %2
%14 = call i32 @ioctl(i32 %13, i32 87, i32 0)
br label %15
15: ; preds = %10, %31
%16 = load i32, i32* %2
%17 = call i32 @read(i32 %16, i32* %3, i32 4)
%18 = load i32, i32* %3
%19 = and i32 %18, 248
switch i32 %19, label %30 [
i32 240, label %20
i32 232, label %22
i32 216, label %24
i32 184, label %26
i32 120, label %28
]
20: ; preds = %15
%21 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %31
22: ; preds = %15
%23 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %31
24: ; preds = %15
%25 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %31
26: ; preds = %15
%27 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %31
28: ; preds = %15
%29 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %31
30: ; preds = %15
br label %31
31: ; preds = %30, %28, %26, %24, %22, %20
br label %15
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/key_ctl\00"
@.str.1 = [20 x i8] c"open device key_gph\00"
@str = [8 x i8] c"key = 7\00"
@str.7 = [8 x i8] c"key = 6\00"
@str.8 = [8 x i8] c"key = 5\00"
@str.9 = [8 x i8] c"key = 4\00"
@str.10 = [8 x i8] c"key = 3\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @open_dev(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
ret i32 %3
7: ; preds = %0
%8 = tail call i32 @ioctl(i32 %3, i32 31, i32 0)
%9 = tail call i32 @ioctl(i32 %3, i32 87, i32 0)
br label %10
10: ; preds = %22, %7
%11 = call i32 @read(i32 %3, i32* %1, i32 4)
%12 = load i32, i32* %1
%13 = trunc i32 %12 to i8
%14 = and i8 %13, -8
switch i8 %14, label %22 [
i8 -16, label %19
i8 -24, label %15
i8 -40, label %16
i8 -72, label %17
i8 120, label %18
]
15: ; preds = %10
br label %19
16: ; preds = %10
br label %19
17: ; preds = %10
br label %19
18: ; preds = %10
br label %19
19: ; preds = %10, %15, %16, %17, %18
%20 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0), %18 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @str.7, i64 0, i64 0), %17 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @str.8, i64 0, i64 0), %16 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0), %15 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @str.10, i64 0, i64 0), %10 ]
%21 = tail call i32 @puts(i8* dereferenceable(1) %20)
br label %22
22: ; preds = %19, %10
br label %10
}