#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int open_input_device(char *input_name) {
  struct dirent *dp;
  DIR *dir;
  char dev[20];
  char name[32];
  int fd;
  dir = opendir("/dev/input/");
  if (!dir)
    return -1;
  while ((dp = readdir(dir)) != NULL) {
    if (dp->d_name && !strncmp(dp->d_name, "event", 5)) {
      sprintf(dev, "/dev/input/%s", dp->d_name);
      fd = open(dev, O_RDONLY);
      if (fd == -1)
        continue;
      if (ioctl(fd, EVIOCGNAME(32), name) < 0) {
        close(fd);
        continue;
      }
      if (!strcmp(name, input_name)) {
        closedir(dir);
        return fd;
      }
      close(fd);
    }
  }
  closedir(dir);
  return -1;
}


x86 assembly (clang, -O0):
<open_input_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <open_input_device+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <open_input_device+0x135>
jmp    11d8 <open_input_device+0x38>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c5 <open_input_device+0x125>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12c0 <open_input_device+0x120>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe01(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12c0 <open_input_device+0x120>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xde2(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    1259 <open_input_device+0xb9>
jmp    11d8 <open_input_device+0x38>
mov    -0x64(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    $0x20,%edi
call   10b0 <EVIOCGNAME@plt>
mov    -0x68(%rbp),%edi
mov    %eax,%esi
lea    -0x60(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    128e <open_input_device+0xee>
mov    -0x64(%rbp),%edi
call   1060 <close@plt>
jmp    11d8 <open_input_device+0x38>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12b8 <open_input_device+0x118>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <open_input_device+0x135>
mov    -0x64(%rbp),%edi
call   1060 <close@plt>
jmp    11d8 <open_input_device+0x38>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_input_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%r15
lea    0xe48(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1292 <open_input_device+0xf2>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     129e <open_input_device+0xfe>
mov    %rax,%r12
lea    0xe23(%rip),%r14
mov    %rsp,%r13
jmp    1214 <open_input_device+0x74>
mov    %ebp,%edi
call   1060 <close@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1299 <open_input_device+0xf9>
mov    (%r12),%rdi
test   %rdi,%rdi
je     1200 <open_input_device+0x60>
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1200 <open_input_device+0x60>
mov    (%r12),%rdx
mov    %r13,%rdi
lea    0xdd6(%rip),%rsi
call   10c0 <sprintf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1200 <open_input_device+0x60>
mov    %eax,%ebp
mov    $0x20,%edi
call   10b0 <EVIOCGNAME@plt>
mov    %ebp,%edi
mov    %eax,%esi
lea    0x20(%rsp),%r12
mov    %r12,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11ee <open_input_device+0x4e>
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    11ee <open_input_device+0x4e>
jmp    129e <open_input_device+0xfe>
mov    $0xffffffff,%ebp
jmp    12a6 <open_input_device+0x106>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/dev/input/\00"
@.str.1 = [6 x i8] c"event\00"
@.str.2 = [14 x i8] c"/dev/input/%s\00"
@O_RDONLY = global i32 0
define i32 @open_input_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca [20 x i8]
%7 = alloca [32 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i32 -1, i32* %2
br label %66
13: ; preds = %1
br label %14
14: ; preds = %62, %47, %40, %13
%15 = load i32*, i32** %5
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %63
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %62
23: ; preds = %18
%24 = load %struct.dirent*, %struct.dirent** %4
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strncmp(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%28 = icmp ne i32 %27, 0
br i1 %28, label %62, label %29
29: ; preds = %23
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%31 = load %struct.dirent*, %struct.dirent** %4
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %29
br label %14
41: ; preds = %29
%42 = load i32, i32* %8
%43 = call i32 @EVIOCGNAME(i32 32)
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%45 = call i64 @ioctl(i32 %42, i32 %43, i8* %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %41
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
br label %14
50: ; preds = %41
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%52 = load i8*, i8** %3
%53 = call i32 @strcmp(i8* %51, i8* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %50
%56 = load i32*, i32** %5
%57 = call i32 @closedir(i32* %56)
%58 = load i32, i32* %8
store i32 %58, i32* %2
br label %66
59: ; preds = %50
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %59, %23, %18
br label %14
63: ; preds = %14
%64 = load i32*, i32** %5
%65 = call i32 @closedir(i32* %64)
store i32 -1, i32* %2
br label %66
66: ; preds = %63, %55, %12
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/dev/input/\00"
@.str.1 = [6 x i8] c"event\00"
@.str.2 = [14 x i8] c"/dev/input/%s\00"
@O_RDONLY = global i32 0
define i32 @open_input_device(i8* nocapture %0) {
%2 = alloca [20 x i8]
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %40, label %8
8: ; preds = %1
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %37, label %11
11: ; preds = %8, %31
%12 = phi %struct.dirent* [ %32, %31 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %31, label %16
16: ; preds = %11
%17 = call i32 @strncmp(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = load i8*, i8** %13
%21 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %4, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = call i32 @EVIOCGNAME(i32 32)
%27 = call i64 @ioctl(i32 %23, i32 %26, i8* %5)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25, %34
%30 = call i32 @close(i32 %23)
br label %31
31: ; preds = %29, %11, %16, %19
%32 = call %struct.dirent* @readdir(i32* %6)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %37, label %11
34: ; preds = %25
%35 = call i32 @strcmp(i8* %5, i8* dereferenceable(1) %0)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %29
37: ; preds = %31, %34, %8
%38 = phi i32 [ -1, %8 ], [ %23, %34 ], [ -1, %31 ]
%39 = call i32 @closedir(i32* %6)
br label %40
40: ; preds = %37, %1
%41 = phi i32 [ -1, %1 ], [ %38, %37 ]
ret i32 %41
}