#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*/ name;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ EVIOCGPHYS(int);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ die_(int, char *);
int /*<<< orphan*/ drop_priv();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, char **);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ take_priv();
int monitor(const char *type, const char *dev) {
  char name[1024];
  int fd;
  if (snprintf(name, sizeof(name), "/dev/input/%s", dev) < 0)
    die_(301, ("snprintf"));
  take_priv();
  if ((fd = open(name, O_RDONLY)) < 0)
    die_(306, ("open"));
  drop_priv();
  memset(name, 0, sizeof name);
  if (ioctl(fd, EVIOCGNAME(sizeof name - 1), &name) < 0)
    die_(313, ("ioctl"));
  fprintf(stderr, "Monitoring %s \"%s\"", type, name);
  memset(name, 0, sizeof name);
  if (ioctl(fd, EVIOCGPHYS(sizeof name - 1), &name) < 0)
    die_(320, ("ioctl"));
  fprintf(stderr, " on %s\n", name);
  return fd;
}


x86 assembly (clang, -O0):
<monitor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
lea    0xe36(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    11ea <monitor+0x4a>
mov    $0x12d,%edi
lea    0xe29(%rip),%rsi
call   10b0 <die_@plt>
mov    $0x0,%al
call   1090 <take_priv@plt>
lea    -0x410(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jge    1226 <monitor+0x86>
mov    $0x132,%edi
lea    0xdf6(%rip),%rsi
call   10b0 <die_@plt>
mov    $0x0,%al
call   1030 <drop_priv@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x418(%rbp)
mov    $0x3ff,%edi
call   10c0 <EVIOCGNAME@plt>
mov    -0x418(%rbp),%edi
mov    %eax,%esi
lea    -0x410(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1285 <monitor+0xe5>
mov    $0x139,%edi
lea    0xd9c(%rip),%rsi
call   10b0 <die_@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    -0x410(%rbp),%rcx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x41c(%rbp)
mov    $0x3ff,%edi
call   10a0 <EVIOCGPHYS@plt>
mov    -0x41c(%rbp),%edi
mov    %eax,%esi
lea    -0x410(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12ff <monitor+0x15f>
mov    $0x140,%edi
lea    0xd22(%rip),%rsi
call   10b0 <die_@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rdx
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x414(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rcx
mov    %rdi,%r14
lea    0xe46(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1040 <snprintf@plt>
test   %rax,%rax
jns    11dd <monitor+0x3d>
lea    0xe3b(%rip),%rsi
mov    $0x12d,%edi
call   10b0 <die_@plt>
xor    %eax,%eax
call   1090 <take_priv@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    120c <monitor+0x6c>
lea    0xe15(%rip),%rsi
mov    $0x132,%edi
call   10b0 <die_@plt>
xor    %eax,%eax
call   1030 <drop_priv@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    $0x3ff,%edi
call   10c0 <EVIOCGNAME@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
jns    1251 <monitor+0xb1>
lea    0xdd5(%rip),%rsi
mov    $0x139,%edi
call   10b0 <die_@plt>
mov    0x2d80(%rip),%r15
mov    (%r15),%edi
lea    0xdc0(%rip),%rsi
mov    %rsp,%rbx
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    $0x3ff,%edi
call   10a0 <EVIOCGPHYS@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
jns    12ad <monitor+0x10d>
lea    0xd79(%rip),%rsi
mov    $0x140,%edi
call   10b0 <die_@plt>
mov    (%r15),%edi
lea    0xd7e(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/input/%s\00"
@.str.1 = [9 x i8] c"snprintf\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"ioctl\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"Monitoring %s \22%s\22\00"
@.str.5 = [8 x i8] c" on %s\0A\00"
define i32 @monitor(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = load i8*, i8** %4
%9 = call i64 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @die_(i32 301, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i32 (...) @take_priv()
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @die_(i32 306, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = call i32 (...) @drop_priv()
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 1024)
%25 = load i32, i32* %6
%26 = call i32 @EVIOCGNAME(i32 1023)
%27 = bitcast [1024 x i8]* %5 to i8**
%28 = call i64 @ioctl(i32 %25, i32 %26, i8** %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @die_(i32 313, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %21
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %3
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %34, i8* %35)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 1024)
%39 = load i32, i32* %6
%40 = call i32 @EVIOCGPHYS(i32 1023)
%41 = bitcast [1024 x i8]* %5 to i8**
%42 = call i64 @ioctl(i32 %39, i32 %40, i8** %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %32
%45 = call i32 @die_(i32 320, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %32
%47 = load i32, i32* @stderr
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/input/%s\00"
@.str.1 = [9 x i8] c"snprintf\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"ioctl\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"Monitoring %s \22%s\22\00"
@.str.5 = [8 x i8] c" on %s\0A\00"
define i32 @monitor(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i64 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @die_(i32 301, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = call i32 (...) @take_priv()
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %4, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @die_(i32 306, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = call i32 (...) @drop_priv()
%18 = call i32 @memset(i8* %4, i32 0, i32 1024)
%19 = call i32 @EVIOCGNAME(i32 1023)
%20 = bitcast [1024 x i8]* %3 to i8**
%21 = call i64 @ioctl(i32 %12, i32 %19, i8** %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @die_(i32 313, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %4)
%28 = call i32 @memset(i8* %4, i32 0, i32 1024)
%29 = call i32 @EVIOCGPHYS(i32 1023)
%30 = call i64 @ioctl(i32 %12, i32 %29, i8** %20)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @die_(i32 320, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %4)
ret i32 %12
}