#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;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ Receiver(int);
int /*<<< orphan*/ Sender(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int msgctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int msgget(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int rtn = 1;
  int count = 0, i = 0;
  int queue_id;
  key_t key = 3102;
  if ((queue_id = msgget(key, IPC_CREAT | 0666)) == -1) {
    fprintf(stderr, "Impossivel criar a fila de mensagens!\n");
    exit(1);
  }
  for (i = 0; i < 2; i++) {
    if (rtn > 0) {
      rtn = fork();
      count++;
    } else {
      break;
    }
  }
  if (rtn == 0 && count == 1) {
    printf("Receptor iniciado ...\n");
    Receiver(queue_id);
    exit(0);
  } else if (rtn == 0 && count == 2) {
    printf("Emissor iniciado ...\n");
    Sender(queue_id);
    exit(0);
  } else {
    printf("Pai aguardando os filhos terminarem...\n");
    wait(NULL);
    wait(NULL);
    if (msgctl(queue_id, IPC_RMID, NULL) == -1) {
      printf("Impossivel remover a fila!\n");
      exit(1);
    }
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0xc1e,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1204 <main+0x74>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    124f <main+0xbf>
cmpl   $0x0,-0x14(%rbp)
jle    1237 <main+0xa7>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    123c <main+0xac>
jmp    124f <main+0xbf>
jmp    1241 <main+0xb1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <main+0x7b>
cmpl   $0x0,-0x14(%rbp)
jne    127e <main+0xee>
cmpl   $0x1,-0x18(%rbp)
jne    127e <main+0xee>
lea    0xdbd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <Receiver@plt>
xor    %edi,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12ad <main+0x11d>
cmpl   $0x2,-0x18(%rbp)
jne    12ad <main+0x11d>
lea    0xda5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1080 <Sender@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xda0(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x20(%rbp),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <msgctl@plt>
cmp    $0xffffffff,%eax
jne    12ff <main+0x16f>
lea    0xd8c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e36(%rip),%rax
mov    $0x1b6,%esi
or     (%rax),%esi
mov    $0xc1e,%edi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
jne    11cf <main+0x3f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11e5 <main+0x55>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1207 <main+0x77>
test   %ebp,%ebp
jg     1207 <main+0x77>
lea    0xe8a(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1060 <Receiver@plt>
xor    %edi,%edi
call   1090 <exit@plt>
test   %eax,%eax
jne    1229 <main+0x99>
test   %ebp,%ebp
jle    1229 <main+0x99>
lea    0xe53(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1080 <Sender@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdf7(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <msgctl@plt>
cmp    $0xffffffff,%eax
jne    1270 <main+0xe0>
lea    0xded(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Impossivel criar a fila de mensagens!\0A\00"
@.str.1 = [23 x i8] c"Receptor iniciado ...\0A\00"
@.str.2 = [22 x i8] c"Emissor iniciado ...\0A\00"
@.str.3 = [40 x i8] c"Pai aguardando os filhos terminarem...\0A\00"
@IPC_RMID = global i32 0
@.str.4 = [28 x i8] c"Impossivel remover a fila!\0A\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
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 3102, i32* %10
%11 = load i32, i32* %10
%12 = load i32, i32* @IPC_CREAT
%13 = or i32 %12, 438
%14 = call i32 @msgget(i32 %11, i32 %13)
store i32 %14, i32* %9
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
store i32 0, i32* %8
br label %21
21: ; preds = %33, %20
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 2
br i1 %23, label %24, label %36
24: ; preds = %21
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 (...) @fork()
store i32 %28, i32* %6
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %32
31: ; preds = %24
br label %36
32: ; preds = %27
br label %33
33: ; preds = %32
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %21
36: ; preds = %31, %21
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* %9
%45 = call i32 @Receiver(i32 %44)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %39, %36
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %47
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 2
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* %9
%56 = call i32 @Sender(i32 %55)
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %50, %47
%59 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @wait(i32* null)
%61 = call i32 @wait(i32* null)
%62 = load i32, i32* %9
%63 = load i32, i32* @IPC_RMID
%64 = call i32 @msgctl(i32 %62, i32 %63, i32* null)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %58
%67 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %58
%70 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Impossivel criar a fila de mensagens!\0A\00"
@IPC_RMID = global i32 0
@str = [39 x i8] c"Pai aguardando os filhos terminarem...\00"
@str.5 = [27 x i8] c"Impossivel remover a fila!\00"
@str.6 = [21 x i8] c"Emissor iniciado ...\00"
@str.7 = [22 x i8] c"Receptor iniciado ...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 438
%5 = tail call i32 @msgget(i32 3102, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 (...) @fork()
%9 = icmp sgt i32 %8, 0
br i1 %9, label %14, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = tail call i32 (...) @fork()
br label %16
16: ; preds = %14, %7
%17 = phi i32 [ %8, %7 ], [ %15, %14 ]
%18 = xor i1 %9, true
%19 = icmp eq i32 %17, 0
%20 = and i1 %19, %18
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%23 = tail call i32 @Receiver(i32 %5)
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %16
%26 = and i1 %19, %9
br i1 %26, label %27, label %31
27: ; preds = %25
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%29 = tail call i32 @Sender(i32 %5)
%30 = tail call i32 @exit(i32 0)
unreachable
31: ; preds = %25
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%33 = tail call i32 @wait(i32* null)
%34 = tail call i32 @wait(i32* null)
%35 = load i32, i32* @IPC_RMID
%36 = tail call i32 @msgctl(i32 %5, i32 %35, i32* null)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %31
%42 = tail call i32 @exit(i32 0)
unreachable
}