#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*/ 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*/ free(char *);
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;
int main() {
  FILE *pFile;
  long lSize;
  char *buffer;
  size_t result;
  pFile = fopen("myfile.bin", "rb");
  if (NULL == pFile) {
    fputs("File error", stderr);
    exit(1);
  }
  fseek(pFile, 0, SEEK_END);
  lSize = ftell(pFile);
  rewind(pFile);
  buffer = (char *)malloc(sizeof(char) * lSize);
  if (NULL == buffer) {
    fputs("Memory error", stderr);
    exit(2);
  }
  result = fread(buffer, 1, lSize, pFile);
  if (lSize != result) {
    fputs("Reading error", stderr);
    exit(3);
  }
  fclose(pFile);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11f1 <main+0x51>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe2c(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1259 <main+0xb9>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    0xdcf(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     12a0 <main+0x100>
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    0xd95(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe54(%rip),%rdi
lea    0xe58(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1226 <main+0x86>
mov    %rax,%rbx
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r14d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1245 <main+0xa5>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %rax,%r14
jne    1264 <main+0xc4>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    0xdc4(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xdb2(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>

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

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