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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ logprintf(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ qfscanf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strdup(char *);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ test_file(char *);
char *name_to_event_dev(char *match_name) {
  DIR *sysfs_dir;
  struct dirent *sysfs_dir_entry;
  errno = 0;
  sysfs_dir = opendir("/sys/class/input");
  if (sysfs_dir == NULL) {
    logprintf("2%s: %s", "error opening sysfs input class dir",
              strerror(errno));
    return NULL;
  }
  sysfs_dir_entry = (struct dirent *)1;
  while (sysfs_dir_entry != NULL) {
    char name_file_path[PATH_MAX];
    char cur_name[256];
    errno = 0;
    sysfs_dir_entry = readdir(sysfs_dir);
    if (errno) {
      logprintf("2%s: %s", "error reading sysfs dir entry", strerror(errno));
      closedir(sysfs_dir);
      return NULL;
    }
    if (strncmp("event", sysfs_dir_entry->d_name, 5))
      continue;
    snprintf(name_file_path, sizeof(name_file_path),
             "/sys/class/input/%s/device/name", sysfs_dir_entry->d_name);
    if (test_file(name_file_path))
      qfscanf(name_file_path, "%s", cur_name);
    if (!strcmp(cur_name, match_name)) {
      closedir(sysfs_dir);
      return strdup(sysfs_dir_entry->d_name);
    }
  }
  closedir(sysfs_dir);
  return NULL;
}


x86 assembly (clang, -O0):
<name_to_event_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e12(%rip),%rax
movq   $0x0,(%rax)
lea    0xe2c(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1219 <name_to_event_dev+0x69>
mov    0x2de9(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%edx
lea    0xe11(%rip),%rdi
lea    0xe12(%rip),%rsi
call   10d0 <logprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13f1 <name_to_event_dev+0x241>
mov    $0x1,%eax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     13e0 <name_to_event_dev+0x230>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x148(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x140(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2d6e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d53(%rip),%rax
cmpq   $0x0,(%rax)
je     12d3 <name_to_event_dev+0x123>
mov    0x2d42(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %eax,%edx
lea    0xd6a(%rip),%rdi
lea    0xd8f(%rip),%rsi
call   10d0 <logprintf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x134(%rbp)
jmp    13a2 <name_to_event_dev+0x1f2>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd7a(%rip),%rdi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1304 <name_to_event_dev+0x154>
movl   $0x2,-0x134(%rbp)
jmp    13a2 <name_to_event_dev+0x1f2>
mov    -0x140(%rbp),%rdi
mov    -0x148(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xd3f(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x140(%rbp),%rdi
call   1060 <test_file@plt>
cmp    $0x0,%rax
je     1357 <name_to_event_dev+0x1a7>
mov    -0x140(%rbp),%rdi
lea    -0x130(%rbp),%rdx
lea    0xcc4(%rip),%rsi
call   10a0 <qfscanf@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1398 <name_to_event_dev+0x1e8>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x134(%rbp)
jmp    13a2 <name_to_event_dev+0x1f2>
movl   $0x0,-0x134(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x134(%rbp),%eax
mov    %eax,-0x14c(%rbp)
test   %eax,%eax
je     13db <name_to_event_dev+0x22b>
jmp    13c2 <name_to_event_dev+0x212>
mov    -0x14c(%rbp),%eax
sub    $0x1,%eax
je     13f1 <name_to_event_dev+0x241>
jmp    13d6 <name_to_event_dev+0x226>
jmp    1222 <name_to_event_dev+0x72>
jmp    1222 <name_to_event_dev+0x72>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_to_event_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,-0x38(%rbp)
mov    0x2e09(%rip),%r14
movq   $0x0,(%r14)
lea    0xe23(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1305 <name_to_event_dev+0x155>
mov    %rsp,%r15
mov    0x2dfb(%rip),%rbx
mov    %r14,%r12
mov    (%rbx),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
movq   $0x0,(%r12)
mov    %rax,-0x30(%rbp)
mov    %rax,%rdi
jmp    1268 <name_to_event_dev+0xb8>
nopl   (%rax)
mov    %r14,%rdi
mov    -0x38(%rbp),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
mov    -0x40(%rbp),%r14
je     1333 <name_to_event_dev+0x183>
mov    %r15,%rsp
mov    %rsp,%r15
mov    (%rbx),%r14d
mov    %rsp,%r13
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2d7c(%rip),%r12
movq   $0x0,(%r12)
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    (%r12),%rdi
test   %rdi,%rdi
jne    12db <name_to_event_dev+0x12b>
mov    (%rax),%rsi
lea    0xddb(%rip),%rdi
mov    $0x5,%edx
mov    %rax,%r12
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1238 <name_to_event_dev+0x88>
mov    %r12,-0x40(%rbp)
mov    (%r12),%rcx
mov    %r13,%rdi
mov    %r14d,%esi
lea    0xdba(%rip),%rdx
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1060 <test_file@plt>
test   %rax,%rax
lea    -0x140(%rbp),%r14
je     1220 <name_to_event_dev+0x70>
mov    %r13,%rdi
lea    0xd48(%rip),%rsi
mov    %r14,%rdx
call   10a0 <qfscanf@plt>
jmp    1220 <name_to_event_dev+0x70>
call   10c0 <strerror@plt>
lea    0xd2a(%rip),%rdi
lea    0xd4f(%rip),%rsi
mov    %eax,%edx
call   10d0 <logprintf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
mov    %r15,%rsp
jmp    1324 <name_to_event_dev+0x174>
mov    (%r14),%rdi
call   10c0 <strerror@plt>
lea    0xcfd(%rip),%rdi
lea    0xcfe(%rip),%rsi
mov    %eax,%edx
call   10d0 <logprintf@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
mov    (%r14),%rdi
call   10b0 <strdup@plt>
mov    %r15,%rsp
jmp    1324 <name_to_event_dev+0x174>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@errno = global i64 0
@.str = [17 x i8] c"/sys/class/input\00"
@.str.1 = [8 x i8] c"2%s: %s\00"
@.str.2 = [36 x i8] c"error opening sysfs input class dir\00"
@PATH_MAX = global i32 0
@.str.3 = [30 x i8] c"error reading sysfs dir entry\00"
@.str.4 = [6 x i8] c"event\00"
@.str.5 = [32 x i8] c"/sys/class/input/%s/device/name\00"
@.str.6 = [3 x i8] c"%s\00"
define i8* @name_to_event_dev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [256 x i8]
%9 = alloca i32
store i8* %0, i8** %3
store i64 0, i64* @errno
%10 = call i32* @opendir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = call i32 @strerror(i64 %14)
%16 = call i32 @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %15)
store i8* null, i8** %2
br label %74
17: ; preds = %1
store %struct.dirent* inttoptr (i64 1 to %struct.dirent*), %struct.dirent** %5
br label %18
18: ; preds = %70, %67, %17
%19 = load %struct.dirent*, %struct.dirent** %5
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %71
21: ; preds = %18
%22 = load i32, i32* @PATH_MAX
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %6
%25 = alloca i8, i64 %23
store i64 %23, i64* %7
store i64 0, i64* @errno
%26 = load i32*, i32** %4
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %5
%28 = load i64, i64* @errno
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i64, i64* @errno
%32 = call i32 @strerror(i64 %31)
%33 = call i32 @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %4
%35 = call i32 @closedir(i32* %34)
store i8* null, i8** %2
store i32 1, i32* %9
br label %67
36: ; preds = %21
%37 = load %struct.dirent*, %struct.dirent** %5
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %39, i32 5)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 2, i32* %9
br label %67
43: ; preds = %36
%44 = trunc i64 %23 to i32
%45 = load %struct.dirent*, %struct.dirent** %5
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 @snprintf(i8* %25, i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %47)
%49 = call i64 @test_file(i8* %25)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%53 = call i32 @qfscanf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %43
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%56 = load i8*, i8** %3
%57 = call i32 @strcmp(i8* %55, i8* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %66, label %59
59: ; preds = %54
%60 = load i32*, i32** %4
%61 = call i32 @closedir(i32* %60)
%62 = load %struct.dirent*, %struct.dirent** %5
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i8* @strdup(i8* %64)
store i8* %65, i8** %2
store i32 1, i32* %9
br label %67
66: ; preds = %54
store i32 0, i32* %9
br label %67
67: ; preds = %66, %59, %42, %30
%68 = load i8*, i8** %6
%69 = load i32, i32* %9
switch i32 %69, label %76 [
i32 0, label %70
i32 1, label %74
i32 2, label %18
]
70: ; preds = %67
br label %18
71: ; preds = %18
%72 = load i32*, i32** %4
%73 = call i32 @closedir(i32* %72)
store i8* null, i8** %2
br label %74
74: ; preds = %71, %67, %13
%75 = load i8*, i8** %2
ret i8* %75
76: ; preds = %67
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@errno = global i64 0
@.str = [17 x i8] c"/sys/class/input\00"
@.str.1 = [8 x i8] c"2%s: %s\00"
@.str.2 = [36 x i8] c"error opening sysfs input class dir\00"
@PATH_MAX = global i32 0
@.str.3 = [30 x i8] c"error reading sysfs dir entry\00"
@.str.4 = [6 x i8] c"event\00"
@.str.5 = [32 x i8] c"/sys/class/input/%s/device/name\00"
@.str.6 = [3 x i8] c"%s\00"
define noalias i8* @name_to_event_dev(i8* nocapture %0) {
%2 = alloca [256 x i8]
store i64 0, i64* @errno
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
store i64 0, i64* @errno
%11 = call %struct.dirent* @readdir(i32* %3)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %24, label %18
14: ; preds = %1
%15 = load i64, i64* @errno
%16 = tail call i32 @strerror(i64 %15)
%17 = tail call i32 @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %58
18: ; preds = %50, %5
%19 = phi i8* [ %9, %5 ], [ %53, %50 ]
%20 = phi i64 [ %12, %5 ], [ %56, %50 ]
%21 = call i32 @strerror(i64 %20)
%22 = call i32 @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = call i32 @closedir(i32* %3)
br label %47
24: ; preds = %5, %50
%25 = phi %struct.dirent* [ %55, %50 ], [ %11, %5 ]
%26 = phi i8* [ %54, %50 ], [ %10, %5 ]
%27 = phi i8* [ %53, %50 ], [ %9, %5 ]
%28 = phi i32 [ %51, %50 ], [ %7, %5 ]
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %30, i32 5)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %24
%34 = load i8*, i8** %29
%35 = call i32 @snprintf(i8* %26, i32 %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %34)
%36 = call i64 @test_file(i8* %26)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 @qfscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %6)
br label %40
40: ; preds = %38, %33
%41 = call i32 @strcmp(i8* %6, i8* dereferenceable(1) %0)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %40
%44 = call i32 @closedir(i32* %3)
%45 = load i8*, i8** %29
%46 = call i8* @strdup(i8* %45)
br label %47
47: ; preds = %18, %43
%48 = phi i8* [ %27, %43 ], [ %19, %18 ]
%49 = phi i8* [ %46, %43 ], [ null, %18 ]
br label %58
50: ; preds = %40, %24
%51 = load i32, i32* @PATH_MAX
%52 = zext i32 %51 to i64
%53 = call i8* @llvm.stacksave()
%54 = alloca i8, i64 %52
store i64 0, i64* @errno
%55 = call %struct.dirent* @readdir(i32* %3)
%56 = load i64, i64* @errno
%57 = icmp eq i64 %56, 0
br i1 %57, label %24, label %18
58: ; preds = %47, %14
%59 = phi i8* [ null, %14 ], [ %49, %47 ]
ret i8* %59
}