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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
void *malloc(int /*<<< orphan*/);
int mq_getattr(scalar_t__, struct mq_attr *);
scalar_t__ mq_open(char *, int);
int mq_receive(scalar_t__, void *, int /*<<< orphan*/, unsigned int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, unsigned int);
int write(int /*<<< orphan*/, char *, int);
int main() {
  int flags, opt;
  mqd_t mqd;
  unsigned int prio;
  void *buffer;
  struct mq_attr attr;
  ssize_t numRead;
  flags = O_RDONLY;
  prio = 60;
  mqd = mq_open("/mymq", flags);
  if (mqd == (mqd_t)-1)
    perror("mq_open");
  if (mq_getattr(mqd, &attr) == -1)
    perror("mq_getattr");
  buffer = malloc(attr.mq_msgsize);
  if (buffer == NULL)
    perror("malloc");
  numRead = mq_receive(mqd, buffer, attr.mq_msgsize, &prio);
  if (numRead == -1)
    perror("mq_receive");
  printf("Read %ld bytes; priority = %u\n", (long)numRead, prio);
  if (write(STDOUT_FILENO, buffer, numRead) == -1)
    perror("write");
  write(STDOUT_FILENO, "\n", 1);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
movl   $0x3c,-0x1c(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe54(%rip),%rdi
call   1050 <mq_open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11cc <main+0x4c>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    11ee <main+0x6e>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1211 <main+0x91>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    -0x1c(%rbp),%rcx
call   1070 <mq_receive@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    123e <main+0xbe>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
movslq -0x34(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xddf(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0xfb>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
movl   $0x3c,0xc(%rsp)
lea    0xe60(%rip),%rdi
call   1050 <mq_open@plt>
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
jne    11ba <main+0x3a>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    11d8 <main+0x58>
lea    0xe3b(%rip),%rdi
call   1080 <perror@plt>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fb <main+0x7b>
lea    0xe27(%rip),%rdi
call   1080 <perror@plt>
mov    0x8(%rsp),%ebp
lea    0xc(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <mq_receive@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1220 <main+0xa0>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
movslq %ebp,%rsi
mov    0xc(%rsp),%edx
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
mov    0x2db6(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0xd8>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
mov    (%r14),%edi
lea    0xde6(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [6 x i8] c"/mymq\00"
@.str.1 = [8 x i8] c"mq_open\00"
@.str.2 = [11 x i8] c"mq_getattr\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [11 x i8] c"mq_receive\00"
@.str.5 = [31 x i8] c"Read %ld bytes; priority = %u\0A\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [6 x i8] c"write\00"
@.str.7 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.mq_attr
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @O_RDONLY
store i32 %9, i32* %2
store i32 60, i32* %5
%10 = load i32, i32* %2
%11 = call i64 @mq_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %10)
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i64, i64* %4
%18 = call i32 @mq_getattr(i64 %17, %struct.mq_attr* %7)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %22
%31 = load i64, i64* %4
%32 = load i8*, i8** %6
%33 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @mq_receive(i64 %31, i8* %32, i32 %34, i32* %5)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %30
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = load i32, i32* %5
%44 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %42, i32 %43)
%45 = load i32, i32* @STDOUT_FILENO
%46 = load i8*, i8** %6
%47 = load i32, i32* %8
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %52
52: ; preds = %50, %40
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @write(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
%55 = load i32, i32* @EXIT_SUCCESS
%56 = call i32 @exit(i32 %55)
unreachable
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [6 x i8] c"/mymq\00"
@.str.1 = [8 x i8] c"mq_open\00"
@.str.2 = [11 x i8] c"mq_getattr\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [11 x i8] c"mq_receive\00"
@.str.5 = [31 x i8] c"Read %ld bytes; priority = %u\0A\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [6 x i8] c"write\00"
@.str.7 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.mq_attr
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.mq_attr* %2 to i8*
%5 = load i32, i32* @O_RDONLY
store i32 60, i32* %1
%6 = tail call i64 @mq_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = call i32 @mq_getattr(i64 %6, %struct.mq_attr* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i8* @malloc(i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* %16
br label %23
23: ; preds = %20, %15
%24 = phi i32 [ %22, %20 ], [ %17, %15 ]
%25 = call i32 @mq_receive(i64 %6, i8* %18, i32 %24, i32* %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = sext i32 %25 to i64
%31 = load i32, i32* %1
%32 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %30, i32 %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @write(i32 %33, i8* %18, i32 %25)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
%41 = load i32, i32* @EXIT_SUCCESS
%42 = call i32 @exit(i32 %41)
unreachable
}