#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 */

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *,
                           ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int write(int, char const *, scalar_t__);
int bind_rpmsg_chrdev(const char *rpmsg_dev_name) {
  char fpath[256];
  char *rpmsg_chdrv = "rpmsg_chrdev";
  int fd;
  int ret;
  sprintf(fpath, "%s/devices/%s/driver_override", "/sys/bus/rpmsg",
          rpmsg_dev_name);
  fd = open(fpath, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s, %s\n", fpath, strerror(errno));
    return -EINVAL;
  }
  ret = write(fd, rpmsg_chdrv, strlen(rpmsg_chdrv) + 1);
  if (ret < 0) {
    fprintf(stderr, "Failed to write %s to %s, %s\n", rpmsg_chdrv, fpath,
            strerror(errno));
    return -EINVAL;
  }
  close(fd);
  sprintf(fpath, "%s/drivers/%s/bind", "/sys/bus/rpmsg", rpmsg_chdrv);
  fd = open(fpath, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s, %s\n", fpath, strerror(errno));
    return -EINVAL;
  }
  ret = write(fd, rpmsg_dev_name, strlen(rpmsg_dev_name) + 1);
  if (ret < 0) {
    fprintf(stderr, "Failed to write %s to %s, %s\n", rpmsg_dev_name, fpath,
            strerror(errno));
    return -EINVAL;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<bind_rpmsg_chrdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe6e(%rip),%rsi
lea    0xe85(%rip),%rdx
call   1080 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
jge    122f <bind_rpmsg_chrdev+0xbf>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x12c(%rbp),%edi
mov    -0x128(%rbp),%rdx
mov    %rax,%rcx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2dab(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    146e <bind_rpmsg_chrdev+0x2fe>
mov    -0x11c(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x118(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    %eax,-0x120(%rbp)
cmpl   $0x0,-0x120(%rbp)
jge    12f2 <bind_rpmsg_chrdev+0x182>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x154(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x154(%rbp),%edi
mov    -0x150(%rbp),%rdx
mov    -0x148(%rbp),%rcx
mov    %rax,%r8
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2ce8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    146e <bind_rpmsg_chrdev+0x2fe>
mov    -0x11c(%rbp),%edi
call   1050 <close@plt>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rcx
lea    0xd5d(%rip),%rsi
lea    0xd12(%rip),%rdx
call   1080 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2c94(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
jge    13a2 <bind_rpmsg_chrdev+0x232>
mov    0x2c93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x164(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x160(%rbp)
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x164(%rbp),%edi
mov    -0x160(%rbp),%rdx
mov    %rax,%rcx
lea    0xcb2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2c38(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    146e <bind_rpmsg_chrdev+0x2fe>
mov    -0x11c(%rbp),%eax
mov    %eax,-0x174(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x170(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x174(%rbp),%edi
mov    -0x170(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    %eax,-0x120(%rbp)
cmpl   $0x0,-0x120(%rbp)
jge    145c <bind_rpmsg_chrdev+0x2ec>
mov    0x2beb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x188(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x180(%rbp)
mov    0x2bbb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18c(%rbp),%edi
mov    -0x188(%rbp),%rdx
mov    -0x180(%rbp),%rcx
mov    %rax,%r8
lea    0xc0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2b7e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    146e <bind_rpmsg_chrdev+0x2fe>
mov    -0x11c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_rpmsg_chrdev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rbx
lea    0xe88(%rip),%rsi
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rcx
call   1080 <sprintf@plt>
mov    0x2e1f(%rip),%r14
mov    (%r14),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     122d <bind_rpmsg_chrdev+0xbd>
mov    %eax,%ebp
lea    0xe47(%rip),%rsi
mov    $0xd,%edx
mov    %eax,%edi
call   1030 <write@plt>
test   %eax,%eax
js     125e <bind_rpmsg_chrdev+0xee>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xe94(%rip),%rsi
lea    0xe49(%rip),%rdx
lea    0xe17(%rip),%rcx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1080 <sprintf@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     122d <bind_rpmsg_chrdev+0xbd>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
test   %eax,%eax
js     128c <bind_rpmsg_chrdev+0x11c>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
jmp    12c9 <bind_rpmsg_chrdev+0x159>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdef(%rip),%rsi
xor    %ebp,%ebp
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12c0 <bind_rpmsg_chrdev+0x150>
mov    0x2d7b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd5(%rip),%rsi
lea    0xd7d(%rip),%rdx
xor    %ebp,%ebp
mov    %rsp,%rcx
mov    %ebx,%edi
jmp    12b6 <bind_rpmsg_chrdev+0x146>
mov    0x2d4d(%rip),%rax
mov    (%rax),%r14d
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda6(%rip),%rsi
xor    %ebp,%ebp
mov    %rsp,%rcx
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d09(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"rpmsg_chrdev\00"
@.str.1 = [30 x i8] c"%s/devices/%s/driver_override\00"
@.str.2 = [15 x i8] c"/sys/bus/rpmsg\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.3 = [23 x i8] c"Failed to open %s, %s\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.4 = [30 x i8] c"Failed to write %s to %s, %s\0A\00"
@.str.5 = [19 x i8] c"%s/drivers/%s/bind\00"
define i32 @bind_rpmsg_chrdev(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %5
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %18, i8* %20)
%22 = load i32, i32* @EINVAL
%23 = sub nsw i32 0, %22
store i32 %23, i32* %2
br label %82
24: ; preds = %1
%25 = load i32, i32* %6
%26 = load i8*, i8** %5
%27 = load i8*, i8** %5
%28 = call i64 @strlen(i8* %27)
%29 = add nsw i64 %28, 1
%30 = call i32 @write(i32 %25, i8* %26, i64 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %24
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %5
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %36, i8* %38)
%40 = load i32, i32* @EINVAL
%41 = sub nsw i32 0, %40
store i32 %41, i32* %2
br label %82
42: ; preds = %24
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%46 = load i8*, i8** %5
%47 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%49 = load i32, i32* @O_WRONLY
%50 = call i32 @open(i8* %48, i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %61
53: ; preds = %42
%54 = load i32, i32* @stderr
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %55, i8* %57)
%59 = load i32, i32* @EINVAL
%60 = sub nsw i32 0, %59
store i32 %60, i32* %2
br label %82
61: ; preds = %42
%62 = load i32, i32* %6
%63 = load i8*, i8** %3
%64 = load i8*, i8** %3
%65 = call i64 @strlen(i8* %64)
%66 = add nsw i64 %65, 1
%67 = call i32 @write(i32 %62, i8* %63, i64 %66)
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %61
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %3
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %72, i8* %73, i8* %75)
%77 = load i32, i32* @EINVAL
%78 = sub nsw i32 0, %77
store i32 %78, i32* %2
br label %82
79: ; preds = %61
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
store i32 0, i32* %2
br label %82
82: ; preds = %79, %70, %53, %33, %16
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
@.str = [13 x i8] c"rpmsg_chrdev\00"
@.str.1 = [30 x i8] c"%s/devices/%s/driver_override\00"
@.str.2 = [15 x i8] c"/sys/bus/rpmsg\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.3 = [23 x i8] c"Failed to open %s, %s\0A\00"
@errno = global i32 0
@EINVAL = global i32 0
@.str.4 = [30 x i8] c"Failed to write %s to %s, %s\0A\00"
@.str.5 = [19 x i8] c"%s/drivers/%s/bind\00"
define i32 @bind_rpmsg_chrdev(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 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)
%5 = load i32, i32* @O_WRONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* %11)
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %52
15: ; preds = %1
%16 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 13)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %21)
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
br label %52
25: ; preds = %15
%26 = tail call i32 @close(i32 %6)
%27 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @O_WRONLY
%29 = call i32 @open(i8* %3, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = tail call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* %34)
%36 = load i32, i32* @EINVAL
%37 = sub nsw i32 0, %36
br label %52
38: ; preds = %25
%39 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%40 = add nsw i64 %39, 1
%41 = tail call i32 @write(i32 %29, i8* %0, i64 %40)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %3, i8* %46)
%48 = load i32, i32* @EINVAL
%49 = sub nsw i32 0, %48
br label %52
50: ; preds = %38
%51 = tail call i32 @close(i32 %29)
br label %52
52: ; preds = %50, %43, %31, %18, %8
%53 = phi i32 [ %14, %8 ], [ %24, %18 ], [ %37, %31 ], [ %49, %43 ], [ 0, %50 ]
ret i32 %53
}