#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 /*<<< orphan*/ MS_ASYNC;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ memcpy(char *, char *, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ msync(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ printfDivLine(char *);
int /*<<< orphan*/ printfMapChar(char *, char *);
int /*<<< orphan*/ printfMmapAddr(char *, char *);
int main() {
  int fd;
  char *mapChar;
  char *checkChar;
  char *ptr;
  printf("mypid is %d\n", getpid());
  fd = open("/tmp/test.txt", O_RDWR);
  mapChar = mmap(NULL, 1000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  printfDivLine("Print map memory zone mapChar");
  printfMapChar("mapChar", mapChar);
  ptr = mapChar;
  memcpy(ptr, "abcdefg", 8);
  printfDivLine("Print map memory zone mapChar after modification");
  printfMapChar("mapChar", mapChar);
  msync(mapChar, 8, MS_ASYNC);
  close(fd);
  printfMmapAddr("mapChar", mapChar);
  munmap(mapChar, 1000);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe31(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    0x2dc4(%rip),%rax
or     (%rax),%edx
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3e8,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
lea    0xdf4(%rip),%rdi
call   10b0 <printfDivLine@plt>
mov    -0x10(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1080 <printfMapChar@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdea(%rip),%rsi
mov    $0x8,%edx
call   10a0 <memcpy@plt>
lea    0xde1(%rip),%rdi
call   10b0 <printfDivLine@plt>
mov    -0x10(%rbp),%rsi
lea    0xdc1(%rip),%rdi
call   1080 <printfMapChar@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
mov    $0x8,%esi
call   1040 <msync@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1070 <printfMmapAddr@plt>
mov    -0x10(%rbp),%rdi
mov    $0x3e8,%esi
call   10c0 <munmap@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   1030 <getpid@plt>
lea    0xe3e(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%ebp
mov    0x2e11(%rip),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x3e8,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
lea    0xe04(%rip),%rdi
call   10b0 <printfDivLine@plt>
lea    0xe0e(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <printfMapChar@plt>
lea    0xe04(%rip),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   10a0 <memcpy@plt>
lea    0xdf8(%rip),%rdi
call   10b0 <printfDivLine@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <printfMapChar@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <msync@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <printfMmapAddr@plt>
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   10c0 <munmap@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"mypid is %d\0A\00"
@.str.1 = [14 x i8] c"/tmp/test.txt\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [30 x i8] c"Print map memory zone mapChar\00"
@.str.3 = [8 x i8] c"mapChar\00"
@.str.4 = [8 x i8] c"abcdefg\00"
@.str.5 = [49 x i8] c"Print map memory zone mapChar after modification\00"
@MS_ASYNC = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i32 (...) @getpid()
%7 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_SHARED
%14 = load i32, i32* %2
%15 = call i8* @mmap(i32* null, i32 1000, i32 %12, i32 %13, i32 %14, i32 0)
store i8* %15, i8** %3
%16 = call i32 @printfDivLine(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8*, i8** %3
%18 = call i32 @printfMapChar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %3
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = call i32 @memcpy(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 8)
%22 = call i32 @printfDivLine(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
%23 = load i8*, i8** %3
%24 = call i32 @printfMapChar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %3
%26 = load i32, i32* @MS_ASYNC
%27 = call i32 @msync(i8* %25, i32 8, i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %3
%31 = call i32 @printfMmapAddr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %3
%33 = call i32 @munmap(i8* %32, i32 1000)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"mypid is %d\0A\00"
@.str.1 = [14 x i8] c"/tmp/test.txt\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [30 x i8] c"Print map memory zone mapChar\00"
@.str.3 = [8 x i8] c"mapChar\00"
@.str.4 = [8 x i8] c"abcdefg\00"
@.str.5 = [49 x i8] c"Print map memory zone mapChar after modification\00"
@MS_ASYNC = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_SHARED
%9 = tail call i8* @mmap(i32* null, i32 1000, i32 %7, i32 %8, i32 %4, i32 0)
%10 = tail call i32 @printfDivLine(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @printfMapChar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%12 = tail call i32 @memcpy(i8* %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 8)
%13 = tail call i32 @printfDivLine(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 @printfMapChar(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%15 = load i32, i32* @MS_ASYNC
%16 = tail call i32 @msync(i8* %9, i32 8, i32 %15)
%17 = tail call i32 @close(i32 %4)
%18 = tail call i32 @printfMmapAddr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%19 = tail call i32 @munmap(i8* %9, i32 1000)
ret i32 0
}