#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *read_file(char fname[]) {
  FILE *fd = fopen(fname, "r");
  if (fd == NULL) {
    fprintf(stderr, "Couldn't open file.\n");
    return NULL;
  }
  if (fseek(fd, 0, SEEK_END)) {
    fprintf(stderr, "Couldn't seek to end of file.\n");
    return NULL;
  }
  size_t fsize = (size_t)ftell(fd);
  rewind(fd);
  char *fstring = malloc(sizeof(char) * (fsize + 1));
  if (fstring == NULL) {
    fprintf(stderr, "Couldn't allocate memory for string.\n");
    return NULL;
  }
  fread(fstring, 1, fsize, fd);
  fclose(fd);
  fstring[fsize] = '\0';
  return fstring;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <read_file+0x4d>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129c <read_file+0x11c>
mov    -0x18(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     120d <read_file+0x8d>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129c <read_file+0x11c>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1269 <read_file+0xe9>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129c <read_file+0x11c>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%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):
<read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11c3 <read_file+0x43>
mov    %rax,%rbx
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
je     11d5 <read_file+0x55>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
jmp    122e <read_file+0xae>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
jmp    122e <read_file+0xae>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <rewind@plt>
lea    0x1(%r15),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     121e <read_file+0x9e>
mov    %rax,%r14
movslq %r15d,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1236 <read_file+0xb6>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1060 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Couldn't open file.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"Couldn't seek to end of file.\0A\00"
@.str.3 = [38 x i8] c"Couldn't allocate memory for string.\0A\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%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 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %49
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = load i32, i32* @SEEK_END
%17 = call i64 @fseek(i32* %15, i32 0, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %49
22: ; preds = %14
%23 = load i32*, i32** %4
%24 = call i32 @ftell(i32* %23)
%25 = sext i32 %24 to i64
store i64 %25, i64* %5
%26 = load i32*, i32** %4
%27 = call i32 @rewind(i32* %26)
%28 = load i64, i64* %5
%29 = add i64 %28, 1
%30 = mul i64 1, %29
%31 = trunc i64 %30 to i32
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %22
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %2
br label %49
38: ; preds = %22
%39 = load i8*, i8** %6
%40 = load i64, i64* %5
%41 = load i32*, i32** %4
%42 = call i32 @fread(i8* %39, i32 1, i64 %40, i32* %41)
%43 = load i32*, i32** %4
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %6
%46 = load i64, i64* %5
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %6
store i8* %48, i8** %2
br label %49
49: ; preds = %38, %35, %19, %11
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Couldn't open file.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [31 x i8] c"Couldn't seek to end of file.\0A\00"
@.str.3 = [38 x i8] c"Couldn't allocate memory for string.\0A\00"
define noalias i8* @read_file(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 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %28
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i64 @fseek(i32* %2, i32 0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %28
14: ; preds = %7
%15 = tail call i32 @ftell(i32* %2)
%16 = sext i32 %15 to i64
%17 = tail call i32 @rewind(i32* %2)
%18 = add i32 %15, 1
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %28
24: ; preds = %14
%25 = tail call i32 @fread(i8* %19, i32 1, i64 %16, i32* %2)
%26 = tail call i32 @fclose(i32* %2)
%27 = getelementptr inbounds i8, i8* %19, i64 %16
store i8 0, i8* %27
br label %28
28: ; preds = %21, %24, %11, %4
%29 = phi i8* [ null, %4 ], [ null, %11 ], [ null, %21 ], [ %19, %24 ]
ret i8* %29
}