#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 key_t;
typedef int gid_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ client();
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ server();
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  gid_t ch1;
  gid_t ch2;
  key_t key;
  int shmid = 0;
  key = 75;
  if ((shmid = shmget(key, 1024, IPC_CREAT | 0777)) == -1) {
    perror("shmget error");
    exit(1);
  }
  printf("shmid = %d\n", shmid);
  if ((ch1 = fork()) == -1) {
    perror("fork child 1 error");
    exit(1);
  }
  if (ch1 == 0) {
    client();
  } else {
    if ((ch2 = fork()) == -1) {
      perror("fork child 2 error");
      exit(1);
    }
    if (ch2 == 0) {
      server();
    } else {
      wait(NULL);
      wait(NULL);
      if (shmctl(shmid, IPC_RMID, 0) == -1) {
        perror("shmctl error");
        exit(1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x4b,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
mov    $0x400,%esi
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11eb <main+0x5b>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe18(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1223 <main+0x93>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1239 <main+0xa9>
mov    $0x0,%al
call   1080 <client@plt>
jmp    12c6 <main+0x136>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1262 <main+0xd2>
lea    0xdd9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1278 <main+0xe8>
mov    $0x0,%al
call   1040 <server@plt>
jmp    12c1 <main+0x131>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x14(%rbp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0x12c>
lea    0xd92(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12c1 <main+0x131>
jmp    12c6 <main+0x136>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e40(%rip),%rax
mov    $0x1ff,%edx
or     (%rax),%edx
mov    $0x4b,%edi
mov    $0x400,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     1225 <main+0x95>
mov    %eax,%ebx
lea    0xe51(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1211 <main+0x81>
cmp    $0xffffffff,%eax
je     122e <main+0x9e>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     121a <main+0x8a>
cmp    $0xffffffff,%eax
je     1237 <main+0xa7>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    1221 <main+0x91>
lea    0xe30(%rip),%rdi
jmp    123e <main+0xae>
xor    %eax,%eax
call   1080 <client@plt>
jmp    1221 <main+0x91>
xor    %eax,%eax
call   1040 <server@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xdd4(%rip),%rdi
jmp    123e <main+0xae>
lea    0xde4(%rip),%rdi
jmp    123e <main+0xae>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [13 x i8] c"shmget error\00"
@.str.1 = [12 x i8] c"shmid = %d\0A\00"
@.str.2 = [19 x i8] c"fork child 1 error\00"
@.str.3 = [19 x i8] c"fork child 2 error\00"
@IPC_RMID = global i32 0
@.str.4 = [13 x i8] c"shmctl error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
store i32 75, i32* %4
%6 = load i32, i32* %4
%7 = load i32, i32* @IPC_CREAT
%8 = or i32 %7, 511
%9 = call i32 @shmget(i32 %6, i32 1024, i32 %8)
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %5
%16 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 (...) @fork()
store i32 %17, i32* %2
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (...) @client()
br label %50
27: ; preds = %22
%28 = call i32 (...) @fork()
store i32 %28, i32* %3
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = load i32, i32* %3
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (...) @server()
br label %49
38: ; preds = %33
%39 = call i32 @wait(i32* null)
%40 = call i32 @wait(i32* null)
%41 = load i32, i32* %5
%42 = load i32, i32* @IPC_RMID
%43 = call i32 @shmctl(i32 %41, i32 %42, i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %38
br label %49
49: ; preds = %48, %36
br label %50
50: ; preds = %49, %25
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [13 x i8] c"shmget error\00"
@.str.1 = [12 x i8] c"shmid = %d\0A\00"
@.str.2 = [19 x i8] c"fork child 1 error\00"
@.str.3 = [19 x i8] c"fork child 2 error\00"
@IPC_RMID = global i32 0
@.str.4 = [13 x i8] c"shmctl error\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 511
%3 = tail call i32 @shmget(i32 75, i32 1024, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%10 = tail call i32 (...) @fork()
switch i32 %10, label %16 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = tail call i32 (...) @client()
br label %32
16: ; preds = %8
%17 = tail call i32 (...) @fork()
switch i32 %17, label %23 [
i32 -1, label %18
i32 0, label %21
]
18: ; preds = %16
%19 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %16
%22 = tail call i32 (...) @server()
br label %32
23: ; preds = %16
%24 = tail call i32 @wait(i32* null)
%25 = tail call i32 @wait(i32* null)
%26 = load i32, i32* @IPC_RMID
%27 = tail call i32 @shmctl(i32 %3, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %21, %23, %14
ret i32 0
}