#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 dummy;
};
typedef size_t ssize_t;
typedef scalar_t__ mqd_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mq_close(scalar_t__);
scalar_t__ mq_open(char *, int /*<<< orphan*/);
size_t mq_receive(scalar_t__, char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
int consumer() {
  struct mq_attr attr;
  char buffer[1024 + 1];
  ssize_t bytes_read;
  mqd_t mq = mq_open("/test_ipc", O_RDONLY);
  if ((mqd_t)-1 == mq) {
    printf("Either the producer has not been started or maybe I cannot access "
           "the same memory...\n");
    exit(1);
  }
  do {
    bytes_read = mq_receive(mq, buffer, 1024, NULL);
    buffer[bytes_read] = '\0';
    printf("Consumed: %s\n", buffer);
  } while (0 != strncmp(buffer, "done", strlen("done")));
  mq_close(mq);
  return 0;
}


x86 assembly (clang, -O0):
<consumer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <mq_open@plt>
mov    %rax,-0x420(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    -0x420(%rbp),%rax
jne    11c3 <consumer+0x53>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11c8 <consumer+0x58>
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <mq_receive@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    0xe4a(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x428(%rbp),%rdi
mov    %eax,%edx
lea    0xe35(%rip),%rsi
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11c8 <consumer+0x58>
mov    -0x420(%rbp),%rdi
call   1060 <mq_close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consumer>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1080 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1216 <consumer+0x96>
mov    %rax,%r14
mov    %rsp,%rbx
lea    0xe54(%rip),%r15
lea    0xe5b(%rip),%r12
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1090 <mq_receive@plt>
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <consumer+0x40>
mov    %r14,%rdi
call   1070 <mq_close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe00(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@.str = [10 x i8] c"/test_ipc\00"
@O_RDONLY = global i32 0
@.str.1 = [86 x i8] c"Either the producer has not been started or maybe I cannot access the same memory...\0A\00"
@.str.2 = [14 x i8] c"Consumed: %s\0A\00"
@.str.3 = [5 x i8] c"done\00"
define i32 @consumer() {
%1 = alloca %struct.mq_attr
%2 = alloca [1025 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = load i32, i32* @O_RDONLY
%6 = call i64 @mq_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 -1, %7
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
br label %13
13: ; preds = %21, %12
%14 = load i64, i64* %4
%15 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%16 = call i64 @mq_receive(i64 %14, i8* %15, i32 1024, i32* null)
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %13
%22 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%23 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%24 = call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = icmp ne i64 0, %24
br i1 %25, label %13, label %26
26: ; preds = %21
%27 = load i64, i64* %4
%28 = call i32 @mq_close(i64 %27)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/test_ipc\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"Consumed: %s\0A\00"
@.str.3 = [5 x i8] c"done\00"
@str = [85 x i8] c"Either the producer has not been started or maybe I cannot access the same memory...\00"
define i32 @consumer() {
%1 = alloca [1025 x i8]
%2 = getelementptr inbounds [1025 x i8], [1025 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i64 @mq_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i64 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0, %9
%10 = call i64 @mq_receive(i64 %4, i8* %2, i32 1024, i32* null)
%11 = getelementptr inbounds [1025 x i8], [1025 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%13 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%14 = call i64 @strncmp(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %9
16: ; preds = %9
%17 = call i32 @mq_close(i64 %4)
ret i32 0
}