#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
char *loadfile(char *path) {
  char *contents = NULL;
  size_t contents_len = 0;
  char buffer[4096];
  size_t len;
  FILE *fin = fopen(path, "r");
  FILE *fout = open_memstream(&contents, &contents_len);
  if (fin == NULL || fout == NULL)
    exit(1);
  while ((len = fread(buffer, sizeof(char), 4096, fin)) > 0)
    fwrite(buffer, sizeof(char), len, fout);
  fclose(fin);
  fclose(fout);
  return (contents);
}


x86 assembly (clang, -O0):
<loadfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x1030(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <open_memstream@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1030(%rbp)
je     11c6 <loadfile+0x66>
cmpq   $0x0,-0x1038(%rbp)
jne    11d0 <loadfile+0x70>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11d5 <loadfile+0x75>
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jbe    1227 <loadfile+0xc7>
lea    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rdx
mov    -0x1038(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
jmp    11d5 <loadfile+0x75>
mov    -0x1030(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1038(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1030 <open_memstream@plt>
test   %r14,%r14
je     121e <loadfile+0xbe>
mov    %rax,%r15
test   %rax,%rax
je     121e <loadfile+0xbe>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11fd <loadfile+0x9d>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    11d0 <loadfile+0x70>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    (%rsp),%rax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @loadfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [4096 x i8]
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %2
store i8* null, i8** %3
store i64 0, i64* %4
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = call i32* @open_memstream(i8** %3, i64* %4)
store i32* %11, i32** %8
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32*, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14, %1
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %14
br label %20
20: ; preds = %25, %19
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %7
%23 = call i64 @fread(i8* %21, i32 1, i32 4096, i32* %22)
store i64 %23, i64* %6
%24 = icmp ugt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%27 = load i64, i64* %6
%28 = load i32*, i32** %8
%29 = call i32 @fwrite(i8* %26, i32 1, i64 %27, i32* %28)
br label %20
30: ; preds = %20
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @loadfile(i8* nocapture %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [4096 x i8]
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%6 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @open_memstream(i8** %2, i64* %3)
%10 = icmp eq i32* %8, null
%11 = icmp eq i32* %9, null
%12 = select i1 %10, i1 true, i1 %11
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = call i64 @fread(i8* %7, i32 1, i32 4096, i32* %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %18
16: ; preds = %1
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %13, %18
%19 = phi i64 [ %21, %18 ], [ %14, %13 ]
%20 = call i32 @fwrite(i8* %7, i32 1, i64 %19, i32* %9)
%21 = call i64 @fread(i8* %7, i32 1, i32 4096, i32* %8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %18
23: ; preds = %18, %13
%24 = call i32 @fclose(i32* %8)
%25 = call i32 @fclose(i32* %9)
%26 = load i8*, i8** %2
ret i8* %26
}