#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 mq_attr {
  int mq_maxmsg;
  scalar_t__ mq_curmsgs;
  scalar_t__ mq_msgsize;
  scalar_t__ mq_flags;
};
typedef scalar_t__ pid_t;
typedef int mqd_t;

/* Variables and functions */
int O_CREAT;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ child(int, int);
scalar_t__ fork();
int /*<<< orphan*/ mq_close(int);
int mq_open(char *, int, int, int /*<<< orphan*/ *);
int mq_receive(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *, long long *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  struct mq_attr attr;
  attr.mq_flags = 0;
  attr.mq_maxmsg = 4;
  attr.mq_msgsize = 0;
  attr.mq_curmsgs = 0;
  mqd_t good = mq_open("good", O_NONBLOCK | O_RDWR | O_CREAT, 0666, NULL);
  if (good == -1) {
    perror("Good create");
  }
  mqd_t bad = mq_open("bad", O_NONBLOCK | O_RDWR | O_CREAT, 0666, NULL);
  if (bad == -1) {
    perror("Bad create");
  }
  pid_t pid = 0;
  for (int i = 0; i < 4; i++) {
    pid = fork();
    if (pid == 0) {
      child(good, bad);
      break;
    }
  }
  if (pid == 0)
    return 0;
  for (int i = 0; i < 4; i++)
    wait(NULL);
  int ans = 0;
  int hello = 0;
  int bye = 0;
  long long int prio = 0;
  while (ans < 4) {
    if (mq_receive(good, NULL, NULL, &prio) != -1) {
      ans += 1;
      hello += 1;
    } else
      perror("Good");
    if (mq_receive(bad, NULL, NULL, &prio) != -1) {
      ans += 1;
      bye += 1;
    } else
      perror("Bad");
    sleep(1);
  }
  printf("Hello=%d, bye=%d\n", hello, bye);
  mq_close(good);
  mq_close(bad);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x4,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe20(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <mq_open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1207 <main+0x77>
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
mov    0x2dd8(%rip),%rax
or     (%rax),%esi
lea    0xde8(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <mq_open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1250 <main+0xc0>
lea    0xdca(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x4,-0x3c(%rbp)
jge    12a2 <main+0x112>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    128f <main+0xff>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1030 <child@plt>
jmp    12a2 <main+0x112>
jmp    1294 <main+0x104>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    125f <main+0xcf>
cmpq   $0x0,-0x38(%rbp)
jne    12b9 <main+0x129>
movl   $0x0,-0x4(%rbp)
jmp    13be <main+0x22e>
movl   $0x0,-0x40(%rbp)
cmpl   $0x4,-0x40(%rbp)
jge    12e1 <main+0x151>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12c0 <main+0x130>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movq   $0x0,-0x58(%rbp)
cmpl   $0x4,-0x44(%rbp)
jge    1395 <main+0x205>
mov    -0x2c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x58(%rbp),%rcx
mov    %rdx,%rsi
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     133b <main+0x1ab>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1347 <main+0x1b7>
lea    0xcde(%rip),%rdi
call   1080 <perror@plt>
mov    -0x30(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x58(%rbp),%rcx
mov    %rdx,%rsi
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     137a <main+0x1ea>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1386 <main+0x1f6>
lea    0xca4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    12fe <main+0x16e>
mov    -0x48(%rbp),%esi
mov    -0x4c(%rbp),%edx
lea    0xc87(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1050 <mq_close@plt>
mov    -0x30(%rbp),%edi
call   1050 <mq_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e36(%rip),%r15
mov    0x2e37(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r15),%esi
mov    0x2e42(%rip),%rbx
or     (%rbx),%esi
lea    0xe41(%rip),%rdi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1312 <main+0x182>
mov    0x0(%rbp),%esi
or     (%r15),%esi
or     (%rbx),%esi
lea    0xe2b(%rip),%rdi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     1323 <main+0x193>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12d4 <main+0x144>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12d4 <main+0x144>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12d4 <main+0x144>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12d4 <main+0x144>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
movq   $0x0,(%rsp)
mov    %rsp,%r12
xor    %ebp,%ebp
xor    %r13d,%r13d
nopl   0x0(%rax)
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
mov    %r12,%rcx
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     12b8 <main+0x128>
add    $0x1,%r13d
add    $0x1,%ebp
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
mov    %r12,%rcx
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     12c6 <main+0x136>
add    $0x1,%r13d
add    $0x1,%ebx
mov    $0x1,%edi
call   1090 <sleep@plt>
cmp    $0x4,%r13d
jl     1270 <main+0xe0>
jmp    12f0 <main+0x160>
lea    0xd61(%rip),%rdi
call   1080 <perror@plt>
jmp    128b <main+0xfb>
lea    0xd58(%rip),%rdi
call   1080 <perror@plt>
jmp    12a6 <main+0x116>
mov    %r14d,%edi
mov    %r15d,%esi
call   1030 <child@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd32(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
mov    %r14d,%edi
call   1050 <mq_close@plt>
mov    %r15d,%edi
call   1050 <mq_close@plt>
jmp    12df <main+0x14f>
lea    0xcec(%rip),%rdi
call   1080 <perror@plt>
jmp    11d7 <main+0x47>
lea    0xceb(%rip),%rdi
call   1080 <perror@plt>
jmp    11fe <main+0x6e>

LLVM IR (-O0):
%struct.mq_attr = type { i32, i64, i64, i64 }
@.str = [5 x i8] c"good\00"
@O_NONBLOCK = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [12 x i8] c"Good create\00"
@.str.2 = [4 x i8] c"bad\00"
@.str.3 = [11 x i8] c"Bad create\00"
@.str.4 = [5 x i8] c"Good\00"
@.str.5 = [4 x i8] c"Bad\00"
@.str.6 = [18 x i8] c"Hello=%d, bye=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.mq_attr
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i32 0, i32* %1
%12 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 3
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 0
store i32 4, i32* %13
%14 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 1
store i64 0, i64* %15
%16 = load i32, i32* @O_NONBLOCK
%17 = load i32, i32* @O_RDWR
%18 = or i32 %16, %17
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = call i32 @mq_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %20, i32 438, i32* null)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %0
%27 = load i32, i32* @O_NONBLOCK
%28 = load i32, i32* @O_RDWR
%29 = or i32 %27, %28
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = call i32 @mq_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 438, i32* null)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %26
store i64 0, i64* %5
store i32 0, i32* %6
br label %38
38: ; preds = %50, %37
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 4
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = call i64 (...) @fork()
store i64 %42, i64* %5
%43 = load i64, i64* %5
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* %3
%47 = load i32, i32* %4
%48 = call i32 @child(i32 %46, i32 %47)
br label %53
49: ; preds = %41
br label %50
50: ; preds = %49
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %38
53: ; preds = %45, %38
%54 = load i64, i64* %5
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
store i32 0, i32* %1
br label %102
57: ; preds = %53
store i32 0, i32* %7
br label %58
58: ; preds = %63, %57
%59 = load i32, i32* %7
%60 = icmp slt i32 %59, 4
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = call i32 @wait(i32* null)
br label %63
63: ; preds = %61
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %58
66: ; preds = %58
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i64 0, i64* %11
br label %67
67: ; preds = %92, %66
%68 = load i32, i32* %8
%69 = icmp slt i32 %68, 4
br i1 %69, label %70, label %94
70: ; preds = %67
%71 = load i32, i32* %3
%72 = call i32 @mq_receive(i32 %71, i32* null, i32* null, i64* %11)
%73 = icmp ne i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i32, i32* %8
%76 = add nsw i32 %75, 1
store i32 %76, i32* %8
%77 = load i32, i32* %9
%78 = add nsw i32 %77, 1
store i32 %78, i32* %9
br label %81
79: ; preds = %70
%80 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %81
81: ; preds = %79, %74
%82 = load i32, i32* %4
%83 = call i32 @mq_receive(i32 %82, i32* null, i32* null, i64* %11)
%84 = icmp ne i32 %83, -1
br i1 %84, label %85, label %90
85: ; preds = %81
%86 = load i32, i32* %8
%87 = add nsw i32 %86, 1
store i32 %87, i32* %8
%88 = load i32, i32* %10
%89 = add nsw i32 %88, 1
store i32 %89, i32* %10
br label %92
90: ; preds = %81
%91 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
br label %92
92: ; preds = %90, %85
%93 = call i32 @sleep(i32 1)
br label %67
94: ; preds = %67
%95 = load i32, i32* %9
%96 = load i32, i32* %10
%97 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %95, i32 %96)
%98 = load i32, i32* %3
%99 = call i32 @mq_close(i32 %98)
%100 = load i32, i32* %4
%101 = call i32 @mq_close(i32 %100)
store i32 0, i32* %1
br label %102
102: ; preds = %94, %56
%103 = load i32, i32* %1
ret i32 %103
}

LLVM IR (-O2):
@.str = [5 x i8] c"good\00"
@O_NONBLOCK = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [12 x i8] c"Good create\00"
@.str.2 = [4 x i8] c"bad\00"
@.str.3 = [11 x i8] c"Bad create\00"
@.str.4 = [5 x i8] c"Good\00"
@.str.5 = [4 x i8] c"Bad\00"
@.str.6 = [18 x i8] c"Hello=%d, bye=%d\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = load i32, i32* @O_NONBLOCK
%3 = load i32, i32* @O_RDWR
%4 = or i32 %3, %2
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = tail call i32 @mq_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438, i32* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @O_NONBLOCK
%13 = load i32, i32* @O_RDWR
%14 = or i32 %13, %12
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = tail call i32 @mq_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 438, i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = tail call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %39, label %24
24: ; preds = %21
%25 = tail call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
br i1 %26, label %39, label %27
27: ; preds = %24
%28 = tail call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %27
%31 = tail call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %30
%34 = tail call i32 @wait(i32* null)
%35 = tail call i32 @wait(i32* null)
%36 = tail call i32 @wait(i32* null)
%37 = tail call i32 @wait(i32* null)
%38 = bitcast i64* %1 to i8*
store i64 0, i64* %1
br label %41
39: ; preds = %30, %27, %24, %21
%40 = tail call i32 @child(i32 %7, i32 %17)
br label %71
41: ; preds = %33, %62
%42 = phi i32 [ 0, %33 ], [ %64, %62 ]
%43 = phi i32 [ 0, %33 ], [ %54, %62 ]
%44 = phi i32 [ 0, %33 ], [ %63, %62 ]
%45 = call i32 @mq_receive(i32 %7, i32* null, i32* null, i64* %1)
%46 = icmp eq i32 %45, -1
br i1 %46, label %50, label %47
47: ; preds = %41
%48 = add nsw i32 %44, 1
%49 = add nsw i32 %43, 1
br label %52
50: ; preds = %41
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %47
%53 = phi i32 [ %48, %47 ], [ %44, %50 ]
%54 = phi i32 [ %49, %47 ], [ %43, %50 ]
%55 = call i32 @mq_receive(i32 %17, i32* null, i32* null, i64* %1)
%56 = icmp eq i32 %55, -1
br i1 %56, label %60, label %57
57: ; preds = %52
%58 = add nsw i32 %53, 1
%59 = add nsw i32 %42, 1
br label %62
60: ; preds = %52
%61 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
br label %62
62: ; preds = %60, %57
%63 = phi i32 [ %58, %57 ], [ %53, %60 ]
%64 = phi i32 [ %59, %57 ], [ %42, %60 ]
%65 = call i32 @sleep(i32 1)
%66 = icmp slt i32 %63, 4
br i1 %66, label %41, label %67
67: ; preds = %62
%68 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %64)
%69 = call i32 @mq_close(i32 %7)
%70 = call i32 @mq_close(i32 %17)
br label %71
71: ; preds = %39, %67
ret i32 0
}