#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 */
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  int fd1, fd2;
  char path[25] = "/tmp/posixtestXXXXXX";
  fd1 = mkstemp(path);
  if (fd1 == -1) {
    perror("An error occurs when calling mkstemp()");
    return 2;
  }
  fd2 = shm_open("posixtest_8-1", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd2 == -1) {
    perror("An error occurs when calling shm_open()");
    unlink(path);
    return 2;
  }
  unlink(path);
  shm_unlink("posixtest_8-1");
  if (fd2 == (fd1 + 1)) {
    printf("Test PASSED\n");
    return 0;
  }
  printf("Test FAILED\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe84(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe82(%rip),%al
mov    %al,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <mkstemp@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c7 <main+0x67>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1276 <main+0x116>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2def(%rip),%rax
or     (%rax),%edx
lea    0xe4e(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1225 <main+0xc5>
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    1276 <main+0x116>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
lea    0xe0b(%rip),%rdi
call   1080 <shm_unlink@plt>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    1263 <main+0x103>
lea    0xe24(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <main+0x116>
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movups 0xe9c(%rip),%xmm0
movups %xmm0,0x9(%rsp)
movaps 0xe87(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1050 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1206 <main+0xa6>
mov    %eax,%ebx
mov    0x2e4d(%rip),%rax
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e23(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe89(%rip),%rdi
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1219 <main+0xb9>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
lea    0xe6e(%rip),%rdi
call   1080 <shm_unlink@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11ee <main+0x8e>
lea    0xe9d(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    11ff <main+0x9f>
lea    0xe81(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe0c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%eax
jmp    11ff <main+0x9f>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    $0x2,%eax
jmp    11ff <main+0x9f>

LLVM IR (-O0):
@__const.main.path = [25 x i8] c"/tmp/posixtestXXXXXX\00\00\00\00\00"
@.str = [39 x i8] c"An error occurs when calling mkstemp()\00"
@.str.1 = [14 x i8] c"posixtest_8-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = 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"
@.str.4 = [13 x i8] c"Test FAILED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [25 x i8]
store i32 0, i32* %1
%5 = bitcast [25 x i8]* %4 to i8*
%6 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%7 = call i32 @mkstemp(i8* %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %38
12: ; preds = %0
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%25 = call i32 @unlink(i8* %24)
store i32 2, i32* %1
br label %38
26: ; preds = %12
%27 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%28 = call i32 @unlink(i8* %27)
%29 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %3
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
%33 = icmp eq i32 %30, %32
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %38
36: ; preds = %26
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %38
38: ; preds = %36, %34, %22, %10
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@__const.main.path = [25 x i8] c"/tmp/posixtestXXXXXX\00\00\00\00\00"
@.str = [39 x i8] c"An error occurs when calling mkstemp()\00"
@.str.1 = [14 x i8] c"posixtest_8-1\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [40 x i8] c"An error occurs when calling shm_open()\00"
@str = [12 x i8] c"Test FAILED\00"
@str.5 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%3 = call i32 @mkstemp(i8* %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %28
7: ; preds = %0
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @unlink(i8* %2)
br label %28
19: ; preds = %7
%20 = call i32 @unlink(i8* %2)
%21 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = add nuw nsw i32 %3, 1
%23 = icmp eq i32 %14, %22
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %28
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %26, %24, %16, %5
%29 = phi i32 [ 2, %5 ], [ 2, %16 ], [ 0, %24 ], [ 1, %26 ]
ret i32 %29
}