#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 EINVAL;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int /*<<< orphan*/);
int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name) {
  char dpath[256];
  char fpath[256];
  char *rpmsg_ctrl_prefix = "rpmsg_ctrl";
  DIR *dir;
  struct dirent *ent;
  int fd;
  sprintf(dpath, "%s/devices/%s/rpmsg", "/sys/bus/rpmsg", rpmsg_dev_name);
  dir = opendir(dpath);
  if (dir == NULL) {
    fprintf(stderr, "Failed to open dir %s\n", dpath);
    return -EINVAL;
  }
  while ((ent = readdir(dir)) != NULL) {
    if (!strncmp(ent->d_name, rpmsg_ctrl_prefix, strlen(rpmsg_ctrl_prefix))) {
      printf("Opening file %s.\n", ent->d_name);
      sprintf(fpath, "/dev/%s", ent->d_name);
      fd = open(fpath, O_RDWR | O_NONBLOCK);
      if (fd < 0) {
        fprintf(stderr, "Failed to open rpmsg char dev %s,%s\n", fpath,
                strerror(errno));
        return fd;
      }
      sprintf(rpmsg_ctrl_name, "%s", ent->d_name);
      return fd;
    }
  }
  fprintf(stderr, "No rpmsg char dev file is found\n");
  return -EINVAL;
}


x86 assembly (clang, -O0):
<get_rpmsg_chrdev_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe56(%rip),%rax
mov    %rax,-0x228(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe48(%rip),%rsi
lea    0xe55(%rip),%rdx
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
jne    1223 <get_rpmsg_chrdev_fd+0x93>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2da7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    139f <get_rpmsg_chrdev_fd+0x20f>
jmp    1228 <get_rpmsg_chrdev_fd+0x98>
mov    -0x230(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x238(%rbp)
cmp    $0x0,%rax
je     137a <get_rpmsg_chrdev_fd+0x1ea>
mov    -0x238(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x250(%rbp)
mov    -0x228(%rbp),%rax
mov    %rax,-0x248(%rbp)
mov    -0x228(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x250(%rbp),%rdi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1375 <get_rpmsg_chrdev_fd+0x1e5>
mov    -0x238(%rbp),%rax
mov    (%rax),%rsi
lea    0xda6(%rip),%rdi
call   1060 <printf@plt>
lea    -0x220(%rbp),%rdi
mov    -0x238(%rbp),%rax
mov    (%rax),%rdx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
mov    0x2d06(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0x0,-0x23c(%rbp)
jge    134b <get_rpmsg_chrdev_fd+0x1bb>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x25c(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x258(%rbp)
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x25c(%rbp),%edi
mov    -0x258(%rbp),%rdx
mov    %rax,%rcx
lea    0xd29(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x23c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139f <get_rpmsg_chrdev_fd+0x20f>
mov    -0x18(%rbp),%rdi
mov    -0x238(%rbp),%rax
mov    (%rax),%rdx
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x23c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139f <get_rpmsg_chrdev_fd+0x20f>
jmp    1228 <get_rpmsg_chrdev_fd+0x98>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
lea    0xcfa(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2c26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rpmsg_chrdev_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
lea    0xe4f(%rip),%rsi
lea    0xe5c(%rip),%rdx
xor    %r15d,%r15d
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1284 <get_rpmsg_chrdev_fd+0xe4>
mov    %rax,%r12
lea    0xe0d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a5 <get_rpmsg_chrdev_fd+0x105>
mov    %rax,%rbx
mov    (%rax),%rbp
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1200 <get_rpmsg_chrdev_fd+0x60>
mov    (%rbx),%rsi
lea    0xe0b(%rip),%rdi
call   1070 <printf@plt>
mov    (%rbx),%rdx
lea    0xe0e(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    0x2d8b(%rip),%rax
mov    0x2d7c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     12dc <get_rpmsg_chrdev_fd+0x13c>
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    12c9 <get_rpmsg_chrdev_fd+0x129>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
lea    0x100(%rsp),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12bf <get_rpmsg_chrdev_fd+0x11f>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2cfa(%rip),%rax
sub    (%rax),%r15d
mov    %r15d,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ced(%rip),%rax
mov    (%rax),%ebx
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd65(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12c9 <get_rpmsg_chrdev_fd+0x129>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"rpmsg_ctrl\00"
@.str.1 = [20 x i8] c"%s/devices/%s/rpmsg\00"
@.str.2 = [15 x i8] c"/sys/bus/rpmsg\00"
@stderr = global i32 0
@.str.3 = [23 x i8] c"Failed to open dir %s\0A\00"
@EINVAL = global i32 0
@.str.4 = [18 x i8] c"Opening file %s.\0A\00"
@.str.5 = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [37 x i8] c"Failed to open rpmsg char dev %s,%s\0A\00"
@errno = global i32 0
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [33 x i8] c"No rpmsg char dev file is found\0A\00"
define i32 @get_rpmsg_chrdev_fd(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %8
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = load i8*, i8** %4
%14 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %76
25: ; preds = %2
br label %26
26: ; preds = %70, %25
%27 = load i32*, i32** %9
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %10
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %71
30: ; preds = %26
%31 = load %struct.dirent*, %struct.dirent** %10
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = load i8*, i8** %8
%35 = load i8*, i8** %8
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @strncmp(i8* %33, i8* %34, i32 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %70, label %39
39: ; preds = %30
%40 = load %struct.dirent*, %struct.dirent** %10
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%45 = load %struct.dirent*, %struct.dirent** %10
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %44, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %47)
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%50 = load i32, i32* @O_RDWR
%51 = load i32, i32* @O_NONBLOCK
%52 = or i32 %50, %51
%53 = call i32 @open(i8* %49, i32 %52)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %39
%57 = load i32, i32* @stderr
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %58, i8* %60)
%62 = load i32, i32* %11
store i32 %62, i32* %3
br label %76
63: ; preds = %39
%64 = load i8*, i8** %5
%65 = load %struct.dirent*, %struct.dirent** %10
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %67)
%69 = load i32, i32* %11
store i32 %69, i32* %3
br label %76
70: ; preds = %30
br label %26
71: ; preds = %26
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0))
%74 = load i32, i32* @EINVAL
%75 = sub nsw i32 0, %74
store i32 %75, i32* %3
br label %76
76: ; preds = %71, %63, %56, %19
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"rpmsg_ctrl\00"
@.str.1 = [20 x i8] c"%s/devices/%s/rpmsg\00"
@.str.2 = [15 x i8] c"/sys/bus/rpmsg\00"
@stderr = global i32 0
@.str.3 = [23 x i8] c"Failed to open dir %s\0A\00"
@EINVAL = global i32 0
@.str.4 = [18 x i8] c"Opening file %s.\0A\00"
@.str.5 = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [37 x i8] c"Failed to open rpmsg char dev %s,%s\0A\00"
@errno = global i32 0
@.str.8 = [33 x i8] c"No rpmsg char dev file is found\0A\00"
define i32 @get_rpmsg_chrdev_fd(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%8 = call i32* @opendir(i8* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %48
15: ; preds = %2, %18
%16 = tail call %struct.dirent* @readdir(i32* %8)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %43, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %15
24: ; preds = %18
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %25
%29 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_NONBLOCK
%32 = or i32 %31, %30
%33 = call i32 @open(i8* %6, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %24
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = tail call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %6, i8* %38)
br label %48
40: ; preds = %24
%41 = load i8*, i8** %25
%42 = tail call i8* @strcpy(i8* dereferenceable(1) %1, i8* dereferenceable(1) %41)
br label %48
43: ; preds = %15
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0))
%46 = load i32, i32* @EINVAL
%47 = sub nsw i32 0, %46
br label %48
48: ; preds = %43, %40, %35, %10
%49 = phi i32 [ %14, %10 ], [ %33, %35 ], [ %33, %40 ], [ %47, %43 ]
ret i32 %49
}