#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*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main() {
  FILE *pFile;
  long lSize;
  char *buffer;
  size_t result;
  pFile = fopen("test.txt", "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);
  printf("%s\n", buffer);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1200 <main+0x50>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1267 <main+0xb7>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0xdbf(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x2,%edi
call   10d0 <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    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12ae <main+0xfe>
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
lea    0xd85(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rsi
lea    0xd70(%rip),%rdi
call   1060 <printf@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    0xe44(%rip),%rdi
lea    0xe46(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1245 <main+0x95>
mov    %rax,%rbx
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %r15d,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1264 <main+0xb4>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    1283 <main+0xd3>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe05(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xda3(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    0xd91(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\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"
@.str.5 = [4 x i8] c"%s\0A\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 ([9 x i8], [9 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* %7, null
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* %26, null
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* %5
%38 = load i64, i64* %3
%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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %47)
%49 = load i8*, i8** %4
%50 = call i32 @free(i8* %49)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\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"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 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 %21, %10
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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %14)
%30 = tail call i32 @free(i8* %14)
ret i32 0
}