#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_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
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 munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void readExampleChange() {
  createFile("hello world");
  int fd = open("mmap.temp", O_RDONLY);
  size_t mmapLen = 15;
  off_t offset = 0;
  char *ptr = (char *)mmap(NULL, mmapLen, PROT_READ | PROT_WRITE, MAP_PRIVATE,
                           fd, offset);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }
  printf("Should say hello world: %s\n", ptr);
  ptr[0] = 'J';
  printf("Should say Jello world: %s\n", ptr);
  createFile("hello WORLD");
  printf("Undefined. Might say hello WORLD: %s\n", ptr);
  if (munmap(ptr, mmapLen) == -1) {
    perror("munmap");
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<readExampleChange>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
call   1060 <createFile@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
movq   $0xf,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x14(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <readExampleChange+0x9d>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x4a,(%rax)
mov    -0x20(%rbp),%rsi
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
lea    0xe08(%rip),%rdi
call   1060 <createFile@plt>
mov    -0x20(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1290 <readExampleChange+0x110>
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readExampleChange>:
push   %rbp
push   %rbx
push   %rax
lea    0xe76(%rip),%rdi
call   1060 <createFile@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x2e4b(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
mov    $0xf,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2ddb(%rip),%rax
cmp    %rbx,(%rax)
je     123d <readExampleChange+0xbd>
lea    0xe32(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
movb   $0x4a,(%rbx)
lea    0xe3c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe49(%rip),%rdi
call   1060 <createFile@plt>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xf,%esi
mov    %rbx,%rdi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     1246 <readExampleChange+0xc6>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <close@plt>
lea    0xdd2(%rip),%rdi
jmp    124d <readExampleChange+0xcd>
lea    0xe38(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [10 x i8] c"mmap.temp\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [28 x i8] c"Should say hello world: %s\0A\00"
@.str.4 = [28 x i8] c"Should say Jello world: %s\0A\00"
@.str.5 = [12 x i8] c"hello WORLD\00"
@.str.6 = [38 x i8] c"Undefined. Might say hello WORLD: %s\0A\00"
@.str.7 = [7 x i8] c"munmap\00"
define void @readExampleChange() {
%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_RDONLY
%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_PRIVATE
%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 @printf(i8* getelementptr inbounds ([28 x i8], [28 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 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%32 = load i8*, i8** %4
%33 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %4
%35 = load i64, i64* %2
%36 = call i32 @munmap(i8* %34, i64 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %24
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %24
%43 = load i32, i32* %1
%44 = call i32 @close(i32 %43)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [10 x i8] c"mmap.temp\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [28 x i8] c"Should say hello world: %s\0A\00"
@.str.4 = [28 x i8] c"Should say Jello world: %s\0A\00"
@.str.5 = [12 x i8] c"hello WORLD\00"
@.str.6 = [38 x i8] c"Undefined. Might say hello WORLD: %s\0A\00"
@.str.7 = [7 x i8] c"munmap\00"
define void @readExampleChange() {
%1 = tail call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @O_RDONLY
%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_PRIVATE
%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 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %9)
store i8 74, i8* %9
%18 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%19 = tail call i32 @createFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%20 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i8* %9)
%21 = tail call i32 @munmap(i8* %9, i64 15)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %16
%28 = tail call i32 @close(i32 %3)
ret void
}