#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 S_IRUSR;
int S_IWUSR;
int getpid();
scalar_t__ mq_open(char *, int, int, int /*<<< orphan*/);
scalar_t__ mq_unlink(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  char mqname[50];
  mqd_t mqdes;
  sprintf(mqname,
          "/"
          "mq_unlink"
          "_"
          "1-1"
          "_%d",
          getpid());
  mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
  if (mqdes == (mqd_t)-1) {
    perror("unexpected error: "
           "mq_unlink"
           " "
           "1-1"
           ": "
           "mq_open");
    return 2;
  }
  if (mq_unlink(mqname) == 0) {
    if (mq_open(mqname, O_RDWR, S_IRUSR | S_IWUSR, 0) == (mqd_t)-1) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("mq_open succeed unexpectly \n");
      return 1;
    }
  } else {
    perror("unexpected error: "
           "mq_unlink"
           " "
           "1-1"
           ": "
           "mq_unlink");
    printf("Test FAILED\n");
    return 1;
  }
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xe75(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1050 <mq_open@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0xffffffffffffffff,-0x48(%rbp)
jne    11e6 <main+0x86>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1278 <main+0x118>
lea    -0x40(%rbp),%rdi
call   1080 <mq_unlink@plt>
cmp    $0x0,%rax
jne    1259 <main+0xf9>
lea    -0x40(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
xor    %ecx,%ecx
call   1050 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
jne    1241 <main+0xe1>
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <main+0x118>
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1278 <main+0x118>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
lea    0xe24(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe87(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e6b(%rip),%rax
mov    0x2e4c(%rip),%r14
mov    (%r14),%esi
or     (%rax),%esi
mov    0x2e28(%rip),%r15
mov    0x2e31(%rip),%r12
mov    (%r12),%edx
or     (%r15),%edx
mov    %rbx,%rdi
xor    %ecx,%ecx
call   1050 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1214 <main+0xb4>
mov    %rsp,%rdi
call   1080 <mq_unlink@plt>
test   %rax,%rax
jne    1227 <main+0xc7>
mov    (%r14),%esi
mov    (%r12),%edx
or     (%r15),%edx
mov    %rsp,%rdi
xor    %ebx,%ebx
xor    %ecx,%ecx
call   1050 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     11fa <main+0x9a>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1206 <main+0xa6>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%ebx
jmp    1206 <main+0xa6>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
lea    0xe2c(%rip),%rdi
jmp    11ee <main+0x8e>

LLVM IR (-O0):
@.str = [18 x i8] c"/mq_unlink_1-1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [41 x i8] c"unexpected error: mq_unlink 1-1: mq_open\00"
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [29 x i8] c"mq_open succeed unexpectly \0A\00"
@.str.4 = [43 x i8] c"unexpected error: mq_unlink 1-1: mq_unlink\00"
@.str.5 = [13 x i8] c"Test FAILED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca i64
store i32 0, i32* %1
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%5 = call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %11, %12
%14 = call i64 @mq_open(i8* %7, i32 %10, i32 %13, i32 0)
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %38
19: ; preds = %0
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%21 = call i64 @mq_unlink(i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i64 @mq_open(i8* %24, i32 %25, i32 %28, i32 0)
%30 = icmp eq i64 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %38
33: ; preds = %23
%34 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %38
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %38
38: ; preds = %35, %33, %31, %17
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [18 x i8] c"/mq_unlink_1-1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [41 x i8] c"unexpected error: mq_unlink 1-1: mq_open\00"
@.str.4 = [43 x i8] c"unexpected error: mq_unlink 1-1: mq_unlink\00"
@str = [12 x i8] c"Test FAILED\00"
@str.6 = [28 x i8] c"mq_open succeed unexpectly \00"
@str.7 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = call i64 @mq_open(i8* %2, i32 %7, i32 %10, i32 0)
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %32
15: ; preds = %0
%16 = call i64 @mq_unlink(i8* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = call i64 @mq_open(i8* %2, i32 %19, i32 %22, i32 0)
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %32
27: ; preds = %18
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br label %32
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %29, %27, %25, %13
%33 = phi i32 [ 2, %13 ], [ 0, %25 ], [ 1, %27 ], [ 1, %29 ]
ret i32 %33
}