#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 */
typedef int /*<<< orphan*/ svc_name;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char const *, ...);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, int);
char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, const char *ept_name,
                             char *ept_dev_name) {
  char sys_rpmsg_ept_name_path[64];
  char svc_name[64];
  char *sys_rpmsg_path = "/sys/class/rpmsg";
  FILE *fp;
  int i;
  int ept_name_len;
  for (i = 0; i < 128; i++) {
    sprintf(sys_rpmsg_ept_name_path, "%s/%s/rpmsg%d/name", sys_rpmsg_path,
            rpmsg_char_name, i);
    printf("checking %s\n", sys_rpmsg_ept_name_path);
    if (access(sys_rpmsg_ept_name_path, F_OK) < 0)
      continue;
    fp = fopen(sys_rpmsg_ept_name_path, "r");
    if (!fp) {
      printf("failed to open %s\n", sys_rpmsg_ept_name_path);
      break;
    }
    fgets(svc_name, sizeof(svc_name), fp);
    fclose(fp);
    printf("svc_name: %s.\n", svc_name);
    ept_name_len = strlen(ept_name);
    if (ept_name_len > sizeof(svc_name))
      ept_name_len = sizeof(svc_name);
    if (!strncmp(svc_name, ept_name, ept_name_len)) {
      sprintf(ept_dev_name, "rpmsg%d", i);
      return ept_dev_name;
    }
  }
  printf("Not able to RPMsg endpoint file for %s:%s.\n", rpmsg_char_name,
         ept_name);
  return NULL;
}


x86 assembly (clang, -O0):
<get_rpmsg_ept_dev_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe62(%rip),%rax
mov    %rax,-0xa8(%rbp)
movl   $0x0,-0xb4(%rbp)
cmpl   $0x80,-0xb4(%rbp)
jge    1312 <get_rpmsg_ept_dev_name+0x192>
lea    -0x60(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0xb4(%rbp),%r8d
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x60(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jge    1216 <get_rpmsg_ept_dev_name+0x96>
jmp    12fe <get_rpmsg_ept_dev_name+0x17e>
lea    -0x60(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    1252 <get_rpmsg_ept_dev_name+0xd2>
lea    -0x60(%rbp),%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1312 <get_rpmsg_ept_dev_name+0x192>
lea    -0xa0(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    $0x40,%esi
call   1070 <fgets@plt>
mov    -0xb0(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0xa0(%rbp),%rsi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xb8(%rbp)
movslq -0xb8(%rbp),%rax
cmp    $0x40,%rax
jbe    12b5 <get_rpmsg_ept_dev_name+0x135>
movl   $0x40,-0xb8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xb8(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12f9 <get_rpmsg_ept_dev_name+0x179>
mov    -0x20(%rbp),%rdi
mov    -0xb4(%rbp),%edx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1330 <get_rpmsg_ept_dev_name+0x1b0>
jmp    12fe <get_rpmsg_ept_dev_name+0x17e>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    11af <get_rpmsg_ept_dev_name+0x2f>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rpmsg_ept_dev_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x10(%rsp),%r14
lea    0x50(%rsp),%r13
xor    %ebp,%ebp
jmp    11bf <get_rpmsg_ept_dev_name+0x3f>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x80,%ebp
je     1299 <get_rpmsg_ept_dev_name+0x119>
mov    %r14,%rdi
lea    0xe48(%rip),%rsi
lea    0xe30(%rip),%rdx
mov    %r15,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe40(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
test   %rax,%rax
js     11b0 <get_rpmsg_ept_dev_name+0x30>
mov    %r14,%rdi
lea    0xe23(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1286 <get_rpmsg_ept_dev_name+0x106>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0xe0c(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x40,%eax
mov    $0x40,%ecx
cmovae %ecx,%eax
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11b0 <get_rpmsg_ept_dev_name+0x30>
lea    0xde2(%rip),%rsi
mov    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <sprintf@plt>
jmp    12b0 <get_rpmsg_ept_dev_name+0x130>
lea    0xda6(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdbd(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/sys/class/rpmsg\00"
@.str.1 = [19 x i8] c"%s/%s/rpmsg%d/name\00"
@.str.2 = [13 x i8] c"checking %s\0A\00"
@F_OK = global i32 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [19 x i8] c"failed to open %s\0A\00"
@.str.5 = [15 x i8] c"svc_name: %s.\0A\00"
@.str.6 = [8 x i8] c"rpmsg%d\00"
@.str.7 = [44 x i8] c"Not able to RPMsg endpoint file for %s:%s.\0A\00"
define i8* @get_rpmsg_ept_dev_name(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [64 x i8]
%9 = alloca [64 x i8]
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %10
store i32 0, i32* %12
br label %14
14: ; preds = %64, %3
%15 = load i32, i32* %12
%16 = icmp slt i32 %15, 128
br i1 %16, label %17, label %67
17: ; preds = %14
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%19 = load i8*, i8** %10
%20 = load i8*, i8** %5
%21 = load i32, i32* %12
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %20, i32 %21)
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* @F_OK
%27 = call i64 @access(i8* %25, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %17
br label %64
30: ; preds = %17
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %32, i32** %11
%33 = load i32*, i32** %11
%34 = icmp ne i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%37 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %67
38: ; preds = %30
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%40 = load i32*, i32** %11
%41 = call i32 @fgets(i8* %39, i32 64, i32* %40)
%42 = load i32*, i32** %11
%43 = call i32 @fclose(i32* %42)
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %6
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %13
%48 = load i32, i32* %13
%49 = sext i32 %48 to i64
%50 = icmp ugt i64 %49, 64
br i1 %50, label %51, label %52
51: ; preds = %38
store i32 64, i32* %13
br label %52
52: ; preds = %51, %38
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%54 = load i8*, i8** %6
%55 = load i32, i32* %13
%56 = call i32 @strncmp(i8* %53, i8* %54, i32 %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %52
%59 = load i8*, i8** %7
%60 = load i32, i32* %12
%61 = call i32 (i8*, i8*, ...) @sprintf(i8* %59, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %60)
%62 = load i8*, i8** %7
store i8* %62, i8** %4
br label %71
63: ; preds = %52
br label %64
64: ; preds = %63, %29
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 1
store i32 %66, i32* %12
br label %14
67: ; preds = %35, %14
%68 = load i8*, i8** %5
%69 = load i8*, i8** %6
%70 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %68, i8* %69)
store i8* null, i8** %4
br label %71
71: ; preds = %67, %58
%72 = load i8*, i8** %4
ret i8* %72
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sys/class/rpmsg\00"
@.str.1 = [19 x i8] c"%s/%s/rpmsg%d/name\00"
@.str.2 = [13 x i8] c"checking %s\0A\00"
@F_OK = global i32 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [19 x i8] c"failed to open %s\0A\00"
@.str.5 = [15 x i8] c"svc_name: %s.\0A\00"
@.str.6 = [8 x i8] c"rpmsg%d\00"
@.str.7 = [44 x i8] c"Not able to RPMsg endpoint file for %s:%s.\0A\00"
define i8* @get_rpmsg_ept_dev_name(i8* %0, i8* %1, i8* writeonly %2) {
%4 = alloca [64 x i8]
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
br label %8
8: ; preds = %3, %31
%9 = phi i32 [ 0, %3 ], [ %32, %31 ]
%10 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %9)
%11 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%12 = load i32, i32* @F_OK
%13 = call i64 @access(i8* %6, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %8
%16 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %34
20: ; preds = %15
%21 = call i32 @fgets(i8* %7, i32 64, i32* %16)
%22 = call i32 @fclose(i32* %16)
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%24 = call i32 @strlen(i8* %1)
%25 = icmp ult i32 %24, 64
%26 = select i1 %25, i32 %24, i32 64
%27 = call i32 @strncmp(i8* %7, i8* %1, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %9)
br label %36
31: ; preds = %20, %8
%32 = add nuw nsw i32 %9, 1
%33 = icmp eq i32 %32, 128
br i1 %33, label %34, label %8
34: ; preds = %31, %18
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %1)
br label %36
36: ; preds = %34, %29
%37 = phi i8* [ %2, %29 ], [ null, %34 ]
ret i8* %37
}