#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 ENOMEM;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
unsigned long getpagesize();
char *malloc(unsigned long);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned long);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, unsigned long);
int sysfs_read_file(const char *path, char **output) {
  int ret;
  char *result = NULL;
  int fd;
  unsigned long resultsize = 0;
  ssize_t length = 0;
  resultsize = getpagesize();
  result = malloc(resultsize);
  if (!result)
    return -ENOMEM;
  memset(result, 0, resultsize);
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    ret = fd;
    goto free_out;
  }
  length = read(fd, result, resultsize - 1);
  close(fd);
  if (length < 0) {
    ret = -1;
    goto free_out;
  }
  result[length] = '\0';
  if (result[length - 1] == '\n')
    result[length - 1] = '\0';
  *output = result;
  ret = 0;
  goto out;
free_out:
  free(result);
out:
  return ret;
}


x86 assembly (clang, -O0):
<sysfs_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ce <sysfs_read_file+0x5e>
xor    %eax,%eax
mov    0x2e14(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <sysfs_read_file+0x129>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1207 <sysfs_read_file+0x97>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    128a <sysfs_read_file+0x11a>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
sub    $0x1,%rdx
call   1060 <read@plt>
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x40(%rbp)
jae    123e <sysfs_read_file+0xce>
movl   $0xffffffff,-0x1c(%rbp)
jmp    128a <sysfs_read_file+0x11a>
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1273 <sysfs_read_file+0x103>
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x1c(%rbp)
jmp    1293 <sysfs_read_file+0x123>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f3 <sysfs_read_file+0x83>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11fe <sysfs_read_file+0x8e>
add    $0xffffffffffffffff,%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <read@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1050 <close@plt>
movb   $0x0,(%rbx,%r15,1)
cmpb   $0xa,-0x1(%rbx,%r15,1)
jne    11ec <sysfs_read_file+0x7c>
movb   $0x0,-0x1(%rbx,%r15,1)
mov    %rbx,(%r14)
xor    %ebp,%ebp
jmp    1206 <sysfs_read_file+0x96>
mov    0x2dde(%rip),%rax
sub    (%rax),%ebp
jmp    1206 <sysfs_read_file+0x96>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@O_RDONLY = global i32 0
define i32 @sysfs_read_file(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i64 0, i64* %9
store i64 0, i64* %10
%11 = call i64 (...) @getpagesize()
store i64 %11, i64* %9
%12 = load i64, i64* %9
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* @ENOMEM
%18 = sub nsw i32 0, %17
store i32 %18, i32* %3
br label %65
19: ; preds = %2
%20 = load i8*, i8** %7
%21 = load i64, i64* %9
%22 = call i32 @memset(i8* %20, i32 0, i64 %21)
%23 = load i8*, i8** %4
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = load i32, i32* %8
store i32 %29, i32* %6
br label %60
30: ; preds = %19
%31 = load i32, i32* %8
%32 = load i8*, i8** %7
%33 = load i64, i64* %9
%34 = sub i64 %33, 1
%35 = call i64 @read(i32 %31, i8* %32, i64 %34)
store i64 %35, i64* %10
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
%38 = load i64, i64* %10
%39 = icmp ult i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %30
store i32 -1, i32* %6
br label %60
41: ; preds = %30
%42 = load i8*, i8** %7
%43 = load i64, i64* %10
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %7
%46 = load i64, i64* %10
%47 = sub i64 %46, 1
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 10
br i1 %51, label %52, label %57
52: ; preds = %41
%53 = load i8*, i8** %7
%54 = load i64, i64* %10
%55 = sub i64 %54, 1
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
br label %57
57: ; preds = %52, %41
%58 = load i8*, i8** %7
%59 = load i8**, i8*** %5
store i8* %58, i8** %59
store i32 0, i32* %6
br label %63
60: ; preds = %40, %28
%61 = load i8*, i8** %7
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %60, %57
%64 = load i32, i32* %6
store i32 %64, i32* %3
br label %65
65: ; preds = %63, %16
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@O_RDONLY = global i32 0
define i32 @sysfs_read_file(i8* nocapture %0, i8** nocapture writeonly %1) {
%3 = tail call i64 (...) @getpagesize()
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @ENOMEM
%8 = sub nsw i32 0, %7
br label %27
9: ; preds = %2
%10 = tail call i32 @memset(i8* %4, i32 0, i64 %3)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %9
%15 = add i64 %3, -1
%16 = tail call i64 @read(i32 %12, i8* %4, i64 %15)
%17 = tail call i32 @close(i32 %12)
%18 = getelementptr inbounds i8, i8* %4, i64 %16
store i8 0, i8* %18
%19 = add i64 %16, -1
%20 = getelementptr inbounds i8, i8* %4, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %14
store i8 0, i8* %20
br label %24
24: ; preds = %23, %14
store i8* %4, i8** %1
br label %27
25: ; preds = %9
%26 = tail call i32 @free(i8* %4)
br label %27
27: ; preds = %24, %25, %6
%28 = phi i32 [ %8, %6 ], [ %12, %25 ], [ 0, %24 ]
ret i32 %28
}