#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__ EBADF;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
scalar_t__ errno;
scalar_t__ ftruncate(int, int);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
scalar_t__ munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int);
scalar_t__ shm_unlink(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  int fd;
  char str[8] = "qwerty";
  char *buf;
  fd = shm_open("posixtest_28-3", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  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_28-3");
    return 2;
  }
  buf = mmap(NULL, 8, PROT_WRITE, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    shm_unlink("posixtest_28-3");
    return 2;
  }
  strcpy(buf, str);
  if (munmap(buf, 8) != 0) {
    perror("An error occurs when calling munmap()");
    shm_unlink("posixtest_28-3");
    return 2;
  }
  if (shm_unlink("posixtest_28-3") != 0) {
    perror("An error occurs when calling shm_unlink()");
    return 2;
  }
  buf = mmap(NULL, 8, PROT_READ, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED && errno == EBADF) {
    printf("The shared memory object was removed.\n");
    return 1;
  } else if (buf == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    return 2;
  }
  if (strcmp(buf, str) == 0) {
    printf("Test PASSED\n");
    return 0;
  }
  printf("The content of the shared memory object was removed.\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    0x2dfc(%rip),%rax
or     (%rax),%edx
lea    0xe33(%rip),%rdi
call   10a0 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ff <main+0x6f>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f8 <main+0x268>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     123a <main+0xaa>
lea    0xe22(%rip),%rdi
call   1090 <perror@plt>
lea    0xddf(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f8 <main+0x268>
mov    0x2d6f(%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   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d28(%rip),%rcx
cmp    (%rcx),%rax
jne    129d <main+0x10d>
lea    0xde8(%rip),%rdi
call   1090 <perror@plt>
lea    0xd7c(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f8 <main+0x268>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1080 <munmap@plt>
cmp    $0x0,%rax
je     12e6 <main+0x156>
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
lea    0xd33(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f8 <main+0x268>
lea    0xd1b(%rip),%rdi
call   10b0 <shm_unlink@plt>
cmp    $0x0,%rax
je     1314 <main+0x184>
lea    0xdaf(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f8 <main+0x268>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edx
mov    0x2cbc(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2c4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1385 <main+0x1f5>
mov    0x2c5e(%rip),%rax
mov    (%rax),%rax
mov    0x2c3c(%rip),%rcx
cmp    (%rcx),%rax
jne    1385 <main+0x1f5>
lea    0xd68(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f8 <main+0x268>
mov    -0x18(%rbp),%rax
mov    0x2c08(%rip),%rcx
cmp    (%rcx),%rax
jne    13b1 <main+0x221>
lea    0xcc8(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f8 <main+0x268>
jmp    13b6 <main+0x226>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    13e5 <main+0x255>
lea    0xd2f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13f8 <main+0x268>
lea    0xd24(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movabs $0x797472657771,%rax
mov    %rax,(%rsp)
mov    0x2e24(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dea(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe30(%rip),%rdi
call   10a0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12d8 <main+0x148>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    12e1 <main+0x151>
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    0x2ddb(%rip),%r14
mov    (%r14),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d77(%rip),%r15
cmp    (%r15),%rax
je     12ea <main+0x15a>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1080 <munmap@plt>
test   %rax,%rax
jne    12f3 <main+0x163>
lea    0xdab(%rip),%rdi
call   10b0 <shm_unlink@plt>
test   %rax,%rax
jne    130d <main+0x17d>
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
mov    (%r14),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    (%r15),%rax
je     129f <main+0x10f>
mov    %rsp,%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12c8 <main+0x138>
lea    0xe37(%rip),%rdi
jmp    12bc <main+0x12c>
mov    0x2d12(%rip),%rax
mov    (%rax),%rax
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%rax
jne    1316 <main+0x186>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    1327 <main+0x197>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1327 <main+0x197>
lea    0xd30(%rip),%rdi
jmp    131d <main+0x18d>
lea    0xd4f(%rip),%rdi
jmp    12fa <main+0x16a>
lea    0xd6f(%rip),%rdi
jmp    12fa <main+0x16a>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
lea    0xcfa(%rip),%rdi
call   10b0 <shm_unlink@plt>
jmp    1322 <main+0x192>
lea    0xd96(%rip),%rdi
jmp    131d <main+0x18d>
lea    0xd43(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.str = [8 x i8] c"qwerty\00\00"
@.str = [15 x i8] c"posixtest_28-3\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 = [41 x i8] c"An error occurs when calling ftruncate()\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [36 x i8] c"An error occurs when calling mmap()\00"
@.str.4 = [38 x i8] c"An error occurs when calling munmap()\00"
@.str.5 = [42 x i8] c"An error occurs when calling shm_unlink()\00"
@PROT_READ = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@.str.6 = [39 x i8] c"The shared memory object was removed.\0A\00"
@.str.7 = [13 x i8] c"Test PASSED\0A\00"
@.str.8 = [54 x i8] c"The content of the shared memory object was removed.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [8 x i8]
%4 = alloca i8*
store i32 0, i32* %1
%5 = bitcast [8 x i8]* %3 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
%12 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = 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 %80
17: ; preds = %0
%18 = load i32, i32* %2
%19 = call i64 @ftruncate(i32 %18, i32 8)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%23 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %80
24: ; preds = %17
%25 = load i32, i32* @PROT_WRITE
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %2
%28 = call i8* @mmap(i32* null, i32 8, i32 %25, i32 %26, i32 %27, i32 0)
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%34 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %80
35: ; preds = %24
%36 = load i8*, i8** %4
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = load i8*, i8** %4
%40 = call i64 @munmap(i8* %39, i32 8)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%44 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %80
45: ; preds = %35
%46 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %80
50: ; preds = %45
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @MAP_SHARED
%53 = load i32, i32* %2
%54 = call i8* @mmap(i32* null, i32 8, i32 %51, i32 %52, i32 %53, i32 0)
store i8* %54, i8** %4
%55 = load i8*, i8** %4
%56 = load i8*, i8** @MAP_FAILED
%57 = icmp eq i8* %55, %56
br i1 %57, label %58, label %64
58: ; preds = %50
%59 = load i64, i64* @errno
%60 = load i64, i64* @EBADF
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %80
64: ; preds = %58, %50
%65 = load i8*, i8** %4
%66 = load i8*, i8** @MAP_FAILED
%67 = icmp eq i8* %65, %66
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %80
70: ; preds = %64
br label %71
71: ; preds = %70
%72 = load i8*, i8** %4
%73 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%74 = call i64 @strcmp(i8* %72, i8* %73)
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %71
%77 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %80
78: ; preds = %71
%79 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %80
80: ; preds = %78, %76, %68, %62, %48, %42, %32, %21, %15
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
@.str = [15 x i8] c"posixtest_28-3\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 = [41 x i8] c"An error occurs when calling ftruncate()\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [36 x i8] c"An error occurs when calling mmap()\00"
@.str.4 = [38 x i8] c"An error occurs when calling munmap()\00"
@.str.5 = [42 x i8] c"An error occurs when calling shm_unlink()\00"
@PROT_READ = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@str = [53 x i8] c"The content of the shared memory object was removed.\00"
@str.9 = [12 x i8] c"Test PASSED\00"
@str.10 = [38 x i8] c"The shared memory object was removed.\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
store i64 133541042419569, i64* %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %61
13: ; preds = %0
%14 = tail call i64 @ftruncate(i32 %9, i32 8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %61
19: ; preds = %13
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i8* @mmap(i32* null, i32 8, i32 %20, i32 %21, i32 %9, i32 0)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %61
28: ; preds = %19
%29 = call i32 @strcpy(i8* %22, i8* %2)
%30 = call i64 @munmap(i8* %22, i32 8)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%34 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %61
35: ; preds = %28
%36 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %61
40: ; preds = %35
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @MAP_SHARED
%43 = call i8* @mmap(i32* null, i32 8, i32 %41, i32 %42, i32 %9, i32 0)
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load i64, i64* @errno
%48 = load i64, i64* @EBADF
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.10, i64 0, i64 0))
br label %61
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %61
54: ; preds = %40
%55 = call i64 @strcmp(i8* %43, i8* %2)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
br label %61
59: ; preds = %54
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
br label %61
61: ; preds = %59, %57, %52, %50, %38, %32, %25, %16, %11
%62 = phi i32 [ 2, %11 ], [ 2, %16 ], [ 2, %25 ], [ 2, %32 ], [ 2, %38 ], [ 1, %50 ], [ 2, %52 ], [ 0, %57 ], [ 1, %59 ]
ret i32 %62
}