#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_PATH_MAX;
int _POSIX_NAME_MAX;
int /*<<< orphan*/ _SC_VERSION;
scalar_t__ errno;
char *malloc(int);
int 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, path_max;
  char *shm_name;
  path_max = pathconf("/", _PC_PATH_MAX);
  if (path_max == -1) {
    perror("An error occurs when calling pathconf()");
    return 2;
  }
  shm_name = malloc(path_max + 1);
  for (i = 0; i < path_max; i++)
    shm_name[i] = (i + 1) % _POSIX_NAME_MAX ? 'a' : '/';
  shm_name[path_max] = 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    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1030 <pathconf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11b9 <main+0x49>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1303 <main+0x193>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1216 <main+0xa6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    0x2e00(%rip),%rcx
cltd
idivl  (%rcx)
mov    $0x2f,%eax
mov    $0x61,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11cf <main+0x5f>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
call   1080 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    128e <main+0x11e>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
jne    128e <main+0x11e>
lea    0xdad(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1303 <main+0x193>
cmpl   $0xffffffff,-0x8(%rbp)
je     12b9 <main+0x149>
lea    0xd98(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    1303 <main+0x193>
jmp    12be <main+0x14e>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0x31060,%rax
jl     12f0 <main+0x180>
lea    0xd77(%rip),%rdi
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    1303 <main+0x193>
lea    0xd93(%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   %r14
push   %rbx
push   %rax
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1040 <pathconf@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x154>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %r14d,%r14d
jle    1222 <main+0xb2>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%r8d
cmp    $0x1,%r14d
jne    11c0 <main+0x50>
xor    %edi,%edi
jmp    1204 <main+0x94>
mov    %r8d,%r10d
and    $0xfffffffe,%r10d
xor    %ecx,%ecx
mov    $0x2f,%r9d
nop
lea    0x1(%rcx),%eax
cltd
idiv   %esi
test   %edx,%edx
mov    $0x61,%eax
cmove  %r9d,%eax
mov    %al,(%rbx,%rcx,1)
lea    0x2(%rcx),%rdi
mov    %edi,%eax
cltd
idiv   %esi
test   %edx,%edx
mov    $0x61,%eax
cmove  %r9d,%eax
mov    %al,0x1(%rbx,%rcx,1)
mov    %rdi,%rcx
cmp    %rdi,%r10
jne    11d0 <main+0x60>
test   $0x1,%r8b
je     1222 <main+0xb2>
lea    0x1(%rdi),%eax
cltd
idiv   %esi
test   %edx,%edx
mov    $0x2f,%eax
mov    $0x61,%ecx
cmove  %eax,%ecx
mov    %cl,(%rbx,%rdi,1)
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    0x2da8(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d66(%rip),%rax
mov    0x2d87(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1275 <main+0x105>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1090 <shm_unlink@plt>
mov    $0x1,%eax
jmp    12d5 <main+0x165>
mov    0x2d34(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <main+0x12b>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    12d5 <main+0x165>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0x31060,%rax
jl     12dd <main+0x16d>
lea    0xd83(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
jmp    12d5 <main+0x165>
lea    0xd37(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd46(%rip),%rdi
call   1060 <perror@plt>
jmp    126e <main+0xfe>

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@_PC_PATH_MAX = global i32 0
@.str.1 = [40 x i8] c"An error occurs when calling pathconf()\00"
@_POSIX_NAME_MAX = global i32 0
@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 = [30 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_PATH_MAX
%2 = tail call i32 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %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 %72
6: ; preds = %0
%7 = add nuw nsw i32 %2, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %44
10: ; preds = %6
%11 = load i32, i32* @_POSIX_NAME_MAX
%12 = zext i32 %2 to i64
%13 = and i64 %12, 1
%14 = icmp eq i32 %2, 1
br i1 %14, label %34, label %15
15: ; preds = %10
%16 = and i64 %12, 4294967294
br label %17
17: ; preds = %17, %15
%18 = phi i64 [ 0, %15 ], [ %26, %17 ]
%19 = phi i64 [ 0, %15 ], [ %32, %17 ]
%20 = or i64 %18, 1
%21 = trunc i64 %20 to i32
%22 = srem i32 %21, %11
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i8 47, i8 97
%25 = getelementptr inbounds i8, i8* %8, i64 %18
store i8 %24, i8* %25
%26 = add nuw nsw i64 %18, 2
%27 = trunc i64 %26 to i32
%28 = srem i32 %27, %11
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, i8 47, i8 97
%31 = getelementptr inbounds i8, i8* %8, i64 %20
store i8 %30, i8* %31
%32 = add i64 %19, 2
%33 = icmp eq i64 %32, %16
br i1 %33, label %34, label %17
34: ; preds = %17, %10
%35 = phi i64 [ 0, %10 ], [ %26, %17 ]
%36 = icmp eq i64 %13, 0
br i1 %36, label %44, label %37
37: ; preds = %34
%38 = trunc i64 %35 to i32
%39 = add i32 %38, 1
%40 = srem i32 %39, %11
%41 = icmp eq i32 %40, 0
%42 = select i1 %41, i8 47, i8 97
%43 = getelementptr inbounds i8, i8* %8, i64 %35
store i8 %42, i8* %43
br label %44
44: ; preds = %37, %34, %6
%45 = sext i32 %2 to i64
%46 = getelementptr inbounds i8, i8* %8, i64 %45
store i8 0, i8* %46
%47 = load i32, i32* @O_RDWR
%48 = load i32, i32* @O_CREAT
%49 = or i32 %48, %47
%50 = load i32, i32* @S_IRUSR
%51 = load i32, i32* @S_IWUSR
%52 = or i32 %51, %50
%53 = tail call i32 @shm_open(i8* %8, i32 %49, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %44
%56 = load i64, i64* @errno
%57 = load i64, i64* @ENAMETOOLONG
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %72
61: ; preds = %44
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%63 = tail call i32 @shm_unlink(i8* %8)
br label %72
64: ; preds = %55
%65 = load i32, i32* @_SC_VERSION
%66 = tail call i64 @sysconf(i32 %65)
%67 = icmp sgt i64 %66, 200799
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %72
70: ; preds = %64
%71 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %72
72: ; preds = %70, %68, %61, %59, %4
%73 = phi i32 [ 2, %4 ], [ 0, %59 ], [ 1, %61 ], [ 5, %68 ], [ 1, %70 ]
ret i32 %73
}