#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 int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ createFile(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ msync(char *, size_t, int /*<<< orphan*/);
int munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printFile();
int /*<<< orphan*/ printf(char *, ...);
void writeExample() {
  createFile("hello world");
  int fd = open("mmap.temp", O_RDWR);
  size_t mmapLen = 15;
  off_t offset = 0;
  char *ptr = (char *)mmap(NULL, mmapLen, PROT_READ | PROT_WRITE, MAP_SHARED,
                           fd, offset);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }
  printf("Before we change the file. Should say hello world: %s\n", ptr);
  ptr[0] = 'J';
  printf("After we change the mmap'd memory. Should say hello world: %s\n",
         ptr);
  printf("File after we change mmap'd memory is undefined before we call "
         "msync():\n");
  printFile();
  msync(ptr, mmapLen, MS_SYNC);
  printf("File after we change mmap'd memory contains all changes after we "
         "call msync():\n");
  printFile();
  if (munmap(ptr, mmapLen) == -1) {
    perror("munmap");
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<writeExample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe51(%rip),%rdi
call   1080 <createFile@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x4(%rbp)
movq   $0xf,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2dc9(%rip),%rax
or     (%rax),%edx
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x14(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <writeExample+0x9d>
lea    0xdec(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x4a,(%rax)
mov    -0x20(%rbp),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <printFile@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
call   1030 <msync@plt>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <printFile@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12d8 <writeExample+0x138>
lea    0xe65(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeExample>:
push   %rbp
push   %rbx
push   %rax
lea    0xe46(%rip),%rdi
call   1090 <createFile@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
mov    0x2e1b(%rip),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    $0xf,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2da3(%rip),%rax
cmp    %rbx,(%rax)
je     1296 <writeExample+0xe6>
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
movb   $0x4a,(%rbx)
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <printFile@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    $0xf,%esi
mov    %rbx,%rdi
call   1040 <msync@plt>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <printFile@plt>
mov    $0xf,%esi
mov    %rbx,%rdi
call   10a0 <munmap@plt>
cmp    $0xffffffff,%eax
je     129f <writeExample+0xef>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <close@plt>
lea    0xd79(%rip),%rdi
jmp    12a6 <writeExample+0xf6>
lea    0xdeb(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [10 x i8] c"mmap.temp\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [55 x i8] c"Before we change the file. Should say hello world: %s\0A\00"
@.str.4 = [63 x i8] c"After we change the mmap'd memory. Should say hello world: %s\0A\00"
@.str.5 = [73 x i8] c"File after we change mmap'd memory is undefined before we call msync():\0A\00"
@MS_SYNC = global i32 0
@.str.6 = [80 x i8] c"File after we change mmap'd memory contains all changes after we call msync():\0A\00"
@.str.7 = [7 x i8] c"munmap\00"
define void @writeExample() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %1
store i64 15, i64* %2
store i32 0, i32* %3
%8 = load i64, i64* %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %9, %10
%12 = load i32, i32* @MAP_SHARED
%13 = load i32, i32* %1
%14 = load i32, i32* %3
%15 = call i64 @mmap(i32* null, i64 %8, i32 %11, i32 %12, i32 %13, i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %24
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %0
%25 = load i8*, i8** %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 74, i8* %28
%29 = load i8*, i8** %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.5, i64 0, i64 0))
%32 = call i32 (...) @printFile()
%33 = load i8*, i8** %4
%34 = load i64, i64* %2
%35 = load i32, i32* @MS_SYNC
%36 = call i32 @msync(i8* %33, i64 %34, i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 (...) @printFile()
%39 = load i8*, i8** %4
%40 = load i64, i64* %2
%41 = call i32 @munmap(i8* %39, i64 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %24
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %24
%48 = load i32, i32* %1
%49 = call i32 @close(i32 %48)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [10 x i8] c"mmap.temp\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [55 x i8] c"Before we change the file. Should say hello world: %s\0A\00"
@.str.4 = [63 x i8] c"After we change the mmap'd memory. Should say hello world: %s\0A\00"
@MS_SYNC = global i32 0
@.str.7 = [7 x i8] c"munmap\00"
@str = [72 x i8] c"File after we change mmap'd memory is undefined before we call msync():\00"
@str.8 = [79 x i8] c"File after we change mmap'd memory contains all changes after we call msync():\00"
define void @writeExample() {
%1 = tail call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_SHARED
%8 = tail call i64 @mmap(i32* null, i64 15, i32 %6, i32 %7, i32 %3, i32 0)
%9 = inttoptr i64 %8 to i8*
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %10, %9
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i8* %9)
store i8 74, i8* %9
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 (...) @printFile()
%21 = load i32, i32* @MS_SYNC
%22 = tail call i32 @msync(i8* %9, i64 15, i32 %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @str.8, i64 0, i64 0))
%24 = tail call i32 (...) @printFile()
%25 = tail call i32 @munmap(i8* %9, i64 15)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %16
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %16
%32 = tail call i32 @close(i32 %3)
ret void
}