#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
scalar_t__ close(int);
scalar_t__ ftruncate(int, int);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
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-2", 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-2");
    return 2;
  }
  buf = mmap(NULL, 8, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    shm_unlink("posixtest_28-2");
    return 2;
  }
  strcpy(buf, str);
  if (close(fd) != 0) {
    perror("An error occurs when calling close()");
    shm_unlink("posixtest_28-2");
    return 2;
  }
  if (shm_unlink("posixtest_28-2") != 0) {
    perror("An error occurs when calling shm_unlink()");
    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    0x2ded(%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    1359 <main+0x1c9>
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    1359 <main+0x1c9>
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    0x2da6(%rip),%rax
or     (%rax),%edx
mov    0x2d8d(%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    0x2d2f(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <main+0x116>
lea    0xddf(%rip),%rdi
call   1090 <perror@plt>
lea    0xd73(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    1359 <main+0x1c9>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
je     12e9 <main+0x159>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
lea    0xd30(%rip),%rdi
call   10b0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    1359 <main+0x1c9>
lea    0xd18(%rip),%rdi
call   10b0 <shm_unlink@plt>
cmp    $0x0,%rax
je     1317 <main+0x187>
lea    0xdab(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1359 <main+0x1c9>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1346 <main+0x1b6>
lea    0xda6(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1359 <main+0x1c9>
lea    0xd9b(%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   %r14
push   %rbx
push   %rax
movabs $0x797472657771,%rax
mov    %rax,(%rsp)
mov    0x2e27(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df5(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe33(%rip),%rdi
call   10a0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1289 <main+0xf9>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    1292 <main+0x102>
mov    0x2dbf(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd5(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
mov    0x2d7f(%rip),%rax
cmp    (%rax),%r14
je     129b <main+0x10b>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %ebx,%edi
call   1070 <close@plt>
test   %rax,%rax
jne    12a4 <main+0x114>
lea    0xdb4(%rip),%rdi
call   10b0 <shm_unlink@plt>
test   %rax,%rax
jne    12be <main+0x12e>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1279 <main+0xe9>
lea    0xe66(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12cf <main+0x13f>
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    12cf <main+0x13f>
lea    0xd7f(%rip),%rdi
jmp    12c5 <main+0x135>
lea    0xd9e(%rip),%rdi
jmp    12ab <main+0x11b>
lea    0xdbe(%rip),%rdi
jmp    12ab <main+0x11b>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
lea    0xd49(%rip),%rdi
call   10b0 <shm_unlink@plt>
jmp    12ca <main+0x13a>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.str = [8 x i8] c"qwerty\00\00"
@.str = [15 x i8] c"posixtest_28-2\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
@PROT_READ = 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 = [37 x i8] c"An error occurs when calling close()\00"
@.str.5 = [42 x i8] c"An error occurs when calling shm_unlink()\00"
@.str.6 = [13 x i8] c"Test PASSED\0A\00"
@.str.7 = [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 %61
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 %61
24: ; preds = %17
%25 = load i32, i32* @PROT_WRITE
%26 = load i32, i32* @PROT_READ
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %2
%30 = call i8* @mmap(i32* null, i32 8, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%36 = 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 %61
37: ; preds = %24
%38 = load i8*, i8** %4
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = load i32, i32* %2
%42 = call i64 @close(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%46 = 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 %61
47: ; preds = %37
%48 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = 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 %61
52: ; preds = %47
%53 = load i8*, i8** %4
%54 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%55 = call i64 @strcmp(i8* %53, i8* %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %61
59: ; preds = %52
%60 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %61
61: ; preds = %59, %57, %50, %44, %34, %21, %15
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [15 x i8] c"posixtest_28-2\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
@PROT_READ = 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 = [37 x i8] c"An error occurs when calling close()\00"
@.str.5 = [42 x i8] c"An error occurs when calling shm_unlink()\00"
@str = [53 x i8] c"The content of the shared memory object was removed.\00"
@str.8 = [12 x i8] c"Test PASSED\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 %49
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 %49
19: ; preds = %13
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @PROT_READ
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i8* @mmap(i32* null, i32 8, i32 %22, i32 %23, i32 %9, i32 0)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %49
30: ; preds = %19
%31 = call i32 @strcpy(i8* %24, i8* %2)
%32 = call i64 @close(i32 %9)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%36 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %49
37: ; preds = %30
%38 = call i64 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %49
42: ; preds = %37
%43 = call i64 @strcmp(i8* %24, i8* %2)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
br label %49
47: ; preds = %42
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %47, %45, %40, %34, %27, %16, %11
%50 = phi i32 [ 2, %11 ], [ 2, %16 ], [ 2, %27 ], [ 2, %34 ], [ 2, %40 ], [ 0, %45 ], [ 1, %47 ]
ret i32 %50
}