#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;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
char *mmap_file(const char *filename, int *size) {
  struct stat st;
  char *buf;
  int fd;
  fd = open(filename, O_RDONLY);
  if (fd == -1)
    goto err;
  if (fstat(fd, &st) == -1)
    goto close_err;
  buf = NULL;
  if (st.st_size) {
    buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (buf == MAP_FAILED)
      goto close_err;
  }
  close(fd);
  *size = st.st_size;
  return buf;
close_err:
  close(fd);
err:
  *size = -1;
  return NULL;
}


x86 assembly (clang, -O0):
<mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1174 <mmap_file+0x34>
jmp    120d <mmap_file+0xcd>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    118e <mmap_file+0x4e>
jmp    1205 <mmap_file+0xc5>
movq   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11e7 <mmap_file+0xa7>
mov    -0x20(%rbp),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    0x2e1d(%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    -0x28(%rbp),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    11e2 <mmap_file+0xa2>
jmp    1205 <mmap_file+0xc5>
jmp    11e7 <mmap_file+0xa7>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121f <mmap_file+0xdf>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b9 <mmap_file+0x79>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11a8 <mmap_file+0x68>
mov    (%rsp),%r15d
test   %r15d,%r15d
je     11ae <mmap_file+0x6e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e25(%rip),%rax
cmp    (%rax),%rbx
jne    11b0 <mmap_file+0x70>
mov    $0xffffffff,%r15d
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11c1 <mmap_file+0x81>
mov    $0xffffffff,%r15d
xor    %ebx,%ebx
mov    %r15d,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 i8* @mmap_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %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 eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
br label %46
15: ; preds = %2
%16 = load i32, i32* %8
%17 = call i32 @fstat(i32 %16, %struct.stat* %6)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
br label %43
20: ; preds = %15
store i8* null, i8** %7
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %20
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_PRIVATE
%29 = load i32, i32* %8
%30 = call i8* @mmap(i32* null, i32 %26, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %35
34: ; preds = %24
br label %43
35: ; preds = %24
br label %36
36: ; preds = %35, %20
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32*, i32** %5
store i32 %40, i32* %41
%42 = load i8*, i8** %7
store i8* %42, i8** %3
br label %48
43: ; preds = %34, %19
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %14
%47 = load i32*, i32** %5
store i32 -1, i32* %47
store i8* null, i8** %3
br label %48
48: ; preds = %46, %36
%49 = load i8*, i8** %3
ret i8* %49
}

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 i8* @mmap_file(i8* nocapture %0, i32* 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 eq i32 %6, -1
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = call i32 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %11
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_PRIVATE
%18 = tail call i8* @mmap(i32* null, i32 %13, i32 %16, i32 %17, i32 %6, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %22
21: ; preds = %15, %8
br label %22
22: ; preds = %11, %15, %21
%23 = phi i32 [ -1, %21 ], [ %13, %15 ], [ %13, %11 ]
%24 = phi i8* [ null, %21 ], [ %18, %15 ], [ null, %11 ]
%25 = tail call i32 @close(i32 %6)
br label %26
26: ; preds = %22, %2
%27 = phi i32 [ -1, %2 ], [ %23, %22 ]
%28 = phi i8* [ null, %2 ], [ %24, %22 ]
store i32 %27, i32* %1
ret i8* %28
}