#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__ EINVAL;
scalar_t__ ENOENT;
int O_CREAT;
int O_RDONLY;
int /*<<< orphan*/ S_IRUSR;
scalar_t__ errno;
int ftruncate(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int /*<<< orphan*/);
int shm_unlink(char *);
int main(void) {
  int fd, result;
  result = shm_unlink("posixtest_20-1");
  if (result != 0 && errno != ENOENT) {
    perror("An error occurs when calling shm_unlink()");
    return 2;
  }
  fd = shm_open("posixtest_20-1", O_RDONLY | O_CREAT, S_IRUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  result = ftruncate(fd, 8);
  if (result == -1 && errno == EINVAL) {
    printf("Test PASSED\n");
    shm_unlink("posixtest_20-1");
    return 0;
  } else if (result == 0) {
    printf("The shared memory object is opened for writing.\n");
    shm_unlink("posixtest_20-1");
    return 1;
  }
  perror("ftruncate");
  shm_unlink("posixtest_20-1");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1070 <shm_unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11aa <main+0x5a>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     11aa <main+0x5a>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a0 <main+0x150>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
lea    0xe34(%rip),%rdi
call   1060 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f6 <main+0xa6>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a0 <main+0x150>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1040 <ftruncate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    124e <main+0xfe>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <main+0xfe>
lea    0xe30(%rip),%rdi
call   1030 <printf@plt>
lea    0xdc3(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a0 <main+0x150>
cmpl   $0x0,-0xc(%rbp)
jne    127c <main+0x12c>
lea    0xe0f(%rip),%rdi
call   1030 <printf@plt>
lea    0xd95(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a0 <main+0x150>
jmp    1281 <main+0x131>
lea    0xe17(%rip),%rdi
call   1050 <perror@plt>
lea    0xd6c(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xea8(%rip),%rdi
call   1070 <shm_unlink@plt>
test   %eax,%eax
je     117b <main+0x2b>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    1224 <main+0xd4>
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
lea    0xe63(%rip),%rdi
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     120d <main+0xbd>
mov    %eax,%edi
mov    $0x8,%esi
call   1040 <ftruncate@plt>
test   %eax,%eax
je     11ee <main+0x9e>
cmp    $0xffffffff,%eax
jne    1216 <main+0xc6>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <main+0xc6>
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1070 <shm_unlink@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
lea    0xdff(%rip),%rdi
call   1070 <shm_unlink@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xe25(%rip),%rdi
jmp    122b <main+0xdb>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
jmp    11fa <main+0xaa>
lea    0xde4(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"posixtest_20-1\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [42 x i8] c"An error occurs when calling shm_unlink()\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@EINVAL = global i64 0
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [49 x i8] c"The shared memory object is opened for writing.\0A\00"
@.str.5 = [10 x i8] c"ftruncate\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %45
13: ; preds = %7, %0
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %45
23: ; preds = %13
%24 = load i32, i32* %2
%25 = call i32 @ftruncate(i32 %24, i32 8)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINVAL
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %45
35: ; preds = %28, %23
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %45
41: ; preds = %35
br label %42
42: ; preds = %41
%43 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %45
45: ; preds = %42, %38, %32, %21, %11
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str = [15 x i8] c"posixtest_20-1\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [42 x i8] c"An error occurs when calling shm_unlink()\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@EINVAL = global i64 0
@.str.5 = [10 x i8] c"ftruncate\00"
@str = [48 x i8] c"The shared memory object is opened for writing.\00"
@str.6 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32 %1, 0
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @ENOENT
%6 = icmp eq i64 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %33
9: ; preds = %3, %0
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRUSR
%14 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %33
18: ; preds = %9
%19 = tail call i32 @ftruncate(i32 %14, i32 8)
switch i32 %19, label %30 [
i32 -1, label %20
i32 0, label %27
]
20: ; preds = %18
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINVAL
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%26 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %33
27: ; preds = %18
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %33
30: ; preds = %20, %18
%31 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%32 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %30, %27, %24, %16, %7
%34 = phi i32 [ 2, %7 ], [ 2, %16 ], [ 0, %24 ], [ 1, %27 ], [ 1, %30 ]
ret i32 %34
}