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

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
char *malloc(scalar_t__);
int /*<<< orphan*/ memcpy(char *, char *, scalar_t__);
char *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, scalar_t__);
int open(char const *, int /*<<< orphan*/);
int read_file(const char *path, char **buffer) {
  struct stat buf;
  char *map;
  int fd = open(path, O_RDONLY);
  if (fd < 0 || fstat(fd, &buf) || buf.st_size <= 0)
    return -1;
  if ((map = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
      MAP_FAILED)
    return -1;
  if ((*buffer = malloc(buf.st_size)) == NULL)
    return -1;
  memcpy(*buffer, map, buf.st_size);
  munmap(map, buf.st_size);
  close(fd);
  return buf.st_size;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     11c0 <read_file+0x50>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jne    11c0 <read_file+0x50>
cmpq   $0x0,-0x20(%rbp)
jg     11cc <read_file+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <read_file+0xf8>
mov    -0x20(%rbp),%rsi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%rax
jne    1212 <read_file+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <read_file+0xf8>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1238 <read_file+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <read_file+0xf8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     121b <read_file+0xab>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    121b <read_file+0xab>
mov    0x8(%rsp),%r15
test   %r15,%r15
jle    121b <read_file+0xab>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e21(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r12
mov    0x2de5(%rip),%rax
cmp    (%rax),%r12
je     121b <read_file+0xab>
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     121b <read_file+0xab>
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <munmap@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15d,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @read_file(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %2
%15 = load i32, i32* %8
%16 = call i64 @fstat(i32 %15, %struct.stat* %6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%20 = load i64, i64* %19
%21 = icmp sle i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18, %14, %2
store i32 -1, i32* %3
br label %56
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %8
%29 = call i8* @mmap(i32* null, i64 %25, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %7
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %33
32: ; preds = %23
store i32 -1, i32* %3
br label %56
33: ; preds = %23
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i64, i64* %34
%36 = call i8* @malloc(i64 %35)
%37 = load i8**, i8*** %5
store i8* %36, i8** %37
%38 = icmp eq i8* %36, null
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %3
br label %56
40: ; preds = %33
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = load i8*, i8** %7
%44 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%45 = load i64, i64* %44
%46 = call i32 @memcpy(i8* %42, i8* %43, i64 %45)
%47 = load i8*, i8** %7
%48 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%49 = load i64, i64* %48
%50 = call i32 @munmap(i8* %47, i64 %49)
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%54 = load i64, i64* %53
%55 = trunc i64 %54 to i32
store i32 %55, i32* %3
br label %56
56: ; preds = %40, %39, %32, %22
%57 = load i32, i32* %3
ret i32 %57
}

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