#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *strstr(char *, char const *);
int get_rpmsg_dev_name(const char *rpmsg_svc_name, char *rpmsg_dev_name) {
  char dpath[64];
  DIR *dir;
  struct dirent *ent;
  int fd;
  sprintf(dpath, "%s/devices", "/sys/bus/rpmsg");
  dir = opendir(dpath);
  if (dir == NULL) {
    fprintf(stderr, "Failed to open dir %s\n", dpath);
    return -EINVAL;
  }
  while ((ent = readdir(dir)) != NULL) {
    if (strstr(ent->d_name, rpmsg_svc_name) != NULL) {
      sprintf(rpmsg_dev_name, "%s", ent->d_name);
      return 0;
    }
  }
  fprintf(stderr, "No rpmsg dev file is found for %s.\n", rpmsg_svc_name);
  return -EINVAL;
}


x86 assembly (clang, -O0):
<get_rpmsg_dev_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe92(%rip),%rsi
lea    0xe96(%rip),%rdx
call   1060 <sprintf@plt>
lea    -0x60(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11be <get_rpmsg_dev_name+0x6e>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xe74(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2e24(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1243 <get_rpmsg_dev_name+0xf3>
jmp    11c3 <get_rpmsg_dev_name+0x73>
mov    -0x68(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x70(%rbp)
cmp    $0x0,%rax
je     121c <get_rpmsg_dev_name+0xcc>
mov    -0x70(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1217 <get_rpmsg_dev_name+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2b(%rip),%rsi
call   1060 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1243 <get_rpmsg_dev_name+0xf3>
jmp    11c3 <get_rpmsg_dev_name+0x73>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe04(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2d9a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rpmsg_dev_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe88(%rip),%rsi
lea    0xe8c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11cf <get_rpmsg_dev_name+0x6f>
mov    %rax,%r12
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11e4 <get_rpmsg_dev_name+0x84>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11a0 <get_rpmsg_dev_name+0x40>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    1207 <get_rpmsg_dev_name+0xa7>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    %rsp,%rdx
jmp    11f7 <get_rpmsg_dev_name+0x97>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    0x2dd5(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"%s/devices\00"
@.str.1 = [15 x i8] c"/sys/bus/rpmsg\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"Failed to open dir %s\0A\00"
@EINVAL = global i32 0
@.str.4 = [36 x i8] c"No rpmsg dev file is found for %s.\0A\00"
define i32 @get_rpmsg_dev_name(i8* %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%11 = load i32, i32* @EINVAL
%12 = sub nsw i32 0, %11
br label %28
13: ; preds = %2, %16
%14 = tail call %struct.dirent* @readdir(i32* %6)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = tail call i32* @strstr(i8* dereferenceable(1) %18, i8* dereferenceable(1) %0)
%20 = icmp eq i32* %19, null
br i1 %20, label %13, label %21
21: ; preds = %16
%22 = tail call i8* @strcpy(i8* dereferenceable(1) %1, i8* dereferenceable(1) %18)
br label %28
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%26 = load i32, i32* @EINVAL
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %23, %21, %8
%29 = phi i32 [ %12, %8 ], [ 0, %21 ], [ %27, %23 ]
ret i32 %29
}