#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 */
char *MAP_FAILED;
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ close(int);
int fork();
int ftruncate(int, int);
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 shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
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));
  fd_memory =
      shm_open("/pw_practice_memory", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (fd_memory == -1)
    syserr("shm_open");
  if (ftruncate(fd_memory, 12) == -1)
    syserr("truncate");
  prot = PROT_READ | PROT_WRITE;
  flags = MAP_SHARED;
  mapped_mem = (char *)mmap(NULL, 12, prot, flags, fd_memory, 0);
  close(fd_memory);
  shm_unlink("/pw_practice_memory");
  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);
  printf("Proces %d, Zmieniłem zawartosć pamięci\n", getpid());
  print_table(mapped_mem, 12);
  wait(0);
  munmap(mapped_mem, 12);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xdfa(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xdf8(%rip),%eax
mov    %eax,-0x8(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
mov    0x2d6e(%rip),%rax
or     (%rax),%edx
lea    0xdc3(%rip),%rdi
call   10d0 <shm_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1281 <main+0x91>
lea    0xdbe(%rip),%rdi
call   1080 <syserr@plt>
mov    -0x1c(%rbp),%edi
mov    $0xc,%esi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12a3 <main+0xb3>
lea    0xda5(%rip),%rdi
call   1080 <syserr@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    0x2d05(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0xc,%esi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
lea    0xd34(%rip),%rdi
call   10e0 <shm_unlink@plt>
mov    -0x18(%rbp),%rax
mov    0x2c9e(%rip),%rcx
cmp    (%rcx),%rax
jne    1317 <main+0x127>
lea    0xd3a(%rip),%rdi
call   1080 <syserr@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x2,%edi
call   10f0 <sleep@plt>
xor    %eax,%eax
call   1110 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     135c <main+0x16c>
jmp    134c <main+0x15c>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     1368 <main+0x178>
jmp    13b2 <main+0x1c2>
lea    0xcee(%rip),%rdi
call   1080 <syserr@plt>
mov    $0x2,%edi
call   10f0 <sleep@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x4,%edi
call   10f0 <sleep@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <munmap@plt>
movl   $0x0,-0x4(%rbp)
jmp    143c <main+0x24c>
jmp    13b7 <main+0x1c7>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%edx
lea    0xc8c(%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   10f0 <sleep@plt>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xc6c(%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   1100 <wait@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movabs $0x6b20616d20616c41,%rax
61 20 6b
mov    %rax,0x8(%rsp)
movl   $0x61746f,0x10(%rsp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
lea    0xdea(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2dc6(%rip),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xdd0(%rip),%rdi
call   10d0 <shm_open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    126e <main+0x7e>
lea    0xdd1(%rip),%rdi
call   1080 <syserr@plt>
mov    %ebp,%edi
mov    $0xc,%esi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    128b <main+0x9b>
lea    0xdbd(%rip),%rdi
call   1080 <syserr@plt>
mov    0x2d5e(%rip),%rax
mov    0x2d1f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d3c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0xc,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   10a0 <close@plt>
lea    0xd5d(%rip),%rdi
call   10e0 <shm_unlink@plt>
mov    0x2ccb(%rip),%rax
cmp    %rbx,(%rax)
jne    12e6 <main+0xf6>
lea    0xd6b(%rip),%rdi
call   1080 <syserr@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x2,%edi
call   10f0 <sleep@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     131b <main+0x12b>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    134b <main+0x15b>
lea    0xd3b(%rip),%rdi
call   1080 <syserr@plt>
mov    $0x2,%edi
call   10f0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    $0x4,%edi
call   10f0 <sleep@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
jmp    13b8 <main+0x1c8>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xcfd(%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   10f0 <sleep@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xc,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xcdc(%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   1100 <wait@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   10b0 <munmap@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
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
@.str.1 = [20 x i8] c"/pw_practice_memory\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [9 x i8] c"shm_open\00"
@.str.3 = [9 x i8] c"truncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [33 x i8] c"Pid rodzica %d, pid dziecka: %d\0A\00"
@.str.7 = [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* @O_CREAT
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = call i32 @shm_open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %0
%24 = load i32, i32* %4
%25 = call i32 @ftruncate(i32 %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
store i32 %32, i32* %6
%33 = load i32, i32* @MAP_SHARED
store i32 %33, i32* %5
%34 = load i32, i32* %6
%35 = load i32, i32* %5
%36 = load i32, i32* %4
%37 = call i64 @mmap(i32* null, i32 12, i32 %34, i32 %35, i32 %36, i32 0)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %3
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = call i32 @shm_unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%42 = load i8*, i8** %3
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %47
45: ; preds = %29
%46 = call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %29
%48 = load i8*, i8** %3
%49 = call i32 @print_table(i8* %48, i32 12)
%50 = call i32 @sleep(i32 2)
%51 = call i32 (...) @fork()
store i32 %51, i32* %7
switch i32 %51, label %63 [
i32 -1, label %52
i32 0, label %54
]
52: ; preds = %47
%53 = call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %47, %52
%55 = call i32 @sleep(i32 2)
%56 = load i8*, i8** %3
%57 = call i32 @print_table(i8* %56, i32 12)
%58 = call i32 @sleep(i32 4)
%59 = load i8*, i8** %3
%60 = call i32 @print_table(i8* %59, i32 12)
%61 = load i8*, i8** %3
%62 = call i32 @munmap(i8* %61, i32 12)
store i32 0, i32* %1
br label %81
63: ; preds = %47
br label %64
64: ; preds = %63
%65 = call i32 (...) @getpid()
%66 = load i32, i32* %7
%67 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %65, i32 %66)
%68 = load i8*, i8** %3
%69 = call i32 @print_table(i8* %68, i32 12)
%70 = call i32 @sleep(i32 4)
%71 = load i8*, i8** %3
%72 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%73 = call i32 @strncpy(i8* %71, i8* %72, i32 12)
%74 = call i32 (...) @getpid()
%75 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %74)
%76 = load i8*, i8** %3
%77 = call i32 @print_table(i8* %76, i32 12)
%78 = call i32 @wait(i32 0)
%79 = load i8*, i8** %3
%80 = call i32 @munmap(i8* %79, i32 12)
store i32 0, i32* %1
br label %81
81: ; preds = %64, %54
%82 = load i32, i32* %1
ret i32 %82
}

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
@.str.1 = [20 x i8] c"/pw_practice_memory\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [9 x i8] c"shm_open\00"
@.str.3 = [9 x i8] c"truncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [33 x i8] c"Pid rodzica %d, pid dziecka: %d\0A\00"
@.str.7 = [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* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @shm_open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = tail call i32 @ftruncate(i32 %12, i32 12)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %23, %22
%25 = load i32, i32* @MAP_SHARED
%26 = tail call i64 @mmap(i32* null, i32 12, i32 %24, i32 %25, i32 %12, i32 0)
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 @close(i32 %12)
%29 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %30, %27
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = tail call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
%35 = tail call i32 @print_table(i8* %27, i32 12)
%36 = tail call i32 @sleep(i32 2)
%37 = tail call i32 (...) @fork()
switch i32 %37, label %46 [
i32 -1, label %38
i32 0, label %40
]
38: ; preds = %34
%39 = tail call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %34, %38
%41 = tail call i32 @sleep(i32 2)
%42 = tail call i32 @print_table(i8* %27, i32 12)
%43 = tail call i32 @sleep(i32 4)
%44 = tail call i32 @print_table(i8* %27, i32 12)
%45 = tail call i32 @munmap(i8* %27, i32 12)
br label %57
46: ; preds = %34
%47 = tail call i32 (...) @getpid()
%48 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %47, i32 %37)
%49 = tail call i32 @print_table(i8* %27, i32 12)
%50 = tail call i32 @sleep(i32 4)
%51 = call i32 @strncpy(i8* %27, i8* %2, i32 12)
%52 = call i32 (...) @getpid()
%53 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %52)
%54 = call i32 @print_table(i8* %27, i32 12)
%55 = call i32 @wait(i32 0)
%56 = call i32 @munmap(i8* %27, i32 12)
br label %57
57: ; preds = %46, %40
ret i32 0
}