#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 /*<<< orphan*/ mqd_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int /*<<< orphan*/);
int mq_getattr(int /*<<< orphan*/, struct mq_attr *);
int mq_receive(int /*<<< orphan*/, char *, int /*<<< orphan*/,
               int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
void msg_rcv(mqd_t mqdes, int i) {
  struct mq_attr attr;
  if (mq_getattr(mqdes, &attr) == -1) {
    perror("mq_getattr");
    exit(1);
  }
  char *buf = malloc(attr.mq_msgsize);
  if (mq_receive(mqdes, buf, attr.mq_msgsize, NULL) == -1) {
    perror("mq_receive");
    fflush(stdout);
    sleep(1);
    exit(1);
  }
  if (i != atoi(buf)) {
    printf("Msg mismatch: expected: %d, got: %s\n", i, buf);
    fflush(stdout);
    sleep(1);
    exit(1);
  }
  free(buf);
}


x86 assembly (clang, -O0):
<msg_rcv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10c0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    11d9 <msg_rcv+0x39>
lea    0xe36(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
jne    122f <msg_rcv+0x8f>
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
je     1280 <msg_rcv+0xe0>
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xdbd(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msg_rcv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rsp,%rsi
call   10c0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
je     11fb <msg_rcv+0x5b>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1070 <mq_receive@plt>
cmp    $0xffffffff,%eax
je     1211 <msg_rcv+0x71>
mov    %rbx,%rdi
call   1090 <atoi@plt>
cmp    %r14d,%eax
jne    121f <msg_rcv+0x7f>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
jmp    1231 <msg_rcv+0x91>
lea    0xdf0(%rip),%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
@.str = [11 x i8] c"mq_getattr\00"
@.str.1 = [11 x i8] c"mq_receive\00"
@stdout = global i32 0
@.str.2 = [37 x i8] c"Msg mismatch: expected: %d, got: %s\0A\00"
define void @msg_rcv(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.mq_attr
%6 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = call i32 @mq_getattr(i32 %7, %struct.mq_attr* %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %6
%17 = load i32, i32* %3
%18 = load i8*, i8** %6
%19 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @mq_receive(i32 %17, i8* %18, i32 %20, i32* null)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
%27 = call i32 @sleep(i32 1)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %13
%30 = load i32, i32* %4
%31 = load i8*, i8** %6
%32 = call i32 @atoi(i8* %31)
%33 = icmp ne i32 %30, %32
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = load i32, i32* %4
%36 = load i8*, i8** %6
%37 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %35, i8* %36)
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
%40 = call i32 @sleep(i32 1)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
ret void
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@.str = [11 x i8] c"mq_getattr\00"
@.str.1 = [11 x i8] c"mq_receive\00"
@stdout = global i32 0
@.str.2 = [37 x i8] c"Msg mismatch: expected: %d, got: %s\0A\00"
define void @msg_rcv(i32 %0, i32 %1) {
%3 = alloca %struct.mq_attr
%4 = bitcast %struct.mq_attr* %3 to i8*
%5 = call i32 @mq_getattr(i32 %0, %struct.mq_attr* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i8* @malloc(i32 %12)
%14 = call i32 @mq_receive(i32 %0, i8* %13, i32 %12, i32* null)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = call i32 @sleep(i32 1)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %10
%23 = call i32 @atoi(i8* %13)
%24 = icmp eq i32 %23, %1
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %1, i8* %13)
%27 = load i32, i32* @stdout
%28 = call i32 @fflush(i32 %27)
%29 = call i32 @sleep(i32 1)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = call i32 @free(i8* %13)
ret void
}