#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*/ sem_t;
typedef int pid_t;

/* Variables and functions */
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int fork();
int getpid();
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ print_table(char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sem_destroy(int /*<<< orphan*/ *);
scalar_t__ sem_init(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
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";
  void *mapped_mem_all;
  char *mapped_mem;
  int fd_memory = -1;
  int flags, prot;
  sem_t *sem;
  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_all = mmap(NULL, sizeof(sem_t) + 12, prot, flags, fd_memory, 0);
  if (mapped_mem_all == MAP_FAILED)
    syserr("mmap");
  mapped_mem = (char *)(mapped_mem_all + sizeof(sem_t));
  sem = (sem_t *)mapped_mem_all;
  print_table(mapped_mem, 12);
  if (sem_init(sem, 1, 0))
    syserr("sem_init");
  switch (pid = fork()) {
  case -1:
    syserr("fork");
  case 0:
    if (sem_wait(sem))
      syserr("sem_wait");
    print_table(mapped_mem, 12);
    munmap(mapped_mem, 12);
    return 0;
  default:
    printf("Pid rodzica %d, pid dziecka: %d\n", getpid(), pid);
    break;
  }
  sleep(2);
  strncpy(mapped_mem, buff, 12);
  printf("Proces %d, Zmieniłem zawartosć pamięci\n", getpid());
  print_table(mapped_mem, 12);
  if (sem_post(sem))
    syserr("sem_post");
  wait(0);
  sem_destroy(sem);
  munmap(mapped_mem_all, sizeof(sem_t) + 12);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xdfa(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xdf8(%rip),%eax
mov    %eax,-0x8(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    0x2d80(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    0x2d7b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10,%esi
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    12a1 <main+0xb1>
lea    0xd8a(%rip),%rdi
call   1090 <syserr@plt>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10c0 <sem_init@plt>
cmp    $0x0,%rax
je     12e9 <main+0xf9>
lea    0xd47(%rip),%rdi
call   1090 <syserr@plt>
xor    %eax,%eax
call   1110 <fork@plt>
mov    %eax,-0x3c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x40(%rbp)
sub    $0xffffffff,%eax
je     1316 <main+0x126>
jmp    1306 <main+0x116>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1322 <main+0x132>
jmp    1368 <main+0x178>
lea    0xd17(%rip),%rdi
call   1090 <syserr@plt>
mov    -0x38(%rbp),%rdi
call   1050 <sem_wait@plt>
cmp    $0x0,%eax
je     1340 <main+0x150>
lea    0xcfe(%rip),%rdi
call   1090 <syserr@plt>
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <munmap@plt>
movl   $0x0,-0x4(%rbp)
jmp    1407 <main+0x217>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
mov    -0x3c(%rbp),%edx
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    -0x20(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <sem_post@plt>
cmp    $0x0,%rax
je     13e2 <main+0x1f2>
lea    0xcb1(%rip),%rdi
call   1090 <syserr@plt>
xor    %edi,%edi
call   10f0 <wait@plt>
mov    -0x38(%rbp),%rdi
call   1100 <sem_destroy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x10,%esi
call   10b0 <munmap@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    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
lea    0xde9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2dc5(%rip),%rax
mov    0x2d8e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da3(%rip),%rax
mov    0x2d8c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x10,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rbx
mov    %rax,%r14
mov    0x2d46(%rip),%rax
cmp    (%rax),%rbx
jne    1283 <main+0x93>
lea    0xda8(%rip),%rdi
call   1090 <syserr@plt>
add    $0x4,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    %r14,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10c0 <sem_init@plt>
test   %rax,%rax
je     12b4 <main+0xc4>
lea    0xd7c(%rip),%rdi
call   1090 <syserr@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     12d2 <main+0xe2>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1304 <main+0x114>
lea    0xd67(%rip),%rdi
call   1090 <syserr@plt>
mov    %r14,%rdi
call   1050 <sem_wait@plt>
test   %eax,%eax
je     12ea <main+0xfa>
lea    0xd54(%rip),%rdi
call   1090 <syserr@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
jmp    138b <main+0x19b>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd30(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xc,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd1e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <print_table@plt>
mov    %r14,%rdi
call   10a0 <sem_post@plt>
test   %rax,%rax
je     1374 <main+0x184>
lea    0xd1f(%rip),%rdi
call   1090 <syserr@plt>
xor    %edi,%edi
call   10f0 <wait@plt>
mov    %r14,%rdi
call   1100 <sem_destroy@plt>
mov    %r14,%rdi
mov    $0x10,%esi
call   10b0 <munmap@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 = [9 x i8] c"sem_init\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [9 x i8] c"sem_wait\00"
@.str.5 = [33 x i8] c"Pid rodzica %d, pid dziecka: %d\0A\00"
@.str.6 = [43 x i8] c"Proces %d, Zmieni\C5\82em zawartos\C4\87 pami\C4\99ci\0A\00"
@.str.7 = [9 x i8] c"sem_post\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %1
%10 = bitcast [12 x i8]* %2 to i8*
store i32 -1, i32* %5
%11 = load i32, i32* @_SC_PAGE_SIZE
%12 = call i32 @sysconf(i32 %11)
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
store i32 %16, i32* %7
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* @MAP_ANONYMOUS
%19 = or i32 %17, %18
store i32 %19, i32* %6
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = load i32, i32* %5
%23 = call i8* @mmap(i32* null, i32 16, i32 %20, i32 %21, i32 %22, i32 0)
store i8* %23, i8** %3
%24 = load i8*, i8** %3
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %0
%28 = call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %0
%30 = load i8*, i8** %3
%31 = getelementptr i8, i8* %30, i64 4
store i8* %31, i8** %4
%32 = load i8*, i8** %3
%33 = bitcast i8* %32 to i32*
store i32* %33, i32** %8
%34 = load i8*, i8** %4
%35 = call i32 @print_table(i8* %34, i32 12)
%36 = load i32*, i32** %8
%37 = call i64 @sem_init(i32* %36, i32 1, i32 0)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %29
%40 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %29
%42 = call i32 (...) @fork()
store i32 %42, i32* %9
switch i32 %42, label %56 [
i32 -1, label %43
i32 0, label %45
]
43: ; preds = %41
%44 = call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %41, %43
%46 = load i32*, i32** %8
%47 = call i32 @sem_wait(i32* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = load i8*, i8** %4
%53 = call i32 @print_table(i8* %52, i32 12)
%54 = load i8*, i8** %4
%55 = call i32 @munmap(i8* %54, i32 12)
store i32 0, i32* %1
br label %80
56: ; preds = %41
%57 = call i32 (...) @getpid()
%58 = load i32, i32* %9
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %57, i32 %58)
br label %60
60: ; preds = %56
%61 = call i32 @sleep(i32 2)
%62 = load i8*, i8** %4
%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.6, i64 0, i64 0), i32 %65)
%67 = load i8*, i8** %4
%68 = call i32 @print_table(i8* %67, i32 12)
%69 = load i32*, i32** %8
%70 = call i64 @sem_post(i32* %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %60
%73 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %74
74: ; preds = %72, %60
%75 = call i32 @wait(i32 0)
%76 = load i32*, i32** %8
%77 = call i32 @sem_destroy(i32* %76)
%78 = load i8*, i8** %3
%79 = call i32 @munmap(i8* %78, i32 16)
store i32 0, i32* %1
br label %80
80: ; preds = %74, %51
%81 = load i32, i32* %1
ret i32 %81
}

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 = [9 x i8] c"sem_init\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [9 x i8] c"sem_wait\00"
@.str.5 = [33 x i8] c"Pid rodzica %d, pid dziecka: %d\0A\00"
@.str.6 = [43 x i8] c"Proces %d, Zmieni\C5\82em zawartos\C4\87 pami\C4\99ci\0A\00"
@.str.7 = [9 x i8] c"sem_post\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 = tail call i8* @mmap(i32* null, i32 16, i32 %8, i32 %11, i32 -1, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = getelementptr i8, i8* %12, i64 4
%19 = bitcast i8* %12 to i32*
%20 = tail call i32 @print_table(i8* %18, i32 12)
%21 = tail call i64 @sem_init(i32* %19, i32 1, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = tail call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = tail call i32 (...) @fork()
switch i32 %26, label %37 [
i32 -1, label %27
i32 0, label %29
]
27: ; preds = %25
%28 = tail call i32 @syserr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %25, %27
%30 = tail call i32 @sem_wait(i32* %19)
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @print_table(i8* %18, i32 12)
%36 = tail call i32 @munmap(i8* %18, i32 12)
br label %53
37: ; preds = %25
%38 = tail call i32 (...) @getpid()
%39 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %26)
%40 = tail call i32 @sleep(i32 2)
%41 = call i32 @strncpy(i8* %18, i8* %2, i32 12)
%42 = call i32 (...) @getpid()
%43 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = call i32 @print_table(i8* %18, i32 12)
%45 = call i64 @sem_post(i32* %19)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %37
%48 = call i32 @syserr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %49
49: ; preds = %47, %37
%50 = call i32 @wait(i32 0)
%51 = call i32 @sem_destroy(i32* %19)
%52 = call i32 @munmap(i8* %12, i32 16)
br label %53
53: ; preds = %49, %34
ret i32 0
}