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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
void *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ mq_close(int);
int mq_getattr(int, struct mq_attr *);
int mq_open(char *, int /*<<< orphan*/);
long mq_receive(int, void *, int /*<<< orphan*/, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, int);
int main(int argc, char *argv[]) {
  int r, prio;
  long n;
  mqd_t mqd;
  struct mq_attr attr;
  void *ptr;
  mqd = mq_open(argv[1], O_RDONLY);
  if (mqd == -1) {
    perror("mq_open");
    exit(-1);
  }
  r = mq_getattr(mqd, &attr);
  if (r == -1) {
    perror("mq_getattr");
    exit(-1);
  }
  ptr = malloc(attr.mq_msgsize);
  n = mq_receive(mqd, ptr, attr.mq_msgsize, &prio);
  printf("receive %ld bytes in %d prio\n", n, prio);
  mq_close(mqd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1050 <mq_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11cf <main+0x4f>
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <mq_getattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fe <main+0x7e>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    -0x18(%rbp),%rcx
call   1070 <mq_receive@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xde4(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
call   1040 <mq_close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1050 <mq_open@plt>
cmp    $0xffffffff,%eax
je     11e9 <main+0x69>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0x72>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1070 <mq_receive@plt>
mov    0x4(%rsp),%edx
lea    0xe42(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <mq_close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe10(%rip),%rdi
jmp    11f9 <main+0x79>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [8 x i8] c"mq_open\00"
@.str.1 = [11 x i8] c"mq_getattr\00"
@.str.2 = [30 x i8] c"receive %ld bytes in %d prio\0A\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 i64
%9 = alloca i32
%10 = alloca %struct.mq_attr
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @mq_open(i8* %14, i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i32, i32* %9
%24 = call i32 @mq_getattr(i32 %23, %struct.mq_attr* %10)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %22
%31 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %10, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %11
%34 = load i32, i32* %9
%35 = load i8*, i8** %11
%36 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %10, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @mq_receive(i32 %34, i8* %35, i32 %37, i32* %7)
store i64 %38, i64* %8
%39 = load i64, i64* %8
%40 = load i32, i32* %7
%41 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 %39, i32 %40)
%42 = load i32, i32* %9
%43 = call i32 @mq_close(i32 %42)
ret i32 0
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@O_RDONLY = global i32 0
@.str = [8 x i8] c"mq_open\00"
@.str.1 = [11 x i8] c"mq_getattr\00"
@.str.2 = [30 x i8] c"receive %ld bytes in %d prio\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.mq_attr
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.mq_attr* %4 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @mq_open(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = call i32 @mq_getattr(i32 %10, %struct.mq_attr* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
%22 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %4, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i8* @malloc(i32 %23)
%25 = call i64 @mq_receive(i32 %10, i8* %24, i32 %23, i32* %3)
%26 = load i32, i32* %3
%27 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 %25, i32 %26)
%28 = call i32 @mq_close(i32 %10)
ret i32 0
}