#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
char *load_file(char *fname) {
  int fd;
  char *data;
  struct stat st;
  fd = open(fname, O_RDONLY);
  fstat(fd, &st);
  data = malloc(st.st_size + 1);
  read(fd, data, st.st_size);
  data[st.st_size] = '\0';
  close(fd);
  return data;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
movslq 0x8(%rsp),%rbx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %r14d,%edi
call   1030 <close@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @fstat(i32 %9, %struct.stat* %5)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %4
%15 = load i32, i32* %3
%16 = load i8*, i8** %4
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @read(i32 %15, i8* %16, i32 %18)
%20 = load i8*, i8** %4
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%22 = load i32, i32* %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 0, i8* %24
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define noalias i8* @load_file(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = call i32 @fstat(i32 %5, %struct.stat* %2)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = add nsw i32 %8, 1
%10 = tail call i8* @malloc(i32 %9)
%11 = tail call i32 @read(i32 %5, i8* %10, i32 %8)
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 0, i8* %13
%14 = tail call i32 @close(i32 %5)
ret i8* %10
}