#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 mqd_t;

/* Variables and functions */
scalar_t__ EBUSY;
int O_CREAT;
int O_RDWR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int mq_open(char const *, int, int, int /*<<< orphan*/);
int msg_notify(int, int);
int /*<<< orphan*/ msg_snd(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
void parent(const char *mqname, const char *mqname2) {
  mqd_t mqdes = mq_open(mqname, O_RDWR | O_CREAT, 0666, 0);
  if (mqdes == -1) {
    perror("mq_open");
    exit(1);
  }
  mqd_t mqdes2 = mq_open(mqname2, O_RDWR | O_CREAT, 0666, 0);
  if (mqdes == -1) {
    perror("mq_open");
    exit(1);
  }
  int i = 1;
  while (1) {
    int rc;
    do {
      sleep(1);
      rc = msg_notify(mqdes2, i + 1);
    } while ((rc == -1) && (errno == EBUSY));
    if (rc == -1) {
      perror("mq_notify");
      exit(1);
    }
    printf("Server: sending %d\n", i);
    fflush(stdout);
    msg_snd(mqdes, i);
    sleep(1);
    i += 2;
  }
  exit(0);
}


x86 assembly (clang, -O0):
<parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d5 <parent+0x55>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121a <parent+0x9a>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1226 <parent+0xa6>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1030 <msg_notify@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jne    126a <parent+0xea>
mov    0x2d79(%rip),%rax
mov    (%rax),%rax
mov    0x2d5f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1226 <parent+0xa6>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1295 <parent+0x115>
lea    0xd82(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%esi
lea    0xd73(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1040 <msg_snd@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1221 <parent+0xa1>

x86 assembly (clang, -O2):
<parent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e4b(%rip),%r15
mov    0x2e5c(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r15),%esi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
cmp    $0xffffffff,%eax
jne    11bf <parent+0x3f>
lea    0xe46(%rip),%rdi
jmp    1265 <parent+0xe5>
mov    %eax,%r14d
mov    0x0(%rbp),%esi
or     (%r15),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
mov    %eax,%ebp
mov    $0x1,%r12d
mov    0x2dea(%rip),%r13
mov    0x2dd3(%rip),%r15
nopl   (%rax)
lea    0x1(%r12),%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <msg_notify@plt>
cmp    $0xffffffff,%eax
jne    1221 <parent+0xa1>
mov    (%r15),%rcx
cmp    %rcx,0x0(%r13)
je     1200 <parent+0x80>
cmp    $0xffffffff,%eax
je     125e <parent+0xde>
lea    0xde5(%rip),%rdi
mov    %r12d,%esi
call   1050 <printf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %r14d,%edi
mov    %r12d,%esi
call   1040 <msg_snd@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
add    $0x2,%r12d
jmp    11f0 <parent+0x70>
lea    0xda3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [8 x i8] c"mq_open\00"
@errno = global i64 0
@EBUSY = global i64 0
@.str.1 = [10 x i8] c"mq_notify\00"
@.str.2 = [20 x i8] c"Server: sending %d\0A\00"
@stdout = global i32 0
define void @parent(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @mq_open(i8* %9, i32 %12, i32 438, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @mq_open(i8* %20, i32 %23, i32 438, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
store i32 1, i32* %7
br label %31
31: ; preds = %30, %53
br label %32
32: ; preds = %45, %31
%33 = call i32 @sleep(i32 1)
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
%37 = call i32 @msg_notify(i32 %34, i32 %36)
store i32 %37, i32* %8
br label %38
38: ; preds = %32
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @EBUSY
%44 = icmp eq i64 %42, %43
br label %45
45: ; preds = %41, %38
%46 = phi i1 [ false, %38 ], [ %44, %41 ]
br i1 %46, label %32, label %47
47: ; preds = %45
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %47
%54 = load i32, i32* %7
%55 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* @stdout
%57 = call i32 @fflush(i32 %56)
%58 = load i32, i32* %5
%59 = load i32, i32* %7
%60 = call i32 @msg_snd(i32 %58, i32 %59)
%61 = call i32 @sleep(i32 1)
%62 = load i32, i32* %7
%63 = add nsw i32 %62, 2
store i32 %63, i32* %7
br label %31
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [8 x i8] c"mq_open\00"
@errno = global i64 0
@EBUSY = global i64 0
@.str.1 = [10 x i8] c"mq_notify\00"
@.str.2 = [20 x i8] c"Server: sending %d\0A\00"
@stdout = global i32 0
define void @parent(i8* %0, i8* %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @mq_open(i8* %0, i32 %5, i32 438, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @mq_open(i8* %1, i32 %14, i32 438, i32 0)
br label %16
16: ; preds = %31, %11
%17 = phi i32 [ 1, %11 ], [ %37, %31 ]
%18 = add nuw nsw i32 %17, 1
br label %19
19: ; preds = %19, %16
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 @msg_notify(i32 %15, i32 %18)
%22 = icmp eq i32 %21, -1
%23 = load i64, i64* @errno
%24 = load i64, i64* @EBUSY
%25 = icmp eq i64 %23, %24
%26 = select i1 %22, i1 %25, i1 false
br i1 %26, label %19, label %27
27: ; preds = %19
br i1 %22, label %28, label %31
28: ; preds = %27
%29 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %27
%32 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%33 = load i32, i32* @stdout
%34 = tail call i32 @fflush(i32 %33)
%35 = tail call i32 @msg_snd(i32 %6, i32 %17)
%36 = tail call i32 @sleep(i32 1)
%37 = add nuw nsw i32 %17, 2
br label %16
}