#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 */

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
scalar_t__ fork();
int ftok(char *, int);
int msgctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int msgget(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ reader(int, int, char *);
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int shmget(int, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ writer(int, int, char *);
int main(int argc, char **argv) {
  if (argc < 3) {
    printf("Too few arguments\n");
    return EXIT_FAILURE;
  }
  int token = ftok(argv[0], 10);
  int shm_id = shmget(token, 2048 + sizeof(int), 0666 | IPC_CREAT);
  if (shm_id == -1) {
    perror("Base could not get shared memory");
    return EXIT_FAILURE;
  }
  int msg_id = msgget(token, IPC_CREAT | 0666);
  if (msg_id == -1) {
    perror("Base could not get message queue");
    return EXIT_FAILURE;
  }
  if (fork() == 0) {
    writer(shm_id, msg_id, argv[1]);
    return EXIT_SUCCESS;
  } else if (fork() == 0) {
    reader(shm_id, msg_id, argv[2]);
    return EXIT_SUCCESS;
  } else {
    wait(NULL);
    wait(NULL);
  }
  if (shmctl(shm_id, IPC_RMID, NULL) == -1) {
    perror("Base could not remove memory");
    return EXIT_FAILURE;
  }
  if (msgctl(msg_id, IPC_RMID, 0) == -1) {
    perror("Base could not remove message queue");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ed <main+0x3d>
lea    0xe29(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0xa,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x804,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1247 <main+0x97>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    -0x14(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1288 <main+0xd8>
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    12bd <main+0x10d>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1070 <writer@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    12f2 <main+0x142>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
call   1040 <reader@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
jmp    1309 <main+0x159>
mov    -0x18(%rbp),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    1344 <main+0x194>
lea    0xd27(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ca6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    -0x1c(%rbp),%edi
mov    0x2c8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <msgctl@plt>
cmp    $0xffffffff,%eax
jne    137d <main+0x1cd>
lea    0xd0b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <main+0x1d9>
mov    0x2c4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%edi
jg     11d5 <main+0x25>
lea    0xebf(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e10(%rip),%rax
jmp    12a9 <main+0xf9>
mov    %rsi,%r12
mov    (%rsi),%rdi
mov    $0xa,%esi
call   10b0 <ftok@plt>
mov    %eax,%ebp
mov    0x2dda(%rip),%r14
mov    $0x1b6,%ebx
mov    (%r14),%edx
or     %ebx,%edx
mov    %eax,%edi
mov    $0x804,%esi
call   10a0 <shmget@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x104>
mov    %eax,%r15d
or     (%r14),%ebx
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
je     12bd <main+0x10d>
mov    %eax,%r14d
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1280 <main+0xd0>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1292 <main+0xe2>
xor    %edi,%edi
call   10c0 <wait@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
mov    0x2d83(%rip),%rbx
mov    (%rbx),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
je     12c6 <main+0x116>
mov    (%rbx),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1060 <msgctl@plt>
cmp    $0xffffffff,%eax
jne    12a2 <main+0xf2>
lea    0xde1(%rip),%rdi
jmp    12cd <main+0x11d>
mov    0x8(%r12),%rdx
mov    %r15d,%edi
mov    %r14d,%esi
call   1070 <writer@plt>
jmp    12a2 <main+0xf2>
mov    0x10(%r12),%rdx
mov    %r15d,%edi
mov    %r14d,%esi
call   1050 <reader@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd45(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xd5d(%rip),%rdi
jmp    12cd <main+0x11d>
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
jmp    11c9 <main+0x19>

LLVM IR (-O0):
@.str = [19 x i8] c"Too few arguments\0A\00"
@EXIT_FAILURE = global i32 0
@IPC_CREAT = global i32 0
@.str.1 = [33 x i8] c"Base could not get shared memory\00"
@.str.2 = [33 x i8] c"Base could not get message queue\00"
@EXIT_SUCCESS = global i32 0
@IPC_RMID = global i32 0
@.str.3 = [29 x i8] c"Base could not remove memory\00"
@.str.4 = [36 x i8] c"Base could not remove message queue\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %3
br label %82
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @ftok(i8* %17, i32 10)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = load i32, i32* @IPC_CREAT
%21 = or i32 438, %20
%22 = call i32 @shmget(i32 %19, i32 2052, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %3
br label %82
28: ; preds = %14
%29 = load i32, i32* %6
%30 = load i32, i32* @IPC_CREAT
%31 = or i32 %30, 438
%32 = call i32 @msgget(i32 %29, i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %82
38: ; preds = %28
%39 = call i64 (...) @fork()
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @writer(i32 %42, i32 %43, i8* %46)
%48 = load i32, i32* @EXIT_SUCCESS
store i32 %48, i32* %3
br label %82
49: ; preds = %38
%50 = call i64 (...) @fork()
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %49
%53 = load i32, i32* %7
%54 = load i32, i32* %8
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @reader(i32 %53, i32 %54, i8* %57)
%59 = load i32, i32* @EXIT_SUCCESS
store i32 %59, i32* %3
br label %82
60: ; preds = %49
%61 = call i32 @wait(i32* null)
%62 = call i32 @wait(i32* null)
br label %63
63: ; preds = %60
br label %64
64: ; preds = %63
%65 = load i32, i32* %7
%66 = load i32, i32* @IPC_RMID
%67 = call i32 @shmctl(i32 %65, i32 %66, i32* null)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %72
69: ; preds = %64
%70 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
store i32 %71, i32* %3
br label %82
72: ; preds = %64
%73 = load i32, i32* %8
%74 = load i32, i32* @IPC_RMID
%75 = call i32 @msgctl(i32 %73, i32 %74, i32 0)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%79 = load i32, i32* @EXIT_FAILURE
store i32 %79, i32* %3
br label %82
80: ; preds = %72
%81 = load i32, i32* @EXIT_SUCCESS
store i32 %81, i32* %3
br label %82
82: ; preds = %80, %77, %69, %52, %41, %35, %25, %11
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@IPC_CREAT = global i32 0
@.str.1 = [33 x i8] c"Base could not get shared memory\00"
@.str.2 = [33 x i8] c"Base could not get message queue\00"
@EXIT_SUCCESS = global i32 0
@IPC_RMID = global i32 0
@.str.3 = [29 x i8] c"Base could not remove memory\00"
@.str.4 = [36 x i8] c"Base could not remove message queue\00"
@str = [18 x i8] c"Too few arguments\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %50
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @ftok(i8* %7, i32 10)
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = tail call i32 @shmget(i32 %8, i32 2052, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %50
15: ; preds = %6
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 %16, 438
%18 = tail call i32 @msgget(i32 %8, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %50
22: ; preds = %15
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @writer(i32 %11, i32 %18, i8* %27)
br label %50
29: ; preds = %22
%30 = tail call i64 (...) @fork()
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = getelementptr inbounds i8*, i8** %1, i64 2
%34 = load i8*, i8** %33
%35 = tail call i32 @reader(i32 %11, i32 %18, i8* %34)
br label %50
36: ; preds = %29
%37 = tail call i32 @wait(i32* null)
%38 = tail call i32 @wait(i32* null)
%39 = load i32, i32* @IPC_RMID
%40 = tail call i32 @shmctl(i32 %11, i32 %39, i32* null)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %50
44: ; preds = %36
%45 = load i32, i32* @IPC_RMID
%46 = tail call i32 @msgctl(i32 %18, i32 %45, i32 0)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %13, %44, %48, %42, %32, %25, %20, %4
%51 = phi i32* [ @EXIT_FAILURE, %4 ], [ @EXIT_FAILURE, %13 ], [ @EXIT_FAILURE, %20 ], [ @EXIT_SUCCESS, %25 ], [ @EXIT_SUCCESS, %32 ], [ @EXIT_FAILURE, %42 ], [ @EXIT_FAILURE, %48 ], [ @EXIT_SUCCESS, %44 ]
%52 = load i32, i32* %51
ret i32 %52
}