#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 closedir(int /*<<< orphan*/ *);
scalar_t__ is_correct_input_type(char *);
int /*<<< orphan*/ loop_read_input_file(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int find_remote_control_device() {
  struct dirent *de;
  DIR *dr = opendir("/dev/input");
  if (dr == NULL) {
    printf("Could not open current directory");
    return 1;
  }
  char path[25];
  while ((de = readdir(dr)) != NULL) {
    sprintf(path, "/dev/input/%s", de->d_name);
    if (is_correct_input_type(path)) {
      printf("found %s as remote control device\n", path);
      loop_read_input_file(path);
    }
  }
  return closedir(dr);
}


x86 assembly (clang, -O0):
<find_remote_control_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe81(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <find_remote_control_device+0x3d>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <find_remote_control_device+0xaf>
jmp    11b2 <find_remote_control_device+0x42>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1213 <find_remote_control_device+0xa3>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe51(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x40(%rbp),%rdi
call   1030 <is_correct_input_type@plt>
cmp    $0x0,%rax
je     120e <find_remote_control_device+0x9e>
lea    -0x40(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
call   1080 <loop_read_input_file@plt>
jmp    11b2 <find_remote_control_device+0x42>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_remote_control_device>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe7e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11f9 <find_remote_control_device+0x89>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11ef <find_remote_control_device+0x7f>
lea    0xe89(%rip),%r14
mov    %rsp,%rbx
lea    0xe8d(%rip),%r12
jmp    11bd <find_remote_control_device+0x4d>
nop
mov    %r15,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11ef <find_remote_control_device+0x7f>
mov    (%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <is_correct_input_type@plt>
test   %rax,%rax
je     11b0 <find_remote_control_device+0x40>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <loop_read_input_file@plt>
jmp    11b0 <find_remote_control_device+0x40>
mov    %r15,%rdi
call   1060 <closedir@plt>
jmp    120c <find_remote_control_device+0x9c>
lea    0xe0b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [33 x i8] c"Could not open current directory\00"
@.str.2 = [14 x i8] c"/dev/input/%s\00"
@.str.3 = [35 x i8] c"found %s as remote control device\0A\00"
define i32 @find_remote_control_device() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca [25 x i8]
%5 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %33
10: ; preds = %0
br label %11
11: ; preds = %29, %10
%12 = load i32*, i32** %3
%13 = call %struct.dirent* @readdir(i32* %12)
store %struct.dirent* %13, %struct.dirent** %2
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%17 = load %struct.dirent*, %struct.dirent** %2
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%22 = call i64 @is_correct_input_type(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %15
%25 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%28 = call i32 @loop_read_input_file(i8* %27)
br label %29
29: ; preds = %24, %15
br label %11
30: ; preds = %11
%31 = load i32*, i32** %3
%32 = call i32 @closedir(i32* %31)
store i32 %32, i32* %1
br label %33
33: ; preds = %30, %8
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [33 x i8] c"Could not open current directory\00"
@.str.2 = [14 x i8] c"/dev/input/%s\00"
@.str.3 = [35 x i8] c"found %s as remote control device\0A\00"
define i32 @find_remote_control_device() {
%1 = alloca [25 x i8]
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %25
6: ; preds = %0
%7 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%8 = call %struct.dirent* @readdir(i32* %2)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %23, label %10
10: ; preds = %6, %20
%11 = phi %struct.dirent* [ %21, %20 ], [ %8, %6 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = call i64 @is_correct_input_type(i8* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%19 = call i32 @loop_read_input_file(i8* %7)
br label %20
20: ; preds = %17, %10
%21 = call %struct.dirent* @readdir(i32* %2)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %23, label %10
23: ; preds = %20, %6
%24 = call i32 @closedir(i32* %2)
br label %25
25: ; preds = %23, %4
%26 = phi i32 [ 1, %4 ], [ %24, %23 ]
ret i32 %26
}