#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;
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 *);
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("shm_open() success.\n");
    shm_unlink(shm_name);
    return 1;
  }
  perror("shm_open");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1030 <pathconf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a9 <main+0x49>
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12c1 <main+0x161>
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    1206 <main+0xa6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    0x2e10(%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    11bf <main+0x5f>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    0x2d98(%rip),%rax
or     (%rax),%edx
call   1070 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    127e <main+0x11e>
mov    0x2d65(%rip),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
jne    127e <main+0x11e>
lea    0xdbd(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c1 <main+0x161>
cmpl   $0xffffffff,-0x8(%rbp)
je     12a9 <main+0x149>
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <shm_unlink@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c1 <main+0x161>
jmp    12ae <main+0x14e>
lea    0xd97(%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    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1040 <pathconf@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x131>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %r14d,%r14d
jle    1212 <main+0xb2>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%r8d
cmp    $0x1,%r14d
jne    11b0 <main+0x50>
xor    %edi,%edi
jmp    11f4 <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    11c0 <main+0x60>
test   $0x1,%r8b
je     1212 <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    0x2db8(%rip),%rax
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1265 <main+0x105>
lea    0xde2(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <shm_unlink@plt>
mov    $0x1,%eax
jmp    1289 <main+0x129>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
jne    12a4 <main+0x144>
lea    0xdc5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd6a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%eax
jmp    1289 <main+0x129>
lea    0xd7f(%rip),%rdi
call   1060 <perror@plt>
jmp    125e <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 = [21 x i8] c"shm_open() success.\0A\00"
@.str.4 = [9 x i8] c"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 %67
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 %67
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 ([21 x i8], [21 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 %67
64: ; preds = %57
br label %65
65: ; preds = %64
%66 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %67
67: ; preds = %65, %60, %55, %10
%68 = load i32, i32* %1
ret i32 %68
}

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
@.str.4 = [9 x i8] c"shm_open\00"
@str = [20 x i8] c"shm_open() success.\00"
@str.5 = [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 %66
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.5, i64 0, i64 0))
br label %66
61: ; preds = %44
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%63 = tail call i32 @shm_unlink(i8* %8)
br label %66
64: ; preds = %55
%65 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %61, %59, %4
%67 = phi i32 [ 2, %4 ], [ 0, %59 ], [ 1, %61 ], [ 1, %64 ]
ret i32 %67
}