#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 */

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           unsigned int, unsigned int);
void *mmap(int /*<<< orphan*/, unsigned int, int, int /*<<< orphan*/, int,
           unsigned int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void *map_memory(const char *fn, unsigned base, unsigned size) {
  int fd;
  void *ptr;
  fd = open(fn, O_RDWR | O_SYNC);
  if (fd < 0) {
    perror("cannot open %s for mapping");
    return MAP_FAILED;
  }
  ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
  close(fd);
  if (ptr == MAP_FAILED) {
    fprintf(stderr, "cannot map %s (@%08x,%08x)\n", fn, base, size);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<map_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a9 <map_memory+0x59>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    121e <map_memory+0xce>
mov    -0x18(%rbp),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    0x2dfc(%rip),%rax
or     (%rax),%edx
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x14(%rbp),%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <map_memory+0xc6>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_memory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%r12
mov    0x2e70(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11e9 <map_memory+0x99>
mov    %eax,%ebx
mov    0x2e73(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
mov    %r15d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2df6(%rip),%rax
cmp    (%rax),%rbp
jne    11dd <map_memory+0x8d>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
mov    %r14d,%r8d
call   1050 <fprintf@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%rbp
jmp    11dd <map_memory+0x8d>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str = [27 x i8] c"cannot open %s for mapping\00"
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"cannot map %s (@%08x,%08x)\0A\00"
define i8* @map_memory(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** @MAP_FAILED
store i8* %19, i8** %4
br label %42
20: ; preds = %3
%21 = load i32, i32* %7
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* %8
%27 = load i32, i32* %6
%28 = call i8* @mmap(i32 0, i32 %21, i32 %24, i32 %25, i32 %26, i32 %27)
store i8* %28, i8** %9
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %9
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %40
34: ; preds = %20
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %5
%37 = load i32, i32* %6
%38 = load i32, i32* %7
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %36, i32 %37, i32 %38)
br label %40
40: ; preds = %34, %20
%41 = load i8*, i8** %9
store i8* %41, i8** %4
br label %42
42: ; preds = %40, %17
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str = [27 x i8] c"cannot open %s for mapping\00"
@MAP_FAILED = global i8* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"cannot map %s (@%08x,%08x)\0A\00"
define i8* @map_memory(i8* %0, i32 %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** @MAP_FAILED
br label %24
12: ; preds = %3
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap(i32 0, i32 %2, i32 %15, i32 %16, i32 %7, i32 %1)
%18 = tail call i32 @close(i32 %7)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %17, %19
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i32 %2)
br label %24
24: ; preds = %12, %21, %9
%25 = phi i8* [ %11, %9 ], [ %17, %21 ], [ %17, %12 ]
ret i8* %25
}