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

/* Variables and functions */
int fread(void *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
long fsize(int /*<<< orphan*/ *);
void *malloc(long);
void *fread_all(FILE *stream, unsigned long *len) {
  void *buffer = NULL;
  long int size = fsize(stream);
  if (size != -1) {
    buffer = malloc(size);
  }
  if (NULL != buffer) {
    if (fread(buffer, size, 1, stream) == 1) {
      *len = (unsigned long)size;
    } else {
      free(buffer);
      buffer = NULL;
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<fread_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fsize@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     117d <fread_all+0x3d>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     11ce <fread_all+0x8e>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11b8 <fread_all+0x78>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c9 <fread_all+0x89>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    11ce <fread_all+0x8e>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_all>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1060 <fsize@plt>
cmp    $0xffffffffffffffff,%rax
je     1191 <fread_all+0x51>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1191 <fread_all+0x51>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1189 <fread_all+0x49>
mov    %rbx,(%r14)
jmp    1194 <fread_all+0x54>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @fread_all(i32* %0, i64* %1) {
%3 = alloca i32*
%4 = alloca i64*
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %3
store i64* %1, i64** %4
store i8* null, i8** %5
%7 = load i32*, i32** %3
%8 = call i64 @fsize(i32* %7)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i64, i64* %6
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %5
br label %14
14: ; preds = %11, %2
%15 = load i8*, i8** %5
%16 = icmp ne i8* null, %15
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = load i64, i64* %6
%20 = load i32*, i32** %3
%21 = call i32 @fread(i8* %18, i64 %19, i32 1, i32* %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i64, i64* %6
%25 = load i64*, i64** %4
store i64 %24, i64* %25
br label %29
26: ; preds = %17
%27 = load i8*, i8** %5
%28 = call i32 @free(i8* %27)
store i8* null, i8** %5
br label %29
29: ; preds = %26, %23
br label %30
30: ; preds = %29, %14
%31 = load i8*, i8** %5
ret i8* %31
}

LLVM IR (-O2):
define i8* @fread_all(i32* %0, i64* nocapture writeonly %1) {
%3 = tail call i64 @fsize(i32* %0)
%4 = icmp eq i64 %3, -1
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = tail call i8* @malloc(i64 %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %5
%9 = tail call i32 @fread(i8* %6, i64 %3, i32 1, i32* %0)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %8
store i64 %3, i64* %1
br label %14
12: ; preds = %8
%13 = tail call i32 @free(i8* %6)
br label %14
14: ; preds = %2, %11, %12, %5
%15 = phi i8* [ %6, %11 ], [ null, %12 ], [ null, %5 ], [ null, %2 ]
ret i8* %15
}