#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 {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char const *, struct stat *);
size_t read_all_bytes(const char *fileName, void **mem) {
  FILE *file;
  struct stat mystat;
  void *content;
  *mem = NULL;
  if (0 != stat(fileName, &mystat)) {
    return 0;
  }
  file = fopen(fileName, "rb");
  if (!file) {
    printf("Read file failed.\n");
    return 0;
  }
  content = malloc(mystat.st_size + 1);
  if (content == NULL) {
    printf("Malloc failed.\n");
    return 0;
  }
  ((char *)content)[mystat.st_size] = 0;
  if (mystat.st_size != fread(content, 1, mystat.st_size, file)) {
    goto FREE_CONTENT;
  }
  *mem = content;
  return mystat.st_size;
FREE_CONTENT:
  free(content);
  return 0;
}


x86 assembly (clang, -O0):
<read_all_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11a3 <read_all_bytes+0x43>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_all_bytes+0x116>
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <read_all_bytes+0x7b>
lea    0xe3a(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_all_bytes+0x116>
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120e <read_all_bytes+0xae>
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_all_bytes+0x116>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movslq -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     124d <read_all_bytes+0xed>
jmp    1265 <read_all_bytes+0x105>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1276 <read_all_bytes+0x116>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all_bytes>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r13
mov    %rdi,%rbx
movq   $0x0,(%rsi)
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     1199 <read_all_bytes+0x39>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe60(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ee <read_all_bytes+0x8e>
mov    %rax,%r14
movslq 0x8(%rsp),%r15
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11fc <read_all_bytes+0x9c>
mov    %rax,%r12
movb   $0x0,(%rax,%r15,1)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    %r15,%rax
jne    120d <read_all_bytes+0xad>
mov    %r12,0x0(%r13)
movslq 0x8(%rsp),%rax
jmp    118b <read_all_bytes+0x2b>
lea    0xe0e(%rip),%rdi
call   1040 <puts@plt>
jmp    1189 <read_all_bytes+0x29>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
jmp    1189 <read_all_bytes+0x29>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1189 <read_all_bytes+0x29>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Read file failed.\0A\00"
@.str.2 = [16 x i8] c"Malloc failed.\0A\00"
define i64 @read_all_bytes(i8* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
store i8* null, i8** %9
%10 = load i8*, i8** %4
%11 = call i64 @stat(i8* %10, %struct.stat* %7)
%12 = icmp ne i64 0, %11
br i1 %12, label %13, label %14
13: ; preds = %2
store i64 0, i64* %3
br label %56
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %3
br label %56
21: ; preds = %14
%22 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %3
br label %56
30: ; preds = %21
%31 = load i8*, i8** %8
%32 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = load i8*, i8** %8
%40 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%41 = load i32, i32* %40
%42 = sext i32 %41 to i64
%43 = load i32*, i32** %6
%44 = call i64 @fread(i8* %39, i32 1, i64 %42, i32* %43)
%45 = icmp ne i64 %38, %44
br i1 %45, label %46, label %47
46: ; preds = %30
br label %53
47: ; preds = %30
%48 = load i8*, i8** %8
%49 = load i8**, i8*** %5
store i8* %48, i8** %49
%50 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = sext i32 %51 to i64
store i64 %52, i64* %3
br label %56
53: ; preds = %46
%54 = load i8*, i8** %8
%55 = call i32 @free(i8* %54)
store i64 0, i64* %3
br label %56
56: ; preds = %53, %47, %28, %19, %13
%57 = load i64, i64* %3
ret i64 %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@str = [18 x i8] c"Read file failed.\00"
@str.3 = [15 x i8] c"Malloc failed.\00"
define i64 @read_all_bytes(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
store i8* null, i8** %1
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %30
7: ; preds = %2
%8 = 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 %10, label %12
10: ; preds = %7
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %30
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
%16 = call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
br label %30
20: ; preds = %12
%21 = sext i32 %14 to i64
%22 = getelementptr inbounds i8, i8* %16, i64 %21
store i8 0, i8* %22
%23 = call i64 @fread(i8* %16, i32 1, i64 %21, i32* %8)
%24 = icmp eq i64 %23, %21
br i1 %24, label %25, label %28
25: ; preds = %20
store i8* %16, i8** %1
%26 = load i32, i32* %13
%27 = sext i32 %26 to i64
br label %30
28: ; preds = %20
%29 = call i32 @free(i8* %16)
br label %30
30: ; preds = %2, %28, %25, %18, %10
%31 = phi i64 [ 0, %18 ], [ 0, %28 ], [ %27, %25 ], [ 0, %10 ], [ 0, %2 ]
ret i64 %31
}