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


x86 assembly (clang, -O0):
<mmapfile_r>:
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_r+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    1235 <mmapfile_r+0xf5>
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11bf <mmapfile_r+0x7f>
mov    -0x10(%rbp),%rsi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1235 <mmapfile_r+0xf5>
mov    -0x14(%rbp),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    0x2e16(%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     1218 <mmapfile_r+0xd8>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122d <mmapfile_r+0xed>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe2a(%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_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11ba <mmapfile_r+0x7a>
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e77(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11be <mmapfile_r+0x7e>
mov    %eax,%ebx
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
mov    0x2e68(%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     11d5 <mmapfile_r+0x95>
lea    0xe71(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %rbp,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11eb <mmapfile_r+0xab>
xor    %ebp,%ebp
jmp    11eb <mmapfile_r+0xab>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
lea    0xe32(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
jmp    11e4 <mmapfile_r+0xa4>
lea    0xe74(%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_RDONLY = global i32 0
@O_SYNC = global i32 0
@.str = [25 x i8] c"Open file %s error: %d.\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [55 x i8] c"Memory Map OK. Map File: %s, length:%#X, addr:0x%08lX\0A\00"
@.str.2 = [49 x i8] c"Memory Map Failure. Map File %s, length:0x%08lX\0A\00"
define i8* @mmapfile_r(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 %45
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* @O_SYNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = load i32, i32* @errno
%22 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
store i8* null, i8** %3
br label %45
23: ; preds = %11
%24 = load i32, i32* %5
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %6
%28 = call i8* @mmap(i32 0, i32 %24, i32 %25, i32 %26, i32 %27, i32 0)
store i8* %28, i8** %7
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %39
33: ; preds = %23
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = load i8*, i8** %7
%37 = ptrtoint i8* %36 to i64
%38 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %34, i32 %35, i64 %37)
br label %43
39: ; preds = %23
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = call i32 (i8*, i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %41)
br label %43
43: ; preds = %39, %33
%44 = load i8*, i8** %7
store i8* %44, i8** %3
br label %45
45: ; preds = %43, %19, %10
%46 = load i8*, i8** %3
ret i8* %46
}

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