#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);
scalar_t__ 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 = (char *)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 %42
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 %39
22: ; preds = %18
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %38
29: ; preds = %22
%30 = load i32, i32* %3
%31 = load i8*, i8** %5
%32 = load i32, i32* %4
%33 = call i32 @read(i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %4
%34 = load i8*, i8** %5
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %29, %22
br label %39
39: ; preds = %38, %18
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %1
%43 = load i8*, i8** %5
ret i8* %43
}

LLVM IR (-O2):
%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 = 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 %23
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 %20, label %11
11: ; preds = %5
%12 = add nsw i32 %8, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = tail call i32 @read(i32 %3, i8* %14, i32 %8)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
store i8 0, i8* %19
br label %20
20: ; preds = %11, %16, %5
%21 = phi i8* [ %14, %16 ], [ null, %11 ], [ null, %5 ]
%22 = tail call i32 @close(i32 %3)
br label %23
23: ; preds = %20, %1
%24 = phi i8* [ %21, %20 ], [ null, %1 ]
ret i8* %24
}