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

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int mq_open(char *, int, int, int /*<<< orphan*/ *);
int mq_receive(int, char *, int, unsigned int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  mqd_t mqd;
  char buffer[8192];
  int n;
  unsigned int prio;
  if (argc != 2) {
    fprintf(stderr, "usage: %s mq\n", argv[0]);
    exit(1);
  }
  mqd = mq_open(argv[1], O_RDONLY | O_CREAT, 0666, NULL);
  if (mqd == -1) {
    perror(argv[1]);
    exit(2);
  }
  n = mq_receive(mqd, buffer, 8192, &prio);
  if (n > 0) {
    buffer[n] = '\0';
    printf("[%d]%s\n", prio, buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a9 <main+0x49>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <mq_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f5 <main+0x95>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
lea    -0x2028(%rbp),%rcx
call   1060 <mq_receive@plt>
mov    %eax,-0x2024(%rbp)
cmpl   $0x0,-0x2024(%rbp)
jle    124b <main+0xeb>
movslq -0x2024(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
mov    -0x2028(%rbp),%esi
lea    -0x2020(%rbp),%rdx
lea    0xdc8(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x2010,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11d8 <main+0x78>
mov    0x8(%rbx),%rdi
mov    0x2e65(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1050 <mq_open@plt>
cmp    $0xffffffff,%eax
je     11fa <main+0x9a>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rcx
mov    %eax,%edi
mov    $0x2000,%edx
call   1060 <mq_receive@plt>
test   %eax,%eax
jle    11cd <main+0x6d>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0xc(%rsp),%esi
lea    0xe4b(%rip),%rdi
lea    0x10(%rsp),%rdx
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x2010,%rsp
pop    %rbx
ret
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe15(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x8(%rbx),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"usage: %s mq\0A\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [8 x i8] c"[%d]%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [8192 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = call i32 @mq_open(i8* %22, i32 %25, i32 438, i32* null)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %19
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @perror(i8* %32)
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %19
%36 = load i32, i32* %6
%37 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%38 = call i32 @mq_receive(i32 %36, i8* %37, i32 8192, i32* %9)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %35
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 %43
store i8 0, i8* %44
%45 = load i32, i32* %9
%46 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%47 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %45, i8* %46)
br label %48
48: ; preds = %41, %35
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"usage: %s mq\0A\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [8 x i8] c"[%d]%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = tail call i32 @mq_open(i8* %15, i32 %18, i32 438, i32* null)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i8*, i8** %14
%23 = tail call i32 @perror(i8* %22)
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %13
%26 = call i32 @mq_receive(i32 %19, i8* %5, i32 8192, i32* %4)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = zext i32 %26 to i64
%30 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %31, i8* %5)
br label %33
33: ; preds = %28, %25
ret i32 0
}