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

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stat(char const *, struct stat *);
int openmapfile(const char *fn, void **outptr, struct stat *outstat) {
  if (fn == NULL)
    return -1;
  stat(fn, outstat);
  int fd = open(fn, O_RDONLY);
  if (fd < 0) {
    perror(fn);
    return fd;
  }
  *outptr =
      mmap(NULL, outstat->st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  return fd;
}


x86 assembly (clang, -O0):
<openmapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <openmapfile+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <openmapfile+0xac>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <stat@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ab <openmapfile+0x6b>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ec <openmapfile+0xac>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    0x2e0f(%rip),%rax
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openmapfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11a4 <openmapfile+0x64>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,%rsi
call   1040 <stat@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11b6 <openmapfile+0x76>
mov    (%r15),%esi
mov    0x2e7a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
jmp    11a9 <openmapfile+0x69>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <perror@plt>
jmp    11a9 <openmapfile+0x69>

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

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