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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int open_device(int index) {
  int fd;
  {
    char path[256];
    sprintf(path, "/dev/input/event%d", index);
    if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
      fprintf(stderr, "Cannot open %s: %s.\n", path, strerror(errno));
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xea1(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d5 <open_device+0x95>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x11c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x118(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x11c(%rbp),%edi
mov    -0x118(%rbp),%rdx
mov    %rax,%rcx
lea    0xe43(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_device>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    %edi,%edx
lea    0xeae(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
mov    0x2e7c(%rip),%rax
mov    0x2e7d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a8 <open_device+0x68>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe78(%rip),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/dev/input/event%d\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Cannot open %s: %s.\0A\00"
@errno = global i32 0
define i32 @open_device(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
store i32 %0, i32* %2
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* %2
%7 = call i32 @sprintf(i8* %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %18)
br label %20
20: ; preds = %14, %1
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
@.str = [19 x i8] c"/dev/input/event%d\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Cannot open %s: %s.\0A\00"
@errno = global i32 0
define i32 @open_device(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %13)
br label %15
15: ; preds = %10, %1
ret i32 %8
}