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

/* Variables and functions */
scalar_t__ DT_CHR;
char *alloca(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int event_add_source(char *);
int /*<<< orphan*/ event_get_name(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void event_add_all_inputs() {
  DIR *d;
  struct dirent *e;
  char *pathbuf;
  int pathsize = 0, pathbufsize = 0, fd;
  (void)fd;
  d = opendir("/dev/input");
  if (d == NULL)
    perror("opendir");
  while ((e = readdir(d)) != NULL) {
    if (e->d_type == DT_CHR) {
      pathsize = strlen("/dev/input/") + strlen(e->d_name);
      if (pathsize > pathbufsize) {
        pathbuf = alloca(pathsize - pathbufsize);
      }
      strcpy(pathbuf, "/dev/input/");
      strcat(pathbuf, e->d_name);
      fd = event_add_source(pathbuf);
    }
  }
  event_get_name(0);
  closedir(d);
}


x86 assembly (clang, -O0):
<event_add_all_inputs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe43(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11dd <event_add_all_inputs+0x3d>
lea    0xe33(%rip),%rdi
call   10b0 <perror@plt>
jmp    11e2 <event_add_all_inputs+0x42>
mov    -0x8(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1283 <event_add_all_inputs+0xe3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%rax
jne    127e <event_add_all_inputs+0xde>
lea    0xdfc(%rip),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1251 <event_add_all_inputs+0xb1>
mov    -0x1c(%rbp),%edi
sub    -0x20(%rbp),%edi
call   1090 <alloca@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   10c0 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   1050 <event_add_source@plt>
mov    %eax,-0x24(%rbp)
jmp    11e2 <event_add_all_inputs+0x42>
xor    %edi,%edi
call   1030 <event_get_name@plt>
mov    -0x8(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<event_add_all_inputs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe4e(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1268 <event_add_all_inputs+0xc8>
mov    %r15,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     124b <event_add_all_inputs+0xab>
mov    %rax,%rbx
mov    0x2e06(%rip),%r13
lea    0xe32(%rip),%r14
jmp    121f <event_add_all_inputs+0x7f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    0x8(%rbx),%rsi
mov    %r12,%rdi
call   10c0 <strcat@plt>
mov    %r12,%rdi
call   1050 <event_add_source@plt>
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     124b <event_add_all_inputs+0xab>
mov    (%rbx),%rax
cmp    0x0(%r13),%rax
jne    120f <event_add_all_inputs+0x6f>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1070 <strlen@plt>
add    %ebp,%eax
jle    11f0 <event_add_all_inputs+0x50>
mov    %eax,%edi
call   1090 <alloca@plt>
mov    %rax,%r12
jmp    11f0 <event_add_all_inputs+0x50>
xor    %edi,%edi
call   1030 <event_get_name@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <closedir@plt>
lea    0xd9c(%rip),%rdi
call   10b0 <perror@plt>
jmp    11c3 <event_add_all_inputs+0x23>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [8 x i8] c"opendir\00"
@DT_CHR = global i64 0
@.str.2 = [12 x i8] c"/dev/input/\00"
define void @event_add_all_inputs() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* %6
%8 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %1
%9 = load i32*, i32** %1
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
br label %14
14: ; preds = %49, %13
%15 = load i32*, i32** %1
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %2
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %50
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %2
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @DT_CHR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %18
%25 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%26 = load %struct.dirent*, %struct.dirent** %2
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 1
%28 = load i8*, i8** %27
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %25, %29
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = icmp sgt i32 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %24
%35 = load i32, i32* %4
%36 = load i32, i32* %5
%37 = sub nsw i32 %35, %36
%38 = call i8* @alloca(i32 %37)
store i8* %38, i8** %3
br label %39
39: ; preds = %34, %24
%40 = load i8*, i8** %3
%41 = call i32 @strcpy(i8* %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%42 = load i8*, i8** %3
%43 = load %struct.dirent*, %struct.dirent** %2
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i8*, i8** %44
%46 = call i32 @strcat(i8* %42, i8* %45)
%47 = load i8*, i8** %3
%48 = call i32 @event_add_source(i8* %47)
store i32 %48, i32* %6
br label %49
49: ; preds = %39, %18
br label %14
50: ; preds = %14
%51 = call i32 @event_get_name(i32 0)
%52 = load i32*, i32** %1
%53 = call i32 @closedir(i32* %52)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [8 x i8] c"opendir\00"
@DT_CHR = global i64 0
@.str.2 = [12 x i8] c"/dev/input/\00"
define void @event_add_all_inputs() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = tail call %struct.dirent* @readdir(i32* %1)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %34, label %8
8: ; preds = %5, %30
%9 = phi %struct.dirent* [ %32, %30 ], [ %6, %5 ]
%10 = phi i8* [ %31, %30 ], [ undef, %5 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @DT_CHR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %8
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%18 = load i8*, i8** %17
%19 = tail call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, %16
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i8* @alloca(i32 %20)
br label %24
24: ; preds = %22, %15
%25 = phi i8* [ %23, %22 ], [ %10, %15 ]
%26 = tail call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%27 = load i8*, i8** %17
%28 = tail call i32 @strcat(i8* %25, i8* %27)
%29 = tail call i32 @event_add_source(i8* %25)
br label %30
30: ; preds = %24, %8
%31 = phi i8* [ %25, %24 ], [ %10, %8 ]
%32 = tail call %struct.dirent* @readdir(i32* %1)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %8
34: ; preds = %30, %5
%35 = tail call i32 @event_get_name(i32 0)
%36 = tail call i32 @closedir(i32* %1)
ret void
}