#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
__attribute__((visibility("hidden"))) char *sysfs_read(char *name) {
  char *buf;
  int n;
  int fd;
  fd = open(name, O_RDONLY);
  buf = malloc(4096);
  if (!buf)
    return NULL;
  n = read(fd, buf, 4096 - 1);
  close(fd);
  if (n <= 0) {
    free(buf);
    return NULL;
  }
  buf[n] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<sysfs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <sysfs_read+0x47>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <sysfs_read+0x97>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0xfff,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jg     11d3 <sysfs_read+0x83>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <sysfs_read+0x97>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
mov    $0x1000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a5 <sysfs_read+0x55>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0xfff,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14d,%r14d
jle    119d <sysfs_read+0x4d>
mov    %r14d,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    11a7 <sysfs_read+0x57>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define hidden i8* @sysfs_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %6
%10 = call i8* @malloc(i32 4096)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* null, i8** %2
br label %31
14: ; preds = %1
%15 = load i32, i32* %6
%16 = load i8*, i8** %4
%17 = call i32 @read(i32 %15, i8* %16, i32 4095)
store i32 %17, i32* %5
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %5
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
store i8* null, i8** %2
br label %31
25: ; preds = %14
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %25, %22, %13
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define hidden noalias i8* @sysfs_read(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%5 = icmp eq i8* %4, null
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = tail call i32 @read(i32 %3, i8* %4, i32 4095)
%8 = tail call i32 @close(i32 %3)
%9 = icmp slt i32 %7, 1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @free(i8* %4)
br label %15
12: ; preds = %6
%13 = zext i32 %7 to i64
%14 = getelementptr inbounds i8, i8* %4, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %1, %12, %10
%16 = phi i8* [ null, %10 ], [ %4, %12 ], [ null, %1 ]
ret i8* %16
}