#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_RDWR;
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-2");
  if (result != 0 && errno != ENOENT) {
    perror("An error occurs when calling shm_unlink()");
    return 2;
  }
  fd = shm_open("posixtest_20-2", O_RDWR | O_CREAT, 0);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    return 2;
  }
  result = ftruncate(fd, 8);
  if (result == 0) {
    printf("Test PASSED\n");
    shm_unlink("posixtest_20-2");
    return 0;
  } else if (result == -1 && errno == EINVAL) {
    printf("The shared memory object is not opened for writing.\n");
    shm_unlink("posixtest_20-2");
    return 1;
  }
  perror("ftruncate");
  shm_unlink("posixtest_20-2");
  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    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
je     11aa <main+0x5a>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1299 <main+0x149>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
lea    0xe3d(%rip),%rdi
xor    %edx,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ef <main+0x9f>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1299 <main+0x149>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1040 <ftruncate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    122d <main+0xdd>
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
lea    0xde4(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    1299 <main+0x149>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1275 <main+0x125>
mov    0x2d92(%rip),%rax
mov    (%rax),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
jne    1275 <main+0x125>
lea    0xe16(%rip),%rdi
call   1030 <printf@plt>
lea    0xd9c(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    1299 <main+0x149>
jmp    127a <main+0x12a>
lea    0xe22(%rip),%rdi
call   1050 <perror@plt>
lea    0xd73(%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    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <main+0xcd>
mov    0x2e5e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6c(%rip),%rdi
xor    %edx,%edx
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1206 <main+0xb6>
mov    %eax,%edi
mov    $0x8,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x81>
test   %eax,%eax
jne    120f <main+0xbf>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
lea    0xe38(%rip),%rdi
call   1070 <shm_unlink@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2df8(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <main+0xbf>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe06(%rip),%rdi
call   1070 <shm_unlink@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xe2c(%rip),%rdi
jmp    1224 <main+0xd4>
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
jmp    11f3 <main+0xa3>
lea    0xdeb(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
pop    %rcx
ret

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

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