#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 */
struct mq_attr {
  int /*<<< orphan*/ mq_msgsize;
};
typedef int ssize_t;
typedef int mqd_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char *malloc(int /*<<< orphan*/);
scalar_t__ mq_getattr(int, struct mq_attr *);
int mq_open(char *, int, int, int /*<<< orphan*/ *);
int mq_receive(int, char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ mq_send(int, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  mqd_t msg_queue = mq_open("/CprE308-Queue", O_RDWR | O_CREAT,
                            S_IRUSR | S_IWUSR | S_IRGRP, NULL);
  if (msg_queue == -1) {
    perror("mq_open\n");
    return -1;
  }
  struct mq_attr attr;
  char *buf;
  if (mq_getattr(msg_queue, &attr)) {
    perror("mq_getattr\n");
    exit(-1);
  }
  buf = malloc(attr.mq_msgsize);
  if (buf == NULL) {
    perror("malloc");
    exit(-1);
  }
  sleep(10);
  ssize_t size;
  size = mq_receive(msg_queue, buf, attr.mq_msgsize, NULL);
  if (size == -1) {
    perror("mq_receive\n");
    exit(-1);
  }
  printf("Received message \"%s\"\n", buf);
  size = mq_receive(msg_queue, buf, attr.mq_msgsize, NULL);
  if (size == -1) {
    perror("mq_receive\n");
    exit(-1);
  }
  printf("Received message \"%s\"\n", buf);
  free(buf);
  char *my_string = "I am Clara";
  if (mq_send(msg_queue, my_string, strlen(my_string), 12)) {
    perror("mq_send\n");
    return -1;
  }
  char *my_string2 = "I am Amelia";
  if (mq_send(msg_queue, my_string2, strlen(my_string2), 20)) {
    perror("mq_send\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    0x2df0(%rip),%rax
or     (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
lea    0xe06(%rip),%rdi
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <mq_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1228 <main+0x78>
lea    0xdf8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d5 <main+0x225>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10d0 <mq_getattr@plt>
cmp    $0x0,%rax
je     1254 <main+0xa4>
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1281 <main+0xd1>
lea    0xdb2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <mq_receive@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12c1 <main+0x111>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xd6b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <mq_receive@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1307 <main+0x157>
lea    0xd33(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xd25(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xd27(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
mov    $0xc,%ecx
call   1060 <mq_send@plt>
cmp    $0x0,%rax
je     1377 <main+0x1c7>
lea    0xcf3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d5 <main+0x225>
lea    0xce4(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
mov    $0x14,%ecx
call   1060 <mq_send@plt>
cmp    $0x0,%rax
je     13ce <main+0x21e>
lea    0xc9c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d5 <main+0x225>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e26(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df4(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
lea    0xe19(%rip),%rdi
xor    %ecx,%ecx
call   1070 <mq_open@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x12f>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   10d0 <mq_getattr@plt>
test   %rax,%rax
jne    12fb <main+0x14b>
mov    (%rsp),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1304 <main+0x154>
mov    %rax,%rbx
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    (%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1090 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     12f2 <main+0x142>
lea    0xdee(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    (%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1090 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     12f2 <main+0x142>
lea    0xdc7(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdc7(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
mov    $0xc,%ecx
call   1060 <mq_send@plt>
test   %rax,%rax
jne    12d6 <main+0x126>
lea    0xdb6(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
mov    $0x14,%ecx
call   1060 <mq_send@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    12d6 <main+0x126>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd7c(%rip),%rdi
jmp    12e6 <main+0x136>
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12cf <main+0x11f>
lea    0xd32(%rip),%rdi
jmp    130b <main+0x15b>
lea    0xd16(%rip),%rdi
jmp    130b <main+0x15b>
lea    0xd19(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@.str = [15 x i8] c"/CprE308-Queue\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.1 = [9 x i8] c"mq_open\0A\00"
@.str.2 = [12 x i8] c"mq_getattr\0A\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [12 x i8] c"mq_receive\0A\00"
@.str.5 = [23 x i8] c"Received message \22%s\22\0A\00"
@.str.6 = [11 x i8] c"I am Clara\00"
@.str.7 = [9 x i8] c"mq_send\0A\00"
@.str.8 = [12 x i8] c"I am Amelia\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.mq_attr
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = call i32 @mq_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %19, i32* null)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %89
25: ; preds = %2
%26 = load i32, i32* %6
%27 = call i64 @mq_getattr(i32 %26, %struct.mq_attr* %7)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %25
%33 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %32
%42 = call i32 @sleep(i32 10)
%43 = load i32, i32* %6
%44 = load i8*, i8** %8
%45 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @mq_receive(i32 %43, i8* %44, i32 %46, i32* null)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %41
%54 = load i8*, i8** %8
%55 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %6
%57 = load i8*, i8** %8
%58 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @mq_receive(i32 %56, i8* %57, i32 %59, i32* null)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %53
%64 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %53
%67 = load i8*, i8** %8
%68 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %8
%70 = call i32 @free(i8* %69)
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8** %10
%71 = load i32, i32* %6
%72 = load i8*, i8** %10
%73 = load i8*, i8** %10
%74 = call i32 @strlen(i8* %73)
%75 = call i64 @mq_send(i32 %71, i8* %72, i32 %74, i32 12)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %66
%78 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %89
79: ; preds = %66
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8** %11
%80 = load i32, i32* %6
%81 = load i8*, i8** %11
%82 = load i8*, i8** %11
%83 = call i32 @strlen(i8* %82)
%84 = call i64 @mq_send(i32 %80, i8* %81, i32 %83, i32 20)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %79
%87 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %89
88: ; preds = %79
store i32 0, i32* %3
br label %89
89: ; preds = %88, %86, %77, %23
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@.str = [15 x i8] c"/CprE308-Queue\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.1 = [9 x i8] c"mq_open\0A\00"
@.str.2 = [12 x i8] c"mq_getattr\0A\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [12 x i8] c"mq_receive\0A\00"
@.str.5 = [23 x i8] c"Received message \22%s\22\0A\00"
@.str.6 = [11 x i8] c"I am Clara\00"
@.str.7 = [9 x i8] c"mq_send\0A\00"
@.str.8 = [12 x i8] c"I am Amelia\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.mq_attr
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = tail call i32 @mq_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %11, i32* null)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %61
16: ; preds = %2
%17 = bitcast %struct.mq_attr* %3 to i8*
%18 = call i64 @mq_getattr(i32 %12, %struct.mq_attr* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %16
%24 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i8* @malloc(i32 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %23
%32 = call i32 @sleep(i32 10)
%33 = load i32, i32* %24
%34 = call i32 @mq_receive(i32 %12, i8* %26, i32 %33, i32* null)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %31
%40 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %26)
%41 = load i32, i32* %24
%42 = call i32 @mq_receive(i32 %12, i8* %26, i32 %41, i32* null)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %39
%48 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %26)
%49 = call i32 @free(i8* %26)
%50 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%51 = call i64 @mq_send(i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %50, i32 12)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %47
%54 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%55 = call i64 @mq_send(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %54, i32 20)
%56 = icmp eq i64 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %53, %47
%58 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %59
59: ; preds = %57, %53
%60 = phi i32 [ 0, %53 ], [ -1, %57 ]
br label %61
61: ; preds = %59, %14
%62 = phi i32 [ -1, %14 ], [ %60, %59 ]
ret i32 %62
}