#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 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*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int);
scalar_t__ shm_unlink(char *);
int main(void) {
  int fd;
  char *buf;
  fd = shm_open("posixtest_3-1", 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_3-1");
    return 2;
  }
  if (shm_unlink("posixtest_3-1") != 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;
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    0x2e37(%rip),%rax
or     (%rax),%edx
lea    0xe66(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c4 <main+0x64>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e2 <main+0x182>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
je     11ff <main+0x9f>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
lea    0xe12(%rip),%rdi
call   1080 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e2 <main+0x182>
lea    0xdfa(%rip),%rdi
call   1080 <shm_unlink@plt>
cmp    $0x0,%rax
je     122d <main+0xcd>
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e2 <main+0x182>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    0x2da3(%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,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%rax
jne    129e <main+0x13e>
mov    0x2d45(%rip),%rax
mov    (%rax),%rax
mov    0x2d2b(%rip),%rcx
cmp    (%rcx),%rax
jne    129e <main+0x13e>
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e2 <main+0x182>
mov    -0x10(%rbp),%rax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%rax
jne    12ca <main+0x16a>
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e2 <main+0x182>
jmp    12cf <main+0x16f>
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e34(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe72(%rip),%rdi
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1231 <main+0xd1>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x8,%esi
call   1050 <ftruncate@plt>
test   %rax,%rax
jne    123a <main+0xda>
lea    0xe46(%rip),%rdi
call   1080 <shm_unlink@plt>
test   %rax,%rax
jne    1254 <main+0xf4>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%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    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
je     1208 <main+0xa8>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
jmp    126e <main+0x10e>
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <main+0xfd>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    126e <main+0x10e>
lea    0xdd6(%rip),%rdi
jmp    1264 <main+0x104>
lea    0xdf5(%rip),%rdi
call   1060 <perror@plt>
lea    0xdb3(%rip),%rdi
call   1080 <shm_unlink@plt>
jmp    1269 <main+0x109>
lea    0xe04(%rip),%rdi
jmp    1264 <main+0x104>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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