#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
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 *, ...);
int readFile() {
  FILE *f;
  char *tmpBuf;
  long lSize;
  f = fopen("/tmp/fileTest", "rt");
  if (f == NULL)
    return 1;
  fseek(f, 0, SEEK_END);
  lSize = ftell(f);
  printf("Length of file is: %ld\n", lSize);
  fseek(f, 0, SEEK_SET);
  tmpBuf = (char *)malloc(sizeof(char) * lSize);
  size_t n = fread(tmpBuf, 1, lSize, f);
  if (n == lSize) {
    printf("Ouput from file is: %s\n", tmpBuf);
  } else {
    printf("Reading failed %zd\n", n);
    return 1;
  }
  free(tmpBuf);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
lea    0xe78(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b6 <readFile+0x36>
movl   $0x1,-0x4(%rbp)
jmp    1286 <readFile+0x106>
mov    -0x10(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    124f <readFile+0xcf>
mov    -0x18(%rbp),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    126d <readFile+0xed>
mov    -0x28(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1286 <readFile+0x106>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe72(%rip),%rdi
lea    0xe79(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1241 <readFile+0xc1>
mov    %rax,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbp
lea    0xe43(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %rbp,%rax
jne    1230 <readFile+0xb0>
lea    0xe15(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1247 <readFile+0xc7>
lea    0xe0a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/fileTest\00"
@.str.1 = [3 x i8] c"rt\00"
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"Length of file is: %ld\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [24 x i8] c"Ouput from file is: %s\0A\00"
@.str.4 = [20 x i8] c"Reading failed %zd\0A\00"
define i32 @readFile() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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 %10
9: ; preds = %0
store i32 1, i32* %1
br label %44
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = load i32, i32* @SEEK_END
%13 = call i32 @fseek(i32* %11, i32 0, i32 %12)
%14 = load i32*, i32** %2
%15 = call i64 @ftell(i32* %14)
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i64 %16)
%18 = load i32*, i32** %2
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i64, i64* %4
%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** %3
%26 = load i8*, i8** %3
%27 = load i64, i64* %4
%28 = load i32*, i32** %2
%29 = call i64 @fread(i8* %26, i32 1, i64 %27, i32* %28)
store i64 %29, i64* %5
%30 = load i64, i64* %5
%31 = load i64, i64* %4
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %10
%34 = load i8*, i8** %3
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %34)
br label %39
36: ; preds = %10
%37 = load i64, i64* %5
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i64 %37)
store i32 1, i32* %1
br label %44
39: ; preds = %33
%40 = load i8*, i8** %3
%41 = call i32 @free(i8* %40)
%42 = load i32*, i32** %2
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %1
br label %44
44: ; preds = %39, %36, %9
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/fileTest\00"
@.str.1 = [3 x i8] c"rt\00"
@SEEK_END = global i32 0
@.str.2 = [24 x i8] c"Length of file is: %ld\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [24 x i8] c"Ouput from file is: %s\0A\00"
@.str.4 = [20 x i8] c"Reading failed %zd\0A\00"
define i32 @readFile() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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 %21, label %3
3: ; preds = %0
%4 = load i32, i32* @SEEK_END
%5 = tail call i32 @fseek(i32* %1, i32 0, i32 %4)
%6 = tail call i64 @ftell(i32* %1)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i64 %6)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %1, i32 0, i32 %8)
%10 = trunc i64 %6 to i32
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i64 @fread(i8* %12, i32 1, i64 %6, i32* %1)
%14 = icmp eq i64 %13, %6
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%17 = tail call i32 @free(i8* %12)
%18 = tail call i32 @fclose(i32* %1)
br label %21
19: ; preds = %3
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i64 %13)
br label %21
21: ; preds = %19, %15, %0
%22 = phi i32 [ 1, %0 ], [ 0, %15 ], [ 1, %19 ]
ret i32 %22
}