#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_maxmsg;
  int mq_msgsize;
  int /*<<< orphan*/ mq_flags;
};
typedef int mqd_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int /*<<< orphan*/ O_NONBLOCK;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ dump_attribute(struct mq_attr *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int mq_close(int);
int mq_getattr(int, struct mq_attr *);
int mq_open(char *, int, int, struct mq_attr *);
int mq_setattr(int, struct mq_attr *, int /*<<< orphan*/ *);
int mq_unlink(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  int flags = O_CREAT | O_RDWR;
  mode_t perms = S_IRUSR | S_IWUSR;
  mqd_t mqd = 0;
  struct mq_attr attr = {.mq_maxmsg = 10, .mq_msgsize = 1024};
  struct mq_attr attr_r;
  mqd = mq_open("/mq", flags, perms, &attr);
  if (mqd == -1)
    do {
      perror("Message queue create failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue created\n");
  if (mq_getattr(mqd, &attr_r) == -1)
    do {
      perror("Get attribute failed");
      exit(EXIT_FAILURE);
    } while (0);
  dump_attribute(&attr_r);
  attr.mq_flags |= O_NONBLOCK;
  if (mq_setattr(mqd, &attr, NULL) == -1)
    do {
      perror("Set attribute failed");
      exit(EXIT_FAILURE);
    } while (0);
  if (mq_getattr(mqd, &attr_r) == -1)
    do {
      perror("Get attribute failed");
      exit(EXIT_FAILURE);
    } while (0);
  dump_attribute(&attr_r);
  if (mq_close(mqd) == -1)
    do {
      perror("Message queue close failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue closed\n");
  if (mq_unlink("/mq") == -1)
    do {
      perror("Message queue unlink failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue unlinked\n");
  return 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    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0xe22(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe20(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe14(%rip),%rdi
lea    -0x28(%rbp),%rcx
call   1060 <mq_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    122d <main+0x9d>
jmp    1213 <main+0x83>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdf8(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    126d <main+0xdd>
jmp    1253 <main+0xc3>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x38(%rbp),%rdi
call   10b0 <dump_attribute@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <mq_setattr@plt>
cmp    $0xffffffff,%eax
jne    12bd <main+0x12d>
jmp    12a3 <main+0x113>
lea    0xdae(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    12f1 <main+0x161>
jmp    12d7 <main+0x147>
lea    0xd65(%rip),%rdi
call   1070 <perror@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x38(%rbp),%rdi
call   10b0 <dump_attribute@plt>
mov    -0x1c(%rbp),%edi
call   1050 <mq_close@plt>
cmp    $0xffffffff,%eax
jne    132a <main+0x19a>
jmp    1310 <main+0x180>
lea    0xd56(%rip),%rdi
call   1070 <perror@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xd57(%rip),%rdi
call   1030 <printf@plt>
lea    0xccf(%rip),%rdi
call   1090 <mq_unlink@plt>
cmp    $0xffffffff,%eax
jne    136a <main+0x1da>
jmp    1350 <main+0x1c0>
lea    0xd47(%rip),%rdi
call   1070 <perror@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xd49(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e5c(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0xe49(%rip),%eax
mov    %eax,0x8(%rsp)
mov    0xe36(%rip),%rax
mov    %rax,(%rsp)
lea    0xe37(%rip),%rdi
mov    %rsp,%rcx
call   1060 <mq_open@plt>
cmp    $0xffffffff,%eax
je     1292 <main+0x102>
mov    %eax,%ebx
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
je     1289 <main+0xf9>
lea    0x10(%rsp),%rdi
call   10b0 <dump_attribute@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <mq_setattr@plt>
cmp    $0xffffffff,%eax
je     129b <main+0x10b>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
je     1289 <main+0xf9>
lea    0x10(%rsp),%rdi
call   10b0 <dump_attribute@plt>
mov    %ebx,%edi
call   1050 <mq_close@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0x114>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
lea    0xda1(%rip),%rdi
call   1090 <mq_unlink@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0x11d>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xd9c(%rip),%rdi
jmp    12b4 <main+0x124>
lea    0xd77(%rip),%rdi
jmp    12b4 <main+0x124>
lea    0xd9f(%rip),%rdi
jmp    12b4 <main+0x124>
lea    0xdab(%rip),%rdi
jmp    12b4 <main+0x124>
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32, i32, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@__const.main.attr = %struct.mq_attr { i32 10, i32 1024, i32 0 }
@.str = [4 x i8] c"/mq\00"
@.str.1 = [28 x i8] c"Message queue create failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [23 x i8] c"Message queue created\0A\00"
@.str.3 = [21 x i8] c"Get attribute failed\00"
@O_NONBLOCK = global i32 0
@.str.4 = [21 x i8] c"Set attribute failed\00"
@.str.5 = [27 x i8] c"Message queue close failed\00"
@.str.6 = [22 x i8] c"Message queue closed\0A\00"
@.str.7 = [28 x i8] c"Message queue unlink failed\00"
@.str.8 = [24 x i8] c"Message queue unlinked\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 %struct.mq_attr
%10 = alloca %struct.mq_attr
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
store i32 %13, i32* %6
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
store i32 %16, i32* %7
store i32 0, i32* %8
%17 = bitcast %struct.mq_attr* %9 to i8*
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = call i32 @mq_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %19, %struct.mq_attr* %9)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %2
br label %24
24: ; preds = %23
%25 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; No predecessors!
br label %29
29: ; preds = %28, %2
%30 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %8
%32 = call i32 @mq_getattr(i32 %31, %struct.mq_attr* %10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %29
br label %35
35: ; preds = %34
%36 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; No predecessors!
br label %40
40: ; preds = %39, %29
%41 = call i32 @dump_attribute(%struct.mq_attr* %10)
%42 = load i32, i32* @O_NONBLOCK
%43 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %9, i32 0, i32 2
%44 = load i32, i32* %43
%45 = or i32 %44, %42
store i32 %45, i32* %43
%46 = load i32, i32* %8
%47 = call i32 @mq_setattr(i32 %46, %struct.mq_attr* %9, i32* null)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %40
br label %50
50: ; preds = %49
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; No predecessors!
br label %55
55: ; preds = %54, %40
%56 = load i32, i32* %8
%57 = call i32 @mq_getattr(i32 %56, %struct.mq_attr* %10)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %65
59: ; preds = %55
br label %60
60: ; preds = %59
%61 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; No predecessors!
br label %65
65: ; preds = %64, %55
%66 = call i32 @dump_attribute(%struct.mq_attr* %10)
%67 = load i32, i32* %8
%68 = call i32 @mq_close(i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %76
70: ; preds = %65
br label %71
71: ; preds = %70
%72 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; No predecessors!
br label %76
76: ; preds = %75, %65
%77 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%78 = call i32 @mq_unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %86
80: ; preds = %76
br label %81
81: ; preds = %80
%82 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
%83 = load i32, i32* @EXIT_FAILURE
%84 = call i32 @exit(i32 %83)
unreachable
85: ; No predecessors!
br label %86
86: ; preds = %85, %76
%87 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.mq_attr = type { i32, i32, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@__const.main.attr = %struct.mq_attr { i32 10, i32 1024, i32 0 }
@.str = [4 x i8] c"/mq\00"
@.str.1 = [28 x i8] c"Message queue create failed\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [21 x i8] c"Get attribute failed\00"
@O_NONBLOCK = global i32 0
@.str.4 = [21 x i8] c"Set attribute failed\00"
@.str.5 = [27 x i8] c"Message queue close failed\00"
@.str.7 = [28 x i8] c"Message queue unlink failed\00"
@str = [22 x i8] c"Message queue created\00"
@str.9 = [21 x i8] c"Message queue closed\00"
@str.10 = [23 x i8] c"Message queue unlinked\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.mq_attr
%4 = alloca %struct.mq_attr
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = bitcast %struct.mq_attr* %3 to i8*
%12 = bitcast %struct.mq_attr* %4 to i8*
%13 = call i32 @mq_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10, %struct.mq_attr* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%21 = call i32 @mq_getattr(i32 %13, %struct.mq_attr* %4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = call i32 @dump_attribute(%struct.mq_attr* %4)
%29 = load i32, i32* @O_NONBLOCK
%30 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 2
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = call i32 @mq_setattr(i32 %13, %struct.mq_attr* %3, i32* null)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %27
%40 = call i32 @mq_getattr(i32 %13, %struct.mq_attr* %4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %39
%47 = call i32 @dump_attribute(%struct.mq_attr* %4)
%48 = call i32 @mq_close(i32 %13)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %46
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%56 = call i32 @mq_unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %54
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
ret i32 0
}