#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int readfile(const char *filename, void **mem, int *size) {
  FILE *file;
  char *filemem;
  int filesize, readsize;
  *mem = NULL;
  *size = 0;
  file = fopen(filename, "rb");
  if (!file)
    return -1;
  fseek(file, 0, SEEK_END);
  filesize = ftell(file);
  fseek(file, 0, SEEK_SET);
  filemem = malloc(filesize);
  if (!filemem) {
    fclose(file);
    return -1;
  }
  readsize = fread(filemem, 1, filesize, file);
  fclose(file);
  if (readsize < filesize) {
    free(filemem);
    return -1;
  }
  *mem = filemem;
  *size = filesize;
  return 0;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <readfile+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <readfile+0x111>
mov    -0x28(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x34(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1224 <readfile+0xb4>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <readfile+0x111>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1266 <readfile+0xf6>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <readfile+0x111>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
movq   $0x0,(%rsi)
movl   $0x0,(%rdx)
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     1234 <readfile+0xc4>
mov    %rax,%rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%r13d
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r13d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1220 <readfile+0xb0>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %r13d,%r14d
jge    122a <readfile+0xba>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r14d
jmp    1234 <readfile+0xc4>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1234 <readfile+0xc4>
mov    %rbp,(%r12)
mov    %r13d,(%r15)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @readfile(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%12 = load i8**, i8*** %6
store i8* null, i8** %12
%13 = load i32*, i32** %7
store i32 0, i32* %13
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 -1, i32* %4
br label %53
19: ; preds = %3
%20 = load i32*, i32** %8
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i32*, i32** %8
%24 = call i32 @ftell(i32* %23)
store i32 %24, i32* %10
%25 = load i32*, i32** %8
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
%28 = load i32, i32* %10
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %19
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
store i32 -1, i32* %4
br label %53
35: ; preds = %19
%36 = load i8*, i8** %9
%37 = load i32, i32* %10
%38 = load i32*, i32** %8
%39 = call i32 @fread(i8* %36, i32 1, i32 %37, i32* %38)
store i32 %39, i32* %11
%40 = load i32*, i32** %8
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* %11
%43 = load i32, i32* %10
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = load i8*, i8** %9
%47 = call i32 @free(i8* %46)
store i32 -1, i32* %4
br label %53
48: ; preds = %35
%49 = load i8*, i8** %9
%50 = load i8**, i8*** %6
store i8* %49, i8** %50
%51 = load i32, i32* %10
%52 = load i32*, i32** %7
store i32 %51, i32* %52
store i32 0, i32* %4
br label %53
53: ; preds = %48, %45, %32, %18
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @readfile(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
store i8* null, i8** %1
store i32 0, i32* %2
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = tail call i8* @malloc(i32 %9)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @fclose(i32* %4)
br label %23
16: ; preds = %6
%17 = tail call i32 @fread(i8* %12, i32 1, i32 %9, i32* %4)
%18 = tail call i32 @fclose(i32* %4)
%19 = icmp slt i32 %17, %9
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @free(i8* %12)
br label %23
22: ; preds = %16
store i8* %12, i8** %1
store i32 %9, i32* %2
br label %23
23: ; preds = %3, %22, %20, %14
%24 = phi i32 [ -1, %20 ], [ 0, %22 ], [ -1, %14 ], [ -1, %3 ]
ret i32 %24
}