#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_SUCCESS;
int /*<<< orphan*/ IPC_RMID;
void *create_shared_memory();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ handle_fatal_error(char *);
int initialize_semaphores();
int /*<<< orphan*/ manage_child(int *, int *, int *, int);
int /*<<< orphan*/ manage_parent(int *, int *, int *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  void *shared_memory;
  int *buffer;
  int *in;
  int *out;
  int semaphores;
  shared_memory = create_shared_memory();
  buffer = (int *)shared_memory;
  in = (int *)shared_memory + 5 * sizeof(int);
  out = (int *)shared_memory + (5 + 1) * sizeof(int);
  *in = *out = 0;
  semaphores = initialize_semaphores();
  if (-1 == (pid = fork())) {
    handle_fatal_error("Error using fork()\n");
  }
  if (0 == pid) {
    manage_child(buffer, in, out, semaphores);
  } else {
    manage_parent(buffer, in, out, semaphores);
  }
  if (semctl(semaphores, 0, IPC_RMID) < 0) {
    handle_fatal_error("Error when trying to remove semaphores.\n");
  }
  printf("Semaphores have been removed.\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <create_shared_memory@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x50,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x60,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   1030 <initialize_semaphores@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    120d <main+0x7d>
lea    0xdf8(%rip),%rdi
call   1050 <handle_fatal_error@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    1231 <main+0xa1>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1070 <manage_child@plt>
jmp    1245 <main+0xb5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1060 <manage_parent@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <semctl@plt>
cmp    $0x0,%rax
jge    126e <main+0xde>
lea    0xdab(%rip),%rdi
call   1050 <handle_fatal_error@plt>
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <create_shared_memory@plt>
mov    %rax,%rbx
lea    0x50(%rax),%r15
lea    0x60(%rax),%r14
movl   $0x0,0x60(%rax)
movl   $0x0,0x50(%rax)
xor    %eax,%eax
call   1040 <initialize_semaphores@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11ee <main+0x5e>
cmp    $0xffffffff,%eax
jne    11dc <main+0x4c>
lea    0xe29(%rip),%rdi
call   1050 <handle_fatal_error@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1060 <manage_parent@plt>
jmp    11fe <main+0x6e>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1070 <manage_child@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <semctl@plt>
test   %rax,%rax
jns    1221 <main+0x91>
lea    0xdf8(%rip),%rdi
call   1050 <handle_fatal_error@plt>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Error using fork()\0A\00"
@IPC_RMID = global i32 0
@.str.1 = [41 x i8] c"Error when trying to remove semaphores.\0A\00"
@.str.2 = [31 x i8] c"Semaphores have been removed.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %1
%8 = call i8* (...) @create_shared_memory()
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %4
%11 = load i8*, i8** %3
%12 = bitcast i8* %11 to i32*
%13 = getelementptr inbounds i32, i32* %12, i64 20
store i32* %13, i32** %5
%14 = load i8*, i8** %3
%15 = bitcast i8* %14 to i32*
%16 = getelementptr inbounds i32, i32* %15, i64 24
store i32* %16, i32** %6
%17 = load i32*, i32** %6
store i32 0, i32* %17
%18 = load i32*, i32** %5
store i32 0, i32* %18
%19 = call i32 (...) @initialize_semaphores()
store i32 %19, i32* %7
%20 = call i32 (...) @fork()
store i32 %20, i32* %2
%21 = icmp eq i32 -1, %20
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @handle_fatal_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %0
%25 = load i32, i32* %2
%26 = icmp eq i32 0, %25
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32*, i32** %4
%29 = load i32*, i32** %5
%30 = load i32*, i32** %6
%31 = load i32, i32* %7
%32 = call i32 @manage_child(i32* %28, i32* %29, i32* %30, i32 %31)
br label %39
33: ; preds = %24
%34 = load i32*, i32** %4
%35 = load i32*, i32** %5
%36 = load i32*, i32** %6
%37 = load i32, i32* %7
%38 = call i32 @manage_parent(i32* %34, i32* %35, i32* %36, i32 %37)
br label %39
39: ; preds = %33, %27
%40 = load i32, i32* %7
%41 = load i32, i32* @IPC_RMID
%42 = call i64 @semctl(i32 %40, i32 0, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @handle_fatal_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_SUCCESS
%49 = call i32 @exit(i32 %48)
unreachable
}

LLVM IR (-O2):
@.str = [20 x i8] c"Error using fork()\0A\00"
@IPC_RMID = global i32 0
@.str.1 = [41 x i8] c"Error when trying to remove semaphores.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [30 x i8] c"Semaphores have been removed.\00"
define i32 @main() {
%1 = tail call i8* (...) @create_shared_memory()
%2 = bitcast i8* %1 to i32*
%3 = getelementptr inbounds i8, i8* %1, i64 80
%4 = bitcast i8* %3 to i32*
%5 = getelementptr inbounds i8, i8* %1, i64 96
%6 = bitcast i8* %5 to i32*
store i32 0, i32* %6
store i32 0, i32* %4
%7 = tail call i32 (...) @initialize_semaphores()
%8 = tail call i32 (...) @fork()
switch i32 %8, label %13 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = tail call i32 @handle_fatal_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %13
11: ; preds = %0
%12 = tail call i32 @manage_child(i32* %2, i32* %4, i32* %6, i32 %7)
br label %15
13: ; preds = %9, %0
%14 = tail call i32 @manage_parent(i32* %2, i32* %4, i32* %6, i32 %7)
br label %15
15: ; preds = %13, %11
%16 = load i32, i32* @IPC_RMID
%17 = tail call i64 @semctl(i32 %7, i32 0, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @handle_fatal_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_SUCCESS
%24 = tail call i32 @exit(i32 %23)
unreachable
}