#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;
};
typedef scalar_t__ mqd_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ SIGUSR1;
int S_IRUSR;
int S_IWUSR;
int fork();
int getpid();
int mq_notify(scalar_t__, struct sigevent *);
scalar_t__ mq_open(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ mqclean(scalar_t__, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ wait(int *);
int main(void) {
  char mqname[50];
  mqd_t mqdes;
  struct sigevent notification;
  int pid;
  int status;
  sprintf(mqname,
          "/"
          "mq_notify"
          "_"
          "2-1"
          "_%d",
          getpid());
  mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
  if (mqdes == (mqd_t)-1) {
    perror("unexpected error: "
           "mq_notify"
           " "
           "2-1"
           ": "
           "mq_open");
    return 2;
  }
  notification.sigev_notify = SIGEV_SIGNAL;
  notification.sigev_signo = SIGUSR1;
  if (mq_notify(mqdes, &notification) != 0) {
    printf("Test FAILED \n");
    mqclean(mqdes, mqname);
    return 1;
  }
  pid = fork();
  if (pid == -1) {
    perror("unexpected error: "
           "mq_notify"
           " "
           "2-1"
           ": "
           "fork");
    mqclean(mqdes, mqname);
    return 2;
  }
  if (pid == 0) {
    if (mq_notify(mqdes, &notification) != -1) {
      printf("Test FAILED \n");
      return 1;
    } else {
      printf("Test PASSED \n");
      return 0;
    }
  } else {
    wait(&status);
    mqclean(mqdes, mqname);
    return status;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xe45(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    1216 <main+0x86>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    130e <main+0x17e>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <mq_notify@plt>
cmp    $0x0,%eax
je     1269 <main+0xd9>
lea    0xdf0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <mqclean@plt>
movl   $0x1,-0x4(%rbp)
jmp    130e <main+0x17e>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    12a2 <main+0x112>
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <mqclean@plt>
movl   $0x2,-0x4(%rbp)
jmp    130e <main+0x17e>
cmpl   $0x0,-0x54(%rbp)
jne    12f2 <main+0x162>
mov    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <mq_notify@plt>
cmp    $0xffffffff,%eax
je     12da <main+0x14a>
lea    0xd72(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130e <main+0x17e>
lea    0xd8e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130e <main+0x17e>
lea    -0x58(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <mqclean@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe5d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    0x2e3f(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ded(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
xor    %ecx,%ecx
call   1060 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1295 <main+0x105>
mov    %rax,%rbx
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <mq_notify@plt>
test   %eax,%eax
je     1235 <main+0xa5>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <mqclean@plt>
mov    $0x1,%eax
jmp    12a6 <main+0x116>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1262 <main+0xd2>
cmp    $0xffffffff,%eax
je     12ac <main+0x11c>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <mqclean@plt>
mov    0xc(%rsp),%eax
jmp    12a6 <main+0x116>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <mq_notify@plt>
cmp    $0xffffffff,%eax
je     1285 <main+0xf5>
lea    0xdf5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12a6 <main+0x116>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    12a6 <main+0x116>
lea    0xd76(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xd88(%rip),%rdi
call   1070 <perror@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <mqclean@plt>
jmp    12a1 <main+0x111>

LLVM IR (-O0):
%struct.sigevent = type { i32, i32 }
@.str = [18 x i8] c"/mq_notify_2-1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [41 x i8] c"unexpected error: mq_notify 2-1: mq_open\00"
@SIGEV_SIGNAL = global i32 0
@SIGUSR1 = global i32 0
@.str.2 = [14 x i8] c"Test FAILED \0A\00"
@.str.3 = [38 x i8] c"unexpected error: mq_notify 2-1: fork\00"
@.str.4 = [14 x i8] c"Test PASSED \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca i64
%4 = alloca %struct.sigevent
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i64 @mq_open(i8* %10, i32 %13, i32 %16, i32 0)
store i64 %17, i64* %3
%18 = load i64, i64* %3
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %61
22: ; preds = %0
%23 = load i32, i32* @SIGEV_SIGNAL
%24 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @SIGUSR1
%26 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i64, i64* %3
%28 = call i32 @mq_notify(i64 %27, %struct.sigevent* %4)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%32 = load i64, i64* %3
%33 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%34 = call i32 @mqclean(i64 %32, i8* %33)
store i32 1, i32* %1
br label %61
35: ; preds = %22
%36 = call i32 (...) @fork()
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%41 = load i64, i64* %3
%42 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%43 = call i32 @mqclean(i64 %41, i8* %42)
store i32 2, i32* %1
br label %61
44: ; preds = %35
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %44
%48 = load i64, i64* %3
%49 = call i32 @mq_notify(i64 %48, %struct.sigevent* %4)
%50 = icmp ne i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %61
53: ; preds = %47
%54 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %61
55: ; preds = %44
%56 = call i32 @wait(i32* %6)
%57 = load i64, i64* %3
%58 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%59 = call i32 @mqclean(i64 %57, i8* %58)
%60 = load i32, i32* %6
store i32 %60, i32* %1
br label %61
61: ; preds = %55, %53, %51, %39, %30, %20
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
%struct.sigevent = type { i32, i32 }
@.str = [18 x i8] c"/mq_notify_2-1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [41 x i8] c"unexpected error: mq_notify 2-1: mq_open\00"
@SIGEV_SIGNAL = global i32 0
@SIGUSR1 = global i32 0
@.str.3 = [38 x i8] c"unexpected error: mq_notify 2-1: fork\00"
@str = [13 x i8] c"Test PASSED \00"
@str.6 = [13 x i8] c"Test FAILED \00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = alloca %struct.sigevent
%3 = alloca i32
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.sigevent* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = call i64 @mq_open(i8* %4, i32 %11, i32 %14, i32 0)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %45
19: ; preds = %0
%20 = load i32, i32* @SIGEV_SIGNAL
%21 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @SIGUSR1
%23 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %2, i64 0, i32 0
store i32 %22, i32* %23
%24 = call i32 @mq_notify(i64 %15, %struct.sigevent* %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.6, i64 0, i64 0))
%28 = call i32 @mqclean(i64 %15, i8* %4)
br label %45
29: ; preds = %19
%30 = call i32 (...) @fork()
switch i32 %30, label %41 [
i32 -1, label %31
i32 0, label %34
]
31: ; preds = %29
%32 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @mqclean(i64 %15, i8* %4)
br label %45
34: ; preds = %29
%35 = call i32 @mq_notify(i64 %15, %struct.sigevent* %2)
%36 = icmp eq i32 %35, -1
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.6, i64 0, i64 0))
br label %45
39: ; preds = %34
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %45
41: ; preds = %29
%42 = call i32 @wait(i32* %3)
%43 = call i32 @mqclean(i64 %15, i8* %4)
%44 = load i32, i32* %3
br label %45
45: ; preds = %41, %39, %37, %31, %26, %17
%46 = phi i32 [ 2, %17 ], [ 1, %26 ], [ 2, %31 ], [ 1, %37 ], [ 0, %39 ], [ %44, %41 ]
ret i32 %46
}