#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  FILE *fp;
  size_t size;
  long length;
  char *buffer;
  fp = fopen("file.txt", "rb");
  if (fp == NULL) {
    printf("Cannot open file\n");
  }
  if (fseek(fp, 0, SEEK_END) != 0) {
    printf("Could not obtain file size\n");
  }
  length = ftell(fp);
  if (fseek(fp, 0L, SEEK_SET) != 0) {
    printf("Could not reposition\n");
  }
  buffer = (char *)malloc(length);
  if (buffer == NULL) {
    printf("Memory allocation failed\n");
  }
  buffer[6] = 'P';
  buffer[7] = 'a';
  buffer[8] = 's';
  buffer[9] = 's';
  buffer[10] = 'w';
  size = 6;
  if (fread(buffer, 1, size, fp) < size) {
    printf("Reading from file failed\n");
    exit(0);
  }
  fclose(fp);
  buffer[5] = '\0';
  printf("Buffer is - %s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bf <main+0x3f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11ed <main+0x6d>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     1228 <main+0xa8>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124e <main+0xce>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
movb   $0x50,0x6(%rax)
mov    -0x28(%rbp),%rax
movb   $0x61,0x7(%rax)
mov    -0x28(%rbp),%rax
movb   $0x73,0x8(%rax)
mov    -0x28(%rbp),%rax
movb   $0x73,0x9(%rax)
mov    -0x28(%rbp),%rax
movb   $0x77,0xa(%rax)
movq   $0x6,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
jae    12b3 <main+0x133>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,0x5(%rax)
mov    -0x28(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe65(%rip),%rdi
lea    0xe67(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11bb <main+0x2b>
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
je     11df <main+0x4f>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbx
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
je     120e <main+0x7e>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    122a <main+0x9a>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
movl   $0x73736150,0x6(%rbx)
movb   $0x77,0xa(%rbx)
mov    $0x6,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x5,%rax
jbe    1277 <main+0xe7>
mov    %r14,%rdi
call   1050 <fclose@plt>
movb   $0x0,0x5(%rbx)
lea    0xda9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [18 x i8] c"Cannot open file\0A\00"
@SEEK_END = global i32 0
@.str.3 = [28 x i8] c"Could not obtain file size\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [22 x i8] c"Could not reposition\0A\00"
@.str.5 = [26 x i8] c"Memory allocation failed\0A\00"
@.str.6 = [26 x i8] c"Reading from file failed\0A\00"
@.str.7 = [16 x i8] c"Buffer is - %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
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 %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32*, i32** %2
%13 = load i32, i32* @SEEK_END
%14 = call i64 @fseek(i32* %12, i64 0, i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i32*, i32** %2
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %4
%21 = load i32*, i32** %2
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @fseek(i32* %21, i64 0, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = load i64, i64* %4
%29 = call i64 @malloc(i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i64 6
store i8 80, i8* %37
%38 = load i8*, i8** %5
%39 = getelementptr inbounds i8, i8* %38, i64 7
store i8 97, i8* %39
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 8
store i8 115, i8* %41
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i64 9
store i8 115, i8* %43
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i64 10
store i8 119, i8* %45
store i64 6, i64* %3
%46 = load i8*, i8** %5
%47 = load i64, i64* %3
%48 = load i32*, i32** %2
%49 = call i64 @fread(i8* %46, i32 1, i64 %47, i32* %48)
%50 = load i64, i64* %3
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %35
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %35
%56 = load i32*, i32** %2
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %5
%59 = getelementptr inbounds i8, i8* %58, i64 5
store i8 0, i8* %59
%60 = load i8*, i8** %5
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %60)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [16 x i8] c"Buffer is - %s\0A\00"
@str = [25 x i8] c"Reading from file failed\00"
@str.8 = [25 x i8] c"Memory allocation failed\00"
@str.9 = [21 x i8] c"Could not reposition\00"
@str.10 = [27 x i8] c"Could not obtain file size\00"
@str.11 = [17 x i8] c"Cannot open file\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 %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @fseek(i32* %1, i64 0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = tail call i64 @ftell(i32* %1)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %1, i64 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = tail call i64 @malloc(i64 %12)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = getelementptr inbounds i8, i8* %20, i64 6
store i8 80, i8* %25
%26 = getelementptr inbounds i8, i8* %20, i64 7
store i8 97, i8* %26
%27 = getelementptr inbounds i8, i8* %20, i64 8
store i8 115, i8* %27
%28 = getelementptr inbounds i8, i8* %20, i64 9
store i8 115, i8* %28
%29 = getelementptr inbounds i8, i8* %20, i64 10
store i8 119, i8* %29
%30 = tail call i64 @fread(i8* %20, i32 1, i64 6, i32* %1)
%31 = icmp ult i64 %30, 6
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%34 = tail call i32 @exit(i32 0)
unreachable
35: ; preds = %24
%36 = tail call i32 @fclose(i32* %1)
%37 = getelementptr inbounds i8, i8* %20, i64 5
store i8 0, i8* %37
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %20)
ret i32 0
}