#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 /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
char *load_buffer(const char *src_file) {
  FILE *handle = fopen(src_file, "r");
  if (NULL == handle) {
    perror(NULL);
    return NULL;
  }
  fseek(handle, 0, SEEK_END);
  long f_size = ftell(handle) + 1;
  fseek(handle, 0, SEEK_SET);
  int c = 0;
  int n = 0;
  char *buffer = (char *)malloc(sizeof(char) * f_size);
  while ((c = fgetc(handle)) != EOF)
    buffer[n++] = (char)c;
  fclose(handle);
  buffer[n] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<load_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11b2 <load_buffer+0x42>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1264 <load_buffer+0xf4>
mov    -0x18(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%eax
je     1247 <load_buffer+0xd7>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    120d <load_buffer+0x9d>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_buffer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1225 <load_buffer+0xb5>
mov    %rax,%r14
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1050 <ftell@plt>
lea    0x1(%rax),%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    0x2e02(%rip),%r12
cmp    (%r12),%eax
je     120a <load_buffer+0x9a>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r12),%eax
jne    11f0 <load_buffer+0x80>
mov    %ebx,%r15d
mov    %r14,%rdi
call   1030 <fclose@plt>
movb   $0x0,0x0(%r13,%r15,1)
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %r13d,%r13d
xor    %edi,%edi
call   1090 <perror@plt>
jmp    1218 <load_buffer+0xa8>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
define i8* @load_buffer(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 %6
4: ; preds = %1
%5 = tail call i32 @perror(i32* null)
br label %33
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %2)
%10 = add nsw i32 %9, 1
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %2, i32 0, i32 %11)
%13 = tail call i64 @malloc(i32 %10)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @fgetc(i32* %2)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %29, label %18
18: ; preds = %6, %18
%19 = phi i64 [ %22, %18 ], [ 0, %6 ]
%20 = phi i32 [ %24, %18 ], [ %15, %6 ]
%21 = trunc i32 %20 to i8
%22 = add nuw i64 %19, 1
%23 = getelementptr inbounds i8, i8* %14, i64 %19
store i8 %21, i8* %23
%24 = tail call i32 @fgetc(i32* %2)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %18
27: ; preds = %18
%28 = and i64 %22, 4294967295
br label %29
29: ; preds = %27, %6
%30 = phi i64 [ 0, %6 ], [ %28, %27 ]
%31 = tail call i32 @fclose(i32* %2)
%32 = getelementptr inbounds i8, i8* %14, i64 %30
store i8 0, i8* %32
br label %33
33: ; preds = %29, %4
%34 = phi i8* [ null, %4 ], [ %14, %29 ]
ret i8* %34
}