#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;
};
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
ssize_t readfile(char **dest, char *fp) {
  int fd;
  off_t len;
  struct stat st;
  if ((fd = open(fp, O_RDONLY)) == -1)
    return -1;
  if (fstat(fd, &st))
    return -1;
  if ((len = st.st_size) <= 0)
    return 0;
  *dest = mmap(NULL, (size_t)len, PROT_READ, MAP_PRIVATE, fd, 0);
  if (*dest == MAP_FAILED)
    return -1;
  close(fd);
  return len;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    117a <readfile+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <readfile+0xd8>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     119c <readfile+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <readfile+0xd8>
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     11b7 <readfile+0x77>
movl   $0x0,-0x4(%rbp)
jmp    1218 <readfile+0xd8>
movslq -0x20(%rbp),%rsi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <readfile+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <readfile+0xd8>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1179 <readfile+0x39>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
je     1186 <readfile+0x46>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%r15d
test   %r15d,%r15d
jle    11cc <readfile+0x8c>
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e31(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     1179 <readfile+0x39>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15d,%ebx
jmp    1179 <readfile+0x39>
xor    %ebx,%ebx
jmp    1179 <readfile+0x39>

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

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