#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*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int mmap_alloc(const char *path, void **ptr, size_t *size) {
  int fd = -1;
  struct stat st;
  int save_errno = 0;
  void *map_data = NULL;
  if (path == NULL || ptr == NULL || size == NULL) {
    save_errno = errno;
    goto end;
  }
  if ((fd = open(path, O_RDONLY)) == -1) {
    save_errno = errno;
    goto end;
  }
  if (fstat(fd, &st) == -1) {
    save_errno = errno;
    goto end;
  }
  map_data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (map_data == NULL) {
    save_errno = errno;
    goto end;
  }
  *ptr = map_data;
  *size = st.st_size;
end:
  if (fd >= 0) {
    close(fd);
    fd = -1;
  }
  if (save_errno != 0) {
    errno = save_errno;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mmap_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118b <mmap_alloc+0x4b>
cmpq   $0x0,-0x18(%rbp)
je     118b <mmap_alloc+0x4b>
cmpq   $0x0,-0x20(%rbp)
jne    119c <mmap_alloc+0x5c>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124d <mmap_alloc+0x10d>
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11cb <mmap_alloc+0x8b>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124d <mmap_alloc+0x10d>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11f1 <mmap_alloc+0xb1>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124d <mmap_alloc+0x10d>
mov    -0x30(%rbp),%rsi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1237 <mmap_alloc+0xf7>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124d <mmap_alloc+0x10d>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x24(%rbp)
jl     1266 <mmap_alloc+0x126>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1288 <mmap_alloc+0x148>
mov    -0x34(%rbp),%ecx
mov    0x2d4e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <mmap_alloc+0x14f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_alloc>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11c2 <mmap_alloc+0x82>
mov    %rsi,%r12
test   %rsi,%rsi
je     11c2 <mmap_alloc+0x82>
mov    %rdx,%r14
test   %rdx,%rdx
je     11c2 <mmap_alloc+0x82>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11c2 <mmap_alloc+0x82>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11cd <mmap_alloc+0x8d>
mov    0x8(%rsp),%r15
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
test   %rax,%rax
je     11cd <mmap_alloc+0x8d>
mov    %rax,(%r12)
mov    %r15,(%r14)
test   %ebp,%ebp
jns    11da <mmap_alloc+0x9a>
jmp    11e1 <mmap_alloc+0xa1>
mov    0x2dff(%rip),%rax
mov    (%rax),%ebx
jmp    11e1 <mmap_alloc+0xa1>
mov    0x2df4(%rip),%rax
mov    (%rax),%ebx
test   %ebp,%ebp
js     11e1 <mmap_alloc+0xa1>
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
je     11f5 <mmap_alloc+0xb5>
mov    0x2ddc(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%eax
jmp    11f7 <mmap_alloc+0xb7>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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