#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 AT_FDCWD;
int /*<<< orphan*/ AT_SYMLINK_FOLLOW;
int O_DIRECTORY;
int O_RDONLY;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int linkat(int, char *, int, char const *, int /*<<< orphan*/);
int mkdirat(int, char const *, int);
int openat(int, char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
int main() {
  int subd = openat(AT_FDCWD, "tmp/subdir1", O_RDONLY | O_DIRECTORY);
  fprintf(stderr, "subd is %d\n", subd);
  const char *longstr =
      "abcdef_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789_abcdef_"
      "0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
  int fard = subd;
  for (int i = 0; i < 40; i++) {
    fprintf(stderr, "strlen is %lu\n", (unsigned long)strlen(longstr));
    fprintf(stderr, "making the %d %s in %d\n", i, longstr, fard);
    int retval = mkdirat(fard, longstr, 0777);
    if (retval) {
      perror("mkdirat");
    }
    fard = openat(fard, longstr, O_RDONLY | O_DIRECTORY);
    if (fard < 0) {
      perror("openat");
      fprintf(stderr, "that was the error...\n");
    }
    fprintf(stderr, "fard = %d, errno = %d\n", fard, errno);
  }
  int retval = linkat(subd, "foo_symlink", fard, longstr, AT_SYMLINK_FOLLOW);
  if (retval < 0) {
    perror("trouble");
  }
  printf("retval was %d\n", retval);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    0x2e38(%rip),%rax
or     (%rax),%edx
lea    0xe5f(%rip),%rsi
call   1040 <openat@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe4e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x28,-0x18(%rbp)
jge    12d9 <main+0x169>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1ff,%edx
call   1070 <mkdirat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     125a <main+0xea>
lea    0xe5a(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    0x2d5f(%rip),%rax
or     (%rax),%edx
call   1040 <openat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a8 <main+0x138>
lea    0xe2b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    0x2d05(%rip),%rax
mov    (%rax),%ecx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11db <main+0x6b>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    0x2cf6(%rip),%rax
mov    (%rax),%r8d
lea    0xdf8(%rip),%rsi
call   1080 <linkat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1312 <main+0x1a2>
lea    0xdeb(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%esi
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e6d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe73(%rip),%rsi
call   1030 <openat@plt>
mov    %eax,%r14d
mov    0x2e2c(%rip),%r15
mov    (%r15),%edi
lea    0xe66(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe62(%rip),%rbx
mov    0x2e03(%rip),%rbp
lea    0xf11(%rip),%r13
mov    %r14d,0x4(%rsp)
xor    %r12d,%r12d
jmp    11f1 <main+0x91>
xchg   %ax,%ax
mov    (%r15),%edi
mov    0x0(%rbp),%ecx
mov    %r13,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r12d
cmp    $0x28,%r12d
je     128d <main+0x12d>
mov    (%r15),%edi
mov    $0x6f,%edx
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
lea    0xe86(%rip),%rsi
mov    %r12d,%edx
mov    %rbx,%rcx
mov    %r14d,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1060 <mkdirat@plt>
test   %eax,%eax
jne    127f <main+0x11f>
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
mov    0x2da3(%rip),%rax
or     (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <openat@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11d0 <main+0x70>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
mov    (%r15),%edi
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11d0 <main+0x70>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
jmp    1235 <main+0xd5>
mov    0x2d4c(%rip),%rax
mov    (%rax),%r8d
lea    0xe4e(%rip),%rsi
lea    0xd73(%rip),%rcx
mov    0x4(%rsp),%edi
mov    %r14d,%edx
call   1070 <linkat@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12d6 <main+0x176>
lea    0xe42(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
jmp    12b7 <main+0x157>

LLVM IR (-O0):
@AT_FDCWD = global i32 0
@.str = [12 x i8] c"tmp/subdir1\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"subd is %d\0A\00"
@.str.2 = [112 x i8] c"abcdef_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789_abcdef_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789\00"
@.str.3 = [15 x i8] c"strlen is %lu\0A\00"
@.str.4 = [24 x i8] c"making the %d %s in %d\0A\00"
@.str.5 = [8 x i8] c"mkdirat\00"
@.str.6 = [7 x i8] c"openat\00"
@.str.7 = [23 x i8] c"that was the error...\0A\00"
@.str.8 = [23 x i8] c"fard = %d, errno = %d\0A\00"
@errno = global i32 0
@.str.9 = [12 x i8] c"foo_symlink\00"
@AT_SYMLINK_FOLLOW = global i32 0
@.str.10 = [8 x i8] c"trouble\00"
@.str.11 = [15 x i8] c"retval was %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AT_FDCWD
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_DIRECTORY
%11 = or i32 %9, %10
%12 = call i32 @openat(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* %2
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.2, i64 0, i64 0), i8** %3
%16 = load i32, i32* %2
store i32 %16, i32* %4
store i32 0, i32* %5
br label %17
17: ; preds = %55, %0
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 40
br i1 %19, label %20, label %58
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %3
%23 = call i64 @strlen(i8* %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 %23)
%25 = load i32, i32* @stderr
%26 = load i32, i32* %5
%27 = load i8*, i8** %3
%28 = load i32, i32* %4
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* %27, i32 %28)
%30 = load i32, i32* %4
%31 = load i8*, i8** %3
%32 = call i32 @mkdirat(i32 %30, i8* %31, i32 511)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %20
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %20
%38 = load i32, i32* %4
%39 = load i8*, i8** %3
%40 = load i32, i32* @O_RDONLY
%41 = load i32, i32* @O_DIRECTORY
%42 = or i32 %40, %41
%43 = call i32 @openat(i32 %38, i8* %39, i32 %42)
store i32 %43, i32* %4
%44 = load i32, i32* %4
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %37
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %46, %37
%51 = load i32, i32* @stderr
%52 = load i32, i32* %4
%53 = load i32, i32* @errno
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i32 %52, i32 %53)
br label %55
55: ; preds = %50
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %17
58: ; preds = %17
%59 = load i32, i32* %2
%60 = load i32, i32* %4
%61 = load i8*, i8** %3
%62 = load i32, i32* @AT_SYMLINK_FOLLOW
%63 = call i32 @linkat(i32 %59, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i32 %60, i8* %61, i32 %62)
store i32 %63, i32* %7
%64 = load i32, i32* %7
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %58
%67 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
br label %68
68: ; preds = %66, %58
%69 = load i32, i32* %7
%70 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0), i32 %69)
ret i32 0
}

LLVM IR (-O2):
@AT_FDCWD = global i32 0
@.str = [12 x i8] c"tmp/subdir1\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"subd is %d\0A\00"
@.str.2 = [112 x i8] c"abcdef_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789_abcdef_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789\00"
@.str.3 = [15 x i8] c"strlen is %lu\0A\00"
@.str.4 = [24 x i8] c"making the %d %s in %d\0A\00"
@.str.5 = [8 x i8] c"mkdirat\00"
@.str.6 = [7 x i8] c"openat\00"
@.str.7 = [23 x i8] c"that was the error...\0A\00"
@.str.8 = [23 x i8] c"fard = %d, errno = %d\0A\00"
@errno = global i32 0
@.str.9 = [12 x i8] c"foo_symlink\00"
@AT_SYMLINK_FOLLOW = global i32 0
@.str.10 = [8 x i8] c"trouble\00"
@.str.11 = [15 x i8] c"retval was %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @AT_FDCWD
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_DIRECTORY
%4 = or i32 %3, %2
%5 = tail call i32 @openat(i32 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %12
8: ; preds = %33
%9 = load i32, i32* @AT_SYMLINK_FOLLOW
%10 = tail call i32 @linkat(i32 %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i32 %27, i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %39, label %41
12: ; preds = %0, %33
%13 = phi i32 [ %5, %0 ], [ %27, %33 ]
%14 = phi i32 [ 0, %0 ], [ %37, %33 ]
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 111)
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %14, i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%19 = tail call i32 @mkdirat(i32 %13, i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.2, i64 0, i64 0), i32 511)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %21, %12
%24 = load i32, i32* @O_RDONLY
%25 = load i32, i32* @O_DIRECTORY
%26 = or i32 %25, %24
%27 = tail call i32 @openat(i32 %13, i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %33
33: ; preds = %29, %23
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i32 %27, i32 %35)
%37 = add nuw nsw i32 %14, 1
%38 = icmp eq i32 %37, 40
br i1 %38, label %8, label %12
39: ; preds = %8
%40 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
br label %41
41: ; preds = %39, %8
%42 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0), i32 %10)
ret i32 0
}