#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 passwd {
  char *pw_name;
  scalar_t__ pw_uid;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EPERM;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
scalar_t__ errno;
struct passwd *getpwent();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ seteuid(scalar_t__);
int shm_open(char *, int, int);
int shm_unlink(char *);
scalar_t__ strcmp(char *, char *);
int main(void) {
  int fd, result;
  struct passwd *pw;
  fd = shm_open("posixtest_9-1", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  while ((pw = getpwent()) != NULL)
    if (strcmp(pw->pw_name, "root") && pw->pw_uid != getuid())
      break;
  if (pw == NULL) {
    printf("There is no other user than current and root.\n");
    return 2;
  }
  if (seteuid(pw->pw_uid) != 0) {
    if (errno == EPERM) {
      printf("You don't have permission to change your UID.\nTry to rerun this "
             "test as root.\n");
      return 2;
    }
    perror("An error occurs when calling seteuid()");
    return 2;
  }
  printf("Testing with user '%s' (uid: %i)\n", pw->pw_name, pw->pw_uid);
  result = shm_unlink("posixtest_9-1");
  if (result == -1 && errno == EACCES) {
    printf("Test PASSED\n");
    seteuid(getuid());
    shm_unlink("posixtest_9-1");
    return 0;
  } else if (result == -1) {
    perror("Unexpected error");
    seteuid(getuid());
    shm_unlink("posixtest_9-1");
    return 1;
  }
  printf("shm_unlink() success.\n");
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
lea    0xe46(%rip),%rdi
call   1080 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e4 <main+0x64>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13ac <main+0x22c>
jmp    11e9 <main+0x69>
mov    $0x0,%al
call   1060 <getpwent@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1248 <main+0xc8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1243 <main+0xc3>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     1243 <main+0xc3>
jmp    1248 <main+0xc8>
jmp    11e9 <main+0x69>
cmpq   $0x0,-0x18(%rbp)
jne    126d <main+0xed>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13ac <main+0x22c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <seteuid@plt>
cmp    $0x0,%rax
je     12d0 <main+0x150>
mov    0x2d35(%rip),%rax
mov    (%rax),%rax
mov    0x2d13(%rip),%rcx
cmp    (%rcx),%rax
jne    12b8 <main+0x138>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13ac <main+0x22c>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13ac <main+0x22c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd0c(%rip),%rdi
call   1090 <shm_unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1355 <main+0x1d5>
mov    0x2cb3(%rip),%rax
mov    (%rax),%rax
mov    0x2cb9(%rip),%rcx
cmp    (%rcx),%rax
jne    1355 <main+0x1d5>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,%rdi
call   10a0 <seteuid@plt>
lea    0xcbc(%rip),%rdi
call   1090 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ac <main+0x22c>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1392 <main+0x212>
lea    0xda9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,%rdi
call   10a0 <seteuid@plt>
lea    0xc7f(%rip),%rdi
call   1090 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ac <main+0x22c>
jmp    1397 <main+0x217>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e44(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe40(%rip),%rdi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1298 <main+0x108>
xor    %eax,%eax
call   1070 <getpwent@plt>
test   %rax,%rax
je     1250 <main+0xc0>
mov    %rax,%rbx
lea    0xe52(%rip),%r14
jmp    11ff <main+0x6f>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getpwent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1250 <main+0xc0>
mov    (%rbx),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <main+0x60>
mov    0x8(%rbx),%r15
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    %rax,%r15
je     11f0 <main+0x60>
mov    0x8(%rbx),%rdi
call   10b0 <seteuid@plt>
test   %rax,%rax
je     1269 <main+0xd9>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rax
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
jne    12ea <main+0x15a>
lea    0xe69(%rip),%rdi
jmp    1257 <main+0xc7>
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%rbx),%rsi
mov    0x8(%rbx),%rdx
lea    0xdeb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd7b(%rip),%rdi
call   10a0 <shm_unlink@plt>
cmp    $0xffffffff,%eax
je     12a6 <main+0x116>
lea    0xdff(%rip),%rdi
jmp    1257 <main+0xc7>
lea    0xd6f(%rip),%rdi
call   1080 <perror@plt>
jmp    125c <main+0xcc>
mov    0x2d13(%rip),%rax
mov    (%rax),%rax
mov    0x2d19(%rip),%rcx
cmp    (%rcx),%rax
jne    12fb <main+0x16b>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%rdi
call   10b0 <seteuid@plt>
lea    0xd20(%rip),%rdi
call   10a0 <shm_unlink@plt>
jmp    1261 <main+0xd1>
lea    0xd4a(%rip),%rdi
call   1080 <perror@plt>
jmp    125c <main+0xcc>
lea    0xd82(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%rdi
call   10b0 <seteuid@plt>
lea    0xce3(%rip),%rdi
call   10a0 <shm_unlink@plt>
mov    $0x1,%ebx
jmp    1261 <main+0xd1>

LLVM IR (-O0):
%struct.passwd = type { i8*, i64 }
@.str = [14 x i8] c"posixtest_9-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [40 x i8] c"An error occurs when calling shm_open()\00"
@.str.2 = [5 x i8] c"root\00"
@.str.3 = [47 x i8] c"There is no other user than current and root.\0A\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.4 = [79 x i8] c"You don't have permission to change your UID.\0ATry to rerun this test as root.\0A\00"
@.str.5 = [39 x i8] c"An error occurs when calling seteuid()\00"
@.str.6 = [34 x i8] c"Testing with user '%s' (uid: %i)\0A\00"
@EACCES = global i64 0
@.str.7 = [13 x i8] c"Test PASSED\0A\00"
@.str.8 = [17 x i8] c"Unexpected error\00"
@.str.9 = [23 x i8] c"shm_unlink() success.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.passwd*
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %84
16: ; preds = %0
br label %17
17: ; preds = %33, %16
%18 = call %struct.passwd* (...) @getpwent()
store %struct.passwd* %18, %struct.passwd** %4
%19 = icmp ne %struct.passwd* %18, null
br i1 %19, label %20, label %34
20: ; preds = %17
%21 = load %struct.passwd*, %struct.passwd** %4
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load %struct.passwd*, %struct.passwd** %4
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 1
%29 = load i64, i64* %28
%30 = call i64 (...) @getuid()
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %26
br label %34
33: ; preds = %26, %20
br label %17
34: ; preds = %32, %17
%35 = load %struct.passwd*, %struct.passwd** %4
%36 = icmp eq %struct.passwd* %35, null
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %84
39: ; preds = %34
%40 = load %struct.passwd*, %struct.passwd** %4
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 1
%42 = load i64, i64* %41
%43 = call i64 @seteuid(i64 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %53
45: ; preds = %39
%46 = load i64, i64* @errno
%47 = load i64, i64* @EPERM
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %84
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %84
53: ; preds = %39
%54 = load %struct.passwd*, %struct.passwd** %4
%55 = getelementptr inbounds %struct.passwd, %struct.passwd* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = load %struct.passwd*, %struct.passwd** %4
%58 = getelementptr inbounds %struct.passwd, %struct.passwd* %57, i32 0, i32 1
%59 = load i64, i64* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* %56, i64 %59)
%61 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 %61, i32* %3
%62 = load i32, i32* %3
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %73
64: ; preds = %53
%65 = load i64, i64* @errno
%66 = load i64, i64* @EACCES
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %73
68: ; preds = %64
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%70 = call i64 (...) @getuid()
%71 = call i64 @seteuid(i64 %70)
%72 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %84
73: ; preds = %64, %53
%74 = load i32, i32* %3
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%78 = call i64 (...) @getuid()
%79 = call i64 @seteuid(i64 %78)
%80 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %84
81: ; preds = %73
br label %82
82: ; preds = %81
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
store i32 2, i32* %1
br label %84
84: ; preds = %82, %76, %68, %51, %49, %37, %14
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i64 }
@.str = [14 x i8] c"posixtest_9-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [40 x i8] c"An error occurs when calling shm_open()\00"
@.str.2 = [5 x i8] c"root\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.5 = [39 x i8] c"An error occurs when calling seteuid()\00"
@.str.6 = [34 x i8] c"Testing with user '%s' (uid: %i)\0A\00"
@EACCES = global i64 0
@.str.8 = [17 x i8] c"Unexpected error\00"
@str = [22 x i8] c"shm_unlink() success.\00"
@str.10 = [12 x i8] c"Test PASSED\00"
@str.11 = [78 x i8] c"You don't have permission to change your UID.\0ATry to rerun this test as root.\00"
@str.12 = [46 x i8] c"There is no other user than current and root.\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = tail call %struct.passwd* (...) @getpwent()
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %28, label %14
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %66
14: ; preds = %9, %25
%15 = phi %struct.passwd* [ %26, %25 ], [ %10, %9 ]
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %14
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 1
%22 = load i64, i64* %21
%23 = tail call i64 (...) @getuid()
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %20, %14
%26 = tail call %struct.passwd* (...) @getpwent()
%27 = icmp eq %struct.passwd* %26, null
br i1 %27, label %28, label %14
28: ; preds = %25, %9
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.12, i64 0, i64 0))
br label %66
30: ; preds = %20
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 1
%32 = load i64, i64* %31
%33 = tail call i64 @seteuid(i64 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %30
%36 = load i64, i64* @errno
%37 = load i64, i64* @EPERM
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @str.11, i64 0, i64 0))
br label %66
41: ; preds = %35
%42 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %66
43: ; preds = %30
%44 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 0
%45 = load i8*, i8** %44
%46 = load i64, i64* %31
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* %45, i64 %46)
%48 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %64
50: ; preds = %43
%51 = load i64, i64* @errno
%52 = load i64, i64* @EACCES
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
%56 = tail call i64 (...) @getuid()
%57 = tail call i64 @seteuid(i64 %56)
%58 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %66
59: ; preds = %50
%60 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%61 = tail call i64 (...) @getuid()
%62 = tail call i64 @seteuid(i64 %61)
%63 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %66
64: ; preds = %43
%65 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %66
66: ; preds = %64, %59, %54, %41, %39, %28, %12
%67 = phi i32 [ 2, %12 ], [ 2, %28 ], [ 2, %39 ], [ 2, %41 ], [ 0, %54 ], [ 1, %59 ], [ 2, %64 ]
ret i32 %67
}