#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 */
typedef int /*<<< orphan*/ pid_t;
typedef scalar_t__ mqd_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
scalar_t__ mq_open(char *, int, int, int /*<<< orphan*/);
scalar_t__ mq_unlink(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int send_receive(int, int, char, char *);
int parent_process(char *mqname, int read_pipe, int write_pipe,
                   pid_t child_pid __attribute__((unused))) {
  mqd_t mqdes;
  char reply;
  int rval;
  mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
  if (mqdes == (mqd_t)-1) {
    perror("unexpected error: "
           "mq_unlink"
           " "
           "2-1"
           ": "
           "mq_open");
    return 2;
  }
  rval = send_receive(read_pipe, write_pipe, 'a', &reply);
  if (rval) {
    return rval;
  }
  if (reply != 'b') {
    printf("unexpected error: "
           "mq_unlink"
           " "
           "2-1"
           ": "
           "send_receive: "
           "expected a 'b'");
    return 2;
  }
  if (mq_unlink(mqname) == 0) {
    if (mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0) == (mqd_t)-1) {
      printf("mq_open to recreate the message	mqueue may fail until all "
             "references to the message queue have been closed, or until the "
             "message queue is actually removed. \n");
      printf("Test PASSED\n");
      return 0;
    } else {
      if (mq_unlink(mqname) != 0) {
        printf("unexpected error: "
               "mq_unlink"
               " "
               "2-1"
               ": "
               "mq_unlink(2)");
        return 2;
      }
      printf("mq_open to recreate the message	mqueue may succeed even if the "
             "references to the message queue have not been closed or the "
             "message queue is not actually removed. \n");
      printf("Test PASSED\n");
      return 0;
    }
  }
  printf("unexpected error: "
         "mq_unlink"
         " "
         "2-1"
         ": "
         "mq_unlink \n");
  return 2;
}


x86 assembly (clang, -O0):
<parent_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    0x2e4d(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1040 <mq_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11bb <parent_process+0x6b>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12de <parent_process+0x18e>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x61,%edx
lea    -0x29(%rbp),%rcx
call   1050 <send_receive@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     11e7 <parent_process+0x97>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12de <parent_process+0x18e>
movsbl -0x29(%rbp),%eax
cmp    $0x62,%eax
je     120c <parent_process+0xbc>
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12de <parent_process+0x18e>
mov    -0x10(%rbp),%rdi
call   1070 <mq_unlink@plt>
cmp    $0x0,%rax
jne    12cb <parent_process+0x17b>
mov    -0x10(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
or     (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    0x2d93(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1040 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
jne    127c <parent_process+0x12c>
lea    0xe08(%rip),%rdi
call   1030 <printf@plt>
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <parent_process+0x18e>
mov    -0x10(%rbp),%rdi
call   1070 <mq_unlink@plt>
cmp    $0x0,%rax
je     12a7 <parent_process+0x157>
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12de <parent_process+0x18e>
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <parent_process+0x18e>
lea    0xf12(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    0x2e60(%rip),%r13
mov    0x0(%r13),%esi
or     (%rax),%esi
mov    0x2e3b(%rip),%r12
mov    0x2e44(%rip),%r15
mov    (%r15),%edx
or     (%r12),%edx
xor    %ecx,%ecx
call   1050 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     123a <parent_process+0xda>
lea    0x7(%rsp),%rcx
mov    %ebx,%edi
mov    %ebp,%esi
mov    $0x61,%edx
call   1060 <send_receive@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    124b <parent_process+0xeb>
cmpb   $0x62,0x7(%rsp)
jne    11eb <parent_process+0x8b>
mov    %r14,%rdi
call   1080 <mq_unlink@plt>
test   %rax,%rax
je     11f9 <parent_process+0x99>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
jmp    1246 <parent_process+0xe6>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
jmp    1246 <parent_process+0xe6>
mov    0x0(%r13),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    (%r15),%edx
or     (%r12),%edx
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %ecx,%ecx
call   1050 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     125c <parent_process+0xfc>
mov    %r14,%rdi
call   1080 <mq_unlink@plt>
test   %rax,%rax
je     1276 <parent_process+0x116>
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
jmp    1246 <parent_process+0xe6>
lea    0xdbf(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xf01(%rip),%rdi
call   1030 <puts@plt>
lea    0xf92(%rip),%rdi
call   1030 <puts@plt>
jmp    124b <parent_process+0xeb>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
lea    0xf78(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    124b <parent_process+0xeb>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [41 x i8] c"unexpected error: mq_unlink 2-1: mq_open\00"
@.str.1 = [62 x i8] c"unexpected error: mq_unlink 2-1: send_receive: expected a 'b'\00"
@.str.2 = [158 x i8] c"mq_open to recreate the message\09mqueue may fail until all references to the message queue have been closed, or until the message queue is actually removed. \0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [46 x i8] c"unexpected error: mq_unlink 2-1: mq_unlink(2)\00"
@.str.5 = [164 x i8] c"mq_open to recreate the message\09mqueue may succeed even if the references to the message queue have not been closed or the message queue is not actually removed. \0A\00"
@.str.6 = [45 x i8] c"unexpected error: mq_unlink 2-1: mq_unlink \0A\00"
define i32 @parent_process(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_RDWR
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i64 @mq_open(i8* %13, i32 %16, i32 %19, i32 0)
store i64 %20, i64* %10
%21 = load i64, i64* %10
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %5
br label %67
25: ; preds = %4
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = call i32 @send_receive(i32 %26, i32 %27, i8 signext 97, i8* %11)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = load i32, i32* %12
store i32 %32, i32* %5
br label %67
33: ; preds = %25
%34 = load i8, i8* %11
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 98
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %5
br label %67
39: ; preds = %33
%40 = load i8*, i8** %6
%41 = call i64 @mq_unlink(i8* %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %65
43: ; preds = %39
%44 = load i8*, i8** %6
%45 = load i32, i32* @O_CREAT
%46 = load i32, i32* @O_RDWR
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IRUSR
%49 = load i32, i32* @S_IWUSR
%50 = or i32 %48, %49
%51 = call i64 @mq_open(i8* %44, i32 %47, i32 %50, i32 0)
%52 = icmp eq i64 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = call i32 @printf(i8* getelementptr inbounds ([158 x i8], [158 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %67
56: ; preds = %43
%57 = load i8*, i8** %6
%58 = call i64 @mq_unlink(i8* %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %5
br label %67
62: ; preds = %56
%63 = call i32 @printf(i8* getelementptr inbounds ([164 x i8], [164 x i8]* @.str.5, i64 0, i64 0))
%64 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %67
65: ; preds = %39
%66 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
store i32 2, i32* %5
br label %67
67: ; preds = %65, %62, %60, %53, %37, %31, %23
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [41 x i8] c"unexpected error: mq_unlink 2-1: mq_open\00"
@.str.1 = [62 x i8] c"unexpected error: mq_unlink 2-1: send_receive: expected a 'b'\00"
@.str.4 = [46 x i8] c"unexpected error: mq_unlink 2-1: mq_unlink(2)\00"
@str = [44 x i8] c"unexpected error: mq_unlink 2-1: mq_unlink \00"
@str.7 = [163 x i8] c"mq_open to recreate the message\09mqueue may succeed even if the references to the message queue have not been closed or the message queue is not actually removed. \00"
@str.9 = [157 x i8] c"mq_open to recreate the message\09mqueue may fail until all references to the message queue have been closed, or until the message queue is actually removed. \00"
@str.10 = [12 x i8] c"Test PASSED\00"
define i32 @parent_process(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i64 @mq_open(i8* %0, i32 %8, i32 %11, i32 0)
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %49
16: ; preds = %4
%17 = call i32 @send_receive(i32 %1, i32 %2, i8 signext 97, i8* %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %49
19: ; preds = %16
%20 = load i8, i8* %5
%21 = icmp eq i8 %20, 98
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
br label %49
24: ; preds = %19
%25 = call i64 @mq_unlink(i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %24
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_RDWR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = call i64 @mq_open(i8* %0, i32 %30, i32 %33, i32 0)
%35 = icmp eq i64 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([157 x i8], [157 x i8]* @str.9, i64 0, i64 0))
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
br label %49
39: ; preds = %27
%40 = call i64 @mq_unlink(i8* %0)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %39
%43 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
br label %49
44: ; preds = %39
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([163 x i8], [163 x i8]* @str.7, i64 0, i64 0))
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
br label %49
47: ; preds = %24
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %16, %47, %44, %42, %36, %22, %14
%50 = phi i32 [ 2, %14 ], [ 2, %22 ], [ 0, %36 ], [ 2, %42 ], [ 0, %44 ], [ 2, %47 ], [ %17, %16 ]
ret i32 %50
}