#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-2"
           ": "
           "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-2"
           ": "
           "send_receive: "
           "expected a 'b'\n");
    return 2;
  }
  if (mq_unlink(mqname) == 0) {
    rval = send_receive(read_pipe, write_pipe, 'c', &reply);
    if (rval) {
      return rval;
    }
    if (reply != 'd') {
      printf("unexpected error: "
             "mq_unlink"
             " "
             "2-2"
             ": "
             "send_receive: "
             "expected a 'd'\n");
      return 2;
    }
    if (mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0) != (mqd_t)-1) {
      if (mq_unlink(mqname) != 0) {
        perror("unexpected error: "
               "mq_unlink"
               " "
               "2-2"
               ": "
               "mq_unlink(2)");
        return 2;
      }
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("mq_open may fail until the message queue is actually removed \n");
      printf("Test PASSED\n");
      return 0;
    }
  }
  printf("unexpected error: "
         "mq_unlink"
         " "
         "2-2"
         ": "
         "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    1323 <parent_process+0x1d3>
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    1323 <parent_process+0x1d3>
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    1323 <parent_process+0x1d3>
mov    -0x10(%rbp),%rdi
call   1070 <mq_unlink@plt>
cmp    $0x0,%rax
jne    1310 <parent_process+0x1c0>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x63,%edx
lea    -0x29(%rbp),%rcx
call   1050 <send_receive@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     124b <parent_process+0xfb>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1323 <parent_process+0x1d3>
movsbl -0x29(%rbp),%eax
cmp    $0x64,%eax
je     1270 <parent_process+0x120>
lea    0xe09(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1323 <parent_process+0x1d3>
mov    -0x10(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    0x2d5c(%rip),%rax
or     (%rax),%esi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
mov    0x2d42(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1040 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     12ec <parent_process+0x19c>
mov    -0x10(%rbp),%rdi
call   1070 <mq_unlink@plt>
cmp    $0x0,%rax
je     12d4 <parent_process+0x184>
lea    0xde4(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1323 <parent_process+0x1d3>
lea    0xdfa(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1323 <parent_process+0x1d3>
lea    0xdef(%rip),%rdi
call   1030 <printf@plt>
lea    0xdd6(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1323 <parent_process+0x1d3>
lea    0xe0a(%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,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e8e(%rip),%rax
mov    0x2e6f(%rip),%r12
mov    (%r12),%esi
or     (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    0x2e53(%rip),%r13
mov    0x0(%r13),%edx
or     (%rax),%edx
xor    %ecx,%ecx
call   1040 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     125f <parent_process+0x10f>
lea    0x7(%rsp),%rcx
mov    %ebx,%edi
mov    %r15d,%esi
mov    $0x61,%edx
call   1050 <send_receive@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11e4 <parent_process+0x94>
cmpb   $0x62,0x7(%rsp)
jne    11d3 <parent_process+0x83>
mov    %r14,%rdi
call   1070 <mq_unlink@plt>
test   %rax,%rax
je     11f5 <parent_process+0xa5>
lea    0xe86(%rip),%rdi
jmp    11da <parent_process+0x8a>
lea    0xf30(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x7(%rsp),%rcx
mov    %ebx,%edi
mov    %r15d,%esi
mov    $0x63,%edx
call   1050 <send_receive@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11e4 <parent_process+0x94>
cmpb   $0x64,0x7(%rsp)
jne    1270 <parent_process+0x120>
mov    (%r12),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
mov    0x0(%r13),%edx
mov    0x2d9a(%rip),%rax
or     (%rax),%edx
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %ecx,%ecx
call   1040 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     127c <parent_process+0x12c>
mov    %r14,%rdi
call   1070 <mq_unlink@plt>
test   %rax,%rax
jne    1299 <parent_process+0x149>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    11e4 <parent_process+0x94>
lea    0xd9a(%rip),%rdi
call   1060 <perror@plt>
jmp    11df <parent_process+0x8f>
lea    0xe55(%rip),%rdi
jmp    11da <parent_process+0x8a>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
jmp    11e4 <parent_process+0x94>
lea    0xd89(%rip),%rdi
call   1060 <perror@plt>
jmp    11df <parent_process+0x8f>

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-2: mq_open\00"
@.str.1 = [63 x i8] c"unexpected error: mq_unlink 2-2: send_receive: expected a 'b'\0A\00"
@.str.2 = [63 x i8] c"unexpected error: mq_unlink 2-2: send_receive: expected a 'd'\0A\00"
@.str.3 = [46 x i8] c"unexpected error: mq_unlink 2-2: mq_unlink(2)\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
@.str.5 = [63 x i8] c"mq_open may fail until the message queue is actually removed \0A\00"
@.str.6 = [44 x i8] c"unexpected error: mq_unlink 2-2: 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 %80
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 %80
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 ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %5
br label %80
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 %78
43: ; preds = %39
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = call i32 @send_receive(i32 %44, i32 %45, i8 signext 99, i8* %11)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = load i32, i32* %12
store i32 %50, i32* %5
br label %80
51: ; preds = %43
%52 = load i8, i8* %11
%53 = sext i8 %52 to i32
%54 = icmp ne i32 %53, 100
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %5
br label %80
57: ; preds = %51
%58 = load i8*, i8** %6
%59 = load i32, i32* @O_CREAT
%60 = load i32, i32* @O_RDWR
%61 = or i32 %59, %60
%62 = load i32, i32* @S_IRUSR
%63 = load i32, i32* @S_IWUSR
%64 = or i32 %62, %63
%65 = call i64 @mq_open(i8* %58, i32 %61, i32 %64, i32 0)
%66 = icmp ne i64 %65, -1
br i1 %66, label %67, label %75
67: ; preds = %57
%68 = load i8*, i8** %6
%69 = call i64 @mq_unlink(i8* %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %5
br label %80
73: ; preds = %67
%74 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %80
75: ; preds = %57
%76 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.5, i64 0, i64 0))
%77 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %80
78: ; preds = %39
%79 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
store i32 2, i32* %5
br label %80
80: ; preds = %78, %75, %73, %71, %55, %49, %37, %31, %23
%81 = load i32, i32* %5
ret i32 %81
}

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-2: mq_open\00"
@.str.3 = [46 x i8] c"unexpected error: mq_unlink 2-2: mq_unlink(2)\00"
@str = [43 x i8] c"unexpected error: mq_unlink 2-2: mq_unlink\00"
@str.7 = [62 x i8] c"mq_open may fail until the message queue is actually removed \00"
@str.9 = [12 x i8] c"Test PASSED\00"
@str.10 = [62 x i8] c"unexpected error: mq_unlink 2-2: send_receive: expected a 'd'\00"
@str.11 = [62 x i8] c"unexpected error: mq_unlink 2-2: send_receive: expected a 'b'\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 %56
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 %56
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 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.11, i64 0, i64 0))
br label %56
24: ; preds = %19
%25 = call i64 @mq_unlink(i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %54
27: ; preds = %24
%28 = call i32 @send_receive(i32 %1, i32 %2, i8 signext 99, i8* %5)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %56
30: ; preds = %27
%31 = load i8, i8* %5
%32 = icmp eq i8 %31, 100
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.10, i64 0, i64 0))
br label %56
35: ; preds = %30
%36 = load i32, i32* @O_CREAT
%37 = load i32, i32* @O_RDWR
%38 = or i32 %37, %36
%39 = load i32, i32* @S_IRUSR
%40 = load i32, i32* @S_IWUSR
%41 = or i32 %40, %39
%42 = call i64 @mq_open(i8* %0, i32 %38, i32 %41, i32 0)
%43 = icmp eq i64 %42, -1
br i1 %43, label %51, label %44
44: ; preds = %35
%45 = call i64 @mq_unlink(i8* %0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
br label %56
49: ; preds = %44
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
br label %56
51: ; preds = %35
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.7, i64 0, i64 0))
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
br label %56
54: ; preds = %24
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %27, %16, %54, %51, %49, %47, %33, %22, %14
%57 = phi i32 [ 2, %14 ], [ 2, %22 ], [ 2, %33 ], [ 2, %47 ], [ 0, %49 ], [ 0, %51 ], [ 2, %54 ], [ %17, %16 ], [ %28, %27 ]
ret i32 %57
}