#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_RDONLY;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
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);
int /*<<< orphan*/ 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_1-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_1-1");
    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_1-1");
    return 2;
  }
  strcpy(buf, str);
  fd = shm_open("posixtest_1-1", O_RDONLY, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    shm_unlink("posixtest_1-1");
    return 2;
  }
  buf = mmap(NULL, 8, PROT_READ, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    shm_unlink("posixtest_1-1");
    return 2;
  }
  shm_unlink("posixtest_1-1");
  if (strcmp(buf, str) == 0) {
    printf("Test PASSED\n");
    return 0;
  }
  printf("Test FAILED\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
lea    0xe43(%rip),%rdi
call   1090 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ef <main+0x6f>
lea    0xe38(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x223>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     122a <main+0xaa>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
lea    0xdef(%rip),%rdi
call   10a0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x223>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    0x2da6(%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    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <main+0x10d>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
lea    0xd8c(%rip),%rdi
call   10a0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x223>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    0x2cfe(%rip),%rax
mov    (%rax),%edx
mov    0x2d0d(%rip),%rax
or     (%rax),%edx
lea    0xd4c(%rip),%rdi
call   1090 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12f2 <main+0x172>
lea    0xd41(%rip),%rdi
call   1080 <perror@plt>
lea    0xd27(%rip),%rdi
call   10a0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x223>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edx
mov    0x2cde(%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    0x2c78(%rip),%rcx
cmp    (%rcx),%rax
jne    1355 <main+0x1d5>
lea    0xd2f(%rip),%rdi
call   1080 <perror@plt>
lea    0xcc4(%rip),%rdi
call   10a0 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x223>
lea    0xcac(%rip),%rdi
call   10a0 <shm_unlink@plt>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1390 <main+0x210>
lea    0xd0c(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a3 <main+0x223>
lea    0xd01(%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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movabs $0x797472657771,%rax
mov    %rax,0x8(%rsp)
mov    0x2e2d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df3(%rip),%r14
mov    0x2e04(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
lea    0xe37(%rip),%rdi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12c8 <main+0x148>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    12d6 <main+0x156>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d85(%rip),%r13
cmp    0x0(%r13),%rax
je     12bf <main+0x13f>
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    (%r15),%edx
or     (%r14),%edx
lea    0xdb8(%rip),%rdi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x15f>
mov    0x2d93(%rip),%rcx
mov    (%rcx),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    0x0(%r13),%rax
je     12bf <main+0x13f>
mov    %rax,%rbx
lea    0xd7b(%rip),%rdi
call   10a0 <shm_unlink@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12af <main+0x12f>
lea    0xde0(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12fc <main+0x17c>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    12fc <main+0x17c>
lea    0xd99(%rip),%rdi
jmp    12e6 <main+0x166>
lea    0xd3f(%rip),%rdi
call   1080 <perror@plt>
jmp    12f7 <main+0x177>
lea    0xd59(%rip),%rdi
jmp    12e6 <main+0x166>
lea    0xd28(%rip),%rdi
call   1080 <perror@plt>
lea    0xd0e(%rip),%rdi
call   10a0 <shm_unlink@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.str = [8 x i8] c"qwerty\00\00"
@.str = [14 x i8] c"posixtest_1-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"
@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"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
@.str.5 = [13 x i8] c"Test FAILED\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 ([14 x i8], [14 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 %70
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 i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %70
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 i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %70
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 i32, i32* @O_RDONLY
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %40, %41
%43 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %39, i32 %42)
store i32 %43, i32* %2
%44 = load i32, i32* %2
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %35
%47 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %70
49: ; preds = %35
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @MAP_SHARED
%52 = load i32, i32* %2
%53 = call i8* @mmap(i32* null, i32 8, i32 %50, i32 %51, i32 %52, i32 0)
store i8* %53, i8** %4
%54 = load i8*, i8** %4
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %60
57: ; preds = %49
%58 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %70
60: ; preds = %49
%61 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%62 = load i8*, i8** %4
%63 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%64 = call i64 @strcmp(i8* %62, i8* %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %70
68: ; preds = %60
%69 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %70
70: ; preds = %68, %66, %57, %46, %32, %21, %15
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
@.str = [14 x i8] c"posixtest_1-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"
@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"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@str = [12 x i8] c"Test FAILED\00"
@str.6 = [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 ([14 x i8], [14 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 %56
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 i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %56
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 i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %56
28: ; preds = %19
%29 = call i32 @strcpy(i8* %22, i8* %2)
%30 = load i32, i32* @O_RDONLY
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %56
39: ; preds = %28
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = call i8* @mmap(i32* null, i32 8, i32 %40, i32 %41, i32 %34, i32 0)
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %56
48: ; preds = %39
%49 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%50 = call i64 @strcmp(i8* %42, i8* %2)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
br label %56
54: ; preds = %48
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %54, %52, %45, %36, %25, %16, %11
%57 = phi i32 [ 2, %11 ], [ 2, %16 ], [ 2, %25 ], [ 2, %36 ], [ 2, %45 ], [ 0, %52 ], [ 1, %54 ]
ret i32 %57
}