#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*/ 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: %ld != %lu\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");
    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    1193 <event+0x63>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2e6f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1303 <event+0x1d3>
cmpl   $0x0,-0x14(%rbp)
jge    11c7 <event+0x97>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2e13(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1303 <event+0x1d3>
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     1207 <event+0xd7>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe62(%rip),%rsi
mov    $0x4,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2dfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1303 <event+0x1d3>
jmp    120c <event+0xdc>
jmp    1211 <event+0xe1>
mov    -0x10(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x5,%eax
ja     12e2 <event+0x1b2>
mov    -0x20(%rbp),%rax
lea    0xdcf(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <event+0x1cc>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <event+0x1cc>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <event+0x1cc>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <event+0x1cc>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <event+0x1cc>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <event+0x1cc>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe21(%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     11a0 <event+0x70>
js     11b9 <event+0x89>
cmp    $0x4,%eax
jne    11e2 <event+0xb2>
mov    0x8(%rsp),%edx
lea    -0x80(%rdx),%eax
cmp    $0x5,%eax
ja     120f <event+0xdf>
lea    0xe7e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xef6(%rip),%rsi
jmp    1283 <event+0x153>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1203 <event+0xd3>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2df6(%rip),%rax
sub    (%rax),%r14d
jmp    128d <event+0x15d>
mov    0x2def(%rip),%rcx
mov    (%rcx),%edi
lea    0xe58(%rip),%rsi
xor    %r14d,%r14d
mov    $0x4,%ecx
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2dee(%rip),%rax
sub    (%rax),%r14d
jmp    128d <event+0x15d>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xef7(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    128d <event+0x15d>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
jmp    1283 <event+0x153>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xeab(%rip),%rsi
jmp    1283 <event+0x153>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
jmp    1283 <event+0x153>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
jmp    1283 <event+0x153>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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: %ld != %lu\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 %63
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 %63
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 %63
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 %57 [
i32 129, label %39
i32 128, label %42
i32 132, label %45
i32 133, label %48
i32 131, label %51
i32 130, label %54
]
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 %62
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 %62
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 %62
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 %62
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))
br label %62
54: ; preds = %36
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %62
57: ; preds = %36
%58 = load i32, i32* @stderr
%59 = getelementptr inbounds %struct.uhid_event, %struct.uhid_event* %4, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %57, %54, %51, %48, %45, %42, %39
store i32 0, i32* %2
br label %63
63: ; preds = %62, %28, %19, %11
%64 = load i32, i32* %2
ret i32 %64
}

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: %ld != %lu\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 %50
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 %50
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 %50
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 %47 [
i32 129, label %29
i32 128, label %32
i32 132, label %35
i32 133, label %38
i32 131, label %41
i32 130, label %44
]
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 %50
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 %50
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 %50
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 %50
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))
br label %50
44: ; preds = %26
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0))
br label %50
47: ; preds = %26
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i32 %28)
br label %50
50: ; preds = %29, %32, %35, %38, %41, %44, %47, %21, %14, %7
%51 = phi i32 [ %11, %7 ], [ %18, %14 ], [ %25, %21 ], [ 0, %47 ], [ 0, %44 ], [ 0, %41 ], [ 0, %38 ], [ 0, %35 ], [ 0, %32 ], [ 0, %29 ]
ret i32 %51
}