#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 */
typedef char *caddr_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
long getpagesize();
char *mmap(char *, long, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char, ...);
scalar_t__ strlen(char *);
int main() {
  int fd;
  long page_size;
  char *mem;
  page_size = getpagesize();
  fd = open("mem_mapped_file.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  mem = mmap((caddr_t)0, page_size, PROT_READ, MAP_SHARED, fd, 0);
  if (mem == (caddr_t)-1) {
    perror("mmap");
    exit(1);
  }
  printf("Text read from file (%d characters):\n%s\n", (int)strlen(mem), mem);
  printf("Random access. Character at pos 3: %c\n", mem[3]);
  munmap(mem, page_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e2 <main+0x52>
lea    0xe41(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    1233 <main+0xa3>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x18(%rbp),%rdx
lea    0xdd7(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
lea    0xdeb(%rip),%rdi
movsbl 0x3(%rax),%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10a0 <getpagesize@plt>
mov    %rax,%r14
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122c <main+0x9c>
mov    %eax,%ebp
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1235 <main+0xa5>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
movsbl %al,%esi
lea    0xe27(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x3(%rbx),%esi
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <munmap@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde1(%rip),%rdi
jmp    123c <main+0xac>
lea    0xddd(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"mem_mapped_file.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [41 x i8] c"Text read from file (%d characters):\0A%s\0A\00"
@.str.4 = [39 x i8] c"Random access. Character at pos 3: %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i64 (...) @getpagesize()
store i64 %5, i64* %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i64, i64* %3
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %2
%18 = call i8* @mmap(i8* null, i64 %14, i32 %15, i32 %16, i32 %17, i32 0)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = icmp eq i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = call i64 @strlen(i8* %25)
%27 = trunc i64 %26 to i32
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %4
%30 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8 signext %28, i8* %29)
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 3
%33 = load i8, i8* %32
%34 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8 signext %33)
%35 = load i8*, i8** %4
%36 = load i64, i64* %3
%37 = call i32 @munmap(i8* %35, i64 %36)
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"mem_mapped_file.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [41 x i8] c"Text read from file (%d characters):\0A%s\0A\00"
@.str.4 = [39 x i8] c"Random access. Character at pos 3: %c\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @getpagesize()
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i8* @mmap(i8* null, i64 %1, i32 %9, i32 %10, i32 %3, i32 0)
%12 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%18 = trunc i64 %17 to i8
%19 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8 signext %18, i8* %11)
%20 = getelementptr inbounds i8, i8* %11, i64 3
%21 = load i8, i8* %20
%22 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8 signext %21)
%23 = tail call i32 @munmap(i8* %11, i64 %1)
%24 = tail call i32 @close(i32 %3)
ret i32 0
}