#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 */

/* Variables and functions */
scalar_t__ EACCES;
int O_CREAT;
int O_RDWR;
scalar_t__ errno;
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ set_nonroot();
int shm_open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shm_unlink(char *);
int main(void) {
  int fd;
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  fd = shm_open("posixtest_32-1", O_RDWR | O_CREAT, 0);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  fd = shm_open("posixtest_32-1", O_RDWR, 0);
  if (fd == -1 && errno == EACCES) {
    printf("Test PASSED\n");
    shm_unlink("posixtest_32-1");
    return 0;
  } else if (fd != -1) {
    printf("shm_open success.\n");
    shm_unlink("posixtest_32-1");
    return 1;
  }
  perror("shm_open");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    11ae <main+0x4e>
mov    $0x0,%al
call   1050 <set_nonroot@plt>
cmp    $0x0,%rax
je     11a9 <main+0x49>
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    129b <main+0x13b>
jmp    11ae <main+0x4e>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xe60(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f3 <main+0x93>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    129b <main+0x13b>
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1255 <main+0xf5>
mov    0x2daa(%rip),%rax
mov    (%rax),%rax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
jne    1255 <main+0xf5>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
lea    0xde3(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    129b <main+0x13b>
cmpl   $0xffffffff,-0x8(%rbp)
je     1283 <main+0x123>
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
lea    0xdb5(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    129b <main+0x13b>
jmp    1288 <main+0x128>
lea    0xdef(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     11ca <main+0x6a>
mov    0x2e6c(%rip),%rbx
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
lea    0xe7a(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     121b <main+0xbb>
mov    (%rbx),%esi
lea    0xe61(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11e9 <main+0x89>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe42(%rip),%rdi
call   1080 <shm_unlink@plt>
mov    $0x1,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1050 <set_nonroot@plt>
test   %rax,%rax
je     116d <main+0xd>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
pop    %rbx
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <main+0xce>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%rdi
call   1080 <shm_unlink@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%eax
pop    %rbx
ret
lea    0xe02(%rip),%rdi
call   1060 <perror@plt>
jmp    11c3 <main+0x63>

LLVM IR (-O0):
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@.str.1 = [15 x i8] c"posixtest_32-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [19 x i8] c"shm_open success.\0A\00"
@.str.5 = [9 x i8] c"shm_open\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i64 (...) @getuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = call i64 (...) @set_nonroot()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %41
10: ; preds = %5
br label %11
11: ; preds = %10, %0
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 0)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %41
20: ; preds = %11
%21 = load i32, i32* @O_RDWR
%22 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 0)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EACCES
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %41
32: ; preds = %25, %20
%33 = load i32, i32* %2
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %41
38: ; preds = %32
br label %39
39: ; preds = %38
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %41
41: ; preds = %39, %35, %29, %18, %8
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"posixtest_32-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.5 = [9 x i8] c"shm_open\00"
@str = [18 x i8] c"shm_open success.\00"
@str.6 = [12 x i8] c"Test PASSED\00"
@str.7 = [38 x i8] c"Cannot run this test as non-root user\00"
define i32 @main() {
%1 = tail call i64 (...) @getuid()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = tail call i64 (...) @set_nonroot()
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0))
br label %32
8: ; preds = %3, %0
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %32
16: ; preds = %8
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EACCES
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%26 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %32
27: ; preds = %16
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %32
30: ; preds = %20
%31 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %27, %24, %14, %6
%33 = phi i32 [ 5, %6 ], [ 2, %14 ], [ 0, %24 ], [ 1, %27 ], [ 1, %30 ]
ret i32 %33
}