#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 /*<<< 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_14-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()");
    return 2;
  }
  buf = mmap(NULL, 8, PROT_WRITE, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    return 2;
  }
  strcpy(buf, str);
  fd = shm_open("posixtest_14-2", O_RDWR, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  buf = mmap(NULL, 8, PROT_READ, MAP_SHARED, fd, 0);
  if (buf == MAP_FAILED) {
    perror("An error occurs when calling mmap()");
    return 2;
  }
  shm_unlink("posixtest_14-2");
  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    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x2e0c(%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    0xe39(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1373 <main+0x1f3>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     121e <main+0x9e>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1373 <main+0x1f3>
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    0x2db2(%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    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
jne    1275 <main+0xf5>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1373 <main+0x1f3>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
mov    0x2d2d(%rip),%rax
or     (%rax),%edx
lea    0xd64(%rip),%rdi
call   1090 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12ce <main+0x14e>
lea    0xd5a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1373 <main+0x1f3>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edx
mov    0x2d02(%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    0x2ca4(%rip),%rcx
cmp    (%rcx),%rax
jne    1325 <main+0x1a5>
lea    0xd54(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1373 <main+0x1f3>
lea    0xcdc(%rip),%rdi
call   10a0 <shm_unlink@plt>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1360 <main+0x1e0>
lea    0xd3d(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <main+0x1f3>
lea    0xd32(%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   %r13
push   %r12
push   %rbx
push   %rax
movabs $0x797472657771,%rax
mov    %rax,(%rsp)
mov    0x2e30(%rip),%r14
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2dfd(%rip),%r15
mov    0x2e0e(%rip),%r12
mov    (%r12),%edx
or     (%r15),%edx
lea    0xe38(%rip),%rdi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12b1 <main+0x131>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x8,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    12c3 <main+0x143>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%r13
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d8e(%rip),%rbp
cmp    0x0(%rbp),%rax
je     12ba <main+0x13a>
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    (%r14),%esi
mov    (%r12),%edx
or     (%r15),%edx
lea    0xdc0(%rip),%rdi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12b1 <main+0x131>
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%edx
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    $0x8,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    0x0(%rbp),%rax
je     12ba <main+0x13a>
mov    %rax,%rbx
lea    0xd87(%rip),%rdi
call   10a0 <shm_unlink@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12a1 <main+0x121>
lea    0xdef(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12d4 <main+0x154>
lea    0xde8(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    12d4 <main+0x154>
lea    0xd57(%rip),%rdi
jmp    12ca <main+0x14a>
lea    0xd9f(%rip),%rdi
jmp    12ca <main+0x14a>
lea    0xd6d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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_14-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
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [36 x i8] c"An error occurs when calling mmap()\00"
@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 ([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 %66
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 %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %66
23: ; preds = %17
%24 = load i32, i32* @PROT_WRITE
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* %2
%27 = call i8* @mmap(i32* null, i32 8, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = 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 %66
33: ; preds = %23
%34 = load i8*, i8** %4
%35 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @S_IRUSR
%39 = load i32, i32* @S_IWUSR
%40 = or i32 %38, %39
%41 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %40)
store i32 %41, i32* %2
%42 = load i32, i32* %2
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = 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 %66
46: ; preds = %33
%47 = load i32, i32* @PROT_READ
%48 = load i32, i32* @MAP_SHARED
%49 = load i32, i32* %2
%50 = call i8* @mmap(i32* null, i32 8, i32 %47, i32 %48, i32 %49, i32 0)
store i8* %50, i8** %4
%51 = load i8*, i8** %4
%52 = load i8*, i8** @MAP_FAILED
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = 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 %66
56: ; preds = %46
%57 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%58 = load i8*, i8** %4
%59 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%60 = call i64 @strcmp(i8* %58, i8* %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = 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 %66
64: ; preds = %56
%65 = 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 %66
66: ; preds = %64, %62, %54, %44, %31, %21, %15
%67 = load i32, i32* %1
ret i32 %67
}

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