#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);
char *strchr(char *, char);
int sysfs_read_file(const char *path, char **output) {
  int ret;
  char *result = NULL, *n;
  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 ((n = strchr(result, '\n')) != NULL)
    *n = '\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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11de <sysfs_read_file+0x5e>
xor    %eax,%eax
mov    0x2e04(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <sysfs_read_file+0x123>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1217 <sysfs_read_file+0x97>
mov    -0x34(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1294 <sysfs_read_file+0x114>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rdx
sub    $0x1,%rdx
call   1070 <read@plt>
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x48(%rbp)
jae    124e <sysfs_read_file+0xce>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1294 <sysfs_read_file+0x114>
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     127d <sysfs_read_file+0xfd>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x1c(%rbp)
jmp    129d <sysfs_read_file+0x11d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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   10a0 <getpagesize@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     120a <sysfs_read_file+0x8a>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1215 <sysfs_read_file+0x95>
add    $0xffffffffffffffff,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1203 <sysfs_read_file+0x83>
movb   $0x0,(%rax)
mov    %r15,(%r14)
xor    %ebp,%ebp
jmp    121d <sysfs_read_file+0x9d>
mov    0x2dc7(%rip),%rax
sub    (%rax),%ebp
jmp    121d <sysfs_read_file+0x9d>
mov    %r15,%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 i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i64 0, i64* %10
store i64 0, i64* %11
%12 = call i64 (...) @getpagesize()
store i64 %12, i64* %10
%13 = load i64, i64* %10
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* @ENOMEM
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %59
20: ; preds = %2
%21 = load i8*, i8** %7
%22 = load i64, i64* %10
%23 = call i32 @memset(i8* %21, i32 0, i64 %22)
%24 = load i8*, i8** %4
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = load i32, i32* %9
store i32 %30, i32* %6
br label %54
31: ; preds = %20
%32 = load i32, i32* %9
%33 = load i8*, i8** %7
%34 = load i64, i64* %10
%35 = sub i64 %34, 1
%36 = call i64 @read(i32 %32, i8* %33, i64 %35)
store i64 %36, i64* %11
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* %11
%40 = icmp ult i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %31
store i32 -1, i32* %6
br label %54
42: ; preds = %31
%43 = load i8*, i8** %7
%44 = load i64, i64* %11
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %7
%47 = call i8* @strchr(i8* %46, i8 signext 10)
store i8* %47, i8** %8
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = load i8*, i8** %8
store i8 0, i8* %50
br label %51
51: ; preds = %49, %42
%52 = load i8*, i8** %7
%53 = load i8**, i8*** %5
store i8* %52, i8** %53
store i32 0, i32* %6
br label %57
54: ; preds = %41, %29
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %51
%58 = load i32, i32* %6
store i32 %58, i32* %3
br label %59
59: ; preds = %57, %17
%60 = load i32, i32* %3
ret i32 %60
}

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 %25
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 %23, 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 = tail call i8* @strchr(i8* %4, i8 signext 10)
%20 = icmp eq i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %14
store i8 0, i8* %19
br label %22
22: ; preds = %21, %14
store i8* %4, i8** %1
br label %25
23: ; preds = %9
%24 = tail call i32 @free(i8* %4)
br label %25
25: ; preds = %22, %23, %6
%26 = phi i32 [ %8, %6 ], [ %12, %23 ], [ 0, %22 ]
ret i32 %26
}