#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*/ name;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int ioctl(int, int /*<<< orphan*/, char **);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strcmp(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int openInputEventDeviceExt(const char *inputName, char *actual) {
  int fd = -1;
  const char *dirname = "/dev/input";
  char devname[PATH_MAX];
  char *filename;
  DIR *dir;
  struct dirent *de;
  char failure_msg[40];
  snprintf(failure_msg, 40, "Could not open device %s", inputName);
  dir = opendir(dirname);
  if (dir == NULL)
    return -1;
  strcpy(devname, dirname);
  filename = devname + strlen(devname);
  *filename++ = '/';
  while ((de = readdir(dir))) {
    if ((de->d_name[0] == '.') &&
        ((de->d_name[1] == '\0') ||
         ((de->d_name[1] == '.') && (de->d_name[2] == '\0'))))
      continue;
    strcpy(filename, de->d_name);
    fd = open(devname, O_RDONLY);
    if (fd >= 0) {
      char name[80];
      if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
        name[0] = '\0';
      }
      if (!strcmp(name, inputName)) {
        if (actual)
          strcpy(actual, filename);
        break;
      } else {
        close(fd);
        fd = -1;
      }
    }
  }
  closedir(dir);
  return fd;
}


x86 assembly (clang, -O0):
<openInputEventDeviceExt>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    0xe2f(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xe8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x28,%esi
lea    0xdf3(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    124b <openInputEventDeviceExt+0x9b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
jmp    13c8 <openInputEventDeviceExt+0x218>
mov    -0xe8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0xe8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0xe8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x2f,(%rax)
mov    -0x48(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     13af <openInputEventDeviceExt+0x1ff>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12f7 <openInputEventDeviceExt+0x147>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     12f2 <openInputEventDeviceExt+0x142>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    12f7 <openInputEventDeviceExt+0x147>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    12f7 <openInputEventDeviceExt+0x147>
jmp    128c <openInputEventDeviceExt+0xdc>
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0xe8(%rbp),%rdi
mov    0x2ccb(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     13aa <openInputEventDeviceExt+0x1fa>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xec(%rbp)
mov    $0x4f,%edi
call   10d0 <EVIOCGNAME@plt>
mov    -0xec(%rbp),%edi
mov    %eax,%esi
lea    -0xe0(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x1,%eax
jge    1360 <openInputEventDeviceExt+0x1b0>
movb   $0x0,-0xe0(%rbp)
lea    -0xe0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1396 <openInputEventDeviceExt+0x1e6>
cmpq   $0x0,-0x18(%rbp)
je     1391 <openInputEventDeviceExt+0x1e1>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    13af <openInputEventDeviceExt+0x1ff>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    13aa <openInputEventDeviceExt+0x1fa>
jmp    128c <openInputEventDeviceExt+0xdc>
mov    -0x48(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x84(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openInputEventDeviceExt>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%r13
mov    %rdi,%rcx
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe20(%rip),%rdx
lea    -0xc0(%rbp),%rdi
mov    $0x28,%esi
mov    %rcx,-0x38(%rbp)
call   1060 <snprintf@plt>
lea    0xdf9(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12f7 <openInputEventDeviceExt+0x147>
mov    %rax,%rbx
lea    0xde1(%rip),%rsi
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%r14
movb   $0x2f,(%r12,%r14,1)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12ff <openInputEventDeviceExt+0x14f>
mov    %r13,-0x30(%rbp)
lea    (%r12,%r14,1),%r15
add    $0x1,%r15
mov    $0xffffffff,%r13d
jmp    1271 <openInputEventDeviceExt+0xc1>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1305 <openInputEventDeviceExt+0x155>
mov    (%rax),%rsi
cmpb   $0x2e,(%rsi)
jne    128b <openInputEventDeviceExt+0xdb>
movzbl 0x1(%rsi),%eax
test   %al,%al
je     1260 <openInputEventDeviceExt+0xb0>
cmp    $0x2e,%al
jne    128b <openInputEventDeviceExt+0xdb>
cmpb   $0x0,0x2(%rsi)
je     1260 <openInputEventDeviceExt+0xb0>
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10c0 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
js     1260 <openInputEventDeviceExt+0xb0>
mov    $0x4f,%edi
call   10d0 <EVIOCGNAME@plt>
mov    %r13d,%edi
mov    %eax,%esi
lea    -0x90(%rbp),%r14
mov    %r14,%rdx
call   1070 <ioctl@plt>
test   %eax,%eax
jg     12d4 <openInputEventDeviceExt+0x124>
movb   $0x0,-0x90(%rbp)
mov    %r14,%rdi
mov    -0x38(%rbp),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     131f <openInputEventDeviceExt+0x16f>
mov    %r13d,%edi
call   1080 <close@plt>
mov    $0xffffffff,%r13d
jmp    1260 <openInputEventDeviceExt+0xb0>
mov    $0xffffffff,%r13d
jmp    130d <openInputEventDeviceExt+0x15d>
mov    $0xffffffff,%r13d
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rdi
test   %rdi,%rdi
je     1305 <openInputEventDeviceExt+0x155>
mov    %r15,%rsi
call   1030 <strcpy@plt>
jmp    1305 <openInputEventDeviceExt+0x155>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/dev/input\00"
@PATH_MAX = global i32 0
@.str.1 = [25 x i8] c"Could not open device %s\00"
@O_RDONLY = global i32 0
define i32 @openInputEventDeviceExt(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca %struct.dirent*
%13 = alloca [40 x i8]
%14 = alloca i32
%15 = alloca [80 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %7
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %8
%19 = alloca i8, i64 %17
store i64 %17, i64* %9
%20 = getelementptr inbounds [40 x i8], [40 x i8]* %13, i64 0, i64 0
%21 = load i8*, i8** %4
%22 = call i32 @snprintf(i8* %20, i32 40, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %7
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %11
%25 = load i32*, i32** %11
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %14
br label %113
28: ; preds = %2
%29 = load i8*, i8** %7
%30 = call i32 @strcpy(i8* %19, i8* %29)
%31 = call i32 @strlen(i8* %19)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %19, i64 %32
store i8* %33, i8** %10
%34 = load i8*, i8** %10
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %10
store i8 47, i8* %34
br label %36
36: ; preds = %108, %72, %28
%37 = load i32*, i32** %11
%38 = call %struct.dirent* @readdir(i32* %37)
store %struct.dirent* %38, %struct.dirent** %12
%39 = icmp ne %struct.dirent* %38, null
br i1 %39, label %40, label %109
40: ; preds = %36
%41 = load %struct.dirent*, %struct.dirent** %12
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 46
br i1 %47, label %48, label %73
48: ; preds = %40
%49 = load %struct.dirent*, %struct.dirent** %12
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 0
br i1 %55, label %72, label %56
56: ; preds = %48
%57 = load %struct.dirent*, %struct.dirent** %12
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 46
br i1 %63, label %64, label %73
64: ; preds = %56
%65 = load %struct.dirent*, %struct.dirent** %12
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = getelementptr inbounds i8, i8* %67, i64 2
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %64, %48
br label %36
73: ; preds = %64, %56, %40
%74 = load i8*, i8** %10
%75 = load %struct.dirent*, %struct.dirent** %12
%76 = getelementptr inbounds %struct.dirent, %struct.dirent* %75, i32 0, i32 0
%77 = load i8*, i8** %76
%78 = call i32 @strcpy(i8* %74, i8* %77)
%79 = load i32, i32* @O_RDONLY
%80 = call i32 @open(i8* %19, i32 %79)
store i32 %80, i32* %6
%81 = load i32, i32* %6
%82 = icmp sge i32 %81, 0
br i1 %82, label %83, label %108
83: ; preds = %73
%84 = load i32, i32* %6
%85 = call i32 @EVIOCGNAME(i32 79)
%86 = bitcast [80 x i8]* %15 to i8**
%87 = call i32 @ioctl(i32 %84, i32 %85, i8** %86)
%88 = icmp slt i32 %87, 1
br i1 %88, label %89, label %91
89: ; preds = %83
%90 = getelementptr inbounds [80 x i8], [80 x i8]* %15, i64 0, i64 0
store i8 0, i8* %90
br label %91
91: ; preds = %89, %83
%92 = getelementptr inbounds [80 x i8], [80 x i8]* %15, i64 0, i64 0
%93 = load i8*, i8** %4
%94 = call i32 @strcmp(i8* %92, i8* %93)
%95 = icmp ne i32 %94, 0
br i1 %95, label %104, label %96
96: ; preds = %91
%97 = load i8*, i8** %5
%98 = icmp ne i8* %97, null
br i1 %98, label %99, label %103
99: ; preds = %96
%100 = load i8*, i8** %5
%101 = load i8*, i8** %10
%102 = call i32 @strcpy(i8* %100, i8* %101)
br label %103
103: ; preds = %99, %96
br label %109
104: ; preds = %91
%105 = load i32, i32* %6
%106 = call i32 @close(i32 %105)
store i32 -1, i32* %6
br label %107
107: ; preds = %104
br label %108
108: ; preds = %107, %73
br label %36
109: ; preds = %103, %36
%110 = load i32*, i32** %11
%111 = call i32 @closedir(i32* %110)
%112 = load i32, i32* %6
store i32 %112, i32* %3
store i32 1, i32* %14
br label %113
113: ; preds = %109, %27
%114 = load i8*, i8** %8
%115 = load i32, i32* %3
ret i32 %115
}

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