#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ mqd_t;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt(int, char **, char *);
scalar_t__ malloc(int /*<<< orphan*/);
int /*<<< orphan*/ mq_getattr(int /*<<< orphan*/, struct mq_attr *);
int /*<<< orphan*/ mq_open(char *, int);
scalar_t__ mq_receive(int /*<<< orphan*/, char *, int /*<<< orphan*/,
                      unsigned int *);
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, long, unsigned int);
int main(int argc, char **argv) {
  int c, flags;
  mqd_t mqd;
  ssize_t n;
  unsigned int prio;
  char *buff;
  struct mq_attr attr;
  flags = O_RDONLY;
  while ((c = getopt(argc, argv, "n")) != -1) {
    switch (c) {
    case 'n':
      flags |= O_NONBLOCK;
      break;
    }
  }
  if (optind != argc - 1) {
    perror("mqreceive error!\n");
  }
  mqd = mq_open(argv[optind], flags);
  mq_getattr(mqd, &attr);
  buff = (char *)malloc(attr.mq_msgsize);
  n = mq_receive(mqd, buff, attr.mq_msgsize, &prio);
  printf("buff = %s, read %ld bytes, priority = %u\n", buff, (long)n, prio);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe50(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11e6 <main+0x66>
mov    -0x14(%rbp),%eax
sub    $0x6e,%eax
jne    11e1 <main+0x61>
jmp    11d2 <main+0x52>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a2 <main+0x22>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     1209 <main+0x89>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%rax
mov    0x2dc4(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%esi
call   1040 <mq_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10a0 <mq_getattr@plt>
mov    -0x40(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    -0x2c(%rbp),%rcx
call   1060 <mq_receive@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
lea    0xdad(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e48(%rip),%rax
mov    (%rax),%r14d
lea    0xe5e(%rip),%r15
mov    0x2e3f(%rip),%r12
jmp    11b0 <main+0x30>
or     (%r12),%r14d
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getopt@plt>
cmp    $0x6e,%eax
je     11ab <main+0x2b>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x30>
mov    0x2e0a(%rip),%r15
mov    (%r15),%eax
add    $0xffffffff,%ebp
cmp    %ebp,%eax
je     11e7 <main+0x67>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    (%r15),%eax
cltq
mov    (%rbx,%rax,8),%rdi
mov    %r14d,%esi
call   1040 <mq_open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <mq_getattr@plt>
mov    0x8(%rsp),%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    %rax,%rsi
call   1060 <mq_receive@plt>
mov    0xc(%rsp),%ecx
lea    0xde7(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [2 x i8] c"n\00"
@O_NONBLOCK = global i32 0
@optind = global i32 0
@.str.1 = [18 x i8] c"mqreceive error!\0A\00"
@.str.2 = [42 x i8] c"buff = %s, read %ld bytes, priority = %u\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 i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.mq_attr
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDONLY
store i32 %13, i32* %7
br label %14
14: ; preds = %25, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %6
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i32, i32* %6
switch i32 %20, label %25 [
i32 110, label %21
]
21: ; preds = %19
%22 = load i32, i32* @O_NONBLOCK
%23 = load i32, i32* %7
%24 = or i32 %23, %22
store i32 %24, i32* %7
br label %25
25: ; preds = %19, %21
br label %14
26: ; preds = %14
%27 = load i32, i32* @optind
%28 = load i32, i32* %4
%29 = sub nsw i32 %28, 1
%30 = icmp ne i32 %27, %29
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i8**, i8*** %5
%35 = load i32, i32* @optind
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
%39 = load i32, i32* %7
%40 = call i32 @mq_open(i8* %38, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = call i32 @mq_getattr(i32 %41, %struct.mq_attr* %12)
%43 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %12, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
store i8* %46, i8** %11
%47 = load i32, i32* %8
%48 = load i8*, i8** %11
%49 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %12, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i64 @mq_receive(i32 %47, i8* %48, i32 %50, i32* %10)
store i64 %51, i64* %9
%52 = load i8*, i8** %11
%53 = load i64, i64* %9
%54 = load i32, i32* %10
%55 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %52, i64 %53, i32 %54)
%56 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [2 x i8] c"n\00"
@O_NONBLOCK = global i32 0
@optind = global i32 0
@.str.1 = [18 x i8] c"mqreceive error!\0A\00"
@.str.2 = [42 x i8] c"buff = %s, read %ld bytes, priority = %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.mq_attr
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.mq_attr* %4 to i8*
%7 = load i32, i32* @O_RDONLY
br label %8
8: ; preds = %12, %2
%9 = phi i32 [ %14, %12 ], [ %7, %2 ]
br label %10
10: ; preds = %8, %10
%11 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %11, label %10 [
i32 -1, label %15
i32 110, label %12
]
12: ; preds = %10
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %9
br label %8
15: ; preds = %10
%16 = load i32, i32* @optind
%17 = add nsw i32 %0, -1
%18 = icmp eq i32 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @optind
br label %22
22: ; preds = %19, %15
%23 = phi i32 [ %21, %19 ], [ %16, %15 ]
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %1, i64 %24
%26 = load i8*, i8** %25
%27 = tail call i32 @mq_open(i8* %26, i32 %9)
%28 = call i32 @mq_getattr(i32 %27, %struct.mq_attr* %4)
%29 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
%33 = load i32, i32* %29
%34 = call i64 @mq_receive(i32 %27, i8* %32, i32 %33, i32* %3)
%35 = load i32, i32* %3
%36 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %32, i64 %34, i32 %35)
%37 = call i32 @exit(i32 0)
unreachable
}