#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 {
  scalar_t__ sigev_signo;
  void *sigev_notify;
};
typedef scalar_t__ mqd_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
void *SIGEV_SIGNAL;
int mq_notify(scalar_t__, struct sigevent *);
scalar_t__ open_queue(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int send_receive(int, int, char, char *);
int child_process(char *qname, int read_pipe, int write_pipe) {
  mqd_t queue;
  struct sigevent se;
  char reply;
  int rval;
  rval = send_receive(read_pipe, write_pipe, 0, &reply);
  if (rval) {
    return rval;
  }
  if (reply != 'a') {
    puts("unexpected error: "
         "mq_close"
         " "
         "2-1"
         ": "
         "send_receive: "
         "expected an 'a'");
    return 2;
  }
  queue = open_queue(qname, O_RDWR, 0);
  if (queue == (mqd_t)-1) {
    return 2;
  }
  se.sigev_notify = SIGEV_SIGNAL;
  if (!mq_notify(queue, &se)) {
    puts("unexpected error: "
         "mq_close"
         " "
         "2-1"
         ": "
         "mq_notify (2): "
         "should have failed");
    return 2;
  }
  rval = send_receive(read_pipe, write_pipe, 'b', &reply);
  if (rval) {
    return rval;
  }
  if (reply != 'c') {
    puts("unexpected error: "
         "mq_close"
         " "
         "2-1"
         ": "
         "send_receive: "
         "expected a 'c'");
    return 2;
  }
  se.sigev_notify = SIGEV_SIGNAL;
  se.sigev_signo = 0;
  rval = mq_notify(queue, &se);
  send_receive(read_pipe, write_pipe, rval ? 'e' : 'd', NULL);
  return 0;
}


x86 assembly (clang, -O0):
<child_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
xor    %edx,%edx
lea    -0x31(%rbp),%rcx
call   1050 <send_receive@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     117b <child_process+0x3b>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ac <child_process+0x16c>
movsbl -0x31(%rbp),%eax
cmp    $0x61,%eax
je     11a0 <child_process+0x60>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ac <child_process+0x16c>
mov    -0x10(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <open_queue@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11cf <child_process+0x8f>
movl   $0x2,-0x4(%rbp)
jmp    12ac <child_process+0x16c>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <mq_notify@plt>
cmp    $0x0,%eax
jne    120b <child_process+0xcb>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ac <child_process+0x16c>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x62,%edx
lea    -0x31(%rbp),%rcx
call   1050 <send_receive@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1237 <child_process+0xf7>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ac <child_process+0x16c>
movsbl -0x31(%rbp),%eax
cmp    $0x63,%eax
je     125c <child_process+0x11c>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ac <child_process+0x16c>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <mq_notify@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    $0x64,%eax
mov    $0x65,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
xor    %ecx,%ecx
movsbl %al,%edx
call   1050 <send_receive@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child_process>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%rbp
lea    0xf(%rsp),%rcx
mov    %esi,%edi
mov    %edx,%esi
xor    %edx,%edx
call   1050 <send_receive@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    122e <child_process+0xee>
cmpb   $0x61,0xf(%rsp)
jne    121d <child_process+0xdd>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %edx,%edx
call   1040 <open_queue@plt>
mov    $0x2,%ebx
cmp    $0xffffffffffffffff,%rax
je     122e <child_process+0xee>
mov    %rax,%r14
mov    0x2e3a(%rip),%rbp
mov    0x0(%rbp),%rax
mov    %rax,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1060 <mq_notify@plt>
test   %eax,%eax
je     123d <child_process+0xfd>
lea    0xf(%rsp),%rcx
mov    %r12d,%edi
mov    %r15d,%esi
mov    $0x62,%edx
call   1050 <send_receive@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    122e <child_process+0xee>
cmpb   $0x63,0xf(%rsp)
jne    1246 <child_process+0x106>
mov    0x0(%rbp),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1060 <mq_notify@plt>
cmp    $0x1,%eax
mov    $0x65,%edx
sbb    $0x0,%edx
xor    %ebx,%ebx
mov    %r12d,%edi
mov    %r15d,%esi
xor    %ecx,%ecx
call   1050 <send_receive@plt>
jmp    122e <child_process+0xee>
lea    0xddc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
jmp    1224 <child_process+0xe4>
lea    0xe33(%rip),%rdi
jmp    1224 <child_process+0xe4>

LLVM IR (-O0):
%struct.sigevent = type { i64, i8* }
@.str = [62 x i8] c"unexpected error: mq_close 2-1: send_receive: expected an 'a'\00"
@O_RDWR = global i32 0
@SIGEV_SIGNAL = global i8* null
@.str.1 = [66 x i8] c"unexpected error: mq_close 2-1: mq_notify (2): should have failed\00"
@.str.2 = [61 x i8] c"unexpected error: mq_close 2-1: send_receive: expected a 'c'\00"
define i32 @child_process(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.sigevent
%10 = alloca i8
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %6
%13 = load i32, i32* %7
%14 = call i32 @send_receive(i32 %12, i32 %13, i8 signext 0, i8* %10)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %11
store i32 %18, i32* %4
br label %68
19: ; preds = %3
%20 = load i8, i8* %10
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 97
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @puts(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %4
br label %68
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = load i32, i32* @O_RDWR
%28 = call i64 @open_queue(i8* %26, i32 %27, i32 0)
store i64 %28, i64* %8
%29 = load i64, i64* %8
%30 = icmp eq i64 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 2, i32* %4
br label %68
32: ; preds = %25
%33 = load i8*, i8** @SIGEV_SIGNAL
%34 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %9, i32 0, i32 1
store i8* %33, i8** %34
%35 = load i64, i64* %8
%36 = call i32 @mq_notify(i64 %35, %struct.sigevent* %9)
%37 = icmp ne i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %32
%39 = call i32 @puts(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %4
br label %68
40: ; preds = %32
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = call i32 @send_receive(i32 %41, i32 %42, i8 signext 98, i8* %10)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = load i32, i32* %11
store i32 %47, i32* %4
br label %68
48: ; preds = %40
%49 = load i8, i8* %10
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 99
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @puts(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %4
br label %68
54: ; preds = %48
%55 = load i8*, i8** @SIGEV_SIGNAL
%56 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %9, i32 0, i32 1
store i8* %55, i8** %56
%57 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %9, i32 0, i32 0
store i64 0, i64* %57
%58 = load i64, i64* %8
%59 = call i32 @mq_notify(i64 %58, %struct.sigevent* %9)
store i32 %59, i32* %11
%60 = load i32, i32* %6
%61 = load i32, i32* %7
%62 = load i32, i32* %11
%63 = icmp ne i32 %62, 0
%64 = zext i1 %63 to i64
%65 = select i1 %63, i32 101, i32 100
%66 = trunc i32 %65 to i8
%67 = call i32 @send_receive(i32 %60, i32 %61, i8 signext %66, i8* null)
store i32 0, i32* %4
br label %68
68: ; preds = %54, %52, %46, %38, %31, %23, %17
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
%struct.sigevent = type { i64, i8* }
@.str = [62 x i8] c"unexpected error: mq_close 2-1: send_receive: expected an 'a'\00"
@O_RDWR = global i32 0
@SIGEV_SIGNAL = global i8* null
@.str.1 = [66 x i8] c"unexpected error: mq_close 2-1: mq_notify (2): should have failed\00"
@.str.2 = [61 x i8] c"unexpected error: mq_close 2-1: send_receive: expected a 'c'\00"
define i32 @child_process(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sigevent
%5 = alloca i8
%6 = bitcast %struct.sigevent* %4 to i8*
%7 = call i32 @send_receive(i32 %1, i32 %2, i8 signext 0, i8* %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %40
9: ; preds = %3
%10 = load i8, i8* %5
%11 = icmp eq i8 %10, 97
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
br label %40
14: ; preds = %9
%15 = load i32, i32* @O_RDWR
%16 = call i64 @open_queue(i8* %0, i32 %15, i32 0)
%17 = icmp eq i64 %16, -1
br i1 %17, label %40, label %18
18: ; preds = %14
%19 = load i8*, i8** @SIGEV_SIGNAL
%20 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i64 0, i32 1
store i8* %19, i8** %20
%21 = call i32 @mq_notify(i64 %16, %struct.sigevent* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %40
25: ; preds = %18
%26 = call i32 @send_receive(i32 %1, i32 %2, i8 signext 98, i8* %5)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = load i8, i8* %5
%30 = icmp eq i8 %29, 99
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0))
br label %40
33: ; preds = %28
%34 = load i8*, i8** @SIGEV_SIGNAL
store i8* %34, i8** %20
%35 = getelementptr inbounds %struct.sigevent, %struct.sigevent* %4, i64 0, i32 0
store i64 0, i64* %35
%36 = call i32 @mq_notify(i64 %16, %struct.sigevent* %4)
%37 = icmp eq i32 %36, 0
%38 = select i1 %37, i8 100, i8 101
%39 = call i32 @send_receive(i32 %1, i32 %2, i8 signext %38, i8* null)
br label %40
40: ; preds = %25, %14, %3, %33, %31, %23, %12
%41 = phi i32 [ 2, %12 ], [ 2, %31 ], [ 0, %33 ], [ 2, %23 ], [ %7, %3 ], [ 2, %14 ], [ %26, %25 ]
ret i32 %41
}