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

/* Variables and functions */
scalar_t__ EACCES;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ add_temp_mq(char *);
scalar_t__ errno;
scalar_t__ geteuid();
int getpid();
scalar_t__ mq_close(scalar_t__);
scalar_t__ mq_open(char *, int, int, struct mq_attr *);
scalar_t__ mq_unlink(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ seteuid(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int do_test(void) {
  if (geteuid() != 0) {
    puts("this test requires root");
    return 0;
  }
  char name[sizeof "/tst-mqueue9-" + sizeof(pid_t) * 3];
  snprintf(name, sizeof(name), "/tst-mqueue9-%u", getpid());
  struct mq_attr attr = {.mq_maxmsg = 1, .mq_msgsize = 1};
  mqd_t q = mq_open(name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q == (mqd_t)-1) {
    printf("mq_open failed with: %m\n");
    return 0;
  } else
    add_temp_mq(name);
  if (seteuid(1) != 0) {
    printf("failed to seteuid (1): %m\n");
    mq_unlink(name);
    return 0;
  }
  int result = 0;
  if (mq_unlink(name) == 0) {
    puts("mq_unlink unexpectedly succeeded");
    result = 1;
  } else if (errno != EACCES) {
    printf("mq_unlink did not fail with EACCES: %m\n");
    result = 1;
    ;
  }
  if (seteuid(0) != 0) {
    printf("failed to seteuid (0): %m\n");
    result = 1;
  }
  if (mq_unlink(name) != 0) {
    printf("mq_unlink failed with: %m\n");
    result = 1;
  }
  if (mq_close(q) != 0) {
    printf("mq_close failed with: %m\n");
    result = 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x0,%al
call   1070 <geteuid@plt>
cmp    $0x0,%rax
je     11d1 <do_test+0x31>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    136e <do_test+0x1ce>
lea    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    $0x1a,%esi
lea    0xe26(%rip),%rdx
call   1060 <snprintf@plt>
mov    0xefa(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
mov    0x2dc1(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
lea    -0x28(%rbp),%rcx
call   1090 <mq_open@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1256 <do_test+0xb6>
lea    0xde3(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    136e <do_test+0x1ce>
lea    -0x20(%rbp),%rdi
call   10b0 <add_temp_mq@plt>
mov    $0x1,%edi
call   10c0 <seteuid@plt>
cmp    $0x0,%rax
je     1294 <do_test+0xf4>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
lea    -0x20(%rbp),%rdi
call   10a0 <mq_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    136e <do_test+0x1ce>
movl   $0x0,-0x34(%rbp)
lea    -0x20(%rbp),%rdi
call   10a0 <mq_unlink@plt>
cmp    $0x0,%rax
jne    12c6 <do_test+0x126>
lea    0xda7(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x34(%rbp)
jmp    12f8 <do_test+0x158>
mov    0x2cf3(%rip),%rax
mov    (%rax),%rax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
je     12f3 <do_test+0x153>
lea    0xd96(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x34(%rbp)
jmp    12f8 <do_test+0x158>
xor    %edi,%edi
call   10c0 <seteuid@plt>
cmp    $0x0,%rax
je     131c <do_test+0x17c>
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x34(%rbp)
lea    -0x20(%rbp),%rdi
call   10a0 <mq_unlink@plt>
cmp    $0x0,%rax
je     1342 <do_test+0x1a2>
lea    0xd8a(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <mq_close@plt>
cmp    $0x0,%rax
je     1368 <do_test+0x1c8>
lea    0xd7f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
sub    $0x38,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <geteuid@plt>
test   %rax,%rax
je     11cb <do_test+0x2b>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3d(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x1a,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    0x2e02(%rip),%rax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movabs $0x100000001,%rax
mov    %rax,0x8(%rsp)
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    $0x180,%edx
call   1090 <mq_open@plt>
cmp    $0xffffffffffffffff,%rax
je     126a <do_test+0xca>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
call   10b0 <add_temp_mq@plt>
mov    $0x1,%edi
call   10c0 <seteuid@plt>
test   %rax,%rax
je     127b <do_test+0xdb>
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
call   10a0 <mq_unlink@plt>
xor    %ebx,%ebx
jmp    11c1 <do_test+0x21>
lea    0xdb7(%rip),%rdi
call   1050 <printf@plt>
jmp    11c1 <do_test+0x21>
lea    0x10(%rsp),%rdi
call   10a0 <mq_unlink@plt>
test   %rax,%rax
je     12b0 <do_test+0x110>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rax
mov    0x2d3d(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     12c1 <do_test+0x121>
lea    0xdd4(%rip),%rdi
call   1050 <printf@plt>
jmp    12bc <do_test+0x11c>
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
xor    %edi,%edi
call   10c0 <seteuid@plt>
test   %rax,%rax
je     12de <do_test+0x13e>
lea    0xdd1(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%ebx
lea    0x10(%rsp),%rdi
call   10a0 <mq_unlink@plt>
test   %rax,%rax
je     12fe <do_test+0x15e>
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    %r14,%rdi
call   1080 <mq_close@plt>
test   %rax,%rax
je     11c1 <do_test+0x21>
lea    0xdc5(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%ebx
jmp    11c1 <do_test+0x21>

LLVM IR (-O0):
%struct.mq_attr = type { i32, i32 }
@.str = [24 x i8] c"this test requires root\00"
@.str.1 = [16 x i8] c"/tst-mqueue9-%u\00"
@__const.do_test.attr = %struct.mq_attr { i32 1, i32 1 }
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@.str.2 = [25 x i8] c"mq_open failed with: %m\0A\00"
@.str.3 = [27 x i8] c"failed to seteuid (1): %m\0A\00"
@.str.4 = [33 x i8] c"mq_unlink unexpectedly succeeded\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.5 = [40 x i8] c"mq_unlink did not fail with EACCES: %m\0A\00"
@.str.6 = [27 x i8] c"failed to seteuid (0): %m\0A\00"
@.str.7 = [27 x i8] c"mq_unlink failed with: %m\0A\00"
@.str.8 = [26 x i8] c"mq_close failed with: %m\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca [26 x i8]
%3 = alloca %struct.mq_attr
%4 = alloca i64
%5 = alloca i32
%6 = call i64 (...) @geteuid()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %68
10: ; preds = %0
%11 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%12 = call i32 (...) @getpid()
%13 = call i32 @snprintf(i8* %11, i32 26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = bitcast %struct.mq_attr* %3 to i8*
%15 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_EXCL
%18 = or i32 %16, %17
%19 = load i32, i32* @O_RDWR
%20 = or i32 %18, %19
%21 = call i64 @mq_open(i8* %15, i32 %20, i32 384, %struct.mq_attr* %3)
store i64 %21, i64* %4
%22 = load i64, i64* %4
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %10
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %68
26: ; preds = %10
%27 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%28 = call i32 @add_temp_mq(i8* %27)
br label %29
29: ; preds = %26
%30 = call i64 @seteuid(i32 1)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%34 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%35 = call i64 @mq_unlink(i8* %34)
store i32 0, i32* %1
br label %68
36: ; preds = %29
store i32 0, i32* %5
%37 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%38 = call i64 @mq_unlink(i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %5
br label %49
42: ; preds = %36
%43 = load i64, i64* @errno
%44 = load i64, i64* @EACCES
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %5
br label %48
48: ; preds = %46, %42
br label %49
49: ; preds = %48, %40
%50 = call i64 @seteuid(i32 0)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %5
br label %54
54: ; preds = %52, %49
%55 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%56 = call i64 @mq_unlink(i8* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %5
br label %60
60: ; preds = %58, %54
%61 = load i64, i64* %4
%62 = call i64 @mq_close(i64 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %5
br label %66
66: ; preds = %64, %60
%67 = load i32, i32* %5
store i32 %67, i32* %1
br label %68
68: ; preds = %66, %32, %24, %8
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
%struct.mq_attr = type { i32, i32 }
@.str = [24 x i8] c"this test requires root\00"
@.str.1 = [16 x i8] c"/tst-mqueue9-%u\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@.str.2 = [25 x i8] c"mq_open failed with: %m\0A\00"
@.str.3 = [27 x i8] c"failed to seteuid (1): %m\0A\00"
@.str.4 = [33 x i8] c"mq_unlink unexpectedly succeeded\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.5 = [40 x i8] c"mq_unlink did not fail with EACCES: %m\0A\00"
@.str.6 = [27 x i8] c"failed to seteuid (0): %m\0A\00"
@.str.7 = [27 x i8] c"mq_unlink failed with: %m\0A\00"
@.str.8 = [26 x i8] c"mq_close failed with: %m\0A\00"
define i32 @do_test() {
%1 = alloca [26 x i8]
%2 = alloca i64
%3 = tail call i64 (...) @geteuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %60
7: ; preds = %0
%8 = bitcast i64* %2 to %struct.mq_attr*
%9 = getelementptr inbounds [26 x i8], [26 x i8]* %1, i64 0, i64 0
%10 = tail call i32 (...) @getpid()
%11 = call i32 @snprintf(i8* %9, i32 26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = bitcast i64* %2 to i8*
store i64 4294967297, i64* %2
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_EXCL
%15 = or i32 %14, %13
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = call i64 @mq_open(i8* %9, i32 %17, i32 384, %struct.mq_attr* %8)
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %7
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %58
22: ; preds = %7
%23 = call i32 @add_temp_mq(i8* %9)
%24 = call i64 @seteuid(i32 1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%28 = call i64 @mq_unlink(i8* %9)
br label %58
29: ; preds = %22
%30 = call i64 @mq_unlink(i8* %9)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %40
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @EACCES
%37 = icmp eq i64 %35, %36
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %34, %38, %32
%41 = phi i32 [ 1, %32 ], [ 1, %38 ], [ 0, %34 ]
%42 = call i64 @seteuid(i32 0)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = phi i32 [ 1, %44 ], [ %41, %40 ]
%48 = call i64 @mq_unlink(i8* %9)
%49 = icmp eq i64 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %46
%51 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = phi i32 [ 1, %50 ], [ %47, %46 ]
%54 = call i64 @mq_close(i64 %18)
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %52
%57 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %52, %56, %26, %20
%59 = phi i32 [ 0, %20 ], [ 0, %26 ], [ 1, %56 ], [ %53, %52 ]
br label %60
60: ; preds = %58, %5
%61 = phi i32 [ 0, %5 ], [ %59, %58 ]
ret i32 %61
}