#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__ ENAMETOOLONG;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _PC_NAME_MAX;
int /*<<< orphan*/ _SC_VERSION;
scalar_t__ errno;
char *malloc(long);
long pathconf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
long sysconf(int /*<<< orphan*/);
int main(void) {
  int fd, i;
  long name_max;
  char *shm_name;
  name_max = pathconf("/", _PC_NAME_MAX);
  if (name_max == -1) {
    perror("An error occurs when calling pathconf()");
    return 2;
  }
  shm_name = malloc(name_max + 3);
  shm_name[0] = '/';
  for (i = 1; i < name_max + 2; i++)
    shm_name[i] = 'a';
  shm_name[name_max + 2] = 0;
  fd = shm_open(shm_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd == -1 && errno == ENAMETOOLONG) {
    printf("Test PASSED\n");
    return 0;
  } else if (fd != -1) {
    printf("FAILED: shm_open() succeeded\n");
    shm_unlink(shm_name);
    return 1;
  }
  if (sysconf(_SC_VERSION) >= 200800L) {
    printf("UNTESTED: shm_open() did not fail with ENAMETOLONG\n");
    return 5;
  }
  perror("FAILED: shm_open");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1030 <pathconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11bb <main+0x4b>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12f7 <main+0x187>
mov    -0x18(%rbp),%rdi
add    $0x3,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
movl   $0x1,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0x2,%rcx
cmp    %rcx,%rax
jge    1209 <main+0x99>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11da <main+0x6a>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,0x2(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
or     (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
mov    0x2d9c(%rip),%rax
or     (%rax),%edx
call   1080 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1282 <main+0x112>
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
jne    1282 <main+0x112>
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f7 <main+0x187>
cmpl   $0xffffffff,-0x8(%rbp)
je     12ad <main+0x13d>
lea    0xda4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f7 <main+0x187>
jmp    12b2 <main+0x142>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0x31060,%rax
jl     12e4 <main+0x174>
lea    0xd82(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    12f7 <main+0x187>
lea    0xd9e(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1040 <pathconf@plt>
cmp    $0xffffffffffffffff,%rax
je     1281 <main+0x101>
mov    %rax,%rbx
lea    0x3(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
movb   $0x2f,(%rax)
lea    0x2(%rbx),%r15
test   %rbx,%rbx
js     11e1 <main+0x61>
mov    %r14,%rdi
add    $0x1,%rdi
cmp    $0x3,%r15
mov    $0x2,%edx
cmovge %r15,%rdx
add    $0xffffffffffffffff,%rdx
mov    $0x61,%esi
call   1050 <memset@plt>
movb   $0x0,(%r14,%r15,1)
mov    0x2df3(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db9(%rip),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   1090 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1232 <main+0xb2>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   10a0 <shm_unlink@plt>
mov    $0x1,%eax
jmp    1292 <main+0x112>
mov    0x2d87(%rip),%rax
mov    (%rax),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <main+0xd8>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1292 <main+0x112>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
cmp    $0x31060,%rax
jl     1298 <main+0x118>
lea    0xdc6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
jmp    1292 <main+0x112>
lea    0xd7a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd8b(%rip),%rdi
call   1070 <perror@plt>
jmp    122b <main+0xab>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@_PC_NAME_MAX = global i32 0
@.str.1 = [40 x i8] c"An error occurs when calling pathconf()\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i64 0
@ENAMETOOLONG = global i64 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [30 x i8] c"FAILED: shm_open() succeeded\0A\00"
@_SC_VERSION = global i32 0
@.str.4 = [52 x i8] c"UNTESTED: shm_open() did not fail with ENAMETOLONG\0A\00"
@.str.5 = [17 x i8] c"FAILED: shm_open\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
store i32 0, i32* %1
%6 = load i32, i32* @_PC_NAME_MAX
%7 = call i64 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %6)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = 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 %69
12: ; preds = %0
%13 = load i64, i64* %4
%14 = add nsw i64 %13, 3
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 47, i8* %17
store i32 1, i32* %3
br label %18
18: ; preds = %29, %12
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = load i64, i64* %4
%22 = add nsw i64 %21, 2
%23 = icmp slt i64 %20, %22
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 97, i8* %28
br label %29
29: ; preds = %24
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
br label %18
32: ; preds = %18
%33 = load i8*, i8** %5
%34 = load i64, i64* %4
%35 = add nsw i64 %34, 2
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %5
%38 = load i32, i32* @O_RDWR
%39 = load i32, i32* @O_CREAT
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IWUSR
%43 = or i32 %41, %42
%44 = call i32 @shm_open(i8* %37, i32 %40, i32 %43)
store i32 %44, i32* %2
%45 = load i32, i32* %2
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %32
%48 = load i64, i64* @errno
%49 = load i64, i64* @ENAMETOOLONG
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %69
53: ; preds = %47, %32
%54 = load i32, i32* %2
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%58 = load i8*, i8** %5
%59 = call i32 @shm_unlink(i8* %58)
store i32 1, i32* %1
br label %69
60: ; preds = %53
br label %61
61: ; preds = %60
%62 = load i32, i32* @_SC_VERSION
%63 = call i64 @sysconf(i32 %62)
%64 = icmp sge i64 %63, 200800
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
store i32 5, i32* %1
br label %69
67: ; preds = %61
%68 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %69
69: ; preds = %67, %65, %56, %51, %10
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@_PC_NAME_MAX = global i32 0
@.str.1 = [40 x i8] c"An error occurs when calling pathconf()\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i64 0
@ENAMETOOLONG = global i64 0
@_SC_VERSION = global i32 0
@.str.5 = [17 x i8] c"FAILED: shm_open\00"
@str = [51 x i8] c"UNTESTED: shm_open() did not fail with ENAMETOLONG\00"
@str.6 = [29 x i8] c"FAILED: shm_open() succeeded\00"
@str.7 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = load i32, i32* @_PC_NAME_MAX
%2 = tail call i64 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i64 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %42
6: ; preds = %0
%7 = add nsw i64 %2, 3
%8 = tail call i8* @malloc(i64 %7)
store i8 47, i8* %8
%9 = add i64 %2, 2
%10 = icmp sgt i64 %2, -1
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = getelementptr i8, i8* %8, i64 1
%13 = call i64 @llvm.smax.i64(i64 %9, i64 2)
%14 = add nsw i64 %13, -1
br label %15
15: ; preds = %11, %6
%16 = getelementptr inbounds i8, i8* %8, i64 %9
store i8 0, i8* %16
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = tail call i32 @shm_open(i8* %8, i32 %19, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENAMETOOLONG
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %42
31: ; preds = %15
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.6, i64 0, i64 0))
%33 = tail call i32 @shm_unlink(i8* %8)
br label %42
34: ; preds = %25
%35 = load i32, i32* @_SC_VERSION
%36 = tail call i64 @sysconf(i32 %35)
%37 = icmp sgt i64 %36, 200799
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %42
40: ; preds = %34
%41 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %38, %31, %29, %4
%43 = phi i32 [ 2, %4 ], [ 0, %29 ], [ 1, %31 ], [ 5, %38 ], [ 1, %40 ]
ret i32 %43
}