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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mq_close(int);
int mq_open(char *, int, int, struct mq_attr *);
int mq_receive(int, char *, int, int *);
int mq_send(int, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int main() {
  mqd_t mqd;
  struct mq_attr mq_attr;
  mq_attr.mq_msgsize = 20;
  mq_attr.mq_maxmsg = 10;
  mqd = mq_open("/test", O_RDWR | O_CREAT,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, &mq_attr);
  if (mqd == -1) {
    perror("mq_open error");
  }
  char *string = "hello world";
  char get[sizeof(string)];
  if (mq_send(mqd, string, mq_attr.mq_msgsize, 1) == -1) {
    perror("mq_send error");
    exit(-2);
  }
  int prio = 1;
  if (mq_receive(mqd, get, mq_attr.mq_msgsize, &prio) == -1) {
    perror("mq_receive error");
    exit(-1);
  }
  puts(get);
  printf("%s\n", get);
  mq_close(mqd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x14,-0x10(%rbp)
movl   $0xa,-0xc(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2e00(%rip),%rax
or     (%rax),%edx
mov    0x2def(%rip),%rax
or     (%rax),%edx
lea    0xe26(%rip),%rdi
lea    -0x10(%rbp),%rcx
call   1070 <mq_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fc <main+0x7c>
lea    0xe0f(%rip),%rdi
call   1090 <perror@plt>
lea    0xe11(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    $0x1,%ecx
call   1060 <mq_send@plt>
cmp    $0xffffffff,%eax
jne    123a <main+0xba>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
lea    -0x24(%rbp),%rcx
call   1080 <mq_receive@plt>
cmp    $0xffffffff,%eax
jne    1273 <main+0xf3>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1030 <puts@plt>
lea    -0x20(%rbp),%rsi
lea    0xdb8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <mq_close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e52(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e18(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e16(%rip),%rax
or     (%rax),%edx
movabs $0xa00000014,%rax
mov    %rax,(%rsp)
mov    0x2df7(%rip),%rax
or     (%rax),%edx
lea    0xe2e(%rip),%rdi
mov    %rsp,%rcx
call   1070 <mq_open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     124a <main+0xca>
mov    (%rsp),%edx
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%ecx
call   1060 <mq_send@plt>
cmp    $0xffffffff,%eax
je     1258 <main+0xd8>
movl   $0x1,0xc(%rsp)
mov    (%rsp),%edx
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
call   1080 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     126e <main+0xee>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xe0e(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <mq_close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
jmp    11e1 <main+0x61>
lea    0xdc1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffe,%edi
call   10a0 <exit@plt>
lea    0xdb9(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32, i32 }
@.str = [6 x i8] c"/test\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [14 x i8] c"mq_open error\00"
@.str.2 = [12 x i8] c"hello world\00"
@.str.3 = [14 x i8] c"mq_send error\00"
@.str.4 = [17 x i8] c"mq_receive error\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.mq_attr
%4 = alloca i8*
%5 = alloca [8 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i32 0, i32 0
store i32 20, i32* %7
%8 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i32 0, i32 1
store i32 10, i32* %8
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = call i32 @mq_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %18, %struct.mq_attr* %3)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %0
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %4
%25 = load i32, i32* %2
%26 = load i8*, i8** %4
%27 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @mq_send(i32 %25, i8* %26, i32 %28, i32 1)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 -2)
unreachable
34: ; preds = %24
store i32 1, i32* %6
%35 = load i32, i32* %2
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%37 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @mq_receive(i32 %35, i8* %36, i32 %38, i32* %6)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %34
%45 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%46 = call i32 @puts(i8* %45)
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%48 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %2
%50 = call i32 @mq_close(i32 %49)
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.mq_attr = type { i32, i32 }
@.str = [6 x i8] c"/test\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [14 x i8] c"mq_open error\00"
@.str.2 = [12 x i8] c"hello world\00"
@.str.3 = [14 x i8] c"mq_send error\00"
@.str.4 = [17 x i8] c"mq_receive error\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca %struct.mq_attr
%2 = alloca [8 x i8]
%3 = alloca i32
%4 = bitcast %struct.mq_attr* %1 to i8*
%5 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 0
store i32 20, i32* %5
%6 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %1, i64 0, i32 1
store i32 10, i32* %6
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = call i32 @mq_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %16, %struct.mq_attr* %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %0
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%23 = load i32, i32* %5
%24 = call i32 @mq_send(i32 %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -2)
unreachable
29: ; preds = %21
%30 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%31 = load i32, i32* %5
%32 = call i32 @mq_receive(i32 %17, i8* %22, i32 %31, i32* %3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %29
%38 = call i32 @puts(i8* %22)
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %22)
%40 = call i32 @mq_close(i32 %17)
ret i32 0
}