#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
char *malloc(scalar_t__);
size_t read(int, char *, scalar_t__);
char *read_entire_sysfs_file(int fd) {
  struct stat s;
  char *buf;
  if (fstat(fd, &s) == -1)
    return NULL;
  buf = malloc(s.st_size + 1);
  if (!buf)
    return NULL;
  size_t sysfs_size = read(fd, buf, s.st_size);
  if ((size_t)-1 == sysfs_size) {
    free(buf);
    return NULL;
  }
  buf[sysfs_size] = 0;
  if (buf[sysfs_size - 1] == '\n')
    buf[sysfs_size - 1] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<read_entire_sysfs_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    116d <read_entire_sysfs_file+0x2d>
movq   $0x0,-0x8(%rbp)
jmp    120e <read_entire_sysfs_file+0xce>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1196 <read_entire_sysfs_file+0x56>
movq   $0x0,-0x8(%rbp)
jmp    120e <read_entire_sysfs_file+0xce>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    -0x28(%rbp),%rax
jne    11d1 <read_entire_sysfs_file+0x91>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    120e <read_entire_sysfs_file+0xce>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1206 <read_entire_sysfs_file+0xc6>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_entire_sysfs_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     119c <read_entire_sysfs_file+0x5c>
mov    0x8(%rsp),%r14
lea    0x1(%r14),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     119c <read_entire_sysfs_file+0x5c>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     1194 <read_entire_sysfs_file+0x54>
movb   $0x0,(%rbx,%rax,1)
cmpb   $0xa,-0x1(%rbx,%rax,1)
jne    119e <read_entire_sysfs_file+0x5e>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    119e <read_entire_sysfs_file+0x5e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
define i8* @read_entire_sysfs_file(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @fstat(i32 %7, %struct.stat* %4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %48
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%13 = load i64, i64* %12
%14 = add nsw i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %11
store i8* null, i8** %2
br label %48
19: ; preds = %11
%20 = load i32, i32* %3
%21 = load i8*, i8** %5
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%23 = load i64, i64* %22
%24 = call i64 @read(i32 %20, i8* %21, i64 %23)
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = icmp eq i64 -1, %25
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
store i8* null, i8** %2
br label %48
30: ; preds = %19
%31 = load i8*, i8** %5
%32 = load i64, i64* %6
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = sub i64 %35, 1
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %46
41: ; preds = %30
%42 = load i8*, i8** %5
%43 = load i64, i64* %6
%44 = sub i64 %43, 1
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %41, %30
%47 = load i8*, i8** %5
store i8* %47, i8** %2
br label %48
48: ; preds = %46, %27, %18, %10
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
%struct.stat = type { i64 }
define noalias i8* @read_entire_sysfs_file(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @fstat(i32 %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %24, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = add nsw i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %24, label %12
12: ; preds = %6
%13 = tail call i64 @read(i32 %0, i8* %10, i64 %8)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @free(i8* %10)
br label %24
17: ; preds = %12
%18 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 0, i8* %18
%19 = add i64 %13, -1
%20 = getelementptr inbounds i8, i8* %10, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %17
store i8 0, i8* %20
br label %24
24: ; preds = %15, %23, %17, %6, %1
%25 = phi i8* [ null, %1 ], [ null, %6 ], [ null, %15 ], [ %10, %23 ], [ %10, %17 ]
ret i8* %25
}