#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 */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int S_IRWXG;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int errno;
void *mmap(int /*<<< orphan*/, unsigned int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *, unsigned int, ...);
void *mmapfile(char *fn, unsigned int size) {
  int fd;
  void *pShm = NULL;
  if (fn == NULL) {
    return NULL;
  }
  fd = open(fn, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | O_SYNC);
  if (fd == -1) {
    printf("Open file %s error: %i.\n", fn, errno);
    return NULL;
  }
  pShm = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  close(fd);
  if (pShm != NULL) {
    printf("Memory Map OK. Map File: %s, length:%#X, addr:0x%08lX", fn, size,
           (unsigned long)pShm);
  } else {
    printf("Memory Map Failure. Map File %s, length:0x%08lX", fn, size);
  }
  return pShm;
}


x86 assembly (clang, -O0):
<mmapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116f <mmapfile+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    1259 <mmapfile+0x119>
mov    -0x10(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    0x2e23(%rip),%rax
or     (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11da <mmapfile+0x9a>
mov    -0x10(%rbp),%rsi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1259 <mmapfile+0x119>
mov    -0x14(%rbp),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    0x2db3(%rip),%rax
or     (%rax),%edx
mov    0x2dea(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x20(%rbp)
je     123c <mmapfile+0xfc>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1251 <mmapfile+0x111>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmapfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11e2 <mmapfile+0xa2>
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e6b(%rip),%rax
mov    0x2e94(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e61(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3f(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e6 <mmapfile+0xa6>
mov    %eax,%ebx
mov    0x2e5a(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1050 <close@plt>
test   %rbp,%rbp
je     11fd <mmapfile+0xbd>
lea    0xe49(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %rbp,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1213 <mmapfile+0xd3>
xor    %ebp,%ebp
jmp    1213 <mmapfile+0xd3>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0xe0a(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
jmp    120c <mmapfile+0xcc>
lea    0xe4b(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@O_SYNC = global i32 0
@.str = [25 x i8] c"Open file %s error: %i.\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [54 x i8] c"Memory Map OK. Map File: %s, length:%#X, addr:0x%08lX\00"
@.str.2 = [48 x i8] c"Memory Map Failure. Map File %s, length:0x%08lX\00"
define i8* @mmapfile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %7
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %52
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRWXU
%17 = load i32, i32* @S_IRWXG
%18 = or i32 %16, %17
%19 = load i32, i32* @O_SYNC
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %12, i32 %15, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %11
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
store i8* null, i8** %3
br label %52
28: ; preds = %11
%29 = load i32, i32* %5
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %6
%35 = call i8* @mmap(i32 0, i32 %29, i32 %32, i32 %33, i32 %34, i32 0)
store i8* %35, i8** %7
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %46
40: ; preds = %28
%41 = load i8*, i8** %4
%42 = load i32, i32* %5
%43 = load i8*, i8** %7
%44 = ptrtoint i8* %43 to i64
%45 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %41, i32 %42, i64 %44)
br label %50
46: ; preds = %28
%47 = load i8*, i8** %4
%48 = load i32, i32* %5
%49 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %47, i32 %48)
br label %50
50: ; preds = %46, %40
%51 = load i8*, i8** %7
store i8* %51, i8** %3
br label %52
52: ; preds = %50, %24, %10
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@O_SYNC = global i32 0
@.str = [25 x i8] c"Open file %s error: %i.\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [54 x i8] c"Memory Map OK. Map File: %s, length:%#X, addr:0x%08lX\00"
@.str.2 = [48 x i8] c"Memory Map Failure. Map File %s, length:0x%08lX\00"
define i8* @mmapfile(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %31, label %4
4: ; preds = %2
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRWXU
%9 = load i32, i32* @S_IRWXG
%10 = or i32 %9, %8
%11 = load i32, i32* @O_SYNC
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %0, i32 %7, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %16)
br label %31
18: ; preds = %4
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_SHARED
%23 = tail call i8* @mmap(i32 0, i32 %1, i32 %21, i32 %22, i32 %13, i32 0)
%24 = tail call i32 @close(i32 %13)
%25 = icmp eq i8* %23, null
br i1 %25, label %29, label %26
26: ; preds = %18
%27 = ptrtoint i8* %23 to i64
%28 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i64 %27)
br label %31
29: ; preds = %18
%30 = tail call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %31
31: ; preds = %26, %29, %2, %15
%32 = phi i8* [ null, %15 ], [ null, %2 ], [ null, %29 ], [ %23, %26 ]
ret i8* %32
}