#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 */
scalar_t__ EBUSY;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SIGEV_SIGNAL;
int /*<<< orphan*/ SIGUSR1;
int S_IRUSR;
int S_IWUSR;
scalar_t__ errno;
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"
          "_"
          "9-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"
           " "
           "9-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"
           " "
           "9-1"
           ": "
           "fork");
    mqclean(mqdes, mqname);
    return 2;
  }
  if (pid == 0) {
    if (mq_notify(mqdes, &notification) == -1) {
      if (EBUSY == errno) {
        printf("Test PASSED \n");
        return 0;
      } else {
        printf("errno != EBUSY \n");
        printf("Test FAILED \n");
        return 1;
      }
    } else {
      printf("Test FAILED \n");
      return 1;
    }
  } 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    0x2dd3(%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    134c <main+0x1bc>
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    134c <main+0x1bc>
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    134c <main+0x1bc>
cmpl   $0x0,-0x54(%rbp)
jne    1330 <main+0x1a0>
mov    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <mq_notify@plt>
cmp    $0xffffffff,%eax
jne    1318 <main+0x188>
mov    0x2cdf(%rip),%rax
mov    (%rax),%rax
mov    0x2ce5(%rip),%rcx
cmp    (%rcx),%rax
jne    12f4 <main+0x164>
lea    0xd8c(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134c <main+0x1bc>
lea    0xd82(%rip),%rdi
call   1050 <printf@plt>
lea    0xd34(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <main+0x1bc>
lea    0xd1c(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <main+0x1bc>
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    0x2de5(%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     12a0 <main+0x110>
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     1239 <main+0xa9>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <mqclean@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     126a <main+0xda>
cmp    $0xffffffff,%eax
je     12d0 <main+0x140>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <mqclean@plt>
mov    0xc(%rsp),%eax
jmp    1233 <main+0xa3>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <mq_notify@plt>
cmp    $0xffffffff,%eax
jne    12bf <main+0x12f>
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%rax
jne    12b3 <main+0x123>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1233 <main+0xa3>
lea    0xd6b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
jmp    1233 <main+0xa3>
lea    0xda7(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb8(%rip),%rdi
call   1030 <puts@plt>
jmp    122e <main+0x9e>
lea    0xd64(%rip),%rdi
call   1070 <perror@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <mqclean@plt>
mov    $0x2,%eax
jmp    1233 <main+0xa3>

LLVM IR (-O0):
%struct.sigevent = type { i32, i32 }
@.str = [18 x i8] c"/mq_notify_9-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 9-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 9-1: fork\00"
@EBUSY = global i64 0
@errno = global i64 0
@.str.4 = [14 x i8] c"Test PASSED \0A\00"
@.str.5 = [17 x i8] c"errno != EBUSY \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 %68
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 %68
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 %68
44: ; preds = %35
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %62
47: ; preds = %44
%48 = load i64, i64* %3
%49 = call i32 @mq_notify(i64 %48, %struct.sigevent* %4)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %60
51: ; preds = %47
%52 = load i64, i64* @EBUSY
%53 = load i64, i64* @errno
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = 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 %68
57: ; preds = %51
%58 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%59 = 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 %68
60: ; preds = %47
%61 = 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 %68
62: ; preds = %44
%63 = call i32 @wait(i32* %6)
%64 = load i64, i64* %3
%65 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%66 = call i32 @mqclean(i64 %64, i8* %65)
%67 = load i32, i32* %6
store i32 %67, i32* %1
br label %68
68: ; preds = %62, %60, %57, %55, %39, %30, %20
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
%struct.sigevent = type { i32, i32 }
@.str = [18 x i8] c"/mq_notify_9-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 9-1: mq_open\00"
@SIGEV_SIGNAL = global i32 0
@SIGUSR1 = global i32 0
@.str.3 = [38 x i8] c"unexpected error: mq_notify 9-1: fork\00"
@EBUSY = global i64 0
@errno = global i64 0
@str.6 = [16 x i8] c"errno != EBUSY \00"
@str.8 = [13 x i8] c"Test PASSED \00"
@str.9 = [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 %52
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.9, i64 0, i64 0))
%28 = call i32 @mqclean(i64 %15, i8* %4)
br label %52
29: ; preds = %19
%30 = call i32 (...) @fork()
switch i32 %30, label %48 [
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 %52
34: ; preds = %29
%35 = call i32 @mq_notify(i64 %15, %struct.sigevent* %2)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = load i64, i64* @EBUSY
%39 = load i64, i64* @errno
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
br label %52
43: ; preds = %37
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
br label %52
46: ; preds = %34
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
br label %52
48: ; preds = %29
%49 = call i32 @wait(i32* %3)
%50 = call i32 @mqclean(i64 %15, i8* %4)
%51 = load i32, i32* %3
br label %52
52: ; preds = %48, %46, %43, %41, %31, %26, %17
%53 = phi i32 [ 2, %17 ], [ 1, %26 ], [ 2, %31 ], [ 0, %41 ], [ 1, %43 ], [ 1, %46 ], [ %51, %48 ]
ret i32 %53
}