#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 */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int mq_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ mq_receive(int, char *, int, int /*<<< orphan*/);
int mq_send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
char *strtok(char *, char *);
int main(int argc, char *argv[]) {
  int N, P, B, id;
  N = atoi(argv[1]);
  B = atoi(argv[2]);
  P = atoi(argv[3]);
  id = atoi(argv[4]);
  mqd_t qdes;
  char *qname = "/m5mathew_mailbox";
  qdes = mq_open(qname, O_RDWR);
  if (qdes == -1) {
    perror("mq_open() failed");
    exit(1);
  }
  mqd_t qdes_cs;
  char *qname_cs = "/m5mathew_mailbox_cs";
  qdes_cs = mq_open(qname_cs, O_RDWR);
  if (qdes_cs == -1) {
    perror("mq_open() for consumption failed");
    exit(1);
  }
  int i = id;
  while (i < N) {
    if (mq_send(qdes, (char *)&i, sizeof(int), 0) == -1) {
      perror("mq_send() failed");
    }
    i += P;
  }
  char *num_prod_cons;
  mq_receive(qdes_cs, num_prod_cons, sizeof(int), 0);
  int producers = atoi(strtok(num_prod_cons, " "));
  producers--;
  int consumers = atoi(strtok(NULL, " "));
  sprintf(num_prod_cons, "%d %d", producers, consumers);
  if (mq_send(qdes_cs, num_prod_cons, sizeof(int), 0) == -1) {
    perror("mq_send() failed");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
lea    0xe23(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   1040 <mq_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1216 <main+0x96>
lea    0xe0b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe06(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1040 <mq_open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1256 <main+0xd6>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    129e <main+0x11e>
mov    -0x24(%rbp),%edi
lea    -0x44(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <mq_send@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0x110>
lea    0xdce(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    125c <main+0xdc>
mov    -0x34(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <mq_receive@plt>
mov    -0x50(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x54(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd8a(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%edx
mov    -0x58(%rbp),%ecx
lea    0xd6b(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x34(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <mq_send@plt>
cmp    $0xffffffff,%eax
jne    132e <main+0x1ae>
lea    0xd30(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
mov    0x18(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
mov    0x20(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%r12d
mov    0x2e30(%rip),%r14
mov    (%r14),%esi
lea    0xe3e(%rip),%rdi
call   1040 <mq_open@plt>
cmp    $0xffffffff,%eax
jne    11d5 <main+0x55>
lea    0xe3f(%rip),%rdi
jmp    11f2 <main+0x72>
mov    %eax,%ebp
mov    (%r14),%esi
lea    0xe42(%rip),%rdi
call   1040 <mq_open@plt>
cmp    $0xffffffff,%eax
jne    1201 <main+0x81>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%r14d
mov    %r12d,0x4(%rsp)
cmp    %r15d,%r12d
jge    124f <main+0xcf>
lea    0x4(%rsp),%r12
lea    0xe3f(%rip),%r13
jmp    122f <main+0xaf>
nopl   0x0(%rax)
mov    0x4(%rsp),%eax
add    %ebx,%eax
mov    %eax,0x4(%rsp)
cmp    %r15d,%eax
jge    124f <main+0xcf>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <mq_send@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0xa0>
mov    %r13,%rdi
call   1060 <perror@plt>
jmp    1220 <main+0xa0>
mov    %r14d,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <mq_receive@plt>
lea    0xe05(%rip),%rbx
mov    %rbx,%rsi
call   1070 <strtok@plt>
mov    %rax,%rdi
call   1080 <atoi@plt>
lea    -0x1(%rax),%ebp
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <strtok@plt>
mov    %rax,%rdi
call   1080 <atoi@plt>
lea    0xddb(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%ecx
call   1090 <sprintf@plt>
mov    %r14d,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <mq_send@plt>
cmp    $0xffffffff,%eax
jne    12ba <main+0x13a>
lea    0xda4(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"/m5mathew_mailbox\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"mq_open() failed\00"
@.str.2 = [21 x i8] c"/m5mathew_mailbox_cs\00"
@.str.3 = [33 x i8] c"mq_open() for consumption failed\00"
@.str.4 = [17 x i8] c"mq_send() failed\00"
@.str.5 = [2 x i8] c" \00"
@.str.6 = [6 x i8] c"%d %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %8
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 3
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %7
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 4
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %9
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %11
%34 = load i8*, i8** %11
%35 = load i32, i32* @O_RDWR
%36 = call i32 @mq_open(i8* %34, i32 %35)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %2
%40 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %2
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8** %13
%43 = load i8*, i8** %13
%44 = load i32, i32* @O_RDWR
%45 = call i32 @mq_open(i8* %43, i32 %44)
store i32 %45, i32* %12
%46 = load i32, i32* %12
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42
%52 = load i32, i32* %9
store i32 %52, i32* %14
br label %53
53: ; preds = %64, %51
%54 = load i32, i32* %14
%55 = load i32, i32* %6
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %68
57: ; preds = %53
%58 = load i32, i32* %10
%59 = bitcast i32* %14 to i8*
%60 = call i32 @mq_send(i32 %58, i8* %59, i32 4, i32 0)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = load i32, i32* %7
%66 = load i32, i32* %14
%67 = add nsw i32 %66, %65
store i32 %67, i32* %14
br label %53
68: ; preds = %53
%69 = load i32, i32* %12
%70 = load i8*, i8** %15
%71 = call i32 @mq_receive(i32 %69, i8* %70, i32 4, i32 0)
%72 = load i8*, i8** %15
%73 = call i8* @strtok(i8* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%74 = call i32 @atoi(i8* %73)
store i32 %74, i32* %16
%75 = load i32, i32* %16
%76 = add nsw i32 %75, -1
store i32 %76, i32* %16
%77 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%78 = call i32 @atoi(i8* %77)
store i32 %78, i32* %17
%79 = load i8*, i8** %15
%80 = load i32, i32* %16
%81 = load i32, i32* %17
%82 = call i32 @sprintf(i8* %79, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %80, i32 %81)
%83 = load i32, i32* %12
%84 = load i8*, i8** %15
%85 = call i32 @mq_send(i32 %83, i8* %84, i32 4, i32 0)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %89
87: ; preds = %68
%88 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %89
89: ; preds = %87, %68
%90 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [18 x i8] c"/m5mathew_mailbox\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"mq_open() failed\00"
@.str.2 = [21 x i8] c"/m5mathew_mailbox_cs\00"
@.str.3 = [33 x i8] c"mq_open() for consumption failed\00"
@.str.4 = [17 x i8] c"mq_send() failed\00"
@.str.5 = [2 x i8] c" \00"
@.str.6 = [6 x i8] c"%d %d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i32 @atoi(i8* %5)
%7 = getelementptr inbounds i8*, i8** %1, i64 3
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 4
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @mq_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* @O_RDWR
%21 = tail call i32 @mq_open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = bitcast i32* %3 to i8*
store i32 %12, i32* %3
%28 = icmp slt i32 %12, %6
br i1 %28, label %29, label %38
29: ; preds = %26, %34
%30 = call i32 @mq_send(i32 %14, i8* %27, i32 4, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* %3
%36 = add nsw i32 %35, %9
store i32 %36, i32* %3
%37 = icmp slt i32 %36, %6
br i1 %37, label %29, label %38
38: ; preds = %34, %26
%39 = call i32 @mq_receive(i32 %21, i8* undef, i32 4, i32 0)
%40 = call i8* @strtok(i8* undef, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @atoi(i8* %40)
%42 = add nsw i32 %41, -1
%43 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @atoi(i8* %43)
%45 = call i32 @sprintf(i8* dereferenceable(1) undef, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %42, i32 %44)
%46 = call i32 @mq_send(i32 %21, i8* undef, i32 4, i32 0)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %38
%51 = call i32 @exit(i32 0)
unreachable
}