#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;
int O_CREAT;
int O_RDONLY;
int S_IRUSR;
int S_IWUSR;
scalar_t__ errno;
int ftruncate(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int main(void) {
  int fd, result;
  fd = shm_open("posixtest_13-1", O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR);
  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_13-1");
    return 0;
  } else if (result == 0) {
    printf("The file is open for write acces.\n");
    shm_unlink("posixtest_13-1");
    return 1;
  }
  perror("ftruncate");
  shm_unlink("posixtest_13-1");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e57(%rip),%rax
or     (%rax),%edx
lea    0xe76(%rip),%rdi
call   1060 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b4 <main+0x64>
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    125e <main+0x10e>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1040 <ftruncate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    120c <main+0xbc>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%rax
jne    120c <main+0xbc>
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
lea    0xe05(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    125e <main+0x10e>
cmpl   $0x0,-0xc(%rbp)
jne    123a <main+0xea>
lea    0xe27(%rip),%rdi
call   1030 <printf@plt>
lea    0xdd7(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    125e <main+0x10e>
jmp    123f <main+0xef>
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
lea    0xdae(%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
mov    0x2e88(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e56(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe84(%rip),%rdi
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11ec <main+0x9c>
mov    %eax,%edi
mov    $0x8,%esi
call   1040 <ftruncate@plt>
test   %eax,%eax
je     11cd <main+0x7d>
cmp    $0xffffffff,%eax
jne    11ff <main+0xaf>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ff <main+0xaf>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3c(%rip),%rdi
call   1070 <shm_unlink@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe20(%rip),%rdi
call   1070 <shm_unlink@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xe1c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0xe31(%rip),%rdi
call   1050 <perror@plt>
jmp    11d9 <main+0x89>

LLVM IR (-O0):
@.str = [15 x i8] c"posixtest_13-1\00"
@O_RDONLY = 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"
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [35 x i8] c"The file is open for write acces.\0A\00"
@.str.4 = [10 x i8] c"ftruncate\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%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 ([15 x i8], [15 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 %37
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @ftruncate(i32 %16, i32 8)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINVAL
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%26 = 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 %37
27: ; preds = %20, %15
%28 = load i32, i32* %3
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%32 = 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 %37
33: ; preds = %27
br label %34
34: ; preds = %33
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%36 = 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 %37
37: ; preds = %34, %30, %24, %13
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [15 x i8] c"posixtest_13-1\00"
@O_RDONLY = 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"
@errno = global i64 0
@EINVAL = global i64 0
@.str.4 = [10 x i8] c"ftruncate\00"
@str = [34 x i8] c"The file is open for write acces.\00"
@str.5 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%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 ([15 x i8], [15 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 %26
11: ; preds = %0
%12 = tail call i32 @ftruncate(i32 %7, i32 8)
switch i32 %12, label %23 [
i32 -1, label %13
i32 0, label %20
]
13: ; preds = %11
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINVAL
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
%19 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
20: ; preds = %11
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
23: ; preds = %13, %11
%24 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %23, %20, %17, %9
%27 = phi i32 [ 2, %9 ], [ 0, %17 ], [ 1, %20 ], [ 1, %23 ]
ret i32 %27
}