#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 rpmsg_endpoint_info {
  int /*<<< orphan*/ name;
};

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ get_rpmsg_ept_dev_name(char *, int /*<<< orphan*/, char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int rpmsg_create_ept(int, struct rpmsg_endpoint_info *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int rpmsg_create_ept_dev(int rpfd, struct rpmsg_endpoint_info *eptinfo) {
  char ept_dev_name[16];
  char ept_dev_path[32];
  char *rpmsg_char_name;
  int fd, ret;
  ret = rpmsg_create_ept(rpfd, eptinfo);
  if (ret) {
    printf("failed to create RPMsg endpoint.\n");
    return ret;
  }
  rpmsg_char_name = "rpmsg_ctrl0";
  if (!get_rpmsg_ept_dev_name(rpmsg_char_name, eptinfo->name, ept_dev_name)) {
    return -1;
  }
  sprintf(ept_dev_path, "/dev/%s", ept_dev_name);
  printf("opening %s.\n", ept_dev_path);
  fd = open(ept_dev_path, O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    perror("Failed to open rpmsg endpoint device.");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<rpmsg_create_ept_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <rpmsg_create_ept@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
je     11a1 <rpmsg_create_ept_dev+0x41>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <rpmsg_create_ept_dev+0xe0>
lea    0xe7a(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <get_rpmsg_ept_dev_name@plt>
cmp    $0x0,%eax
jne    11d4 <rpmsg_create_ept_dev+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <rpmsg_create_ept_dev+0xe0>
lea    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x40(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    123a <rpmsg_create_ept_dev+0xda>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <rpmsg_create_ept_dev+0xe0>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpmsg_create_ept_dev>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
call   1030 <rpmsg_create_ept@plt>
test   %eax,%eax
je     1193 <rpmsg_create_ept_dev+0x23>
mov    %eax,%ebx
lea    0xebb(%rip),%rdi
call   1040 <puts@plt>
jmp    11f6 <rpmsg_create_ept_dev+0x86>
mov    (%r14),%esi
lea    0xe63(%rip),%rdi
mov    %rsp,%rdx
call   1050 <get_rpmsg_ept_dev_name@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
je     11f6 <rpmsg_create_ept_dev+0x86>
lea    0xe57(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
lea    0xe48(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1200 <rpmsg_create_ept_dev+0x90>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f6 <rpmsg_create_ept_dev+0x86>

LLVM IR (-O0):
%struct.rpmsg_endpoint_info = type { i32 }
@.str = [34 x i8] c"failed to create RPMsg endpoint.\0A\00"
@.str.1 = [12 x i8] c"rpmsg_ctrl0\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@.str.3 = [13 x i8] c"opening %s.\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [38 x i8] c"Failed to open rpmsg endpoint device.\00"
define i32 @rpmsg_create_ept_dev(i32 %0, %struct.rpmsg_endpoint_info* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.rpmsg_endpoint_info*
%6 = alloca [16 x i8]
%7 = alloca [32 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store %struct.rpmsg_endpoint_info* %1, %struct.rpmsg_endpoint_info** %5
%11 = load i32, i32* %4
%12 = load %struct.rpmsg_endpoint_info*, %struct.rpmsg_endpoint_info** %5
%13 = call i32 @rpmsg_create_ept(i32 %11, %struct.rpmsg_endpoint_info* %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %10
store i32 %18, i32* %3
br label %45
19: ; preds = %2
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %8
%20 = load i8*, i8** %8
%21 = load %struct.rpmsg_endpoint_info*, %struct.rpmsg_endpoint_info** %5
%22 = getelementptr inbounds %struct.rpmsg_endpoint_info, %struct.rpmsg_endpoint_info* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%25 = call i32 @get_rpmsg_ept_dev_name(i8* %20, i32 %23, i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %19
store i32 -1, i32* %3
br label %45
28: ; preds = %19
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %34, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
43: ; preds = %28
%44 = load i32, i32* %9
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %41, %27, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.rpmsg_endpoint_info = type { i32 }
@.str.1 = [12 x i8] c"rpmsg_ctrl0\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@.str.3 = [13 x i8] c"opening %s.\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [38 x i8] c"Failed to open rpmsg endpoint device.\00"
@str = [33 x i8] c"failed to create RPMsg endpoint.\00"
define i32 @rpmsg_create_ept_dev(i32 %0, %struct.rpmsg_endpoint_info* %1) {
%3 = alloca [16 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @rpmsg_create_ept(i32 %0, %struct.rpmsg_endpoint_info* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %26
11: ; preds = %2
%12 = getelementptr inbounds %struct.rpmsg_endpoint_info, %struct.rpmsg_endpoint_info* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @get_rpmsg_ept_dev_name(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %6, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %16, %11, %24, %9
%27 = phi i32 [ %7, %9 ], [ -1, %24 ], [ -1, %11 ], [ %22, %16 ]
ret i32 %27
}