#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 EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ panicif(int, char *, ...);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stdin;
char *loadfile(const char *filename) {
  int len = -1, c, alloc;
  FILE *fp;
  char *buf;
  if (filename == NULL) {
    len = 0;
    alloc = 1024;
    buf = (char *)malloc(alloc);
    while ((c = fgetc(stdin)) != EOF) {
      buf[len] = c;
      len++;
      if (len >= alloc) {
        alloc += 1024;
        buf = (char *)realloc(buf, alloc);
      }
    }
  } else {
    panicif((fp = fopen(filename, "r")) == NULL, "Unable to open '%s'",
            filename);
    panicif((fseek(fp, 0L, SEEK_END) == -1), "fseek failed");
    panicif((len = ftell(fp)) == -1, "ftell failed");
    panicif((fseek(fp, 0L, SEEK_SET) == -1), "fseek failed");
    buf = (char *)malloc(len + 1);
    panicif((fread(buf, len, 1, fp) == -1), "fread failed");
    buf[len] = '\0';
    fclose(fp);
  }
  return buf;
}


x86 assembly (clang, -O0):
<loadfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1232 <loadfile+0xa2>
movl   $0x0,-0xc(%rbp)
movl   $0x400,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1060 <fgetc@plt>
mov    %eax,-0x10(%rbp)
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
je     122d <loadfile+0x9d>
mov    -0x10(%rbp),%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1228 <loadfile+0x98>
mov    -0x14(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    11c8 <loadfile+0x38>
jmp    1339 <loadfile+0x1a9>
mov    -0x8(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x8(%rbp),%rdx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1050 <panicif@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   1050 <panicif@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1050 <panicif@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1050 <panicif@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   1050 <panicif@plt>
mov    -0x28(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     1289 <loadfile+0xf9>
mov    %rdi,%rbx
lea    0xe55(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
sete   %dil
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <panicif@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xe26(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <panicif@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %eax,%r15d
xor    %edi,%edi
mov    %eax,%ebp
inc    %ebp
sete   %dil
lea    0xe0d(%rip),%rsi
xor    %eax,%eax
call   1050 <panicif@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <panicif@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
lea    0xdc0(%rip),%rsi
xor    %eax,%eax
call   1050 <panicif@plt>
movslq %r15d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    12b6 <loadfile+0x126>
mov    $0x400,%ebp
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    0x2d3e(%rip),%r14
mov    (%r14),%edi
call   1060 <fgetc@plt>
mov    0x2d27(%rip),%r15
cmp    (%r15),%eax
jne    12c2 <loadfile+0x132>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    12e1 <loadfile+0x151>
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
call   1060 <fgetc@plt>
add    $0x1,%rbx
cmp    (%r15),%eax
je     12b6 <loadfile+0x126>
mov    %al,(%r12,%rbx,1)
lea    0x1(%rbx),%eax
cmp    %eax,%ebp
jg     12d0 <loadfile+0x140>
add    $0x400,%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   10a0 <realloc@plt>
mov    %rax,%r12
jmp    12d0 <loadfile+0x140>

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Unable to open '%s'\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"fseek failed\00"
@.str.3 = [13 x i8] c"ftell failed\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"fread failed\00"
define i8* @loadfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %2
store i32 -1, i32* %3
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %40
10: ; preds = %1
store i32 0, i32* %3
store i32 1024, i32* %5
%11 = load i32, i32* %5
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %7
br label %14
14: ; preds = %38, %10
%15 = load i32, i32* @stdin
%16 = call i32 @fgetc(i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %39
19: ; preds = %14
%20 = load i32, i32* %4
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %7
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %21, i8* %25
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = load i32, i32* %5
%30 = icmp sge i32 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %19
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1024
store i32 %33, i32* %5
%34 = load i8*, i8** %7
%35 = load i32, i32* %5
%36 = call i64 @realloc(i8* %34, i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %7
br label %38
38: ; preds = %31, %19
br label %14
39: ; preds = %14
br label %81
40: ; preds = %1
%41 = load i8*, i8** %2
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %42, i32** %6
%43 = icmp eq i32* %42, null
%44 = zext i1 %43 to i32
%45 = load i8*, i8** %2
%46 = call i32 (i32, i8*, ...) @panicif(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = load i32*, i32** %6
%48 = load i32, i32* @SEEK_END
%49 = call i32 @fseek(i32* %47, i64 0, i32 %48)
%50 = icmp eq i32 %49, -1
%51 = zext i1 %50 to i32
%52 = call i32 (i32, i8*, ...) @panicif(i32 %51, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32*, i32** %6
%54 = call i32 @ftell(i32* %53)
store i32 %54, i32* %3
%55 = icmp eq i32 %54, -1
%56 = zext i1 %55 to i32
%57 = call i32 (i32, i8*, ...) @panicif(i32 %56, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32*, i32** %6
%59 = load i32, i32* @SEEK_SET
%60 = call i32 @fseek(i32* %58, i64 0, i32 %59)
%61 = icmp eq i32 %60, -1
%62 = zext i1 %61 to i32
%63 = call i32 (i32, i8*, ...) @panicif(i32 %62, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%64 = load i32, i32* %3
%65 = add nsw i32 %64, 1
%66 = call i64 @malloc(i32 %65)
%67 = inttoptr i64 %66 to i8*
store i8* %67, i8** %7
%68 = load i8*, i8** %7
%69 = load i32, i32* %3
%70 = load i32*, i32** %6
%71 = call i32 @fread(i8* %68, i32 %69, i32 1, i32* %70)
%72 = icmp eq i32 %71, -1
%73 = zext i1 %72 to i32
%74 = call i32 (i32, i8*, ...) @panicif(i32 %73, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%75 = load i8*, i8** %7
%76 = load i32, i32* %3
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
store i8 0, i8* %78
%79 = load i32*, i32** %6
%80 = call i32 @fclose(i32* %79)
br label %81
81: ; preds = %40, %39
%82 = load i8*, i8** %7
ret i8* %82
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Unable to open '%s'\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"fseek failed\00"
@.str.3 = [13 x i8] c"ftell failed\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"fread failed\00"
define i8* @loadfile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %31
3: ; preds = %1
%4 = tail call i64 @malloc(i32 1024)
%5 = inttoptr i64 %4 to i8*
%6 = load i32, i32* @stdin
%7 = tail call i32 @fgetc(i32 %6)
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %60, label %10
10: ; preds = %3, %24
%11 = phi i64 [ %17, %24 ], [ 0, %3 ]
%12 = phi i32 [ %28, %24 ], [ %7, %3 ]
%13 = phi i8* [ %26, %24 ], [ %5, %3 ]
%14 = phi i32 [ %25, %24 ], [ 1024, %3 ]
%15 = trunc i32 %12 to i8
%16 = getelementptr inbounds i8, i8* %13, i64 %11
store i8 %15, i8* %16
%17 = add nuw i64 %11, 1
%18 = trunc i64 %17 to i32
%19 = icmp sgt i32 %14, %18
br i1 %19, label %24, label %20
20: ; preds = %10
%21 = add nsw i32 %14, 1024
%22 = tail call i64 @realloc(i8* %13, i32 %21)
%23 = inttoptr i64 %22 to i8*
br label %24
24: ; preds = %20, %10
%25 = phi i32 [ %21, %20 ], [ %14, %10 ]
%26 = phi i8* [ %23, %20 ], [ %13, %10 ]
%27 = load i32, i32* @stdin
%28 = tail call i32 @fgetc(i32 %27)
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %60, label %10
31: ; preds = %1
%32 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i32* %32, null
%34 = zext i1 %33 to i32
%35 = tail call i32 (i32, i8*, ...) @panicif(i32 %34, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%36 = load i32, i32* @SEEK_END
%37 = tail call i32 @fseek(i32* %32, i64 0, i32 %36)
%38 = icmp eq i32 %37, -1
%39 = zext i1 %38 to i32
%40 = tail call i32 (i32, i8*, ...) @panicif(i32 %39, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%41 = tail call i32 @ftell(i32* %32)
%42 = icmp eq i32 %41, -1
%43 = zext i1 %42 to i32
%44 = tail call i32 (i32, i8*, ...) @panicif(i32 %43, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* @SEEK_SET
%46 = tail call i32 @fseek(i32* %32, i64 0, i32 %45)
%47 = icmp eq i32 %46, -1
%48 = zext i1 %47 to i32
%49 = tail call i32 (i32, i8*, ...) @panicif(i32 %48, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%50 = add nsw i32 %41, 1
%51 = tail call i64 @malloc(i32 %50)
%52 = inttoptr i64 %51 to i8*
%53 = tail call i32 @fread(i8* %52, i32 %41, i32 1, i32* %32)
%54 = icmp eq i32 %53, -1
%55 = zext i1 %54 to i32
%56 = tail call i32 (i32, i8*, ...) @panicif(i32 %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%57 = sext i32 %41 to i64
%58 = getelementptr inbounds i8, i8* %52, i64 %57
store i8 0, i8* %58
%59 = tail call i32 @fclose(i32* %32)
br label %60
60: ; preds = %24, %3, %31
%61 = phi i8* [ %52, %31 ], [ %5, %3 ], [ %26, %24 ]
ret i8* %61
}