#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 size_t ssize_t;

/* Variables and functions */
int EIO;
int ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int sysfs_read_attr_str(const char *path, char *buff, size_t buff_size,
                        ssize_t *out_len) {
  int fd, result;
  ssize_t result_len;
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    result = ENOENT;
    goto out;
  }
  result_len = read(fd, buff, buff_size);
  if (result_len < 0) {
    result = EIO;
    goto out;
  }
  buff[result_len] = '\0';
  *out_len = result_len;
  result = 0;
out:
  if (fd >= 0)
    close(fd);
  return result;
}


x86 assembly (clang, -O0):
<sysfs_read_attr_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1178 <sysfs_read_attr_str+0x48>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11c6 <sysfs_read_attr_str+0x96>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    11a8 <sysfs_read_attr_str+0x78>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11c6 <sysfs_read_attr_str+0x96>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11d8 <sysfs_read_attr_str+0xa8>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_attr_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1173 <sysfs_read_attr_str+0x43>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
movb   $0x0,(%rbx,%rax,1)
mov    %rax,(%r14)
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    117c <sysfs_read_attr_str+0x4c>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@ENOENT = global i32 0
@EIO = global i32 0
define i32 @sysfs_read_attr_str(i8* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64* %3, i64** %8
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i32, i32* @ENOENT
store i32 %18, i32* %10
br label %34
19: ; preds = %4
%20 = load i32, i32* %9
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = call i64 @read(i32 %20, i8* %21, i64 %22)
store i64 %23, i64* %11
%24 = load i64, i64* %11
%25 = icmp ult i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = load i32, i32* @EIO
store i32 %27, i32* %10
br label %34
28: ; preds = %19
%29 = load i8*, i8** %6
%30 = load i64, i64* %11
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 0, i8* %31
%32 = load i64, i64* %11
%33 = load i64*, i64** %8
store i64 %32, i64* %33
store i32 0, i32* %10
br label %34
34: ; preds = %28, %26, %17
%35 = load i32, i32* %9
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %9
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %10
ret i32 %41
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@ENOENT = global i32 0
@EIO = global i32 0
define i32 @sysfs_read_attr_str(i8* nocapture %0, i8* nocapture %1, i64 %2, i64* nocapture writeonly %3) {
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = load i32, i32* @ENOENT
br label %14
10: ; preds = %4
%11 = tail call i64 @read(i32 %6, i8* %1, i64 %2)
%12 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %12
store i64 %11, i64* %3
%13 = tail call i32 @close(i32 %6)
br label %14
14: ; preds = %8, %10
%15 = phi i32 [ 0, %10 ], [ %9, %8 ]
ret i32 %15
}