#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
scalar_t__ fstat(int, struct stat *);
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;
  struct stat stat_buf;
  fd = shm_open("posixtest_25-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, 16) == -1) {
    perror("An error occurs when calling ftruncate()");
    return 2;
  }
  fd = shm_open("posixtest_25-1", O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("An error occurs when calling shm_open()");
    shm_unlink("posixtest_25-1");
    return 2;
  }
  if (fstat(fd, &stat_buf) != 0) {
    perror("An error occurs when calling fstat()");
    shm_unlink("posixtest_25-1");
    return 2;
  }
  shm_unlink("posixtest_25-1");
  if (stat_buf.st_size == 0) {
    printf("Test PASSED\n");
    return 0;
  }
  printf("The shared memory object is not zero length.\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    0x2e3f(%rip),%rax
or     (%rax),%edx
lea    0xe66(%rip),%rdi
call   1060 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c4 <main+0x64>
lea    0xe5c(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12cf <main+0x16f>
mov    -0x8(%rbp),%edi
mov    $0x10,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11f2 <main+0x92>
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12cf <main+0x16f>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
lea    0xde3(%rip),%rdi
call   1060 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1253 <main+0xf3>
lea    0xdd9(%rip),%rdi
call   1050 <perror@plt>
lea    0xdbe(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    12cf <main+0x16f>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     128d <main+0x12d>
lea    0xdf0(%rip),%rdi
call   1050 <perror@plt>
lea    0xd84(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    12cf <main+0x16f>
lea    0xd6c(%rip),%rdi
call   1070 <shm_unlink@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12bc <main+0x15c>
lea    0xdda(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cf <main+0x16f>
lea    0xdcf(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r14
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e3d(%rip),%r15
mov    0x2e46(%rip),%rbx
mov    (%rbx),%edx
or     (%r15),%edx
lea    0xe6a(%rip),%rdi
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     121b <main+0xbb>
mov    %eax,%edi
mov    $0x10,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1224 <main+0xc4>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    (%rbx),%edx
or     (%r15),%edx
lea    0xe37(%rip),%rdi
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1232 <main+0xd2>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    123b <main+0xdb>
lea    0xe15(%rip),%rdi
call   1070 <shm_unlink@plt>
cmpq   $0x0,0x8(%rsp)
je     120b <main+0xab>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1258 <main+0xf8>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1258 <main+0xf8>
lea    0xded(%rip),%rdi
jmp    122b <main+0xcb>
lea    0xe0c(%rip),%rdi
call   1050 <perror@plt>
jmp    1253 <main+0xf3>
lea    0xdd6(%rip),%rdi
jmp    1242 <main+0xe2>
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
lea    0xdb2(%rip),%rdi
call   1070 <shm_unlink@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [15 x i8] c"posixtest_25-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"
@O_TRUNC = global i32 0
@.str.3 = [37 x i8] c"An error occurs when calling fstat()\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
@.str.5 = [46 x i8] c"The shared memory object is not zero length.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%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 %50
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @ftruncate(i32 %16, i32 16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = 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 %50
21: ; preds = %15
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %27)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %50
34: ; preds = %21
%35 = load i32, i32* %2
%36 = call i64 @fstat(i32 %35, %struct.stat* %3)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, 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 2, i32* %1
br label %50
41: ; preds = %34
%42 = call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%43 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%44 = load i64, i64* %43
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = 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 %50
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %50
50: ; preds = %48, %46, %38, %31, %19, %13
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [15 x i8] c"posixtest_25-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"
@O_TRUNC = global i32 0
@.str.3 = [37 x i8] c"An error occurs when calling fstat()\00"
@str = [45 x i8] c"The shared memory object is not zero length.\00"
@str.6 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%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 %45
13: ; preds = %0
%14 = tail call i32 @ftruncate(i32 %9, i32 16)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
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 %45
18: ; preds = %13
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %20, %19
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = tail call i32 @shm_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, 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 %45
30: ; preds = %18
%31 = call i64 @fstat(i32 %25, %struct.stat* %1)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %45
36: ; preds = %30
%37 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%38 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%39 = load i64, i64* %38
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
br label %45
43: ; preds = %36
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %43, %41, %33, %27, %16, %11
%46 = phi i32 [ 2, %11 ], [ 2, %16 ], [ 2, %27 ], [ 2, %33 ], [ 0, %41 ], [ 1, %43 ]
ret i32 %46
}