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

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ EVIOCGRAB;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, void *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ sleep(int);
char *strerror(scalar_t__);
int main() {
  int i, fd;
  char fname[] = "/dev/input/event0";
  for (i = 0; i < 10; ++i) {
    fname[sizeof(fname) - 2] = i + '0';
    fd = open(fname, O_RDWR);
    if (fd == -1) {
      if (errno == EACCES)
        printf("cannot open %s for r/w\n", fname);
      continue;
    }
    if (ioctl(fd, EVIOCGRAB, (void *)1) == 0)
      printf("grabbed %s\n", fname);
    else
      printf("cannot grab %s: %s\n", fname, strerror(errno));
  }
  sleep(5);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe94(%rip),%ax
mov    %ax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1260 <main+0x110>
mov    -0x8(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ea <main+0x9a>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11e5 <main+0x95>
lea    -0x20(%rbp),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1252 <main+0x102>
mov    -0xc(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    1221 <main+0xd1>
lea    -0x20(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124d <main+0xfd>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1252 <main+0x102>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1187 <main+0x37>
mov    $0x5,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movaps 0xe9b(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x30,0x10(%rsp)
mov    $0xfffffff6,%ebx
mov    0x2e6c(%rip),%rbp
mov    %rsp,%r13
mov    0x2e42(%rip),%r14
mov    0x2e53(%rip),%r15
lea    0xea2(%rip),%r12
jmp    11bc <main+0x6c>
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%rdi
call   1060 <strerror@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
inc    %ebx
je     1212 <main+0xc2>
lea    0x3a(%rbx),%eax
mov    %al,0x10(%rsp)
mov    0x0(%rbp),%esi
mov    %r13,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0xa0>
mov    (%r15),%esi
mov    $0x1,%edx
mov    %eax,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
lea    0xe3c(%rip),%rdi
jmp    1206 <main+0xb6>
mov    (%r14),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <main+0x68>
lea    0xe0c(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11b8 <main+0x68>
mov    $0x5,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.fname = [18 x i8] c"/dev/input/event0\00"
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str = [24 x i8] c"cannot open %s for r/w\0A\00"
@EVIOCGRAB = global i32 0
@.str.1 = [12 x i8] c"grabbed %s\0A\00"
@.str.2 = [20 x i8] c"cannot grab %s: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [18 x i8]
store i32 0, i32* %1
%5 = bitcast [18 x i8]* %4 to i8*
store i32 0, i32* %2
br label %6
6: ; preds = %41, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 10
br i1 %8, label %9, label %44
9: ; preds = %6
%10 = load i32, i32* %2
%11 = add nsw i32 %10, 48
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 16
store i8 %12, i8* %13
%14 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %9
%20 = load i64, i64* @errno
%21 = load i64, i64* @EACCES
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %19
br label %41
27: ; preds = %9
%28 = load i32, i32* %3
%29 = load i32, i32* @EVIOCGRAB
%30 = call i64 @ioctl(i32 %28, i32 %29, i8* inttoptr (i64 1 to i8*))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %40
35: ; preds = %27
%36 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %38)
br label %40
40: ; preds = %35, %32
br label %41
41: ; preds = %40, %26
%42 = load i32, i32* %2
%43 = add nsw i32 %42, 1
store i32 %43, i32* %2
br label %6
44: ; preds = %6
%45 = call i32 @sleep(i32 5)
ret i32 0
}

LLVM IR (-O2):
@__const.main.fname = [18 x i8] c"/dev/input/event0\00"
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str = [24 x i8] c"cannot open %s for r/w\0A\00"
@EVIOCGRAB = global i32 0
@.str.1 = [12 x i8] c"grabbed %s\0A\00"
@.str.2 = [20 x i8] c"cannot grab %s: %s\0A\00"
define i32 @main() {
%1 = alloca [18 x i8]
%2 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 0
%3 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 16
br label %4
4: ; preds = %0, %27
%5 = phi i32 [ 0, %0 ], [ %28, %27 ]
%6 = trunc i32 %5 to i8
%7 = add nuw nsw i8 %6, 48
store i8 %7, i8* %3
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %2, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = load i64, i64* @EACCES
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %27
17: ; preds = %4
%18 = load i32, i32* @EVIOCGRAB
%19 = call i64 @ioctl(i32 %9, i32 %18, i8* inttoptr (i64 1 to i8*))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %27
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %25)
br label %27
27: ; preds = %21, %23, %11, %15
%28 = add nuw nsw i32 %5, 1
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %4
30: ; preds = %27
%31 = call i32 @sleep(i32 5)
ret i32 0
}