#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 {
  scalar_t__ mq_msgsize;
};
typedef int mqd_t;
typedef int mode_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mq_close(int);
int mq_getattr(int, struct mq_attr *);
int mq_open(char *, int, int, int /*<<< orphan*/ *);
int mq_receive(int, char *, size_t, unsigned int *);
int mq_send(int, char *, int, int /*<<< orphan*/);
int mq_unlink(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char buf[64] = {0};
  unsigned int prio = 0;
  int flags = O_CREAT | O_RDWR;
  mode_t perms = S_IRUSR | S_IWUSR;
  mqd_t mqd = 0;
  struct mq_attr attr_r;
  mqd = mq_open("/mq", flags, perms, NULL);
  if (mqd == -1)
    do {
      perror("Message queue create failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue created\n");
  if (mq_getattr(mqd, &attr_r) == -1)
    do {
      perror("Get attribute failed");
      exit(EXIT_FAILURE);
    } while (0);
  if (mq_send(mqd, "Hello POSIX message queue",
              sizeof("Hello POSIX message queue"), 0) == -1)
    do {
      perror("Send message failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Send message: %s\n", "Hello POSIX message queue");
  memset(buf, 0, sizeof(buf));
  if (mq_receive(mqd, buf, (size_t)attr_r.mq_msgsize, &prio) == -1)
    do {
      perror("Receive message failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Received message: %s\n", buf);
  if (mq_close(mqd) == -1)
    do {
      perror("Message queue close failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue closed\n");
  if (mq_unlink("/mq") == -1)
    do {
      perror("Message queue unlink failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue unlinked\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
movl   $0x0,-0x54(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x58(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    0x2de6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x58(%rbp),%esi
mov    -0x5c(%rbp),%edx
lea    0xdf5(%rip),%rdi
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <mq_open@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0xffffffff,-0x60(%rbp)
jne    1240 <main+0xa0>
jmp    1226 <main+0x86>
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x60(%rbp),%edi
lea    -0x68(%rbp),%rsi
call   10c0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    1282 <main+0xe2>
jmp    1268 <main+0xc8>
lea    0xdc8(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x60(%rbp),%edi
lea    0xdc0(%rip),%rsi
mov    $0x1a,%edx
xor    %ecx,%ecx
call   1060 <mq_send@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0x120>
jmp    12a6 <main+0x106>
lea    0xdb9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdb3(%rip),%rdi
lea    0xd7e(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    -0x60(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x68(%rbp),%rdx
lea    -0x54(%rbp),%rcx
call   1080 <mq_receive@plt>
cmp    $0xffffffff,%eax
jne    1321 <main+0x181>
jmp    1307 <main+0x167>
lea    0xd7e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x50(%rbp),%rsi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x60(%rbp),%edi
call   1050 <mq_close@plt>
cmp    $0xffffffff,%eax
jne    1363 <main+0x1c3>
jmp    1349 <main+0x1a9>
lea    0xd69(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xc88(%rip),%rdi
call   10b0 <mq_unlink@plt>
cmp    $0xffffffff,%eax
jne    13a5 <main+0x205>
jmp    138b <main+0x1eb>
lea    0xd58(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x4(%rsp)
mov    0x2e1b(%rip),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xdff(%rip),%rdi
xor    %ecx,%ecx
call   1080 <mq_open@plt>
cmp    $0xffffffff,%eax
je     12e6 <main+0x136>
mov    %eax,%ebx
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10d0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
je     12ef <main+0x13f>
lea    0xdfa(%rip),%rsi
mov    %ebx,%edi
mov    $0x1a,%edx
xor    %ecx,%ecx
call   1070 <mq_send@plt>
cmp    $0xffffffff,%eax
je     12f8 <main+0x148>
lea    0xe0a(%rip),%rdi
lea    0xdd5(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
mov    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     1301 <main+0x151>
lea    0xdf1(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <mq_close@plt>
cmp    $0xffffffff,%eax
je     130a <main+0x15a>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
lea    0xd3a(%rip),%rdi
call   10c0 <mq_unlink@plt>
cmp    $0xffffffff,%eax
je     1313 <main+0x163>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd17(%rip),%rdi
jmp    131a <main+0x16a>
lea    0xd2a(%rip),%rdi
jmp    131a <main+0x16a>
lea    0xd50(%rip),%rdi
jmp    131a <main+0x16a>
lea    0xd6d(%rip),%rdi
jmp    131a <main+0x16a>
lea    0xd91(%rip),%rdi
jmp    131a <main+0x16a>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.mq_attr = type { i64 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [4 x i8] c"/mq\00"
@.str.1 = [28 x i8] c"Message queue create failed\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [21 x i8] c"Get attribute failed\00"
@.str.4 = [26 x i8] c"Hello POSIX message queue\00"
@.str.5 = [20 x i8] c"Send message failed\00"
@.str.6 = [18 x i8] c"Send message: %s\0A\00"
@.str.7 = [23 x i8] c"Receive message failed\00"
@.str.8 = [22 x i8] c"Received message: %s\0A\00"
@.str.9 = [27 x i8] c"Message queue close failed\00"
@.str.11 = [28 x i8] c"Message queue unlink failed\00"
@str = [22 x i8] c"Message queue created\00"
@str.13 = [21 x i8] c"Message queue closed\00"
@str.14 = [23 x i8] c"Message queue unlinked\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = alloca %struct.mq_attr
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = bitcast %struct.mq_attr* %5 to i8*
%15 = tail call i32 @mq_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %13, i32* null)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%23 = call i32 @mq_getattr(i32 %15, %struct.mq_attr* %5)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = call i32 @mq_send(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 26, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %29
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @memset(i8* %6, i32 0, i32 64)
%39 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %5, i64 0, i32 0
%40 = load i64, i64* %39
%41 = call i32 @mq_receive(i32 %15, i8* %6, i64 %40, i32* %4)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %36
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i8* %6)
%49 = call i32 @mq_close(i32 %15)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %47
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.13, i64 0, i64 0))
%57 = call i32 @mq_unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %55
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.14, i64 0, i64 0))
ret i32 0
}