#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 pid_t;

/* Variables and functions */
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int fork();
int getpid();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ print_table(char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ syserr(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main() {
  char buff[12] = "Ala ma kota";
  char *mapped_mem;
  int fd_memory = -1;
  int flags, prot;
  pid_t pid;
  printf("Wielkość strony to %lu\n", sysconf(_SC_PAGE_SIZE));
  prot = PROT_READ | PROT_WRITE;
  flags = MAP_SHARED | MAP_ANONYMOUS;
  mapped_mem = (char *)mmap(NULL, 12 + sysconf(_SC_PAGE_SIZE), prot, flags,
                            fd_memory, 0);
  if (mapped_mem == MAP_FAILED)
    syserr("mmap");
  print_table(mapped_mem, 12);
  sleep(2);
  switch (pid = fork()) {
  case -1:
    syserr("fork");
  case 0:
    sleep(2);
    print_table(mapped_mem, 12);
    sleep(2 * 2);
    print_table(mapped_mem, 12);
    munmap(mapped_mem, 12);
    return 0;
  default:
    break;
  }
  printf("Pid rodzica %d, pid dziecka: %d\n", getpid(), pid);
  print_table(mapped_mem, 12);
  sleep(2 * 2);
  strncpy(mapped_mem, buff, 12);
  strncpy(mapped_mem + sysconf(_SC_PAGE_SIZE), buff, 12);
  printf("Proces %d, Zmieniłem zawartosć pamięci\n", getpid());
  print_table(mapped_mem, 12);
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe3a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe38(%rip),%eax
mov    %eax,-0x8(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    0x2dbb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%esi
add    $0xc,%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
jne    126f <main+0xbf>
lea    0xdbc(%rip),%rdi
call   1080 <syserr@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     12b4 <main+0x104>
jmp    12a4 <main+0xf4>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     12c0 <main+0x110>
jmp    130a <main+0x15a>
lea    0xd70(%rip),%rdi
call   1080 <syserr@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x4,%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1090 <munmap@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b3 <main+0x203>
jmp    130f <main+0x15f>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%edx
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x4,%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x6b20616d20616c41,%rax
61 20 6b
mov    %rax,(%rsp)
movl   $0x61746f,0x8(%rsp)
mov    0x2deb(%rip),%r14
mov    (%r14),%edi
call   10a0 <sysconf@plt>
lea    0xe28(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e04(%rip),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2de2(%rip),%rax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    (%r14),%edi
call   10a0 <sysconf@plt>
lea    0xc(%rax),%esi
xor    %edi,%edi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2d7e(%rip),%rax
cmp    %rbx,(%rax)
jne    124b <main+0x9b>
lea    0xde0(%rip),%rdi
call   1080 <syserr@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1280 <main+0xd0>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12c0 <main+0x110>
lea    0xdb0(%rip),%rdi
call   1080 <syserr@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x4,%edi
call   10b0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1090 <munmap@plt>
jmp    134b <main+0x19b>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd62(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x4,%edi
call   10b0 <sleep@plt>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
mov    (%r14),%edi
call   10a0 <sysconf@plt>
cltq
mov    %rbx,%rdi
add    %rax,%rdi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd23(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buff = [12 x i8] c"Ala ma kota\00"
@.str = [26 x i8] c"Wielko\C5\9B\C4\87 strony to %lu\0A\00"
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [33 x i8] c"Pid rodzica %d, pid dziecka: %d\0A\00"
@.str.4 = [43 x i8] c"Proces %d, Zmieni\C5\82em zawartos\C4\87 pami\C4\99ci\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [12 x i8]* %2 to i8*
store i32 -1, i32* %4
%9 = load i32, i32* @_SC_PAGE_SIZE
%10 = call i32 @sysconf(i32 %9)
%11 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
store i32 %14, i32* %6
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = or i32 %15, %16
store i32 %17, i32* %5
%18 = load i32, i32* @_SC_PAGE_SIZE
%19 = call i32 @sysconf(i32 %18)
%20 = add nsw i32 12, %19
%21 = load i32, i32* %6
%22 = load i32, i32* %5
%23 = load i32, i32* %4
%24 = call i64 @mmap(i32* null, i32 %20, i32 %21, i32 %22, i32 %23, i32 0)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %31
29: ; preds = %0
%30 = call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %0
%32 = load i8*, i8** %3
%33 = call i32 @print_table(i8* %32, i32 12)
%34 = call i32 @sleep(i32 2)
%35 = call i32 (...) @fork()
store i32 %35, i32* %7
switch i32 %35, label %47 [
i32 -1, label %36
i32 0, label %38
]
36: ; preds = %31
%37 = call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %31, %36
%39 = call i32 @sleep(i32 2)
%40 = load i8*, i8** %3
%41 = call i32 @print_table(i8* %40, i32 12)
%42 = call i32 @sleep(i32 4)
%43 = load i8*, i8** %3
%44 = call i32 @print_table(i8* %43, i32 12)
%45 = load i8*, i8** %3
%46 = call i32 @munmap(i8* %45, i32 12)
store i32 0, i32* %1
br label %70
47: ; preds = %31
br label %48
48: ; preds = %47
%49 = call i32 (...) @getpid()
%50 = load i32, i32* %7
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 %50)
%52 = load i8*, i8** %3
%53 = call i32 @print_table(i8* %52, i32 12)
%54 = call i32 @sleep(i32 4)
%55 = load i8*, i8** %3
%56 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%57 = call i32 @strncpy(i8* %55, i8* %56, i32 12)
%58 = load i8*, i8** %3
%59 = load i32, i32* @_SC_PAGE_SIZE
%60 = call i32 @sysconf(i32 %59)
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
%63 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%64 = call i32 @strncpy(i8* %62, i8* %63, i32 12)
%65 = call i32 (...) @getpid()
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %65)
%67 = load i8*, i8** %3
%68 = call i32 @print_table(i8* %67, i32 12)
%69 = call i32 @wait(i32 0)
store i32 0, i32* %1
br label %70
70: ; preds = %48, %38
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
@__const.main.buff = [12 x i8] c"Ala ma kota\00"
@.str = [26 x i8] c"Wielko\C5\9B\C4\87 strony to %lu\0A\00"
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [33 x i8] c"Pid rodzica %d, pid dziecka: %d\0A\00"
@.str.4 = [43 x i8] c"Proces %d, Zmieni\C5\82em zawartos\C4\87 pami\C4\99ci\0A\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @_SC_PAGE_SIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_SHARED
%10 = load i32, i32* @MAP_ANONYMOUS
%11 = or i32 %10, %9
%12 = load i32, i32* @_SC_PAGE_SIZE
%13 = tail call i32 @sysconf(i32 %12)
%14 = add nsw i32 %13, 12
%15 = tail call i64 @mmap(i32* null, i32 %14, i32 %8, i32 %11, i32 -1, i32 0)
%16 = inttoptr i64 %15 to i8*
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %17, %16
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = tail call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %0
%22 = tail call i32 @print_table(i8* %16, i32 12)
%23 = tail call i32 @sleep(i32 2)
%24 = tail call i32 (...) @fork()
switch i32 %24, label %33 [
i32 -1, label %25
i32 0, label %27
]
25: ; preds = %21
%26 = tail call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %21, %25
%28 = tail call i32 @sleep(i32 2)
%29 = tail call i32 @print_table(i8* %16, i32 12)
%30 = tail call i32 @sleep(i32 4)
%31 = tail call i32 @print_table(i8* %16, i32 12)
%32 = tail call i32 @munmap(i8* %16, i32 12)
br label %48
33: ; preds = %21
%34 = tail call i32 (...) @getpid()
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %24)
%36 = tail call i32 @print_table(i8* %16, i32 12)
%37 = tail call i32 @sleep(i32 4)
%38 = call i32 @strncpy(i8* %16, i8* %2, i32 12)
%39 = load i32, i32* @_SC_PAGE_SIZE
%40 = call i32 @sysconf(i32 %39)
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %16, i64 %41
%43 = call i32 @strncpy(i8* %42, i8* %2, i32 12)
%44 = call i32 (...) @getpid()
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = call i32 @print_table(i8* %16, i32 12)
%47 = call i32 @wait(i32 0)
br label %48
48: ; preds = %33, %27
ret i32 0
}