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

/* Variables and functions */
int ReadEntireFile(char *, char *, char *, long *);
long _GetFileSize(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(long);
int /*<<< orphan*/ memset(char *, int, long);
int ReadEntireFileSafely(char *filename, char *mode, char **pbuf,
                         long *pbufsize) {
  long filesize;
  int nret;
  filesize = _GetFileSize(filename);
  (*pbuf) = (char *)malloc(filesize + 1);
  if ((*pbuf) == NULL)
    return -1;
  memset((*pbuf), 0x00, filesize + 1);
  nret = ReadEntireFile(filename, mode, (*pbuf), &filesize);
  if (nret) {
    free((*pbuf));
    (*pbuf) = NULL;
    return nret;
  } else {
    if (pbufsize)
      (*pbufsize) = filesize;
    return 0;
  }
}


x86 assembly (clang, -O0):
<ReadEntireFileSafely>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <_GetFileSize@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a6 <ReadEntireFileSafely+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <ReadEntireFileSafely+0xd0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <ReadEntireFile@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1203 <ReadEntireFileSafely+0xb3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <ReadEntireFileSafely+0xd0>
cmpq   $0x0,-0x28(%rbp)
je     1219 <ReadEntireFileSafely+0xc9>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadEntireFileSafely>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%rbp
call   1060 <_GetFileSize@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,0x0(%r13)
test   %rax,%rax
je     11c6 <ReadEntireFileSafely+0x76>
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    0x0(%r13),%rdx
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <ReadEntireFile@plt>
test   %eax,%eax
je     11ce <ReadEntireFileSafely+0x7e>
mov    %eax,%ebx
mov    0x0(%r13),%rdi
call   1030 <free@plt>
movq   $0x0,0x0(%r13)
mov    %ebx,%r12d
jmp    11da <ReadEntireFileSafely+0x8a>
mov    $0xffffffff,%r12d
jmp    11da <ReadEntireFileSafely+0x8a>
test   %r14,%r14
je     11da <ReadEntireFileSafely+0x8a>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ReadEntireFileSafely(i8* %0, i8* %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
%12 = load i8*, i8** %6
%13 = call i64 @_GetFileSize(i8* %12)
store i64 %13, i64* %10
%14 = load i64, i64* %10
%15 = add nsw i64 %14, 1
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = load i8**, i8*** %8
store i8* %17, i8** %18
%19 = load i8**, i8*** %8
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 -1, i32* %5
br label %49
23: ; preds = %4
%24 = load i8**, i8*** %8
%25 = load i8*, i8** %24
%26 = load i64, i64* %10
%27 = add nsw i64 %26, 1
%28 = call i32 @memset(i8* %25, i32 0, i64 %27)
%29 = load i8*, i8** %6
%30 = load i8*, i8** %7
%31 = load i8**, i8*** %8
%32 = load i8*, i8** %31
%33 = call i32 @ReadEntireFile(i8* %29, i8* %30, i8* %32, i64* %10)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %23
%37 = load i8**, i8*** %8
%38 = load i8*, i8** %37
%39 = call i32 @free(i8* %38)
%40 = load i8**, i8*** %8
store i8* null, i8** %40
%41 = load i32, i32* %11
store i32 %41, i32* %5
br label %49
42: ; preds = %23
%43 = load i64*, i64** %9
%44 = icmp ne i64* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i64, i64* %10
%47 = load i64*, i64** %9
store i64 %46, i64* %47
br label %48
48: ; preds = %45, %42
store i32 0, i32* %5
br label %49
49: ; preds = %48, %36, %22
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
define i32 @ReadEntireFileSafely(i8* %0, i8* %1, i8** nocapture %2, i64* writeonly %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = tail call i64 @_GetFileSize(i8* %0)
store i64 %7, i64* %5
%8 = add nsw i64 %7, 1
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %2
%11 = icmp eq i64 %9, 0
br i1 %11, label %24, label %12
12: ; preds = %4
%13 = tail call i32 @memset(i8* %10, i32 0, i64 %8)
%14 = load i8*, i8** %2
%15 = call i32 @ReadEntireFile(i8* %0, i8* %1, i8* %14, i64* %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = call i32 @free(i8* %18)
store i8* null, i8** %2
br label %24
20: ; preds = %12
%21 = icmp eq i64* %3, null
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = load i64, i64* %5
store i64 %23, i64* %3
br label %24
24: ; preds = %20, %22, %4, %17
%25 = phi i32 [ %15, %17 ], [ -1, %4 ], [ 0, %22 ], [ 0, %20 ]
ret i32 %25
}