#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
char *readfile(const char *fname) {
  FILE *f = fopen(fname, "r");
  fseek(f, 0, SEEK_END);
  ssize_t fsz = ftell(f);
  fseek(f, 0, SEEK_SET);
  char *res = (char *)malloc((fsz + 1) * sizeof(char));
  if (res == NULL) {
    return res;
  }
  if (fread(res, sizeof(char), fsz, f) != (size_t)fsz) {
    free(res);
    return NULL;
  }
  res[fsz] = '\0';
  return res;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e3 <readfile+0x83>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122f <readfile+0xcf>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
je     121b <readfile+0xbb>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    122f <readfile+0xcf>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r12
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r12,%rdi
call   1050 <ftell@plt>
mov    %eax,%r15d
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11eb <readfile+0x8b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
movslq %r15d,%rcx
cmp    %rcx,%rax
jne    11e0 <readfile+0x80>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%r14
jmp    11eb <readfile+0x8b>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @readfile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @SEEK_END
%4 = tail call i32 @fseek(i32* %2, i32 0, i32 %3)
%5 = tail call i32 @ftell(i32* %2)
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @fseek(i32* %2, i32 0, i32 %6)
%8 = add nsw i32 %5, 1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %20, label %12
12: ; preds = %1
%13 = tail call i64 @fread(i8* %10, i32 1, i32 %5, i32* %2)
%14 = sext i32 %5 to i64
%15 = icmp eq i64 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @free(i8* %10)
br label %20
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 0, i8* %19
br label %20
20: ; preds = %1, %18, %16
%21 = phi i8* [ null, %16 ], [ %10, %18 ], [ null, %1 ]
ret i8* %21
}