#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt(int, char **, char *);
void *malloc(int /*<<< orphan*/);
int mq_getattr(int, struct mq_attr *);
int mq_open(char *, int);
int mq_receive(int, void *, int /*<<< orphan*/, unsigned int *);
int optind;
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int c, flags;
  mqd_t mqd;
  ssize_t n;
  unsigned prio;
  void *buff;
  struct mq_attr attr;
  flags = O_RDWR;
  while (-1 != (c = getopt(argc, argv, "n"))) {
    switch (c) {
    case 'n':
      flags |= O_NONBLOCK;
      break;
    }
  }
  if (optind != argc - 1) {
    printf("usage: %s [-n] <name>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  mqd = mq_open(argv[optind], flags);
  if (-1 == mqd) {
    printf("mq_open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (-1 == mq_getattr(mqd, &attr)) {
    printf("mq_getattr error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  buff = malloc(attr.mq_msgsize);
  if (NULL == buff) {
    printf("malloc error\n");
    exit(EXIT_FAILURE);
  }
  if (-1 == (n = mq_receive(mqd, buff, attr.mq_msgsize, &prio))) {
    printf("mq_receive error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  printf("read %ld bytes, priority = %u\n", (long)n, prio);
  exit(EXIT_SUCCESS);
}


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    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe50(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     11ec <main+0x6c>
mov    -0x14(%rbp),%eax
sub    $0x6e,%eax
jne    11e7 <main+0x67>
jmp    11d8 <main+0x58>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a2 <main+0x22>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     1226 <main+0xa6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2da7(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%esi
call   1040 <mq_open@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    127e <main+0xfe>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <mq_getattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12c6 <main+0x146>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    12fa <main+0x17a>
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    -0x24(%rbp),%rcx
call   1060 <mq_receive@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    134c <main+0x1cc>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movslq -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xd0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c50(%rip),%rax
mov    (%rax),%edi
call   1080 <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    0x2e40(%rip),%rax
mov    (%rax),%r14d
lea    0xe4e(%rip),%r15
mov    0x2e27(%rip),%r12
jmp    11c0 <main+0x30>
or     (%r12),%r14d
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getopt@plt>
cmp    $0x6e,%eax
je     11bb <main+0x2b>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x30>
mov    0x2dfa(%rip),%rax
movslq (%rax),%rax
add    $0xffffffff,%ebp
cmp    %ebp,%eax
jne    1210 <main+0x80>
mov    (%rbx,%rax,8),%rdi
mov    %r14d,%esi
call   1050 <mq_open@plt>
cmp    $0xffffffff,%eax
jne    121c <main+0x8c>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0b(%rip),%rdi
jmp    128f <main+0xff>
mov    (%rbx),%rsi
lea    0xde8(%rip),%rdi
jmp    1292 <main+0x102>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    1246 <main+0xb6>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde8(%rip),%rdi
jmp    128f <main+0xff>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    1264 <main+0xd4>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
jmp    1299 <main+0x109>
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
jne    12a7 <main+0x117>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movslq %eax,%rsi
mov    0xc(%rsp),%edx
lea    0xda3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@O_RDWR = global i32 0
@.str = [2 x i8] c"n\00"
@O_NONBLOCK = global i32 0
@optind = global i32 0
@.str.1 = [23 x i8] c"usage: %s [-n] <name>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [19 x i8] c"mq_open error: %s\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"mq_getattr error: %s\0A\00"
@.str.4 = [14 x i8] c"malloc error\0A\00"
@.str.5 = [22 x i8] c"mq_receive error: %s\0A\00"
@.str.6 = [31 x i8] c"read %ld bytes, priority = %u\0A\00"
@EXIT_SUCCESS = global i32 0
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 i32
%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_RDWR
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 -1, %17
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 %38
31: ; preds = %26
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %26
%39 = load i8**, i8*** %5
%40 = load i32, i32* @optind
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = load i32, i32* %7
%45 = call i32 @mq_open(i8* %43, i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp eq i32 -1, %46
br i1 %47, label %48, label %54
48: ; preds = %38
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %38
%55 = load i32, i32* %8
%56 = call i32 @mq_getattr(i32 %55, %struct.mq_attr* %12)
%57 = icmp eq i32 -1, %56
br i1 %57, label %58, label %64
58: ; preds = %54
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %60)
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %54
%65 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %12, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i8* @malloc(i32 %66)
store i8* %67, i8** %11
%68 = load i8*, i8** %11
%69 = icmp eq i8* null, %68
br i1 %69, label %70, label %74
70: ; preds = %64
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %64
%75 = load i32, i32* %8
%76 = load i8*, i8** %11
%77 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %12, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i32 @mq_receive(i32 %75, i8* %76, i32 %78, i32* %10)
store i32 %79, i32* %9
%80 = icmp eq i32 -1, %79
br i1 %80, label %81, label %87
81: ; preds = %74
%82 = load i32, i32* @errno
%83 = call i8* @strerror(i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %83)
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %74
%88 = load i32, i32* %9
%89 = sext i32 %88 to i64
%90 = load i32, i32* %10
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i64 %89, i32 %90)
%92 = load i32, i32* @EXIT_SUCCESS
%93 = call i32 @exit(i32 %92)
unreachable
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@O_RDWR = global i32 0
@.str = [2 x i8] c"n\00"
@O_NONBLOCK = global i32 0
@optind = global i32 0
@.str.1 = [23 x i8] c"usage: %s [-n] <name>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [19 x i8] c"mq_open error: %s\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"mq_getattr error: %s\0A\00"
@.str.5 = [22 x i8] c"mq_receive error: %s\0A\00"
@.str.6 = [31 x i8] c"read %ld bytes, priority = %u\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [13 x i8] c"malloc error\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_RDWR
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 %24, label %19
19: ; preds = %15
%20 = load i8*, i8** %1
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = sext i32 %16 to i64
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i32 @mq_open(i8* %27, i32 %9)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %24
%37 = call i32 @mq_getattr(i32 %28, %struct.mq_attr* %4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %36
%46 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %4, i64 0, i32 0
%47 = load i32, i32* %46
%48 = call i8* @malloc(i32 %47)
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %45
%55 = call i32 @mq_receive(i32 %28, i8* %48, i32 %47, i32* %3)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %54
%64 = sext i32 %55 to i64
%65 = load i32, i32* %3
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i64 %64, i32 %65)
%67 = load i32, i32* @EXIT_SUCCESS
%68 = call i32 @exit(i32 %67)
unreachable
}