#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;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(unsigned char *, void *, size_t);
void *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char const *, struct stat *);
int copy_file_to_mem(const char *path, unsigned char **p_buf, size_t *p_sz) {
  struct stat fst;
  stat(path, &fst);
  if (S_ISREG(fst.st_mode) && fst.st_size > 0) {
    int fd = open(path, O_RDONLY, 0);
    if (fd < 0)
      return (-1);
    size_t fsz = fst.st_size;
    *p_sz = fsz;
    *p_buf = (unsigned char *)malloc(fsz);
    void *p = mmap(0, fsz, PROT_READ, MAP_PRIVATE, fd, 0);
    memcpy(*p_buf, p, fsz);
    munmap(p, fsz);
    close(fd);
    return (0);
  } else
    return (-1);
}


x86 assembly (clang, -O0):
<copy_file_to_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x28(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     126f <copy_file_to_mem+0xef>
cmpq   $0x0,-0x30(%rbp)
jbe    126f <copy_file_to_mem+0xef>
mov    -0x10(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11eb <copy_file_to_mem+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <copy_file_to_mem+0xf6>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rsi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    0x2dab(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <copy_file_to_mem+0xf6>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_to_mem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    0x10(%rsp),%edi
call   1070 <S_ISREG@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     122e <copy_file_to_mem+0xae>
mov    0x8(%rsp),%r13
test   %r13,%r13
je     122e <copy_file_to_mem+0xae>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     122e <copy_file_to_mem+0xae>
mov    %eax,%r12d
mov    %r13,(%r15)
mov    %r13,%rdi
call   1080 <malloc@plt>
mov    %rax,(%r14)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    0x2dda(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r13,%rsi
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1090 <munmap@plt>
mov    %r12d,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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