#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 */
struct input_event {
  scalar_t__ type;
  int value;
  int code;
};

/* Variables and functions */
scalar_t__ EV_KEY;
#define KEY_DOWN 130
#define KEY_ENTER 129
#define KEY_UP 128
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, struct input_event *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system(char *);
void handle_events(int evfd) {
  struct input_event ev[64];
  int i, rd;
  rd = read(evfd, ev, sizeof(struct input_event) * 64);
  if (rd < (int)sizeof(struct input_event)) {
    fprintf(stderr, "expected %d bytes, got %d\n",
            (int)sizeof(struct input_event), rd);
    return;
  }
  for (i = 0; i < rd / sizeof(struct input_event); i++) {
    if (ev->type != EV_KEY)
      continue;
    if (ev->value != 1)
      continue;
    switch (ev->code) {
    case KEY_ENTER:
    case KEY_DOWN:
    case KEY_UP:
      printf("Key pressed\n");
      system("/sbin/poweroff");
      break;
    default:
      break;
    }
  }
}


x86 assembly (clang, -O0):
<handle_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x10,-0x418(%rbp)
jge    119a <handle_events+0x5a>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    -0x418(%rbp),%ecx
lea    0xe75(%rip),%rsi
mov    $0x10,%edx
call   1060 <fprintf@plt>
jmp    123f <handle_events+0xff>
movl   $0x0,-0x414(%rbp)
movslq -0x414(%rbp),%rax
movslq -0x418(%rbp),%rcx
shr    $0x4,%rcx
cmp    %rcx,%rax
jae    123f <handle_events+0xff>
mov    -0x410(%rbp),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
je     11db <handle_events+0x9b>
jmp    122b <handle_events+0xeb>
cmpl   $0x1,-0x408(%rbp)
je     11ed <handle_events+0xad>
jmp    122b <handle_events+0xeb>
mov    -0x404(%rbp),%eax
add    $0xffffff80,%eax
sub    $0x2,%eax
ja     1221 <handle_events+0xe1>
jmp    1204 <handle_events+0xc4>
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
lea    0xe11(%rip),%rdi
call   1030 <system@plt>
jmp    1226 <handle_events+0xe6>
jmp    1226 <handle_events+0xe6>
jmp    122b <handle_events+0xeb>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    11a4 <handle_events+0x64>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_events>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
cmp    $0x10,%eax
jge    1192 <handle_events+0x52>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
mov    $0x10,%edx
mov    %ebx,%ecx
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
shr    $0x4,%ebx
mov    (%rsp),%r12
mov    0x8(%rsp),%ebp
mov    $0xffffff7d,%r13d
add    0xc(%rsp),%r13d
mov    0x2e39(%rip),%r14
mov    (%r14),%rax
lea    0xe62(%rip),%r15
jmp    11c6 <handle_events+0x86>
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
je     11ef <handle_events+0xaf>
cmp    %rax,%r12
jne    11c0 <handle_events+0x80>
cmp    $0x1,%ebp
jne    11c0 <handle_events+0x80>
cmp    $0xfffffffd,%r13d
jb     11c0 <handle_events+0x80>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <system@plt>
mov    (%r14),%rax
jmp    11c0 <handle_events+0x80>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i64, i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"expected %d bytes, got %d\0A\00"
@EV_KEY = global i64 0
@.str.1 = [13 x i8] c"Key pressed\0A\00"
@.str.2 = [15 x i8] c"/sbin/poweroff\00"
define void @handle_events(i32 %0) {
%2 = alloca i32
%3 = alloca [64 x %struct.input_event]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 0
%8 = call i32 @read(i32 %6, %struct.input_event* %7, i32 1024)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 16
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* %5
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 16, i32 %13)
br label %48
15: ; preds = %1
store i32 0, i32* %4
br label %16
16: ; preds = %45, %15
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = udiv i64 %20, 16
%22 = icmp ult i64 %18, %21
br i1 %22, label %23, label %48
23: ; preds = %16
%24 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 0
%25 = getelementptr inbounds %struct.input_event, %struct.input_event* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @EV_KEY
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23
br label %45
30: ; preds = %23
%31 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 0
%32 = getelementptr inbounds %struct.input_event, %struct.input_event* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %30
br label %45
36: ; preds = %30
%37 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 0
%38 = getelementptr inbounds %struct.input_event, %struct.input_event* %37, i32 0, i32 2
%39 = load i32, i32* %38
switch i32 %39, label %43 [
i32 129, label %40
i32 130, label %40
i32 128, label %40
]
40: ; preds = %36, %36, %36
%41 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %44
43: ; preds = %36
br label %44
44: ; preds = %43, %40
br label %45
45: ; preds = %44, %35, %29
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
br label %16
48: ; preds = %11, %16
ret void
}

LLVM IR (-O2):
%struct.input_event = type { i64, i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"expected %d bytes, got %d\0A\00"
@EV_KEY = global i64 0
@.str.2 = [15 x i8] c"/sbin/poweroff\00"
@str = [12 x i8] c"Key pressed\00"
define void @handle_events(i32 %0) {
%2 = alloca [64 x %struct.input_event]
%3 = bitcast [64 x %struct.input_event]* %2 to i8*
%4 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %2, i64 0, i64 0
%5 = call i32 @read(i32 %0, %struct.input_event* %4, i32 1024)
%6 = icmp slt i32 %5, 16
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = lshr i32 %5, 4
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %2, i64 0, i64 0, i32 0
%11 = load i64, i64* %10
%12 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %2, i64 0, i64 0, i32 1
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, 1
%15 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %2, i64 0, i64 0, i32 2
%16 = load i32, i32* %15
%17 = add i32 %16, -131
%18 = icmp ult i32 %17, -3
%19 = load i64, i64* @EV_KEY
br label %23
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 16, i32 %5)
br label %37
23: ; preds = %7, %33
%24 = phi i64 [ %19, %7 ], [ %34, %33 ]
%25 = phi i64 [ 0, %7 ], [ %35, %33 ]
%26 = icmp ne i64 %11, %24
%27 = select i1 %26, i1 true, i1 %14
%28 = select i1 %27, i1 true, i1 %18
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%32 = load i64, i64* @EV_KEY
br label %33
33: ; preds = %23, %29
%34 = phi i64 [ %24, %23 ], [ %32, %29 ]
%35 = add nuw nsw i64 %25, 1
%36 = icmp eq i64 %35, %9
br i1 %36, label %37, label %23
37: ; preds = %33, %20
ret void
}