#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;
int O_TRUNC;
int /*<<< orphan*/ S_IRUSR;
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_34-1", O_RDWR | O_CREAT, S_IRUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  fd = shm_open("posixtest_34-1", O_RDWR | O_TRUNC, 0);
  if (fd == -1 && errno == EACCES) {
    printf("Test PASSED\n");
    shm_unlink("posixtest_34-1");
    return 0;
  } else if (fd != -1) {
    printf("shm_open success.\n");
    shm_unlink("posixtest_34-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    12ab <main+0x14b>
jmp    11ae <main+0x4e>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fa <main+0x9a>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ab <main+0x14b>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
lea    0xe14(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1265 <main+0x105>
mov    0x2d92(%rip),%rax
mov    (%rax),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <main+0x105>
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
lea    0xdd3(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ab <main+0x14b>
cmpl   $0xffffffff,-0x8(%rbp)
je     1293 <main+0x133>
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
lea    0xda5(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ab <main+0x14b>
jmp    1298 <main+0x138>
lea    0xddf(%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     11da <main+0x7a>
mov    0x2e64(%rip),%rbx
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
lea    0xe71(%rip),%rdi
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xcb>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
or     (%rbx),%esi
lea    0xe51(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11f9 <main+0x99>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe32(%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    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
pop    %rbx
ret
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <main+0xde>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
lea    0xdde(%rip),%rdi
call   1080 <shm_unlink@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xddd(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%eax
pop    %rbx
ret
lea    0xdf2(%rip),%rdi
call   1060 <perror@plt>
jmp    11d3 <main+0x73>

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_34-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@O_TRUNC = global i32 0
@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 %44
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 = load i32, i32* @S_IRUSR
%16 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = 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 %44
21: ; preds = %11
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 0)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %21
%29 = load i64, i64* @errno
%30 = load i64, i64* @EACCES
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%34 = 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 %44
35: ; preds = %28, %21
%36 = load i32, i32* %2
%37 = icmp ne i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%40 = 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 %44
41: ; preds = %35
br label %42
42: ; preds = %41
%43 = 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 %44
44: ; preds = %42, %38, %32, %19, %8
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"posixtest_34-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@O_TRUNC = global i32 0
@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 %35
8: ; preds = %3, %0
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRUSR
%13 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %35
17: ; preds = %8
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %19, %18
%21 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = load i64, i64* @EACCES
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, 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 %35
30: ; preds = %17
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %35
33: ; preds = %23
%34 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %30, %27, %15, %6
%36 = phi i32 [ 5, %6 ], [ 2, %15 ], [ 0, %27 ], [ 1, %30 ], [ 1, %33 ]
ret i32 %36
}