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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mq_close(scalar_t__);
scalar_t__ mq_open(char *, int, int, int /*<<< orphan*/ *);
int mq_receive(scalar_t__, char *, int, unsigned int *);
int /*<<< orphan*/ mq_unlink(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  mqd_t mqd = mq_open("/uplook", O_RDWR | O_CREAT, 0666, NULL);
  if (mqd == (mqd_t)-1) {
    do {
      perror("mq_open");
      exit(-1);
    } while (0);
  }
  char rbuf[9000] = {0};
  unsigned prio = 0;
  int n = 0;
  while (1) {
    if ((n = mq_receive(mqd, rbuf, 9000, &prio)) > 0) {
      printf("reveive_num = %d\n", n);
      printf("buf:%s, proj:%u\n", rbuf, prio);
    } else if (n == 0) {
      printf("receive end\n");
      break;
    } else {
      do {
        perror("mq_receive");
        exit(-1);
      } while (0);
    }
  }
  printf("mq_recv over\n");
  mq_close(mqd);
  mq_unlink("/uplook");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2350,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
lea    0xe4e(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <mq_open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11ea <main+0x6a>
jmp    11d4 <main+0x54>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x2340(%rbp),%rdi
xor    %esi,%esi
mov    $0x2328,%edx
call   1040 <memset@plt>
movl   $0x0,-0x2344(%rbp)
movl   $0x0,-0x2348(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2340(%rbp),%rsi
mov    $0x2328,%edx
lea    -0x2344(%rbp),%rcx
call   1070 <mq_receive@plt>
mov    %eax,-0x2348(%rbp)
cmp    $0x0,%eax
jle    1270 <main+0xf0>
mov    -0x2348(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x2340(%rbp),%rsi
mov    -0x2344(%rbp),%edx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ab <main+0x12b>
cmpl   $0x0,-0x2348(%rbp)
jne    1290 <main+0x110>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b0 <main+0x130>
jmp    1295 <main+0x115>
lea    0xda4(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    1211 <main+0x91>
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <mq_close@plt>
lea    0xd32(%rip),%rdi
call   10a0 <mq_unlink@plt>
xor    %eax,%eax
add    $0x2350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2340,%rsp
mov    0x2e39(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1070 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1297 <main+0x107>
mov    %rax,%r12
lea    0x10(%rsp),%rbx
mov    $0x2328,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rcx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x2328,%edx
call   1080 <mq_receive@plt>
test   %eax,%eax
jle    1254 <main+0xc4>
lea    0xe02(%rip),%r14
lea    0xe0d(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r13
nop
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x2328,%edx
mov    %r13,%rcx
call   1080 <mq_receive@plt>
test   %eax,%eax
jg     1220 <main+0x90>
test   %eax,%eax
jne    12a0 <main+0x110>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1060 <mq_close@plt>
lea    0xd81(%rip),%rdi
call   10b0 <mq_unlink@plt>
xor    %eax,%eax
add    $0x2340,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd6a(%rip),%rdi
jmp    12a7 <main+0x117>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/uplook\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [8 x i8] c"mq_open\00"
@.str.2 = [18 x i8] c"reveive_num = %d\0A\00"
@.str.3 = [17 x i8] c"buf:%s, proj:%u\0A\00"
@.str.4 = [13 x i8] c"receive end\0A\00"
@.str.5 = [11 x i8] c"mq_receive\00"
@.str.6 = [14 x i8] c"mq_recv over\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [9000 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i64 @mq_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12, i32 438, i32* null)
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %2
br label %17
17: ; preds = %16
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; No predecessors!
br label %21
21: ; preds = %20, %2
%22 = bitcast [9000 x i8]* %7 to i8*
store i32 0, i32* %8
store i32 0, i32* %9
br label %23
23: ; preds = %21, %45
%24 = load i64, i64* %6
%25 = getelementptr inbounds [9000 x i8], [9000 x i8]* %7, i64 0, i64 0
%26 = call i32 @mq_receive(i64 %24, i8* %25, i32 9000, i32* %8)
store i32 %26, i32* %9
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i32, i32* %9
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [9000 x i8], [9000 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %31, i32 %32)
br label %45
34: ; preds = %23
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %46
39: ; preds = %34
br label %40
40: ; preds = %39
%41 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; No predecessors!
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44, %28
br label %23
46: ; preds = %37
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%48 = load i64, i64* %6
%49 = call i32 @mq_close(i64 %48)
%50 = call i32 @mq_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"/uplook\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [8 x i8] c"mq_open\00"
@.str.2 = [18 x i8] c"reveive_num = %d\0A\00"
@.str.3 = [17 x i8] c"buf:%s, proj:%u\0A\00"
@.str.5 = [11 x i8] c"mq_receive\00"
@str = [12 x i8] c"receive end\00"
@str.7 = [13 x i8] c"mq_recv over\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [9000 x i8]
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i64 @mq_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438, i32* null)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds [9000 x i8], [9000 x i8]* %3, i64 0, i64 0
%15 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%16 = call i32 @mq_receive(i64 %8, i8* %14, i32 9000, i32* %4)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %13, %18
%19 = phi i32 [ %23, %18 ], [ %16, %13 ]
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %14, i32 %21)
%23 = call i32 @mq_receive(i64 %8, i8* %14, i32 9000, i32* %4)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %18, label %25
25: ; preds = %18, %13
%26 = phi i32 [ %16, %13 ], [ %23, %18 ]
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
%31 = call i32 @mq_close(i64 %8)
%32 = call i32 @mq_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
ret i32 0
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
}