#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;
scalar_t__ ENOENT;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_NONE;
scalar_t__ errno;
scalar_t__ ftruncate(int, int);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int /*<<< orphan*/);
int shm_unlink(char *);
int main(void) {
  int fd, result;
  void *ptr;
  result = shm_unlink("posixtest_20-3");
  if (result != 0 && errno != ENOENT) {
    perror("An error occurs when calling shm_unlink()");
    return 2;
  }
  fd = shm_open("posixtest_20-3", O_RDWR | O_CREAT, 0);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  if (ftruncate(fd, 8) != 0) {
    perror("An error occurs when calling ftruncate()");
    shm_unlink("posixtest_20-3");
    return 2;
  }
  ptr = mmap(NULL, 8, PROT_NONE, MAP_SHARED, fd, 0);
  if (ptr != MAP_FAILED) {
    printf("Test PASSED\n");
    shm_unlink("posixtest_20-3");
    return 0;
  } else if (errno == EACCES) {
    printf("The shared memory object is not opened for reading.\n");
    shm_unlink("posixtest_20-3");
    return 1;
  }
  perror("mmap");
  shm_unlink("posixtest_20-3");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1080 <shm_unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ba <main+0x5a>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
je     11ba <main+0x5a>
lea    0xe66(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ff <main+0x19f>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
lea    0xe2d(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ff <main+0x9f>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ff <main+0x19f>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
je     123a <main+0xda>
lea    0xe44(%rip),%rdi
call   1060 <perror@plt>
lea    0xdd7(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ff <main+0x19f>
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
je     129d <main+0x13d>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
lea    0xd74(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <main+0x19f>
mov    0x2d14(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    12db <main+0x17b>
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
lea    0xd36(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ff <main+0x19f>
jmp    12e0 <main+0x180>
lea    0xde5(%rip),%rdi
call   1060 <perror@plt>
lea    0xd0d(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe96(%rip),%rdi
call   1080 <shm_unlink@plt>
test   %eax,%eax
je     118d <main+0x2d>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    1271 <main+0x111>
mov    0x2e3c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5a(%rip),%rdi
xor    %edx,%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     124e <main+0xee>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x8,%esi
call   1050 <ftruncate@plt>
test   %rax,%rax
jne    1257 <main+0xf7>
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
je     1219 <main+0xb9>
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%rdi
call   1080 <shm_unlink@plt>
jmp    1282 <main+0x122>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jne    128b <main+0x12b>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
lea    0xdbe(%rip),%rdi
call   1080 <shm_unlink@plt>
mov    $0x1,%ebx
jmp    1282 <main+0x122>
lea    0xde4(%rip),%rdi
jmp    1278 <main+0x118>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
lea    0xd96(%rip),%rdi
call   1080 <shm_unlink@plt>
jmp    127d <main+0x11d>
lea    0xd97(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1060 <perror@plt>
jmp    123b <main+0xdb>

LLVM IR (-O0):
@.str = [15 x i8] c"posixtest_20-3\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [42 x i8] c"An error occurs when calling shm_unlink()\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"
@.str.3 = [41 x i8] c"An error occurs when calling ftruncate()\00"
@PROT_NONE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
@EACCES = global i64 0
@.str.5 = [53 x i8] c"The shared memory object is not opened for reading.\0A\00"
@.str.6 = [5 x i8] c"mmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp ne i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %52
14: ; preds = %8, %0
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %17, i32 0)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = 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 %52
23: ; preds = %14
%24 = load i32, i32* %2
%25 = call i64 @ftruncate(i32 %24, i32 8)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %52
30: ; preds = %23
%31 = load i32, i32* @PROT_NONE
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* %2
%34 = call i8* @mmap(i32* null, i32 8, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** %4
%35 = load i8*, i8** %4
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp ne i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %52
41: ; preds = %30
%42 = load i64, i64* @errno
%43 = load i64, i64* @EACCES
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %52
48: ; preds = %41
br label %49
49: ; preds = %48
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%51 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %52
52: ; preds = %49, %45, %38, %27, %21, %12
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [15 x i8] c"posixtest_20-3\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [42 x i8] c"An error occurs when calling shm_unlink()\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"
@.str.3 = [41 x i8] c"An error occurs when calling ftruncate()\00"
@PROT_NONE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@EACCES = global i64 0
@.str.6 = [5 x i8] c"mmap\00"
@str = [52 x i8] c"The shared memory object is not opened for reading.\00"
@str.7 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32 %1, 0
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @ENOENT
%6 = icmp eq i64 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %42
9: ; preds = %3, %0
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %42
17: ; preds = %9
%18 = tail call i64 @ftruncate(i32 %13, i32 8)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %42
23: ; preds = %17
%24 = load i32, i32* @PROT_NONE
%25 = load i32, i32* @MAP_SHARED
%26 = tail call i8* @mmap(i32* null, i32 8, i32 %24, i32 %25, i32 %13, i32 0)
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %32, label %29
29: ; preds = %23
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%31 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %42
32: ; preds = %23
%33 = load i64, i64* @errno
%34 = load i64, i64* @EACCES
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%38 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %42
39: ; preds = %32
%40 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%41 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %39, %36, %29, %20, %15, %7
%43 = phi i32 [ 2, %7 ], [ 2, %15 ], [ 2, %20 ], [ 0, %29 ], [ 1, %36 ], [ 1, %39 ]
ret i32 %43
}