#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 dummy;
};
typedef int /*<<< orphan*/ ev;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGRAB;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pp_event(struct input_event *);
scalar_t__ read_all(int, struct input_event *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd = open(argv[1], O_RDONLY);
  struct input_event ev;
  int rv;
  fprintf(stderr, "opened device\n");
  alarm(10);
  rv = ioctl(fd, EVIOCGRAB, 1);
  fprintf(stderr, "grabbed device (%d)\n", rv);
  while ((read_all(fd, &ev, sizeof(ev)))) {
    pp_event(&ev);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xa,%edi
call   1050 <alarm@plt>
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read_all@plt>
cmp    $0x0,%rax
je     120c <main+0xac>
lea    -0x18(%rbp),%rdi
call   1030 <pp_event@plt>
jmp    11e3 <main+0x83>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
mov    0x2e52(%rip),%rbx
mov    (%rbx),%edi
lea    0xe79(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xa,%edi
call   1050 <alarm@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    (%rbx),%edi
lea    0xe59(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1080 <read_all@plt>
test   %rax,%rax
je     11fc <main+0x9c>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <pp_event@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <read_all@plt>
test   %rax,%rax
jne    11e0 <main+0x80>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"opened device\0A\00"
@EVIOCGRAB = global i32 0
@.str.1 = [21 x i8] c"grabbed device (%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.input_event
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @alarm(i32 10)
%17 = load i32, i32* %6
%18 = load i32, i32* @EVIOCGRAB
%19 = call i32 @ioctl(i32 %17, i32 %18, i32 1)
store i32 %19, i32* %8
%20 = load i32, i32* @stderr
%21 = load i32, i32* %8
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %27, %2
%24 = load i32, i32* %6
%25 = call i64 @read_all(i32 %24, %struct.input_event* %7, i32 4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @pp_event(%struct.input_event* %7)
br label %23
29: ; preds = %23
ret i32 0
}

LLVM IR (-O2):
%struct.input_event = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"opened device\0A\00"
@EVIOCGRAB = global i32 0
@.str.1 = [21 x i8] c"grabbed device (%d)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %5, i32 %6)
%8 = bitcast %struct.input_event* %3 to i8*
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @alarm(i32 10)
%12 = load i32, i32* @EVIOCGRAB
%13 = tail call i32 @ioctl(i32 %7, i32 %12, i32 1)
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%16 = call i64 @read_all(i32 %7, %struct.input_event* %3, i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %2, %18
%19 = call i32 @pp_event(%struct.input_event* %3)
%20 = call i64 @read_all(i32 %7, %struct.input_event* %3, i32 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %18
22: ; preds = %18, %2
ret i32 0
}