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

/* Variables and functions */
int EFAULT;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int strlen(char const *);
int write(int, char const *, int);
int vbus_device_attr_set(const char *dev, const char *attr, const char *val) {
  char path[1024];
  int ret;
  int fd;
  int len = strlen(val);
  snprintf(path, sizeof(path), "/sys/vbus/devices/%s/%s", dev, attr);
  fd = open(path, O_WRONLY);
  if (fd < 0)
    return -errno;
  ret = write(fd, val, len);
  close(fd);
  if (ret < 0)
    return -errno;
  if (ret != len)
    return -EFAULT;
  return 0;
}


x86 assembly (clang, -O0):
<vbus_device_attr_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe6f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jge    11d1 <vbus_device_attr_set+0x81>
xor    %eax,%eax
mov    0x2e11(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1243 <vbus_device_attr_set+0xf3>
mov    -0x428(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x42c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x424(%rbp)
mov    -0x428(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x424(%rbp)
jge    1217 <vbus_device_attr_set+0xc7>
xor    %eax,%eax
mov    0x2dcb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1243 <vbus_device_attr_set+0xf3>
mov    -0x424(%rbp),%eax
cmp    -0x42c(%rbp),%eax
je     123c <vbus_device_attr_set+0xec>
xor    %eax,%eax
mov    0x2dc6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1243 <vbus_device_attr_set+0xf3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vbus_device_attr_set>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
lea    0xe86(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
mov    %rbp,%r8
call   1050 <snprintf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11d1 <vbus_device_attr_set+0x81>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
js     11d1 <vbus_device_attr_set+0x81>
xor    %eax,%eax
cmp    %r14d,%ebx
je     11dc <vbus_device_attr_set+0x8c>
mov    0x2e29(%rip),%rcx
jmp    11d8 <vbus_device_attr_set+0x88>
mov    0x2e00(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/sys/vbus/devices/%s/%s\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@EFAULT = global i32 0
define i32 @vbus_device_attr_set(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i8*, i8** %7
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %11
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = call i32 @snprintf(i8* %14, i32 1024, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %4
br label %46
26: ; preds = %3
%27 = load i32, i32* %10
%28 = load i8*, i8** %7
%29 = load i32, i32* %11
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %10
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %4
br label %46
38: ; preds = %26
%39 = load i32, i32* %9
%40 = load i32, i32* %11
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @EFAULT
%44 = sub nsw i32 0, %43
store i32 %44, i32* %4
br label %46
45: ; preds = %38
store i32 0, i32* %4
br label %46
46: ; preds = %45, %42, %35, %23
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [24 x i8] c"/sys/vbus/devices/%s/%s\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@EFAULT = global i32 0
define i32 @vbus_device_attr_set(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %2)
%7 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %5, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %26
14: ; preds = %3
%15 = call i32 @write(i32 %9, i8* %2, i32 %6)
%16 = call i32 @close(i32 %9)
%17 = icmp slt i32 %15, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
br label %26
21: ; preds = %14
%22 = icmp eq i32 %15, %6
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = load i32, i32* @EFAULT
%25 = sub nsw i32 0, %24
br label %26
26: ; preds = %21, %23, %18, %11
%27 = phi i32 [ %13, %11 ], [ %20, %18 ], [ %25, %23 ], [ 0, %21 ]
ret i32 %27
}