#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SIGTERM;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execl(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int wait(int *);
int main() {
  int status = 0;
  pid_t shareMem, getMem;
  shareMem = fork();
  if (shareMem == 0) {
    execl("SharedMemory", 0);
    exit(EXIT_FAILURE);
  } else if (shareMem == -1) {
    printf("Error\n");
    exit(EXIT_FAILURE);
  }
  getMem = fork();
  if (getMem == 0) {
    execl("GettingSharedMemory", 0);
    exit(EXIT_FAILURE);
  } else if (shareMem == -1) {
    printf("Error\n");
    exit(EXIT_FAILURE);
  }
  if (wait(&status) == -1)
    printf("Error waiting\n");
  else {
    if (WIFEXITED(status))
      if (WEXITSTATUS(status) == 3) {
        printf("server stopped, exiting client\n");
        kill(getMem, SIGTERM);
      }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11c6 <main+0x46>
lea    0xe5d(%rip),%rdi
xor    %esi,%esi
call   1080 <execl@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ea <main+0x6a>
lea    0xe29(%rip),%rdi
call   1030 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11ef <main+0x6f>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    121f <main+0x9f>
lea    0xdfd(%rip),%rdi
xor    %esi,%esi
call   1080 <execl@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1243 <main+0xc3>
lea    0xdd0(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1248 <main+0xc8>
lea    -0x8(%rbp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    126b <main+0xeb>
lea    0xdba(%rip),%rdi
call   1030 <printf@plt>
jmp    12b5 <main+0x135>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12b0 <main+0x130>
mov    -0x8(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x3,%eax
jne    12ab <main+0x12b>
lea    0xd95(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
jmp    12b0 <main+0x130>
jmp    12b5 <main+0x135>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11a6 <main+0x26>
test   %eax,%eax
jne    11b4 <main+0x34>
lea    0xe63(%rip),%rdi
jmp    11c6 <main+0x46>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
jmp    11cd <main+0x4d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11db <main+0x5b>
lea    0xe3a(%rip),%rdi
xor    %esi,%esi
call   1080 <execl@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    11ff <main+0x7f>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     1237 <main+0xb7>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x3,%eax
jne    1237 <main+0xb7>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <kill@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"SharedMemory\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"Error\0A\00"
@.str.2 = [20 x i8] c"GettingSharedMemory\00"
@.str.3 = [15 x i8] c"Error waiting\0A\00"
@.str.4 = [32 x i8] c"server stopped, exiting client\0A\00"
@SIGTERM = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = call i32 (...) @fork()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
br label %20
20: ; preds = %19
%21 = call i32 (...) @fork()
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = call i32 @execl(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 0)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %20
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %28
br label %36
36: ; preds = %35
%37 = call i32 @wait(i32* %2)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %56
41: ; preds = %36
%42 = load i32, i32* %2
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = load i32, i32* %2
%47 = call i32 @WEXITSTATUS(i32 %46)
%48 = icmp eq i32 %47, 3
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%51 = load i32, i32* %4
%52 = load i32, i32* @SIGTERM
%53 = call i32 @kill(i32 %51, i32 %52)
br label %54
54: ; preds = %49, %45
br label %55
55: ; preds = %54, %41
br label %56
56: ; preds = %55, %39
%57 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"SharedMemory\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [20 x i8] c"GettingSharedMemory\00"
@SIGTERM = global i32 0
@str = [31 x i8] c"server stopped, exiting client\00"
@str.5 = [14 x i8] c"Error waiting\00"
@str.6 = [6 x i8] c"Error\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 (...) @fork()
switch i32 %3, label %12 [
i32 0, label %4
i32 -1, label %8
]
4: ; preds = %0
%5 = tail call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.6, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = tail call i32 (...) @fork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = tail call i32 @execl(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 0)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = call i32 @wait(i32* %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %36
24: ; preds = %19
%25 = load i32, i32* %1
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %24
%29 = load i32, i32* %1
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = icmp eq i32 %30, 3
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%34 = load i32, i32* @SIGTERM
%35 = call i32 @kill(i32 %13, i32 %34)
br label %36
36: ; preds = %24, %32, %28, %22
%37 = call i32 @exit(i32 0)
unreachable
}