#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 sigevent {
  int /*<<< orphan*/ sigev_signo;
  int /*<<< orphan*/ sigev_notify;
};
struct mq_attr {
  int mq_maxmsg;
  int mq_msgsize;
};
typedef scalar_t__ mqd_t;

/* Variables and functions */
scalar_t__ EBUSY;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ SIGRTMIN;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int mq_close(scalar_t__);
int mq_getattr(scalar_t__, struct mq_attr *);
int mq_notify(scalar_t__, struct sigevent *);
scalar_t__ mq_open(char *, int, int, struct mq_attr *);
int mq_unlink(char *);
int main() {
  struct mq_attr attr, attr2;
  struct sigevent sigev;
  mqd_t mq;
  int status;
  attr.mq_maxmsg = 2;
  attr.mq_msgsize = 100;
  mq = mq_open("/mytstqueue1", O_CREAT | O_RDWR | O_EXCL, 0666, &attr);
  if (mq == (mqd_t)-1)
    err(1, "mq_open");
  status = mq_unlink("/mytstqueue1");
  if (status)
    err(1, "mq_unlink");
  status = mq_getattr(mq, &attr2);
  if (status)
    err(1, "mq_getattr");
  if (attr.mq_maxmsg != attr2.mq_maxmsg)
    err(1, "mq_maxmsg changed");
  if (attr.mq_msgsize != attr2.mq_msgsize)
    err(1, "mq_msgsize changed");
  sigev.sigev_notify = SIGEV_SIGNAL;
  sigev.sigev_signo = SIGRTMIN;
  status = mq_notify(mq, &sigev);
  if (status)
    err(1, "mq_notify");
  status = mq_notify(mq, &sigev);
  if (status == 0)
    err(1, "mq_notify 2");
  else if (errno != EBUSY)
    err(1, "mq_notify 3");
  status = mq_notify(mq, NULL);
  if (status)
    err(1, "mq_notify 4");
  status = mq_close(mq);
  if (status)
    err(1, "mq_close");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0x10(%rbp)
movl   $0x64,-0xc(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
mov    $0x1b6,%edx
lea    -0x10(%rbp),%rcx
call   1040 <mq_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11cd <main+0x6d>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
call   1050 <err@plt>
lea    0xe2c(%rip),%rdi
call   1070 <mq_unlink@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11f7 <main+0x97>
mov    $0x1,%edi
lea    0xe23(%rip),%rsi
call   1050 <err@plt>
mov    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <mq_getattr@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1222 <main+0xc2>
mov    $0x1,%edi
lea    0xe02(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     123f <main+0xdf>
mov    $0x1,%edi
lea    0xdf0(%rip),%rsi
call   1050 <err@plt>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     125c <main+0xfc>
mov    $0x1,%edi
lea    0xde5(%rip),%rsi
call   1050 <err@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <mq_notify@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     129f <main+0x13f>
mov    $0x1,%edi
lea    0xdb5(%rip),%rsi
call   1050 <err@plt>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <mq_notify@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12cf <main+0x16f>
mov    $0x1,%edi
lea    0xd94(%rip),%rsi
call   1050 <err@plt>
jmp    12ff <main+0x19f>
mov    0x2ce2(%rip),%rax
mov    (%rax),%rax
mov    0x2cd0(%rip),%rcx
cmp    (%rcx),%rax
je     12fa <main+0x19a>
mov    $0x1,%edi
lea    0xd70(%rip),%rsi
call   1050 <err@plt>
jmp    12ff <main+0x19f>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <mq_notify@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     132a <main+0x1ca>
mov    $0x1,%edi
lea    0xd4c(%rip),%rsi
call   1050 <err@plt>
mov    -0x28(%rbp),%rdi
call   1030 <mq_close@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1351 <main+0x1f1>
mov    $0x1,%edi
lea    0xd31(%rip),%rsi
call   1050 <err@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e8c(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movabs $0x6400000002,%rax
mov    %rax,0x10(%rsp)
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
lea    0xe6a(%rip),%rdi
lea    0x10(%rsp),%rcx
mov    $0x1b6,%edx
call   1040 <mq_open@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11bf <main+0x5f>
lea    0xe58(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
lea    0xe3a(%rip),%rdi
call   1070 <mq_unlink@plt>
test   %eax,%eax
je     11e0 <main+0x80>
lea    0xe3f(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
lea    0x18(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <mq_getattr@plt>
test   %eax,%eax
je     1202 <main+0xa2>
lea    0xe27(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0x10(%rsp),%eax
cmp    0x18(%rsp),%eax
je     121d <main+0xbd>
lea    0xe17(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0x14(%rsp),%eax
cmp    0x1c(%rsp),%eax
je     1238 <main+0xd8>
lea    0xe0e(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <mq_notify@plt>
test   %eax,%eax
je     1274 <main+0x114>
lea    0xde5(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <mq_notify@plt>
test   %eax,%eax
je     12a4 <main+0x144>
mov    0x2d2c(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
je     12b5 <main+0x155>
lea    0xdc3(%rip),%rsi
jmp    12ab <main+0x14b>
lea    0xdae(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <mq_notify@plt>
test   %eax,%eax
je     12d4 <main+0x174>
lea    0xda7(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %rbx,%rdi
call   1030 <mq_close@plt>
test   %eax,%eax
je     12f1 <main+0x191>
lea    0xd96(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.mq_attr = type { i32, i32 }
%struct.sigevent = type { i32, i32 }
@.str = [13 x i8] c"/mytstqueue1\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@.str.1 = [8 x i8] c"mq_open\00"
@.str.2 = [10 x i8] c"mq_unlink\00"
@.str.3 = [11 x i8] c"mq_getattr\00"
@.str.4 = [18 x i8] c"mq_maxmsg changed\00"
@.str.5 = [19 x i8] c"mq_msgsize changed\00"
@SIGEV_SIGNAL = global i32 0
@SIGRTMIN = global i32 0
@.str.6 = [10 x i8] c"mq_notify\00"
@.str.7 = [12 x i8] c"mq_notify 2\00"
@errno = global i64 0
@EBUSY = global i64 0
@.str.8 = [12 x i8] c"mq_notify 3\00"
@.str.9 = [12 x i8] c"mq_notify 4\00"
@.str.10 = [9 x i8] c"mq_close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.mq_attr
%3 = alloca %struct.mq_attr
%4 = alloca %struct.sigevent
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 0
store i32 2, i32* %7
%8 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 1
store i32 100, i32* %8
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = call i64 @mq_open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13, i32 438, %struct.mq_attr* %2)
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = call i32 @mq_unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i64, i64* %5
%27 = call i32 @mq_getattr(i64 %26, %struct.mq_attr* %3)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
%33 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i32 0, i32 0
%36 = load i32, i32* %35
%37 = icmp ne i32 %34, %36
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
%41 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i32 0, i32 1
%44 = load i32, i32* %43
%45 = icmp ne i32 %42, %44
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %40
%49 = load i32, i32* @SIGEV_SIGNAL
%50 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i32 0, i32 1
store i32 %49, i32* %50
%51 = load i32, i32* @SIGRTMIN
%52 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i32 0, i32 0
store i32 %51, i32* %52
%53 = load i64, i64* %5
%54 = call i32 @mq_notify(i64 %53, %struct.sigevent* %4)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %48
%58 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %48
%60 = load i64, i64* %5
%61 = call i32 @mq_notify(i64 %60, %struct.sigevent* %4)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %73
66: ; preds = %59
%67 = load i64, i64* @errno
%68 = load i64, i64* @EBUSY
%69 = icmp ne i64 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
br label %73
73: ; preds = %72, %64
%74 = load i64, i64* %5
%75 = call i32 @mq_notify(i64 %74, %struct.sigevent* null)
store i32 %75, i32* %6
%76 = load i32, i32* %6
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %73
%79 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %80
80: ; preds = %78, %73
%81 = load i64, i64* %5
%82 = call i32 @mq_close(i64 %81)
store i32 %82, i32* %6
%83 = load i32, i32* %6
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %80
%86 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
br label %87
87: ; preds = %85, %80
ret i32 0
}

LLVM IR (-O2):
%struct.mq_attr = type { i32, i32 }
%struct.sigevent = type { i32, i32 }
@.str = [13 x i8] c"/mytstqueue1\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@.str.1 = [8 x i8] c"mq_open\00"
@.str.2 = [10 x i8] c"mq_unlink\00"
@.str.3 = [11 x i8] c"mq_getattr\00"
@.str.4 = [18 x i8] c"mq_maxmsg changed\00"
@.str.5 = [19 x i8] c"mq_msgsize changed\00"
@SIGEV_SIGNAL = global i32 0
@SIGRTMIN = global i32 0
@.str.6 = [10 x i8] c"mq_notify\00"
@.str.7 = [12 x i8] c"mq_notify 2\00"
@errno = global i64 0
@EBUSY = global i64 0
@.str.8 = [12 x i8] c"mq_notify 3\00"
@.str.9 = [12 x i8] c"mq_notify 4\00"
@.str.10 = [9 x i8] c"mq_close\00"
define i32 @main() {
%1 = alloca %struct.mq_attr
%2 = alloca %struct.mq_attr
%3 = alloca %struct.sigevent
%4 = bitcast %struct.mq_attr* %1 to i8*
%5 = bitcast %struct.mq_attr* %2 to i8*
%6 = bitcast %struct.sigevent* %3 to i8*
%7 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 0
store i32 2, i32* %7
%8 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 1
store i32 100, i32* %8
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = call i64 @mq_open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13, i32 438, %struct.mq_attr* %1)
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = call i32 @mq_unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = call i32 @mq_getattr(i64 %14, %struct.mq_attr* %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = load i32, i32* %7
%30 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i64 0, i32 0
%31 = load i32, i32* %30
%32 = icmp eq i32 %29, %31
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %8
%37 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i64 0, i32 1
%38 = load i32, i32* %37
%39 = icmp eq i32 %36, %38
br i1 %39, label %42, label %40
40: ; preds = %35
%41 = call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = load i32, i32* @SIGEV_SIGNAL
%44 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %3, i64 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* @SIGRTMIN
%46 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %3, i64 0, i32 0
store i32 %45, i32* %46
%47 = call i32 @mq_notify(i64 %14, %struct.sigevent* %3)
%48 = icmp eq i32 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %42
%50 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
%52 = call i32 @mq_notify(i64 %14, %struct.sigevent* %3)
%53 = icmp eq i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %51
%55 = load i64, i64* @errno
%56 = load i64, i64* @EBUSY
%57 = icmp eq i64 %55, %56
br i1 %57, label %61, label %58
58: ; preds = %54, %51
%59 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), %51 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), %54 ]
%60 = call i32 @err(i32 1, i8* %59)
br label %61
61: ; preds = %58, %54
%62 = call i32 @mq_notify(i64 %14, %struct.sigevent* null)
%63 = icmp eq i32 %62, 0
br i1 %63, label %66, label %64
64: ; preds = %61
%65 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
%67 = call i32 @mq_close(i64 %14)
%68 = icmp eq i32 %67, 0
br i1 %68, label %71, label %69
69: ; preds = %66
%70 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
br label %71
71: ; preds = %69, %66
ret i32 0
}