#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, int);
const char *om_sysfs_readfile(const char *pathname) {
  static char buf[1024];
  const char *res = NULL;
  ssize_t count;
  int fd = -1;
  fd = open(pathname, O_RDONLY);
  if (fd < 0)
    return NULL;
  count = read(fd, buf, 1023);
  if (count < 0)
    goto cleanup;
  buf[count] = 0;
  res = buf;
cleanup:
  if (fd >= 0)
    close(fd);
  return buf;
}


x86 assembly (clang, -O0):
<om_sysfs_readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1177 <om_sysfs_readfile+0x47>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <om_sysfs_readfile+0xa6>
mov    -0x24(%rbp),%edi
lea    0x2ecf(%rip),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    119f <om_sysfs_readfile+0x6f>
jmp    11b9 <om_sysfs_readfile+0x89>
mov    -0x20(%rbp),%rcx
lea    0x2ea6(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e9b(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11cb <om_sysfs_readfile+0x9b>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
lea    0x2e7e(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<om_sysfs_readfile>:
push   %rbp
push   %rbx
push   %rax
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     116a <om_sysfs_readfile+0x3a>
mov    %eax,%ebp
lea    0x2f02(%rip),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
movb   $0x0,(%rax,%rbx,1)
mov    %ebp,%edi
call   1030 <close@plt>
jmp    116c <om_sysfs_readfile+0x3c>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@om_sysfs_readfile.buf = internal global [1024 x i8] zeroinitializer
@O_RDONLY = global i32 0
define i8* @om_sysfs_readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i32 -1, i32* %6
%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 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %29
13: ; preds = %1
%14 = load i32, i32* %6
%15 = call i64 @read(i32 %14, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 0), i32 1023)
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp ult i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
br label %22
19: ; preds = %13
%20 = load i64, i64* %5
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 %20
store i8 0, i8* %21
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 0), i8** %4
br label %22
22: ; preds = %19, %18
%23 = load i32, i32* %6
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %22
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 0), i8** %2
br label %29
29: ; preds = %28, %12
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@om_sysfs_readfile.buf = internal global [1024 x i8] zeroinitializer
@O_RDONLY = global i32 0
define i8* @om_sysfs_readfile(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = tail call i64 @read(i32 %3, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 0), i32 1023)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 %6
store i8 0, i8* %7
%8 = tail call i32 @close(i32 %3)
br label %9
9: ; preds = %1, %5
%10 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @om_sysfs_readfile.buf, i64 0, i64 0), %5 ], [ null, %1 ]
ret i8* %10
}