#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 mq_msgsize;
  int mq_maxmsg;
  int mq_flags;
  int mq_curmsgs;
};
typedef int mqd_t;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_RDWR;
int getopt(int, char **, char *);
int /*<<< orphan*/ mq_getattr(int, struct mq_attr *);
int mq_open(char *, int, int, struct mq_attr *);
int /*<<< orphan*/ optarg;
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strtoul(int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/);
int main(int argc, char **argv) {
  int perms = 0600;
  int oflags = O_RDWR;
  int rd = 0, wr = 0;
  mqd_t mqd;
  int c;
  char *path;
  struct mq_attr buf;
  buf.mq_msgsize = 1024;
  buf.mq_maxmsg = 16;
  while (-1 != (c = getopt(argc, argv, "p:b:m:cx"))) {
    switch (c) {
    case 'p':
      perms = (int)strtoul(optarg, NULL, 0);
      break;
    case 'b':
      buf.mq_msgsize = (int)strtoul(optarg, NULL, 0);
      break;
    case 'm':
      buf.mq_maxmsg = (int)strtoul(optarg, NULL, 0);
      break;
    case 'c':
      oflags |= O_CREAT;
      break;
    case 'x':
      oflags |= O_EXCL;
      break;
    default:
      return -1;
    }
  }
  if (optind < argc)
    path = argv[optind];
  else {
    printf("Queue pathname required\n");
    return -1;
  }
  mqd = mq_open(path, oflags, perms, &buf);
  if (-1 != mqd) {
    if (!mq_getattr(mqd, &buf)) {
      printf("flags: 0x%x  maxmsg: %d  msgsize: %d  curmsgs: %d\n",
             buf.mq_flags, buf.mq_maxmsg, buf.mq_msgsize, buf.mq_curmsgs);
    } else
      perror("mq_getattr()");
  } else
    perror("mq_open()");
}


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)
movl   $0x180,-0x14(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x400,-0x40(%rbp)
movl   $0x10,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xea9(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1280 <main+0x120>
mov    -0x28(%rbp),%eax
add    $0xffffff9e,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x16,%eax
ja     126f <main+0x10f>
mov    -0x48(%rbp),%rax
lea    0xe16(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x14(%rbp)
jmp    127b <main+0x11b>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x40(%rbp)
jmp    127b <main+0x11b>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x3c(%rbp)
jmp    127b <main+0x11b>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    127b <main+0x11b>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    127b <main+0x11b>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <main+0x1dd>
jmp    11a5 <main+0x45>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jge    12ad <main+0x14d>
mov    -0x10(%rbp),%rax
mov    0x2d33(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
jmp    12c7 <main+0x167>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <main+0x1dd>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    -0x40(%rbp),%rcx
call   1040 <mq_open@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
je     1331 <main+0x1d1>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1080 <mq_getattr@plt>
cmp    $0x0,%eax
jne    1320 <main+0x1c0>
mov    -0x38(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    132c <main+0x1cc>
lea    0xd8a(%rip),%rdi
call   1050 <perror@plt>
jmp    133d <main+0x1dd>
lea    0xd86(%rip),%rdi
call   1050 <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    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    $0x180,%r15d
mov    $0x10,%eax
mov    %rax,0x20(%rsp)
mov    $0x400,%eax
mov    %rax,0x18(%rsp)
lea    0xeab(%rip),%r12
mov    0x2e38(%rip),%r13
lea    0xe41(%rip),%r14
jmp    11e0 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <getopt@plt>
cmp    $0x70,%eax
je     11d0 <main+0x60>
lea    -0x62(%rax),%ecx
cmp    $0x16,%ecx
ja     124b <main+0xdb>
movslq (%r14,%rcx,4),%rax
add    %r14,%rax
jmp    *%rax
mov    0x0(%r13),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,0x18(%rsp)
jmp    11e0 <main+0x70>
mov    0x4(%rsp),%eax
mov    0x2dd6(%rip),%rcx
jmp    1243 <main+0xd3>
mov    0x0(%r13),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,0x20(%rsp)
jmp    11e0 <main+0x70>
mov    0x4(%rsp),%eax
mov    0x2d7d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x4(%rsp)
jmp    11e0 <main+0x70>
cmp    $0xffffffff,%eax
jne    12c9 <main+0x159>
mov    0x20(%rsp),%rax
mov    %eax,0xc(%rsp)
mov    0x18(%rsp),%rax
mov    %eax,0x8(%rsp)
mov    0x2d67(%rip),%rax
movslq (%rax),%rax
cmp    %ebp,%eax
jge    12bd <main+0x14d>
mov    (%rbx,%rax,8),%rdi
lea    0x8(%rsp),%rcx
mov    0x4(%rsp),%esi
mov    %r15d,%edx
call   1050 <mq_open@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x16f>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <mq_getattr@plt>
test   %eax,%eax
jne    12e8 <main+0x178>
mov    0x10(%rsp),%esi
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
mov    0x14(%rsp),%r8d
lea    0xdb3(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12ce <main+0x15e>
lea    0xdeb(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbf(%rip),%rdi
jmp    12ef <main+0x17f>
lea    0xda9(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    12ce <main+0x15e>

LLVM IR (-O0):
%struct.mq_attr = type { i32, i32, i32, i32 }
@O_RDWR = global i32 0
@.str = [9 x i8] c"p:b:m:cx\00"
@optarg = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@optind = global i32 0
@.str.1 = [25 x i8] c"Queue pathname required\0A\00"
@.str.2 = [51 x i8] c"flags: 0x%x maxmsg: %d msgsize: %d curmsgs: %d\0A\00"
@.str.3 = [13 x i8] c"mq_getattr()\00"
@.str.4 = [10 x i8] c"mq_open()\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 i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca %struct.mq_attr
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 384, i32* %6
%14 = load i32, i32* @O_RDWR
store i32 %14, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%15 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 0
store i32 1024, i32* %15
%16 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 1
store i32 16, i32* %16
br label %17
17: ; preds = %47, %2
%18 = load i32, i32* %4
%19 = load i8**, i8*** %5
%20 = call i32 @getopt(i32 %18, i8** %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %20, i32* %11
%21 = icmp ne i32 -1, %20
br i1 %21, label %22, label %48
22: ; preds = %17
%23 = load i32, i32* %11
switch i32 %23, label %46 [
i32 112, label %24
i32 98, label %28
i32 109, label %33
i32 99, label %38
i32 120, label %42
]
24: ; preds = %22
%25 = load i32, i32* @optarg
%26 = call i64 @strtoul(i32 %25, i32* null, i32 0)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %6
br label %47
28: ; preds = %22
%29 = load i32, i32* @optarg
%30 = call i64 @strtoul(i32 %29, i32* null, i32 0)
%31 = trunc i64 %30 to i32
%32 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 0
store i32 %31, i32* %32
br label %47
33: ; preds = %22
%34 = load i32, i32* @optarg
%35 = call i64 @strtoul(i32 %34, i32* null, i32 0)
%36 = trunc i64 %35 to i32
%37 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 1
store i32 %36, i32* %37
br label %47
38: ; preds = %22
%39 = load i32, i32* @O_CREAT
%40 = load i32, i32* %7
%41 = or i32 %40, %39
store i32 %41, i32* %7
br label %47
42: ; preds = %22
%43 = load i32, i32* @O_EXCL
%44 = load i32, i32* %7
%45 = or i32 %44, %43
store i32 %45, i32* %7
br label %47
46: ; preds = %22
store i32 -1, i32* %3
br label %86
47: ; preds = %42, %38, %33, %28, %24
br label %17
48: ; preds = %17
%49 = load i32, i32* @optind
%50 = load i32, i32* %4
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %58
52: ; preds = %48
%53 = load i8**, i8*** %5
%54 = load i32, i32* @optind
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
store i8* %57, i8** %12
br label %60
58: ; preds = %48
%59 = 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 %86
60: ; preds = %52
%61 = load i8*, i8** %12
%62 = load i32, i32* %7
%63 = load i32, i32* %6
%64 = call i32 @mq_open(i8* %61, i32 %62, i32 %63, %struct.mq_attr* %13)
store i32 %64, i32* %10
%65 = load i32, i32* %10
%66 = icmp ne i32 -1, %65
br i1 %66, label %67, label %84
67: ; preds = %60
%68 = load i32, i32* %10
%69 = call i32 @mq_getattr(i32 %68, %struct.mq_attr* %13)
%70 = icmp ne i32 %69, 0
br i1 %70, label %81, label %71
71: ; preds = %67
%72 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 2
%73 = load i32, i32* %72
%74 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 1
%75 = load i32, i32* %74
%76 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 0
%77 = load i32, i32* %76
%78 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %13, i32 0, i32 3
%79 = load i32, i32* %78
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %73, i32 %75, i32 %77, i32 %79)
br label %83
81: ; preds = %67
%82 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %83
83: ; preds = %81, %71
br label %86
84: ; preds = %60
%85 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %86
86: ; preds = %46, %58, %84, %83
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.mq_attr = type { i32, i32, i32, i32 }
@O_RDWR = global i32 0
@.str = [9 x i8] c"p:b:m:cx\00"
@optarg = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@optind = global i32 0
@.str.2 = [51 x i8] c"flags: 0x%x maxmsg: %d msgsize: %d curmsgs: %d\0A\00"
@.str.3 = [13 x i8] c"mq_getattr()\00"
@.str.4 = [10 x i8] c"mq_open()\00"
@str = [24 x i8] c"Queue pathname required\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.mq_attr
%4 = load i32, i32* @O_RDWR
%5 = bitcast %struct.mq_attr* %3 to i8*
%6 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 0
%7 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 1
br label %8
8: ; preds = %27, %2
%9 = phi i32 [ %30, %27 ], [ 1024, %2 ]
%10 = phi i32 [ %14, %27 ], [ 16, %2 ]
%11 = phi i32 [ %18, %27 ], [ %4, %2 ]
%12 = phi i32 [ %21, %27 ], [ 384, %2 ]
br label %13
13: ; preds = %8, %31
%14 = phi i32 [ %10, %8 ], [ %34, %31 ]
%15 = phi i32 [ %11, %8 ], [ %18, %31 ]
%16 = phi i32 [ %12, %8 ], [ %21, %31 ]
br label %17
17: ; preds = %36, %13
%18 = phi i32 [ %15, %13 ], [ %39, %36 ]
%19 = phi i32 [ %16, %13 ], [ %21, %36 ]
br label %20
20: ; preds = %17, %23
%21 = phi i32 [ %26, %23 ], [ %19, %17 ]
%22 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
switch i32 %22, label %66 [
i32 -1, label %40
i32 112, label %23
i32 98, label %27
i32 109, label %31
i32 99, label %36
i32 120, label %35
]
23: ; preds = %20
%24 = load i32, i32* @optarg
%25 = tail call i64 @strtoul(i32 %24, i32* null, i32 0)
%26 = trunc i64 %25 to i32
br label %20
27: ; preds = %20
%28 = load i32, i32* @optarg
%29 = tail call i64 @strtoul(i32 %28, i32* null, i32 0)
%30 = trunc i64 %29 to i32
br label %8
31: ; preds = %20
%32 = load i32, i32* @optarg
%33 = tail call i64 @strtoul(i32 %32, i32* null, i32 0)
%34 = trunc i64 %33 to i32
br label %13
35: ; preds = %20
br label %36
36: ; preds = %20, %35
%37 = phi i32* [ @O_EXCL, %35 ], [ @O_CREAT, %20 ]
%38 = load i32, i32* %37
%39 = or i32 %38, %18
br label %17
40: ; preds = %20
store i32 %14, i32* %7
store i32 %9, i32* %6
%41 = load i32, i32* @optind
%42 = icmp slt i32 %41, %0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = sext i32 %41 to i64
%45 = getelementptr inbounds i8*, i8** %1, i64 %44
%46 = load i8*, i8** %45
%47 = call i32 @mq_open(i8* %46, i32 %18, i32 %21, %struct.mq_attr* %3)
%48 = icmp eq i32 %47, -1
br i1 %48, label %64, label %51
49: ; preds = %40
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %66
51: ; preds = %43
%52 = call i32 @mq_getattr(i32 %47, %struct.mq_attr* %3)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 2
%56 = load i32, i32* %55
%57 = load i32, i32* %7
%58 = load i32, i32* %6
%59 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 3
%60 = load i32, i32* %59
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %57, i32 %58, i32 %60)
br label %66
62: ; preds = %51
%63 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %66
64: ; preds = %43
%65 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %20, %64, %62, %54, %49
%67 = phi i32 [ -1, %49 ], [ 0, %54 ], [ 0, %62 ], [ 0, %64 ], [ -1, %20 ]
ret i32 %67
}