#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ error();
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int stat(char const *, struct stat *);
char *file2bin(const char *filename, size_t *size) {
  int fd;
  struct stat st;
  char *buf;
  if (stat(filename, &st) == -1)
    error();
  if ((fd = open(filename, O_RDONLY)) == -1)
    error();
  if ((buf = malloc(st.st_size)) == NULL)
    error();
  assert(read(fd, buf, st.st_size) == st.st_size);
  *size = st.st_size;
  return buf;
}


x86 assembly (clang, -O0):
<file2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    118d <file2bin+0x2d>
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b2 <file2bin+0x52>
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d0 <file2bin+0x70>
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
cmp    -0x20(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file2bin>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    1181 <file2bin+0x21>
xor    %eax,%eax
call   1070 <error@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11a1 <file2bin+0x41>
xor    %eax,%eax
call   1070 <error@plt>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11bc <file2bin+0x5c>
xor    %eax,%eax
call   1070 <error@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
xor    %edi,%edi
cmp    %rbx,%rax
sete   %dil
call   1040 <assert@plt>
mov    %rbx,(%r14)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
define noalias i8* @file2bin(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (...) @error()
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 (...) @error()
br label %15
15: ; preds = %13, %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i64, i64* %16
%18 = tail call i8* @malloc(i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 (...) @error()
br label %22
22: ; preds = %20, %15
%23 = tail call i64 @read(i32 %11, i8* %18, i64 %17)
%24 = icmp eq i64 %23, %17
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
store i64 %17, i64* %1
ret i8* %18
}