#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);
char *malloc(int);
int open(char const *, int /*<<< orphan*/, int);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
char *load_from_file(const char *filename) {
  int f, size;
  static struct stat st;
  char *buf = 0;
  f = open(filename, O_RDONLY, 0777);
  if (f >= 0) {
    size = (stat((char *)filename, &st) == 0) ? st.st_size : 0;
    if (size) {
      buf = malloc(size + 1);
      if (buf) {
        size = read(f, buf, size);
        buf[size] = 0;
      }
    }
    close(f);
  }
  return buf;
}


x86 assembly (clang, -O0):
<load_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     120f <load_from_file+0xbf>
mov    -0x8(%rbp),%rdi
lea    0x2eb9(%rip),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11b0 <load_from_file+0x60>
mov    0x2ea4(%rip),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ba <load_from_file+0x6a>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ba <load_from_file+0x6a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1207 <load_from_file+0xb7>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1202 <load_from_file+0xb2>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1207 <load_from_file+0xb7>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_from_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11b8 <load_from_file+0x68>
mov    %eax,%ebp
lea    0x2ed5(%rip),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11be <load_from_file+0x6e>
mov    0x2ebf(%rip),%r14d
test   %r14d,%r14d
je     11be <load_from_file+0x6e>
lea    0x1(%r14),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11bc <load_from_file+0x6c>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
cltq
movb   $0x0,(%rbx,%rax,1)
jmp    11be <load_from_file+0x6e>
xor    %ebx,%ebx
jmp    11c5 <load_from_file+0x75>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@load_from_file.st = internal global %struct.stat zeroinitializer
@O_RDONLY = global i32 0
define i8* @load_from_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %5
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7, i32 511)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %41
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i64 @stat(i8* %12, %struct.stat* @load_from_file.st)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* getelementptr inbounds (%struct.stat, %struct.stat* @load_from_file.st, i32 0, i32 0)
br label %18
17: ; preds = %11
br label %18
18: ; preds = %17, %15
%19 = phi i32 [ %16, %15 ], [ 0, %17 ]
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %37
28: ; preds = %22
%29 = load i32, i32* %3
%30 = load i8*, i8** %5
%31 = load i32, i32* %4
%32 = call i32 @read(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %4
%33 = load i8*, i8** %5
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %28, %22
br label %38
38: ; preds = %37, %18
%39 = load i32, i32* %3
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %1
%42 = load i8*, i8** %5
ret i8* %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@load_from_file.st = internal global %struct.stat zeroinitializer
@O_RDONLY = global i32 0
define noalias i8* @load_from_file(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2, i32 511)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %5, label %22
5: ; preds = %1
%6 = tail call i64 @stat(i8* %0, %struct.stat* @load_from_file.st)
%7 = icmp ne i64 %6, 0
%8 = load i32, i32* getelementptr inbounds (%struct.stat, %struct.stat* @load_from_file.st, i64 0, i32 0)
%9 = icmp eq i32 %8, 0
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %19, label %11
11: ; preds = %5
%12 = add nsw i32 %8, 1
%13 = tail call i8* @malloc(i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = tail call i32 @read(i32 %3, i8* %13, i32 %8)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %11, %15, %5
%20 = phi i8* [ %13, %15 ], [ null, %11 ], [ null, %5 ]
%21 = tail call i32 @close(i32 %3)
br label %22
22: ; preds = %19, %1
%23 = phi i8* [ %20, %19 ], [ null, %1 ]
ret i8* %23
}