#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 const *d_name;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ ksft_exit_fail_msg(char *, char *);
int /*<<< orphan*/ ksft_exit_skip(char *, char const *);
int open(char *, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strncmp(char const *, char *, int);
int lirc_open(const char *rc) {
  struct dirent *dent;
  char buf[256 + 256];
  DIR *d;
  int fd;
  snprintf(buf, sizeof(buf), "/sys/class/rc/%s", rc);
  d = opendir(buf);
  if (!d)
    ksft_exit_fail_msg("cannot open %s: %m\n", buf);
  while ((dent = readdir(d)) != NULL) {
    if (!strncmp(dent->d_name, "lirc", 4)) {
      snprintf(buf, sizeof(buf), "/dev/%s", dent->d_name);
      break;
    }
  }
  if (!dent)
    ksft_exit_skip("cannot find lirc device for %s\n", rc);
  closedir(d);
  fd = open(buf, O_RDWR | O_NONBLOCK);
  if (fd == -1)
    ksft_exit_fail_msg("cannot open: %s: %m\n", buf);
  return fd;
}


x86 assembly (clang, -O0):
<lirc_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x200,%esi
lea    0xe5a(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11df <lirc_open+0x5f>
lea    -0x210(%rbp),%rsi
lea    0xe37(%rip),%rdi
call   1050 <ksft_exit_fail_msg@plt>
jmp    11e4 <lirc_open+0x64>
mov    -0x218(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1248 <lirc_open+0xc8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe19(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1243 <lirc_open+0xc3>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x200,%esi
lea    0xdf1(%rip),%rdx
call   1060 <snprintf@plt>
jmp    1248 <lirc_open+0xc8>
jmp    11e4 <lirc_open+0x64>
cmpq   $0x0,-0x10(%rbp)
jne    1263 <lirc_open+0xe3>
mov    -0x8(%rbp),%rsi
lea    0xdd4(%rip),%rdi
call   1070 <ksft_exit_skip@plt>
mov    -0x218(%rbp),%rdi
call   1080 <closedir@plt>
lea    -0x210(%rbp),%rdi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    0x2d5a(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0xffffffff,-0x21c(%rbp)
jne    12b3 <lirc_open+0x133>
lea    -0x210(%rbp),%rsi
lea    0xda4(%rip),%rdi
call   1050 <ksft_exit_fail_msg@plt>
mov    -0x21c(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lirc_open>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
lea    0xe68(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11ca <lirc_open+0x4a>
lea    0xe4f(%rip),%rdi
mov    %rsp,%rsi
call   1050 <ksft_exit_fail_msg@plt>
lea    0xe54(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     121d <lirc_open+0x9d>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11e0 <lirc_open+0x60>
mov    (%rbx),%rcx
lea    0xe1c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x200,%esi
call   1060 <snprintf@plt>
jmp    122c <lirc_open+0xac>
lea    0xe0e(%rip),%rdi
mov    %r14,%rsi
call   1070 <ksft_exit_skip@plt>
mov    %r12,%rdi
call   1080 <closedir@plt>
mov    0x2dad(%rip),%rax
mov    0x2d9e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1264 <lirc_open+0xe4>
lea    0xdf6(%rip),%rdi
mov    %rsp,%rsi
call   1050 <ksft_exit_fail_msg@plt>
mov    %ebx,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"/sys/class/rc/%s\00"
@.str.1 = [20 x i8] c"cannot open %s: %m\0A\00"
@.str.2 = [5 x i8] c"lirc\00"
@.str.3 = [8 x i8] c"/dev/%s\00"
@.str.4 = [32 x i8] c"cannot find lirc device for %s\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.5 = [21 x i8] c"cannot open: %s: %m\0A\00"
define i32 @lirc_open(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.dirent*
%4 = alloca [512 x i8]
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %2
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @snprintf(i8* %7, i32 512, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%16 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %1
br label %18
18: ; preds = %34, %17
%19 = load i32*, i32** %5
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %3
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %3
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @strncmp(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%27 = icmp ne i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%30 = load %struct.dirent*, %struct.dirent** %3
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @snprintf(i8* %29, i32 512, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %35
34: ; preds = %22
br label %18
35: ; preds = %28, %18
%36 = load %struct.dirent*, %struct.dirent** %3
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i8*, i8** %2
%40 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %35
%42 = load i32*, i32** %5
%43 = call i32 @closedir(i32* %42)
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%45 = load i32, i32* @O_RDWR
%46 = load i32, i32* @O_NONBLOCK
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %44, i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%53 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %41
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"/sys/class/rc/%s\00"
@.str.1 = [20 x i8] c"cannot open %s: %m\0A\00"
@.str.2 = [5 x i8] c"lirc\00"
@.str.3 = [8 x i8] c"/dev/%s\00"
@.str.4 = [32 x i8] c"cannot find lirc device for %s\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.5 = [21 x i8] c"cannot open: %s: %m\0A\00"
define i32 @lirc_open(i8* %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 512, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32* @opendir(i8* %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %9
9: ; preds = %7, %1
br label %10
10: ; preds = %9, %13
%11 = call %struct.dirent* @readdir(i32* %5)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @strncmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %10
18: ; preds = %13
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @snprintf(i8* %3, i32 512, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %24
22: ; preds = %10
%23 = call i32 @ksft_exit_skip(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %24
24: ; preds = %18, %22
%25 = call i32 @closedir(i32* %5)
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %27, %26
%29 = call i32 @open(i8* %3, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @ksft_exit_fail_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %3)
br label %33
33: ; preds = %31, %24
ret i32 %29
}