#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 size_t uint;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
uint readfiletobuffer(char **ptr_buffer, char *file_name) {
  FILE *pFile;
  long lSize;
  char *buffer;
  size_t result;
  pFile = fopen(file_name, "rb");
  if (pFile == NULL) {
    fputs("File error", stderr);
    exit(1);
  }
  fseek(pFile, 0, SEEK_END);
  lSize = ftell(pFile);
  rewind(pFile);
  buffer = (char *)malloc((sizeof(char) * lSize));
  if (buffer == NULL) {
    fputs("Memory error", stderr);
    exit(2);
  }
  result = fread(buffer, 1, lSize, pFile);
  if (result != lSize) {
    fputs("Reading error", stderr);
    exit(3);
  }
  fclose(pFile);
  *ptr_buffer = buffer;
  return result;
}


x86 assembly (clang, -O0):
<readfiletobuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <readfiletobuffer+0x4e>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1245 <readfiletobuffer+0xb5>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     128c <readfiletobuffer+0xfc>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
lea    0xd9e(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfiletobuffer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe5b(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121d <readfiletobuffer+0x8d>
mov    %rax,%rbx
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r15d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     123c <readfiletobuffer+0xac>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
jne    125b <readfiletobuffer+0xcb>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,(%r14)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    0xdd6(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xdc2(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
lea    0xdb0(%rip),%rdi
call   1060 <fputs@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [11 x i8] c"File error\00"
@stderr = global i32 0
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Memory error\00"
@.str.3 = [14 x i8] c"Reading error\00"
define i64 @readfiletobuffer(i8** nocapture writeonly %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fputs(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %3, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %3)
%13 = tail call i32 @rewind(i32* %3)
%14 = trunc i64 %12 to i32
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %22
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = tail call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %9
%23 = tail call i64 @fread(i8* %16, i32 1, i64 %12, i32* %3)
%24 = icmp eq i64 %23, %12
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = tail call i32 @exit(i32 3)
unreachable
29: ; preds = %22
%30 = tail call i32 @fclose(i32* %3)
store i8* %16, i8** %0
ret i64 %12
}