#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;
  int mq_msgsize;
  scalar_t__ mq_curmsgs;
  scalar_t__ mq_flags;
};
typedef int /*<<< orphan*/ mqd_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mq_close(int /*<<< orphan*/);
int /*<<< orphan*/ mq_open(char *, int, int, struct mq_attr *);
int /*<<< orphan*/ mq_send(int /*<<< orphan*/, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ mq_unlink(char *);
int /*<<< orphan*/ printf(char *, char *);
int rand();
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int producer() {
  mqd_t mq;
  struct mq_attr attr;
  char buffer[1024];
  int msg, i;
  attr.mq_flags = 0;
  attr.mq_maxmsg = 10;
  attr.mq_msgsize = 1024;
  attr.mq_curmsgs = 0;
  mq = mq_open("/test_ipc", O_CREAT | O_WRONLY, 0644, &attr);
  srand(time(NULL));
  i = 0;
  while (i < 1000000) {
    msg = rand() % 256;
    memset(buffer, 0, 1024);
    sprintf(buffer, "%x", msg);
    printf("Produced: %s\n", buffer);
    fflush(stdout);
    mq_send(mq, buffer, 1024, 0);
    i = i + 1;
  }
  memset(buffer, 0, 1024);
  sprintf(buffer, "done");
  mq_send(mq, buffer, 1024, 0);
  mq_close(mq);
  mq_unlink("/test_ipc");
  return 0;
}


x86 assembly (clang, -O0):
<producer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movq   $0x0,-0x10(%rbp)
movl   $0xa,-0x20(%rbp)
movl   $0x400,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
lea    0xe0e(%rip),%rdi
mov    $0x1a4,%edx
lea    -0x20(%rbp),%rcx
call   1080 <mq_open@plt>
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movl   $0x0,-0x428(%rbp)
cmpl   $0xf4240,-0x428(%rbp)
jge    12bb <producer+0x10b>
mov    $0x0,%al
call   10d0 <rand@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %edx,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%edx
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x420(%rbp),%rsi
lea    0xd8f(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <mq_send@plt>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    121d <producer+0x6d>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
lea    -0x420(%rbp),%rdi
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <mq_send@plt>
mov    -0x4(%rbp),%edi
call   1050 <mq_close@plt>
lea    0xcf8(%rip),%rdi
call   10c0 <mq_unlink@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<producer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
movabs $0x4000000000a,%rax
mov    %rax,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2e19(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0f(%rip),%rdi
lea    0x8(%rsp),%rcx
mov    $0x1a4,%edx
call   1080 <mq_open@plt>
mov    %eax,%r12d
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0xf4240,%ebx
lea    0x20(%rsp),%r13
lea    0xdeb(%rip),%r15
mov    0x2daf(%rip),%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   10d0 <rand@plt>
mov    %eax,%ebp
lea    0xff(%rbp),%eax
test   %ebp,%ebp
cmovns %ebp,%eax
and    $0xffffff00,%eax
sub    %eax,%ebp
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    %r13,%rdi
lea    0xda6(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <printf@plt>
mov    (%r14),%edi
call   10a0 <fflush@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <mq_send@plt>
add    $0xffffffff,%ebx
jne    1230 <producer+0x80>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movl   $0x656e6f64,0x20(%rsp)
65
movb   $0x0,0x24(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <mq_send@plt>
mov    %r12d,%edi
call   1050 <mq_close@plt>
lea    0xd27(%rip),%rdi
call   10c0 <mq_unlink@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mq_attr = type { i32, i32, i64, i64 }
@.str = [10 x i8] c"/test_ipc\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [14 x i8] c"Produced: %s\0A\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"done\00"
define i32 @producer() {
%1 = alloca i32
%2 = alloca %struct.mq_attr
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 3
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 0
store i32 10, i32* %7
%8 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 1
store i32 1024, i32* %8
%9 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %2, i32 0, i32 2
store i64 0, i64* %9
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = call i32 @mq_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12, i32 420, %struct.mq_attr* %2)
store i32 %13, i32* %1
%14 = call i32 @time(i32* null)
%15 = call i32 @srand(i32 %14)
store i32 0, i32* %5
br label %16
16: ; preds = %19, %0
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 1000000
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 256
store i32 %21, i32* %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 1024)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %4
%26 = call i32 (i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i32, i32* %1
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%33 = call i32 @mq_send(i32 %31, i8* %32, i32 1024, i32 0)
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %16
36: ; preds = %16
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 1024)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %1
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%43 = call i32 @mq_send(i32 %41, i8* %42, i32 1024, i32 0)
%44 = load i32, i32* %1
%45 = call i32 @mq_close(i32 %44)
%46 = call i32 @mq_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.mq_attr = type { i32, i32, i64, i64 }
@.str = [10 x i8] c"/test_ipc\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [14 x i8] c"Produced: %s\0A\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"done\00"
define i32 @producer() {
%1 = alloca %struct.mq_attr
%2 = alloca [1024 x i8]
%3 = bitcast %struct.mq_attr* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 0
store i32 10, i32* %5
%6 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 1
store i32 1024, i32* %6
%7 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 2
%8 = bitcast i64* %7 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %8
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_WRONLY
%11 = or i32 %10, %9
%12 = call i32 @mq_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11, i32 420, %struct.mq_attr* %1)
%13 = call i32 @time(i32* null)
%14 = call i32 @srand(i32 %13)
br label %15
15: ; preds = %0, %15
%16 = phi i32 [ 0, %0 ], [ %25, %15 ]
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 256
%19 = call i32 @memset(i8* %4, i32 0, i32 1024)
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%21 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = call i32 @mq_send(i32 %12, i8* %4, i32 1024, i32 0)
%25 = add nuw nsw i32 %16, 1
%26 = icmp eq i32 %25, 1000000
br i1 %26, label %27, label %15
27: ; preds = %15
%28 = call i32 @memset(i8* %4, i32 0, i32 1024)
%29 = call i32 @mq_send(i32 %12, i8* %4, i32 1024, i32 0)
%30 = call i32 @mq_close(i32 %12)
%31 = call i32 @mq_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
ret i32 0
}