#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 int /*<<< orphan*/ pid_t;
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 mq_close(scalar_t__);
scalar_t__ mq_notify(scalar_t__, struct sigevent *);
int /*<<< orphan*/ mq_unlink(char *);
scalar_t__ open_queue(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int send_receive(int, int, char, char *);
int parent_process(char *qname, int read_pipe, int write_pipe,
                   pid_t child_pid __attribute__((unused))) {
  mqd_t queue;
  struct sigevent se;
  int rval;
  char reply;
  queue = open_queue(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (queue == (mqd_t)-1) {
    return 2;
  }
  se.sigev_notify = SIGEV_SIGNAL;
  se.sigev_signo = SIGUSR1;
  if (mq_notify(queue, &se)) {
    perror("unexpected error: "
           "mq_close"
           " "
           "2-1"
           ": "
           "mq_notify (1)");
    mq_close(queue);
    mq_unlink(qname);
    return 2;
  }
  rval = send_receive(read_pipe, write_pipe, 'a', &reply);
  if (rval) {
    mq_close(queue);
    mq_unlink(qname);
    return rval;
  }
  if (reply != 'b') {
    puts("unexpected error: "
         "mq_close"
         " "
         "2-1"
         ": "
         "send_receive: "
         "expected a 'b'");
    mq_close(queue);
    mq_unlink(qname);
    return 2;
  }
  rval = mq_close(queue);
  mq_unlink(qname);
  if (rval) {
    perror("unexpected error: "
           "mq_close"
           " "
           "2-1"
           ": "
           "mq_close:");
    return 2;
  }
  rval = send_receive(read_pipe, write_pipe, 'c', &reply);
  if (rval) {
    return rval;
  }
  if (reply == 'd') {
    puts("Test PASSED");
    return 0;
  } else if (reply == 'e') {
    puts("Test FAILED");
    return 1;
  } else {
    puts("unexpected error: "
         "mq_close"
         " "
         "2-1"
         ": "
         "bad reply from child");
    return 2;
  }
}


x86 assembly (clang, -O0):
<parent_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
call   1050 <open_queue@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11cd <parent_process+0x5d>
movl   $0x2,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <mq_notify@plt>
cmp    $0x0,%rax
je     1226 <parent_process+0xb6>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1040 <mq_close@plt>
mov    -0x10(%rbp),%rdi
call   1090 <mq_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x61,%edx
lea    -0x35(%rbp),%rcx
call   1060 <send_receive@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1264 <parent_process+0xf4>
mov    -0x28(%rbp),%rdi
call   1040 <mq_close@plt>
mov    -0x10(%rbp),%rdi
call   1090 <mq_unlink@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
movsbl -0x35(%rbp),%eax
cmp    $0x62,%eax
je     129b <parent_process+0x12b>
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
mov    -0x28(%rbp),%rdi
call   1040 <mq_close@plt>
mov    -0x10(%rbp),%rdi
call   1090 <mq_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
mov    -0x28(%rbp),%rdi
call   1040 <mq_close@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <mq_unlink@plt>
cmpl   $0x0,-0x34(%rbp)
je     12d2 <parent_process+0x162>
lea    0xdaa(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x63,%edx
lea    -0x35(%rbp),%rcx
call   1060 <send_receive@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12fe <parent_process+0x18e>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
movsbl -0x35(%rbp),%eax
cmp    $0x64,%eax
jne    1323 <parent_process+0x1b3>
lea    0xd83(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
movsbl -0x35(%rbp),%eax
cmp    $0x65,%eax
jne    1348 <parent_process+0x1d8>
lea    0xd6a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <parent_process+0x1eb>
lea    0xd5e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_process>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%r12
mov    0x2e6c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1a(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <open_queue@plt>
mov    $0x2,%ebx
cmp    $0xffffffffffffffff,%rax
je     12a7 <parent_process+0x137>
mov    %rax,%rbp
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1080 <mq_notify@plt>
test   %rax,%rax
jne    1286 <parent_process+0x116>
lea    0x7(%rsp),%rcx
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x61,%edx
call   1060 <send_receive@plt>
test   %eax,%eax
je     1220 <parent_process+0xb0>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <mq_close@plt>
mov    %r12,%rdi
call   1090 <mq_unlink@plt>
jmp    12a7 <parent_process+0x137>
cmpb   $0x62,0x7(%rsp)
jne    1278 <parent_process+0x108>
mov    %rbp,%rdi
call   1040 <mq_close@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1090 <mq_unlink@plt>
test   %ebx,%ebx
jne    12d7 <parent_process+0x167>
lea    0x7(%rsp),%rcx
mov    %r15d,%edi
mov    %r14d,%esi
mov    $0x63,%edx
call   1060 <send_receive@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    12a7 <parent_process+0x137>
mov    0x7(%rsp),%al
cmp    $0x65,%al
je     12b6 <parent_process+0x146>
cmp    $0x64,%al
jne    12c9 <parent_process+0x159>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    12a7 <parent_process+0x137>
lea    0xdaf(%rip),%rdi
call   1030 <puts@plt>
jmp    1292 <parent_process+0x122>
lea    0xd73(%rip),%rdi
call   1070 <perror@plt>
mov    %rbp,%rdi
call   1040 <mq_close@plt>
mov    %r12,%rdi
call   1090 <mq_unlink@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12a7 <parent_process+0x137>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
jmp    12a2 <parent_process+0x132>
lea    0xd8d(%rip),%rdi
call   1070 <perror@plt>
jmp    12a2 <parent_process+0x132>

LLVM IR (-O0):
%struct.sigevent = type { i32, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SIGEV_SIGNAL = global i32 0
@SIGUSR1 = global i32 0
@.str = [46 x i8] c"unexpected error: mq_close 2-1: mq_notify (1)\00"
@.str.1 = [61 x i8] c"unexpected error: mq_close 2-1: send_receive: expected a 'b'\00"
@.str.2 = [42 x i8] c"unexpected error: mq_close 2-1: mq_close:\00"
@.str.3 = [12 x i8] c"Test PASSED\00"
@.str.4 = [12 x i8] c"Test FAILED\00"
@.str.5 = [53 x i8] c"unexpected error: mq_close 2-1: bad reply from child\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 %struct.sigevent
%12 = alloca i32
%13 = alloca i8
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = call i64 @open_queue(i8* %14, i32 %17, i32 %20)
store i64 %21, i64* %10
%22 = load i64, i64* %10
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 2, i32* %5
br label %92
25: ; preds = %4
%26 = load i32, i32* @SIGEV_SIGNAL
%27 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %11, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @SIGUSR1
%29 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %11, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i64, i64* %10
%31 = call i64 @mq_notify(i64 %30, %struct.sigevent* %11)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%35 = load i64, i64* %10
%36 = call i32 @mq_close(i64 %35)
%37 = load i8*, i8** %6
%38 = call i32 @mq_unlink(i8* %37)
store i32 2, i32* %5
br label %92
39: ; preds = %25
%40 = load i32, i32* %7
%41 = load i32, i32* %8
%42 = call i32 @send_receive(i32 %40, i32 %41, i8 signext 97, i8* %13)
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i64, i64* %10
%47 = call i32 @mq_close(i64 %46)
%48 = load i8*, i8** %6
%49 = call i32 @mq_unlink(i8* %48)
%50 = load i32, i32* %12
store i32 %50, i32* %5
br label %92
51: ; preds = %39
%52 = load i8, i8* %13
%53 = sext i8 %52 to i32
%54 = icmp ne i32 %53, 98
br i1 %54, label %55, label %61
55: ; preds = %51
%56 = call i32 @puts(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%57 = load i64, i64* %10
%58 = call i32 @mq_close(i64 %57)
%59 = load i8*, i8** %6
%60 = call i32 @mq_unlink(i8* %59)
store i32 2, i32* %5
br label %92
61: ; preds = %51
%62 = load i64, i64* %10
%63 = call i32 @mq_close(i64 %62)
store i32 %63, i32* %12
%64 = load i8*, i8** %6
%65 = call i32 @mq_unlink(i8* %64)
%66 = load i32, i32* %12
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %61
%69 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %5
br label %92
70: ; preds = %61
%71 = load i32, i32* %7
%72 = load i32, i32* %8
%73 = call i32 @send_receive(i32 %71, i32 %72, i8 signext 99, i8* %13)
store i32 %73, i32* %12
%74 = load i32, i32* %12
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %70
%77 = load i32, i32* %12
store i32 %77, i32* %5
br label %92
78: ; preds = %70
%79 = load i8, i8* %13
%80 = sext i8 %79 to i32
%81 = icmp eq i32 %80, 100
br i1 %81, label %82, label %84
82: ; preds = %78
%83 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %92
84: ; preds = %78
%85 = load i8, i8* %13
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 101
br i1 %87, label %88, label %90
88: ; preds = %84
%89 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %5
br label %92
90: ; preds = %84
%91 = call i32 @puts(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %5
br label %92
92: ; preds = %90, %88, %82, %76, %68, %55, %45, %33, %24
%93 = load i32, i32* %5
ret i32 %93
}

LLVM IR (-O2):
%struct.sigevent = type { i32, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SIGEV_SIGNAL = global i32 0
@SIGUSR1 = global i32 0
@.str = [46 x i8] c"unexpected error: mq_close 2-1: mq_notify (1)\00"
@.str.1 = [61 x i8] c"unexpected error: mq_close 2-1: send_receive: expected a 'b'\00"
@.str.2 = [42 x i8] c"unexpected error: mq_close 2-1: mq_close:\00"
@.str.3 = [12 x i8] c"Test PASSED\00"
@.str.4 = [12 x i8] c"Test FAILED\00"
@.str.5 = [53 x i8] c"unexpected error: mq_close 2-1: bad reply from child\00"
define i32 @parent_process(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sigevent
%6 = alloca i8
%7 = bitcast %struct.sigevent* %5 to i8*
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = tail call i64 @open_queue(i8* %0, i32 %10, i32 %13)
%15 = icmp eq i64 %14, -1
br i1 %15, label %57, label %16
16: ; preds = %4
%17 = load i32, i32* @SIGEV_SIGNAL
%18 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %5, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @SIGUSR1
%20 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %5, i64 0, i32 0
store i32 %19, i32* %20
%21 = call i64 @mq_notify(i64 %14, %struct.sigevent* %5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @mq_close(i64 %14)
%26 = call i32 @mq_unlink(i8* %0)
br label %57
27: ; preds = %16
%28 = call i32 @send_receive(i32 %1, i32 %2, i8 signext 97, i8* %6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i32 @mq_close(i64 %14)
%32 = call i32 @mq_unlink(i8* %0)
br label %57
33: ; preds = %27
%34 = load i8, i8* %6
%35 = icmp eq i8 %34, 98
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @mq_close(i64 %14)
%39 = call i32 @mq_unlink(i8* %0)
br label %57
40: ; preds = %33
%41 = call i32 @mq_close(i64 %14)
%42 = call i32 @mq_unlink(i8* %0)
%43 = icmp eq i32 %41, 0
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %57
46: ; preds = %40
%47 = call i32 @send_receive(i32 %1, i32 %2, i8 signext 99, i8* %6)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = load i8, i8* %6
switch i8 %50, label %55 [
i8 100, label %51
i8 101, label %53
]
51: ; preds = %49
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %57
53: ; preds = %49
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %57
55: ; preds = %49
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %46, %4, %55, %53, %51, %44, %36, %30, %23
%58 = phi i32 [ 2, %23 ], [ %28, %30 ], [ 2, %36 ], [ 2, %44 ], [ 0, %51 ], [ 1, %53 ], [ 2, %55 ], [ 2, %4 ], [ %47, %46 ]
ret i32 %58
}