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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int device_open(int nr, int verbose) {
  char filename[32];
  int fd;
  snprintf(filename, sizeof(filename), "/dev/input/event%d", nr);
  fd = open(filename, O_RDONLY);
  if (-1 == fd) {
    fprintf(stderr, "open %s: %s\n", filename, strerror(errno));
    return -1;
  }
  if (verbose)
    fprintf(stderr, "%s\n", filename);
  return fd;
}


x86 assembly (clang, -O0):
<device_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe9f(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
jne    11cf <device_open+0x8f>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <device_open+0xba>
cmpl   $0x0,-0xc(%rbp)
je     11f4 <device_open+0xb4>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_open>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeaf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1199 <device_open+0x59>
test   %ebp,%ebp
je     11c6 <device_open+0x86>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11c6 <device_open+0x86>
mov    0x2e38(%rip),%rax
mov    (%rax),%ebp
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe5c(%rip),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/dev/input/event%d\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @device_open(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %20)
store i32 -1, i32* %3
br label %31
22: ; preds = %2
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %22
%30 = load i32, i32* %7
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %16
%32 = load i32, i32* %3
ret i32 %32
}

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