#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*/ *fopen(char *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ printf(char *, ...);
char *strlen(char *);
int main() {
  FILE *fp;
  long file_size;
  char *buffer;
  int i = 0;
  fp = fopen("foo.txt", "rb");
  if (fp == NULL) {
    printf("Cannot open file\n");
  }
  if (fseek(fp, 0, SEEK_END) != 0) {
    printf("Cannot reposition file pointer\n");
  }
  file_size = ftell(fp);
  if (file_size == -1) {
    printf("File size calculation failed\n");
  } else {
    printf("File size is %lu\n", file_size);
  }
  buffer = (char *)malloc(file_size);
  if (buffer == NULL) {
    printf("Size allocation failed\n");
  } else {
    i = 0;
    int count = 0;
    while (i < file_size - 1) {
      buffer[i] = 'a';
      printf("%d\t%c\n", count, buffer[i]);
      count += 1;
      i += 1;
    }
    buffer[file_size - 1] = '\0';
    printf("Length of buffer is %lu\n", strlen(buffer));
    printf("Buffer is %s\n", buffer);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <main+0x46>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11d2 <main+0x72>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11fd <main+0x9d>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120f <main+0xaf>
mov    -0x18(%rbp),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123a <main+0xda>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d9 <main+0x179>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jge    129d <main+0x13d>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1248 <main+0xe8>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe81(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119f <main+0x2f>
lea    0xf04(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     11c3 <main+0x53>
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
je     11e7 <main+0x77>
lea    0xe30(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f3 <main+0x83>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1270 <main+0x100>
mov    %rax,%r15
lea    -0x1(%r14),%r12
cmp    $0x2,%r14
jl     123f <main+0xcf>
lea    0xe09(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movb   $0x61,(%r15,%rbx,1)
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x61,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1220 <main+0xb0>
movb   $0x0,(%r15,%r12,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xdd1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd9(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    127c <main+0x10c>
lea    0xdd4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"foo.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 = [32 x i8] c"Cannot reposition file pointer\0A\00"
@.str.4 = [30 x i8] c"File size calculation failed\0A\00"
@.str.5 = [18 x i8] c"File size is %lu\0A\00"
@.str.6 = [24 x i8] c"Size allocation failed\0A\00"
@.str.7 = [7 x i8] c"%d\09%c\0A\00"
@.str.8 = [25 x i8] c"Length of buffer is %lu\0A\00"
@.str.9 = [14 x i8] c"Buffer is %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%7 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32*, i32** %2
%14 = load i32, i32* @SEEK_END
%15 = call i64 @fseek(i32* %13, i32 0, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i32*, i32** %2
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %3
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %29
26: ; preds = %19
%27 = load i64, i64* %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 %27)
br label %29
29: ; preds = %26, %24
%30 = load i64, i64* %3
%31 = call i64 @malloc(i64 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %4
%33 = load i8*, i8** %4
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %71
37: ; preds = %29
store i32 0, i32* %5
store i32 0, i32* %6
br label %38
38: ; preds = %44, %37
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = load i64, i64* %3
%42 = sub nsw i64 %41, 1
%43 = icmp slt i64 %40, %42
br i1 %43, label %44, label %61
44: ; preds = %38
%45 = load i8*, i8** %4
%46 = load i32, i32* %5
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 97, i8* %48
%49 = load i32, i32* %6
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %49, i32 %55)
%57 = load i32, i32* %6
%58 = add nsw i32 %57, 1
store i32 %58, i32* %6
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %38
61: ; preds = %38
%62 = load i8*, i8** %4
%63 = load i64, i64* %3
%64 = sub nsw i64 %63, 1
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %4
%67 = call i8* @strlen(i8* %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %4
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %61, %35
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
@.str = [8 x i8] c"foo.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.5 = [18 x i8] c"File size is %lu\0A\00"
@.str.7 = [7 x i8] c"%d\09%c\0A\00"
@.str.8 = [25 x i8] c"Length of buffer is %lu\0A\00"
@.str.9 = [14 x i8] c"Buffer is %s\0A\00"
@str = [23 x i8] c"Size allocation failed\00"
@str.10 = [29 x i8] c"File size calculation failed\00"
@str.11 = [31 x i8] c"Cannot reposition file pointer\00"
@str.12 = [17 x i8] c"Cannot open file\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 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.12, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @fseek(i32* %1, i32 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 ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = tail call i64 @ftell(i32* %1)
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.10, i64 0, i64 0))
br label %18
16: ; preds = %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 %12)
br label %18
18: ; preds = %16, %14
%19 = tail call i64 @malloc(i64 %12)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = add i64 %12, -1
%24 = icmp sgt i64 %12, 1
br i1 %24, label %27, label %34
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %39
27: ; preds = %22, %27
%28 = phi i64 [ %32, %27 ], [ 0, %22 ]
%29 = trunc i64 %28 to i32
%30 = getelementptr inbounds i8, i8* %20, i64 %28
store i8 97, i8* %30
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i32 %29, i32 97)
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %23
br i1 %33, label %34, label %27
34: ; preds = %27, %22
%35 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 0, i8* %35
%36 = tail call i8* @strlen(i8* %20)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %36)
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* %20)
br label %39
39: ; preds = %34, %25
ret i32 0
}