#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 {
  int type;
  int /*<<< orphan*/ value;
  int /*<<< orphan*/ code;
};
typedef int /*<<< orphan*/ event;

/* Variables and functions */
#define EV_SW 128
int /*<<< orphan*/ handle_switch(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, struct input_event *, int);
bool handle_device_event(int evdevfd) {
  struct input_event event;
  int rbytes = read(evdevfd, &event, sizeof(event));
  if (rbytes == -1) {
    perror("read");
    return false;
  } else if (rbytes == 0) {
    printf("EOF\n");
    return false;
  } else if (rbytes != sizeof(event)) {
    printf("Invalid input\n");
    return false;
  }
  switch (event.type) {
  case EV_SW:
    handle_switch(event.code, event.value);
    break;
  }
  return true;
}


x86 assembly (clang, -O0):
<handle_device_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1181 <handle_device_event+0x41>
lea    0xe90(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_device_event+0xb8>
cmpl   $0x0,-0x1c(%rbp)
jne    11a3 <handle_device_event+0x63>
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_device_event+0xb8>
movslq -0x1c(%rbp),%rax
cmp    $0xc,%rax
je     11c9 <handle_device_event+0x89>
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <handle_device_event+0xb8>
jmp    11ce <handle_device_event+0x8e>
jmp    11d3 <handle_device_event+0x93>
mov    -0x18(%rbp),%eax
sub    $0x80,%eax
jne    11f1 <handle_device_event+0xb1>
jmp    11e6 <handle_device_event+0xa6>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <handle_switch@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_device_event>:
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rsi
mov    $0xc,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1169 <handle_device_event+0x29>
cmp    $0xc,%eax
je     1179 <handle_device_event+0x39>
cmp    $0xffffffff,%eax
je     119c <handle_device_event+0x5c>
lea    0xe9e(%rip),%rdi
jmp    1170 <handle_device_event+0x30>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1194 <handle_device_event+0x54>
mov    $0x1,%ebx
cmpl   $0x80,(%rsp)
jne    1194 <handle_device_event+0x54>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edi
call   1050 <handle_switch@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    1194 <handle_device_event+0x54>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@.str = [5 x i8] c"read\00"
@.str.1 = [5 x i8] c"EOF\0A\00"
@.str.2 = [15 x i8] c"Invalid input\0A\00"
define i32 @handle_device_event(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.input_event
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @read(i32 %6, %struct.input_event* %4, i32 12)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %35
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %35
17: ; preds = %12
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = icmp ne i64 %19, 12
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %35
23: ; preds = %17
br label %24
24: ; preds = %23
br label %25
25: ; preds = %24
%26 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 0
%27 = load i32, i32* %26
switch i32 %27, label %34 [
i32 128, label %28
]
28: ; preds = %25
%29 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 2
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @handle_switch(i32 %30, i32 %32)
br label %34
34: ; preds = %25, %28
store i32 1, i32* %2
br label %35
35: ; preds = %34, %21, %15, %10
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@.str = [5 x i8] c"read\00"
@str = [14 x i8] c"Invalid input\00"
@str.3 = [4 x i8] c"EOF\00"
define i32 @handle_device_event(i32 %0) {
%2 = alloca %struct.input_event
%3 = bitcast %struct.input_event* %2 to i8*
%4 = call i32 @read(i32 %0, %struct.input_event* %2, i32 12)
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %7
i32 12, label %11
]
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.3, i64 0, i64 0))
br label %21
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %21
11: ; preds = %1
%12 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 128
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 2
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i32 @handle_switch(i32 %17, i32 %19)
br label %21
21: ; preds = %15, %11, %9, %7, %5
%22 = phi i32 [ 0, %5 ], [ 0, %7 ], [ 0, %9 ], [ 1, %11 ], [ 1, %15 ]
ret i32 %22
}