#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 uhid_event {
  int type;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ ev;

/* Variables and functions */
int EFAULT;
#define UHID_CLOSE 133
#define UHID_OPEN 132
#define UHID_OUTPUT 131
#define UHID_OUTPUT_EV 130
#define UHID_START 129
#define UHID_STOP 128
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ handle_output(struct uhid_event *);
int /*<<< orphan*/ memset(struct uhid_event *, int /*<<< orphan*/, int);
int read(int, struct uhid_event *, int);
int /*<<< orphan*/ stderr;
int event(int fd) {
  struct uhid_event ev;
  ssize_t ret;
  memset(&ev, 0, sizeof(ev));
  ret = read(fd, &ev, sizeof(ev));
  if (ret == 0) {
    fprintf(stderr, "Read HUP on uhid-cdev\n");
    return -EFAULT;
  } else if (ret < 0) {
    fprintf(stderr, "Cannot read uhid-cdev: %m\n");
    return -errno;
  } else if (ret != sizeof(ev)) {
    fprintf(stderr, "Invalid size read from uhid-dev: %zd != %zu\n", ret,
            sizeof(ev));
    return -EFAULT;
  }
  switch (ev.type) {
  case UHID_START:
    fprintf(stderr, "UHID_START from uhid-dev\n");
    break;
  case UHID_STOP:
    fprintf(stderr, "UHID_STOP from uhid-dev\n");
    break;
  case UHID_OPEN:
    fprintf(stderr, "UHID_OPEN from uhid-dev\n");
    break;
  case UHID_CLOSE:
    fprintf(stderr, "UHID_CLOSE from uhid-dev\n");
    break;
  case UHID_OUTPUT:
    fprintf(stderr, "UHID_OUTPUT from uhid-dev\n");
    handle_output(&ev);
    break;
  case UHID_OUTPUT_EV:
    fprintf(stderr, "UHID_OUTPUT_EV from uhid-dev\n");
    break;
  default:
    fprintf(stderr, "Invalid event from uhid-dev: %u\n", ev.type);
  }
  return 0;
}


x86 assembly (clang, -O0):
<event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a3 <event+0x63>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2e5f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <event+0x1dc>
cmpl   $0x0,-0x14(%rbp)
jge    11d7 <event+0x97>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2e03(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <event+0x1dc>
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     1217 <event+0xd7>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe52(%rip),%rsi
mov    $0x4,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2deb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <event+0x1dc>
jmp    121c <event+0xdc>
jmp    1221 <event+0xe1>
mov    -0x10(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x5,%eax
ja     12fb <event+0x1bb>
mov    -0x20(%rbp),%rax
lea    0xdbf(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1315 <event+0x1d5>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1315 <event+0x1d5>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1315 <event+0x1d5>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1315 <event+0x1d5>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x10(%rbp),%rdi
call   1060 <handle_output@plt>
jmp    1315 <event+0x1d5>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1315 <event+0x1d5>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<event>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11b0 <event+0x70>
js     11c9 <event+0x89>
cmp    $0x4,%eax
jne    11ec <event+0xac>
mov    0x8(%rsp),%edx
lea    -0x80(%rdx),%eax
cmp    $0x5,%eax
ja     1223 <event+0xe3>
lea    0xe6e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xee6(%rip),%rsi
jmp    12ab <event+0x16b>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    120d <event+0xcd>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2de6(%rip),%rax
jmp    1214 <event+0xd4>
mov    0x2de5(%rip),%rcx
mov    (%rcx),%edi
lea    0xe4e(%rip),%rsi
xor    %r14d,%r14d
mov    $0x4,%ecx
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2de4(%rip),%rax
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xee3(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1217 <event+0xd7>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
jmp    12ab <event+0x16b>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
jmp    12ab <event+0x16b>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0x8(%rsp),%rdi
call   1060 <handle_output@plt>
jmp    1217 <event+0xd7>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
jmp    12ab <event+0x16b>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1217 <event+0xd7>

LLVM IR (-O0):
%struct.uhid_event = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Read HUP on uhid-cdev\0A\00"
@EFAULT = global i32 0
@.str.1 = [27 x i8] c"Cannot read uhid-cdev: %m\0A\00"
@errno = global i32 0
@.str.2 = [45 x i8] c"Invalid size read from uhid-dev: %zd != %zu\0A\00"
@.str.3 = [26 x i8] c"UHID_START from uhid-dev\0A\00"
@.str.4 = [25 x i8] c"UHID_STOP from uhid-dev\0A\00"
@.str.5 = [25 x i8] c"UHID_OPEN from uhid-dev\0A\00"
@.str.6 = [26 x i8] c"UHID_CLOSE from uhid-dev\0A\00"
@.str.7 = [27 x i8] c"UHID_OUTPUT from uhid-dev\0A\00"
@.str.8 = [30 x i8] c"UHID_OUTPUT_EV from uhid-dev\0A\00"
@.str.9 = [33 x i8] c"Invalid event from uhid-dev: %u\0A\00"
define i32 @event(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.uhid_event
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i32 @memset(%struct.uhid_event* %4, i32 0, i32 4)
%7 = load i32, i32* %3
%8 = call i32 @read(i32 %7, %struct.uhid_event* %4, i32 4)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EFAULT
%15 = sub nsw i32 0, %14
store i32 %15, i32* %2
br label %64
16: ; preds = %1
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %2
br label %64
24: ; preds = %16
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = icmp ne i64 %26, 4
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i32, i32* %5
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %30, i64 4)
%32 = load i32, i32* @EFAULT
%33 = sub nsw i32 0, %32
store i32 %33, i32* %2
br label %64
34: ; preds = %24
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
%37 = getelementptr inbounds %struct.uhid_event, %struct.uhid_event* %4, i32 0, i32 0
%38 = load i32, i32* %37
switch i32 %38, label %58 [
i32 129, label %39
i32 128, label %42
i32 132, label %45
i32 133, label %48
i32 131, label %51
i32 130, label %55
]
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %63
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %63
45: ; preds = %36
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %63
48: ; preds = %36
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %63
51: ; preds = %36
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 @handle_output(%struct.uhid_event* %4)
br label %63
55: ; preds = %36
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %63
58: ; preds = %36
%59 = load i32, i32* @stderr
%60 = getelementptr inbounds %struct.uhid_event, %struct.uhid_event* %4, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %58, %55, %51, %48, %45, %42, %39
store i32 0, i32* %2
br label %64
64: ; preds = %63, %28, %19, %11
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.uhid_event = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Read HUP on uhid-cdev\0A\00"
@EFAULT = global i32 0
@.str.1 = [27 x i8] c"Cannot read uhid-cdev: %m\0A\00"
@errno = global i32 0
@.str.2 = [45 x i8] c"Invalid size read from uhid-dev: %zd != %zu\0A\00"
@.str.3 = [26 x i8] c"UHID_START from uhid-dev\0A\00"
@.str.4 = [25 x i8] c"UHID_STOP from uhid-dev\0A\00"
@.str.5 = [25 x i8] c"UHID_OPEN from uhid-dev\0A\00"
@.str.6 = [26 x i8] c"UHID_CLOSE from uhid-dev\0A\00"
@.str.7 = [27 x i8] c"UHID_OUTPUT from uhid-dev\0A\00"
@.str.8 = [30 x i8] c"UHID_OUTPUT_EV from uhid-dev\0A\00"
@.str.9 = [33 x i8] c"Invalid event from uhid-dev: %u\0A\00"
define i32 @event(i32 %0) {
%2 = alloca %struct.uhid_event
%3 = bitcast %struct.uhid_event* %2 to i8*
%4 = call i32 @memset(%struct.uhid_event* %2, i32 0, i32 4)
%5 = call i32 @read(i32 %0, %struct.uhid_event* %2, i32 4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EFAULT
%11 = sub nsw i32 0, %10
br label %51
12: ; preds = %1
%13 = icmp slt i32 %5, 0
br i1 %13, label %14, label %19
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %51
19: ; preds = %12
%20 = icmp eq i32 %5, 4
br i1 %20, label %26, label %21
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %5, i64 4)
%24 = load i32, i32* @EFAULT
%25 = sub nsw i32 0, %24
br label %51
26: ; preds = %19
%27 = getelementptr inbounds %struct.uhid_event, %struct.uhid_event* %2, i64 0, i32 0
%28 = load i32, i32* %27
switch i32 %28, label %48 [
i32 129, label %29
i32 128, label %32
i32 132, label %35
i32 133, label %38
i32 131, label %41
i32 130, label %45
]
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %51
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %51
35: ; preds = %26
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %51
38: ; preds = %26
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %51
41: ; preds = %26
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @handle_output(%struct.uhid_event* %2)
br label %51
45: ; preds = %26
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %51
48: ; preds = %26
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %28)
br label %51
51: ; preds = %29, %32, %35, %38, %41, %45, %48, %21, %14, %7
%52 = phi i32 [ %11, %7 ], [ %18, %14 ], [ %25, %21 ], [ 0, %48 ], [ 0, %45 ], [ 0, %41 ], [ 0, %38 ], [ 0, %35 ], [ 0, %32 ], [ 0, %29 ]
ret i32 %52
}