#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 *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(int);
char *readfiletomem(char *file) {
  char *source = NULL;
  FILE *fp = fopen(file, "rb");
  if (fp != NULL) {
    if (fseek(fp, 0L, SEEK_END) == 0) {
      long bufsize = ftell(fp);
      if (bufsize == -1) {
      }
      source = malloc(sizeof(char) * (bufsize + 1));
      if (source != NULL) {
        size_t newLen;
        fseek(fp, 0L, SEEK_SET);
        newLen = fread(source, sizeof(char), bufsize, fp);
        source[++newLen] = '\0';
      }
    }
    fclose(fp);
  }
  return (source);
}


x86 assembly (clang, -O0):
<readfiletomem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1248 <readfiletomem+0xe8>
mov    -0x18(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
jne    123f <readfiletomem+0xdf>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11d0 <readfiletomem+0x70>
jmp    11d0 <readfiletomem+0x70>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     123a <readfiletomem+0xda>
mov    -0x18(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
movb   $0x0,0x1(%rax,%rcx,1)
jmp    123f <readfiletomem+0xdf>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfiletomem>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11dd <readfiletomem+0x7d>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
jne    11e5 <readfiletomem+0x85>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r15
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e2 <readfiletomem+0x82>
mov    %rax,%r14
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
movb   $0x0,0x1(%rax,%r14,1)
jmp    11e5 <readfiletomem+0x85>
xor    %r14d,%r14d
jmp    11ed <readfiletomem+0x8d>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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