#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 int /*<<< orphan*/ uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _SC_PAGE_SIZE;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
int sysconf(int /*<<< orphan*/);
uint8_t *xh_read_bin_sysfs(const char *file, uint32_t *length_out) {
  uint32_t alloc_size = sysconf(_SC_PAGE_SIZE);
  FILE *fs = NULL;
  size_t rs;
  uint32_t total_length = 0;
  uint8_t *buffer, *tmp;
  int __err;
  buffer = malloc(alloc_size);
  if (buffer == NULL)
    return NULL;
  fs = fopen(file, "rb");
  if (fs == NULL)
    goto error_out;
  for (;;) {
    rs = fread((buffer + total_length), 1, alloc_size, fs);
    if (feof(fs)) {
      total_length += rs;
      break;
    }
    if (rs != alloc_size) {
      goto error_out;
    }
    total_length += alloc_size;
    tmp = realloc(buffer, total_length + alloc_size);
    if (tmp == NULL)
      goto error_out;
    buffer = tmp;
  }
  if (fs != NULL)
    fclose(fs);
  *length_out = total_length;
  return buffer;
error_out:
  __err = errno;
  if (fs != NULL)
    fclose(fs);
  free(buffer);
  errno = __err;
  return NULL;
}


x86 assembly (clang, -O0):
<xh_read_bin_sysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11d4 <xh_read_bin_sysfs+0x54>
movq   $0x0,-0x8(%rbp)
jmp    12fd <xh_read_bin_sysfs+0x17d>
mov    -0x10(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <xh_read_bin_sysfs+0x78>
jmp    12c0 <xh_read_bin_sysfs+0x140>
jmp    11fd <xh_read_bin_sysfs+0x7d>
mov    -0x40(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     1247 <xh_read_bin_sysfs+0xc7>
mov    -0x30(%rbp),%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
jmp    1296 <xh_read_bin_sysfs+0x116>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
je     125d <xh_read_bin_sysfs+0xdd>
jmp    12c0 <xh_read_bin_sysfs+0x140>
mov    -0x1c(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
add    -0x1c(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1289 <xh_read_bin_sysfs+0x109>
jmp    12c0 <xh_read_bin_sysfs+0x140>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11fd <xh_read_bin_sysfs+0x7d>
cmpq   $0x0,-0x28(%rbp)
je     12aa <xh_read_bin_sysfs+0x12a>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12fd <xh_read_bin_sysfs+0x17d>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12e0 <xh_read_bin_sysfs+0x160>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4c(%rbp),%ecx
mov    0x2ced(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xh_read_bin_sysfs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
mov    %rdi,%r12
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     127f <xh_read_bin_sysfs+0xff>
mov    %rax,%rbx
lea    0xe41(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1262 <xh_read_bin_sysfs+0xe2>
mov    %rax,%r12
mov    %rbp,0x8(%rsp)
movslq %r15d,%rax
mov    %rax,0x20(%rsp)
lea    0x0(,%rax,4),%rax
mov    %rax,0x18(%rsp)
lea    (%r15,%r15,1),%eax
mov    %rax,0x10(%rsp)
xor    %ebp,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%r13
lea    (%rbx,%rbp,1),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    1294 <xh_read_bin_sysfs+0x114>
cmp    0x20(%rsp),%rbx
jne    124f <xh_read_bin_sysfs+0xcf>
mov    0x10(%rsp),%rax
lea    (%rax,%r14,1),%esi
mov    %r13,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbx
add    %r15d,%r14d
add    0x18(%rsp),%rbp
test   %rax,%rax
jne    1200 <xh_read_bin_sysfs+0x80>
mov    0x2d8a(%rip),%rax
mov    (%rax),%ebp
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    126e <xh_read_bin_sysfs+0xee>
mov    0x2d77(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%r13
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x2d63(%rip),%rax
mov    %ebp,(%rax)
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    %r14d,%ebx
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rax
mov    %ebx,(%rax)
jmp    1282 <xh_read_bin_sysfs+0x102>

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
define i32* @xh_read_bin_sysfs(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%13 = load i32, i32* @_SC_PAGE_SIZE
%14 = call i32 @sysconf(i32 %13)
store i32 %14, i32* %6
store i32* null, i32** %7
store i32 0, i32* %9
%15 = load i32, i32* %6
%16 = call i32* @malloc(i32 %15)
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i32* null, i32** %3
br label %85
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %7
%23 = load i32*, i32** %7
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
br label %74
26: ; preds = %20
br label %27
27: ; preds = %62, %26
%28 = load i32*, i32** %10
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %6
%33 = load i32*, i32** %7
%34 = call i64 @fread(i32* %31, i32 1, i32 %32, i32* %33)
store i64 %34, i64* %8
%35 = load i32*, i32** %7
%36 = call i64 @feof(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %27
%39 = load i64, i64* %8
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = add i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %9
br label %64
44: ; preds = %27
%45 = load i64, i64* %8
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = icmp ne i64 %45, %47
br i1 %48, label %49, label %50
49: ; preds = %44
br label %74
50: ; preds = %44
%51 = load i32, i32* %6
%52 = load i32, i32* %9
%53 = add nsw i32 %52, %51
store i32 %53, i32* %9
%54 = load i32*, i32** %10
%55 = load i32, i32* %9
%56 = load i32, i32* %6
%57 = add nsw i32 %55, %56
%58 = call i32* @realloc(i32* %54, i32 %57)
store i32* %58, i32** %11
%59 = load i32*, i32** %11
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %62
61: ; preds = %50
br label %74
62: ; preds = %50
%63 = load i32*, i32** %11
store i32* %63, i32** %10
br label %27
64: ; preds = %38
%65 = load i32*, i32** %7
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
br label %70
70: ; preds = %67, %64
%71 = load i32, i32* %9
%72 = load i32*, i32** %5
store i32 %71, i32* %72
%73 = load i32*, i32** %10
store i32* %73, i32** %3
br label %85
74: ; preds = %61, %49, %25
%75 = load i32, i32* @errno
store i32 %75, i32* %12
%76 = load i32*, i32** %7
%77 = icmp ne i32* %76, null
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i32*, i32** %7
%80 = call i32 @fclose(i32* %79)
br label %81
81: ; preds = %78, %74
%82 = load i32*, i32** %10
%83 = call i32 @free(i32* %82)
%84 = load i32, i32* %12
store i32 %84, i32* @errno
store i32* null, i32** %3
br label %85
85: ; preds = %81, %70, %19
%86 = load i32*, i32** %3
ret i32* %86
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
define i32* @xh_read_bin_sysfs(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = load i32, i32* @_SC_PAGE_SIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = tail call i32* @malloc(i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %41, label %7
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = sext i32 %4 to i64
br label %14
12: ; preds = %7
%13 = load i32, i32* @errno
br label %37
14: ; preds = %10, %28
%15 = phi i64 [ 0, %10 ], [ %29, %28 ]
%16 = phi i32* [ %5, %10 ], [ %32, %28 ]
%17 = getelementptr inbounds i32, i32* %16, i64 %15
%18 = tail call i64 @fread(i32* %17, i32 1, i32 %4, i32* %8)
%19 = tail call i64 @feof(i32* %8)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = trunc i64 %15 to i32
%23 = trunc i64 %18 to i32
%24 = add i32 %22, %23
%25 = tail call i32 @fclose(i32* %8)
store i32 %24, i32* %1
br label %41
26: ; preds = %14
%27 = icmp eq i64 %18, %11
br i1 %27, label %28, label %34
28: ; preds = %26
%29 = add i64 %15, %11
%30 = trunc i64 %29 to i32
%31 = add i32 %4, %30
%32 = tail call i32* @realloc(i32* %16, i32 %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %14
34: ; preds = %26, %28
%35 = load i32, i32* @errno
%36 = tail call i32 @fclose(i32* %8)
br label %37
37: ; preds = %12, %34
%38 = phi i32 [ %13, %12 ], [ %35, %34 ]
%39 = phi i32* [ %5, %12 ], [ %16, %34 ]
%40 = tail call i32 @free(i32* %39)
store i32 %38, i32* @errno
br label %41
41: ; preds = %2, %37, %21
%42 = phi i32* [ null, %37 ], [ %16, %21 ], [ null, %2 ]
ret i32* %42
}