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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
char *calloc(int, int /*<<< orphan*/);
int getopt(int, char **, char *);
scalar_t__ isascii(char);
int /*<<< orphan*/ mq_getattr(int, struct mq_attr *);
int mq_open(char *, int);
int mq_receive(int, char *, int /*<<< orphan*/, unsigned int *);
int /*<<< orphan*/ optarg;
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int strtoul(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char *path;
  int oflags = O_RDONLY;
  int quiet = 0;
  int count = 1;
  mqd_t mqd;
  char *msgptr;
  unsigned int msg_prio;
  int c, ret;
  struct mq_attr obuf;
  while (-1 != (c = getopt(argc, argv, "c:nq"))) {
    switch (c) {
    case 'c':
      count = strtoul(optarg, NULL, 0);
      break;
    case 'q':
      quiet = 1;
      break;
    case 'n':
      oflags |= O_NONBLOCK;
      break;
    default:
      return -1;
    }
  }
  if (optind < argc)
    path = argv[optind];
  else {
    printf("Queue pathname required\n");
    return -1;
  }
  mqd = mq_open(path, oflags);
  if (-1 != mqd) {
    if (!(mq_getattr(mqd, &obuf))) {
      msgptr = calloc(1, obuf.mq_msgsize);
      for (c = 1; c <= count; ++c) {
        ret = mq_receive(mqd, msgptr, obuf.mq_msgsize, &msg_prio);
        if (ret >= 0) {
          if (!quiet) {
            if (isascii(*msgptr))
              printf("%d: priority %ld  len %d text %-32.32s\n", c, msg_prio,
                     ret, msgptr);
            else
              printf("%d: priority %ld  len %d (nonascii)\n", c, msg_prio, ret);
          }
        } else {
          perror("mq_receive()");
          break;
        }
      }
    } else {
      perror("mq_getattr()");
      return -1;
    }
  } else
    perror("mq_open(O_WRONLY)");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe32(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1268 <main+0xd8>
mov    -0x38(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x63,%eax
je     121b <main+0x8b>
jmp    11f9 <main+0x69>
mov    -0x44(%rbp),%eax
sub    $0x6e,%eax
je     1243 <main+0xb3>
jmp    120a <main+0x7a>
mov    -0x44(%rbp),%eax
sub    $0x71,%eax
je     1237 <main+0xa7>
jmp    1257 <main+0xc7>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x24(%rbp)
jmp    1263 <main+0xd3>
movl   $0x1,-0x20(%rbp)
jmp    1263 <main+0xd3>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1263 <main+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <main+0x248>
jmp    11c0 <main+0x30>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jge    1295 <main+0x105>
mov    -0x10(%rbp),%rax
mov    0x2d4b(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
jmp    12af <main+0x11f>
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <main+0x248>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <mq_open@plt>
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
je     13cc <main+0x23c>
mov    -0x28(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10b0 <mq_getattr@plt>
cmp    $0x0,%eax
jne    13af <main+0x21f>
mov    -0x40(%rbp),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     13aa <main+0x21a>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    -0x34(%rbp),%rcx
call   1060 <mq_receive@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     1386 <main+0x1f6>
cmpl   $0x0,-0x20(%rbp)
jne    1381 <main+0x1f1>
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   10a0 <isascii@plt>
cmp    $0x0,%rax
je     1365 <main+0x1d5>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%r8
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    137c <main+0x1ec>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x3c(%rbp),%ecx
lea    0xcd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1381 <main+0x1f1>
jmp    1397 <main+0x207>
lea    0xcde(%rip),%rdi
call   1070 <perror@plt>
jmp    13aa <main+0x21a>
jmp    139c <main+0x20c>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12f9 <main+0x169>
jmp    13c7 <main+0x237>
lea    0xcc2(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <main+0x248>
jmp    13d8 <main+0x248>
lea    0xcb2(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e1e(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%r15d
lea    0xe30(%rip),%r12
mov    0x2e09(%rip),%r13
jmp    11e4 <main+0x44>
nopl   0x0(%rax)
or     0x0(%r13),%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1090 <getopt@plt>
cmp    $0x6e,%eax
je     11e0 <main+0x40>
cmp    $0xffffffff,%eax
je     122c <main+0x8c>
cmp    $0x71,%eax
je     1220 <main+0x80>
cmp    $0x63,%eax
jne    1329 <main+0x189>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %eax,%r15d
jmp    11e4 <main+0x44>
mov    $0x1,%eax
mov    %rax,0x10(%rsp)
jmp    11e4 <main+0x44>
mov    0x2d9d(%rip),%rax
movslq (%rax),%rax
cmp    %ebp,%eax
jge    131d <main+0x17d>
mov    (%r14,%rax,8),%rdi
mov    %ebx,%esi
call   1060 <mq_open@plt>
cmp    $0xffffffff,%eax
je     1346 <main+0x1a6>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <mq_getattr@plt>
test   %eax,%eax
jne    1356 <main+0x1b6>
mov    0x8(%rsp),%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
test   %r15d,%r15d
jle    132e <main+0x18e>
lea    0xc(%rsp),%rcx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <mq_receive@plt>
test   %eax,%eax
mov    0x10(%rsp),%r13
js     133d <main+0x19d>
mov    %eax,%r14d
mov    $0x1,%ebp
xchg   %ax,%ax
test   %r13d,%r13d
jne    12dc <main+0x13c>
movsbl (%rbx),%edi
call   10b0 <isascii@plt>
mov    0xc(%rsp),%edx
test   %rax,%rax
je     1301 <main+0x161>
lea    0xd38(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %ebp,%r15d
je     1319 <main+0x179>
add    $0x1,%ebp
mov    0x8(%rsp),%edx
mov    %r12d,%edi
mov    %rbx,%rsi
lea    0xc(%rsp),%rcx
call   1070 <mq_receive@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    12b0 <main+0x110>
jmp    133d <main+0x19d>
lea    0xd25(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %ebp,%r15d
jne    12e1 <main+0x141>
xor    %eax,%eax
jmp    132e <main+0x18e>
lea    0xd5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0e(%rip),%rdi
jmp    134d <main+0x1ad>
lea    0xd1f(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    132e <main+0x18e>
lea    0xd02(%rip),%rdi
call   1080 <perror@plt>
jmp    1329 <main+0x189>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"c:nq\00"
@optarg = global i32 0
@O_NONBLOCK = global i32 0
@optind = global i32 0
@.str.1 = [25 x i8] c"Queue pathname required\0A\00"
@.str.2 = [40 x i8] c"%d: priority %ld len %d text %-32.32s\0A\00"
@.str.3 = [37 x i8] c"%d: priority %ld len %d (nonascii)\0A\00"
@.str.4 = [13 x i8] c"mq_receive()\00"
@.str.5 = [13 x i8] c"mq_getattr()\00"
@.str.6 = [18 x i8] c"mq_open(O_WRONLY)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.mq_attr
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* @O_RDONLY
store i32 %16, i32* %7
store i32 0, i32* %8
store i32 1, i32* %9
br label %17
17: ; preds = %33, %2
%18 = load i32, i32* %4
%19 = load i8**, i8*** %5
%20 = call i32 @getopt(i32 %18, i8** %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %20, i32* %13
%21 = icmp ne i32 -1, %20
br i1 %21, label %22, label %34
22: ; preds = %17
%23 = load i32, i32* %13
switch i32 %23, label %32 [
i32 99, label %24
i32 113, label %27
i32 110, label %28
]
24: ; preds = %22
%25 = load i32, i32* @optarg
%26 = call i32 @strtoul(i32 %25, i32* null, i32 0)
store i32 %26, i32* %9
br label %33
27: ; preds = %22
store i32 1, i32* %8
br label %33
28: ; preds = %22
%29 = load i32, i32* @O_NONBLOCK
%30 = load i32, i32* %7
%31 = or i32 %30, %29
store i32 %31, i32* %7
br label %33
32: ; preds = %22
store i32 -1, i32* %3
br label %105
33: ; preds = %28, %27, %24
br label %17
34: ; preds = %17
%35 = load i32, i32* @optind
%36 = load i32, i32* %4
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %34
%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
store i8* %43, i8** %6
br label %46
44: ; preds = %34
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %105
46: ; preds = %38
%47 = load i8*, i8** %6
%48 = load i32, i32* %7
%49 = call i32 @mq_open(i8* %47, i32 %48)
store i32 %49, i32* %10
%50 = load i32, i32* %10
%51 = icmp ne i32 -1, %50
br i1 %51, label %52, label %103
52: ; preds = %46
%53 = load i32, i32* %10
%54 = call i32 @mq_getattr(i32 %53, %struct.mq_attr* %15)
%55 = icmp ne i32 %54, 0
br i1 %55, label %100, label %56
56: ; preds = %52
%57 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %15, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i8* @calloc(i32 1, i32 %58)
store i8* %59, i8** %11
store i32 1, i32* %13
br label %60
60: ; preds = %96, %56
%61 = load i32, i32* %13
%62 = load i32, i32* %9
%63 = icmp sle i32 %61, %62
br i1 %63, label %64, label %99
64: ; preds = %60
%65 = load i32, i32* %10
%66 = load i8*, i8** %11
%67 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %15, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @mq_receive(i32 %65, i8* %66, i32 %68, i32* %12)
store i32 %69, i32* %14
%70 = load i32, i32* %14
%71 = icmp sge i32 %70, 0
br i1 %71, label %72, label %93
72: ; preds = %64
%73 = load i32, i32* %8
%74 = icmp ne i32 %73, 0
br i1 %74, label %92, label %75
75: ; preds = %72
%76 = load i8*, i8** %11
%77 = load i8, i8* %76
%78 = call i64 @isascii(i8 signext %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %75
%81 = load i32, i32* %13
%82 = load i32, i32* %12
%83 = load i32, i32* %14
%84 = load i8*, i8** %11
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %81, i32 %82, i32 %83, i8* %84)
br label %91
86: ; preds = %75
%87 = load i32, i32* %13
%88 = load i32, i32* %12
%89 = load i32, i32* %14
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %87, i32 %88, i32 %89)
br label %91
91: ; preds = %86, %80
br label %92
92: ; preds = %91, %72
br label %95
93: ; preds = %64
%94 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %99
95: ; preds = %92
br label %96
96: ; preds = %95
%97 = load i32, i32* %13
%98 = add nsw i32 %97, 1
store i32 %98, i32* %13
br label %60
99: ; preds = %93, %60
br label %102
100: ; preds = %52
%101 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %105
102: ; preds = %99
br label %105
103: ; preds = %46
%104 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %105
105: ; preds = %32, %44, %100, %103, %102
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"c:nq\00"
@optarg = global i32 0
@O_NONBLOCK = global i32 0
@optind = global i32 0
@.str.2 = [40 x i8] c"%d: priority %ld len %d text %-32.32s\0A\00"
@.str.3 = [37 x i8] c"%d: priority %ld len %d (nonascii)\0A\00"
@.str.4 = [13 x i8] c"mq_receive()\00"
@.str.5 = [13 x i8] c"mq_getattr()\00"
@.str.6 = [18 x i8] c"mq_open(O_WRONLY)\00"
@str = [24 x i8] c"Queue pathname required\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.mq_attr
%5 = load i32, i32* @O_RDONLY
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.mq_attr* %4 to i8*
br label %8
8: ; preds = %18, %2
%9 = phi i32 [ %20, %18 ], [ 1, %2 ]
%10 = phi i32 [ %13, %18 ], [ 0, %2 ]
%11 = phi i32 [ %16, %18 ], [ %5, %2 ]
br label %12
12: ; preds = %15, %8
%13 = phi i32 [ %10, %8 ], [ 1, %15 ]
%14 = phi i32 [ %11, %8 ], [ %16, %15 ]
br label %15
15: ; preds = %12, %21
%16 = phi i32 [ %23, %21 ], [ %14, %12 ]
%17 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %17, label %72 [
i32 -1, label %24
i32 99, label %18
i32 113, label %12
i32 110, label %21
]
18: ; preds = %15
%19 = load i32, i32* @optarg
%20 = tail call i32 @strtoul(i32 %19, i32* null, i32 0)
br label %8
21: ; preds = %15
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %22, %16
br label %15
24: ; preds = %15
%25 = load i32, i32* @optind
%26 = icmp slt i32 %25, %0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = sext i32 %25 to i64
%29 = getelementptr inbounds i8*, i8** %1, i64 %28
%30 = load i8*, i8** %29
%31 = tail call i32 @mq_open(i8* %30, i32 %16)
%32 = icmp eq i32 %31, -1
br i1 %32, label %70, label %35
33: ; preds = %24
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %72
35: ; preds = %27
%36 = call i32 @mq_getattr(i32 %31, %struct.mq_attr* %4)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %68
38: ; preds = %35
%39 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %4, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i8* @calloc(i32 1, i32 %40)
%42 = icmp slt i32 %9, 1
br i1 %42, label %72, label %43
43: ; preds = %38
%44 = icmp eq i32 %13, 0
%45 = call i32 @mq_receive(i32 %31, i8* %41, i32 %40, i32* %3)
%46 = icmp sgt i32 %45, -1
br i1 %46, label %47, label %59
47: ; preds = %43, %63
%48 = phi i32 [ %66, %63 ], [ %45, %43 ]
%49 = phi i32 [ %64, %63 ], [ 1, %43 ]
br i1 %44, label %50, label %61
50: ; preds = %47
%51 = load i8, i8* %41
%52 = call i64 @isascii(i8 signext %51)
%53 = icmp eq i64 %52, 0
%54 = load i32, i32* %3
br i1 %53, label %57, label %55
55: ; preds = %50
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %54, i32 %48, i8* %41)
br label %61
57: ; preds = %50
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 %54, i32 %48)
br label %61
59: ; preds = %63, %43
%60 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %72
61: ; preds = %55, %57, %47
%62 = icmp eq i32 %49, %9
br i1 %62, label %72, label %63
63: ; preds = %61
%64 = add nuw i32 %49, 1
%65 = load i32, i32* %39
%66 = call i32 @mq_receive(i32 %31, i8* %41, i32 %65, i32* %3)
%67 = icmp sgt i32 %66, -1
br i1 %67, label %47, label %59
68: ; preds = %35
%69 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %72
70: ; preds = %27
%71 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %72
72: ; preds = %15, %61, %38, %70, %59, %68, %33
%73 = phi i32 [ -1, %68 ], [ -1, %33 ], [ 0, %59 ], [ 0, %70 ], [ 0, %38 ], [ 0, %61 ], [ -1, %15 ]
ret i32 %73
}