#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
char *get_rpmsg_ept_dev_name(char *rpmsg_char_name, 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);
    fp = fopen(sys_rpmsg_ept_name_path, "r");
    if (!fp) {
      printf("ERROR: failed to open %s\n", sys_rpmsg_ept_name_path);
      break;
    }
    fgets(svc_name, sizeof(svc_name), fp);
    fclose(fp);
    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 create 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    0xe72(%rip),%rax
mov    %rax,-0xa8(%rbp)
movl   $0x0,-0xb4(%rbp)
cmpl   $0x80,-0xb4(%rbp)
jge    12ba <get_rpmsg_ept_dev_name+0x14a>
lea    -0x60(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0xb4(%rbp),%r8d
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    120f <get_rpmsg_ept_dev_name+0x9f>
lea    -0x60(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ba <get_rpmsg_ept_dev_name+0x14a>
lea    -0xa0(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    $0x40,%esi
call   1070 <fgets@plt>
mov    -0xb0(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xb8(%rbp)
movslq -0xb8(%rbp),%rax
cmp    $0x40,%rax
jbe    125d <get_rpmsg_ept_dev_name+0xed>
movl   $0x40,-0xb8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xb8(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12a1 <get_rpmsg_ept_dev_name+0x131>
mov    -0x20(%rbp),%rdi
mov    -0xb4(%rbp),%edx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d8 <get_rpmsg_ept_dev_name+0x168>
jmp    12a6 <get_rpmsg_ept_dev_name+0x136>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    119f <get_rpmsg_ept_dev_name+0x2f>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd7f(%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
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
lea    0xe67(%rip),%rsi
lea    0xe4f(%rip),%rdx
mov    %r15,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %r14,%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     121e <get_rpmsg_ept_dev_name+0xae>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %r12,%rdi
call   1040 <fclose@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
je     124a <get_rpmsg_ept_dev_name+0xda>
add    $0x1,%ebp
cmp    $0x80,%ebp
jne    11a0 <get_rpmsg_ept_dev_name+0x30>
jmp    1231 <get_rpmsg_ept_dev_name+0xc1>
lea    0xe01(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe10(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1262 <get_rpmsg_ept_dev_name+0xf2>
lea    0xdef(%rip),%rsi
mov    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <sprintf@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 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"ERROR: failed to open %s\0A\00"
@.str.4 = [8 x i8] c"rpmsg%d\00"
@.str.5 = [51 x i8] c"Not able to create 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 = %54, %3
%15 = load i32, i32* %12
%16 = icmp slt i32 %15, 128
br i1 %16, label %17, label %57
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* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %11
%25 = load i32*, i32** %11
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %17
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %28)
br label %57
30: ; preds = %17
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%32 = load i32*, i32** %11
%33 = call i32 @fgets(i8* %31, i32 64, i32* %32)
%34 = load i32*, i32** %11
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %6
%37 = call i32 @strlen(i8* %36)
store i32 %37, i32* %13
%38 = load i32, i32* %13
%39 = sext i32 %38 to i64
%40 = icmp ugt i64 %39, 64
br i1 %40, label %41, label %42
41: ; preds = %30
store i32 64, i32* %13
br label %42
42: ; preds = %41, %30
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%44 = load i8*, i8** %6
%45 = load i32, i32* %13
%46 = call i32 @strncmp(i8* %43, i8* %44, i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %42
%49 = load i8*, i8** %7
%50 = load i32, i32* %12
%51 = call i32 (i8*, i8*, ...) @sprintf(i8* %49, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %50)
%52 = load i8*, i8** %7
store i8* %52, i8** %4
br label %61
53: ; preds = %42
br label %54
54: ; preds = %53
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 1
store i32 %56, i32* %12
br label %14
57: ; preds = %27, %14
%58 = load i8*, i8** %5
%59 = load i8*, i8** %6
%60 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %58, i8* %59)
store i8* null, i8** %4
br label %61
61: ; preds = %57, %48
%62 = load i8*, i8** %4
ret i8* %62
}

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 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"ERROR: failed to open %s\0A\00"
@.str.4 = [8 x i8] c"rpmsg%d\00"
@.str.5 = [51 x i8] c"Not able to create 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, %25
%9 = phi i32 [ 0, %3 ], [ %26, %25 ]
%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* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %28
15: ; preds = %8
%16 = call i32 @fgets(i8* %7, i32 64, i32* %11)
%17 = call i32 @fclose(i32* %11)
%18 = call i32 @strlen(i8* %1)
%19 = icmp ult i32 %18, 64
%20 = select i1 %19, i32 %18, i32 64
%21 = call i32 @strncmp(i8* %7, i8* %1, i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %9)
br label %30
25: ; preds = %15
%26 = add nuw nsw i32 %9, 1
%27 = icmp eq i32 %26, 128
br i1 %27, label %28, label %8
28: ; preds = %25, %13
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %1)
br label %30
30: ; preds = %28, %23
%31 = phi i8* [ %2, %23 ], [ null, %28 ]
ret i8* %31
}