#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int vbus_device_attr_get(const char *dev, const char *attr, char *val,
                         size_t len) {
  char path[1024];
  int ret;
  int fd;
  snprintf(path, sizeof(path), "/sys/vbus/devices/%s/%s", dev, attr);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return -errno;
  ret = read(fd, val, len);
  close(fd);
  if (ret < 0)
    return -errno;
  return ret;
}


x86 assembly (clang, -O0):
<vbus_device_attr_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe8a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x438(%rbp)
jge    11b6 <vbus_device_attr_get+0x76>
xor    %eax,%eax
mov    0x2e2c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1203 <vbus_device_attr_get+0xc3>
mov    -0x438(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x434(%rbp)
mov    -0x438(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x434(%rbp)
jge    11fa <vbus_device_attr_get+0xba>
xor    %eax,%eax
mov    0x2de8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1203 <vbus_device_attr_get+0xc3>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vbus_device_attr_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r8
mov    %rdi,%rcx
lea    0xea2(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
call   1030 <snprintf@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     119f <vbus_device_attr_get+0x5f>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
jns    11aa <vbus_device_attr_get+0x6a>
mov    0x2e32(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [24 x i8] c"/sys/vbus/devices/%s/%s\00"
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @vbus_device_attr_get(i8* %0, i8* %1, i8* nocapture %2, i64 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, 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_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %4
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %21
14: ; preds = %4
%15 = call i32 @read(i32 %9, i8* %2, i64 %3)
%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 %21
21: ; preds = %14, %18, %11
%22 = phi i32 [ %13, %11 ], [ %20, %18 ], [ %15, %14 ]
ret i32 %22
}