#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 */
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ pathsub;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ ENOTDIR;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ check_realpath_step_by_step(char const *,
                                               int /*<<< orphan*/);
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ e(int);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ pathncat(char *, int, char const *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
void check_realpath_recurse(const char *path, int depth) {
  DIR *dir;
  struct dirent *dirent;
  char pathsub[PATH_MAX + 1];
  check_realpath_step_by_step(path, 0);
  if (depth < 1)
    return;
  if (!(dir = opendir(path))) {
    if (errno != ENOTDIR)
      (e(190));
    return;
  }
  while (dirent = readdir(dir)) {
    if (!pathncat(pathsub, sizeof(pathsub), path, dirent->d_name)) {
      (e(198));
      continue;
    }
    check_realpath_recurse(pathsub, depth - 1);
  }
  if (closedir(dir) < 0)
    (e(205));
}


x86 assembly (clang, -O0):
<check_realpath_recurse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <check_realpath_step_by_step@plt>
cmpl   $0x1,-0xc(%rbp)
jge    11d3 <check_realpath_recurse+0x63>
movl   $0x1,-0x34(%rbp)
jmp    129f <check_realpath_recurse+0x12f>
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    121a <check_realpath_recurse+0xaa>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
je     120e <check_realpath_recurse+0x9e>
mov    $0xbe,%edi
call   1080 <e@plt>
movl   $0x1,-0x34(%rbp)
jmp    129f <check_realpath_recurse+0x12f>
jmp    121f <check_realpath_recurse+0xaf>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     127b <check_realpath_recurse+0x10b>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
call   1090 <pathncat@plt>
cmp    $0x0,%eax
jne    1267 <check_realpath_recurse+0xf7>
mov    $0xc6,%edi
call   1080 <e@plt>
jmp    121f <check_realpath_recurse+0xaf>
mov    -0x40(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1050 <check_realpath_recurse@plt>
jmp    121f <check_realpath_recurse+0xaf>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
cmp    $0x0,%rax
jge    1298 <check_realpath_recurse+0x128>
mov    $0xcd,%edi
call   1080 <e@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_realpath_recurse>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e5d(%rip),%rax
mov    (%rax),%r12d
add    $0x1,%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %esi,%esi
call   1060 <check_realpath_step_by_step@plt>
test   %r14d,%r14d
jle    1247 <check_realpath_recurse+0xd7>
mov    %r15,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1231 <check_realpath_recurse+0xc1>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1218 <check_realpath_recurse+0xa8>
add    $0xffffffff,%r14d
jmp    11f8 <check_realpath_recurse+0x88>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <check_realpath_recurse@plt>
mov    %r13,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1218 <check_realpath_recurse+0xa8>
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1090 <pathncat@plt>
test   %eax,%eax
jne    11e0 <check_realpath_recurse+0x70>
mov    $0xc6,%edi
call   1080 <e@plt>
jmp    11eb <check_realpath_recurse+0x7b>
mov    %r13,%rdi
call   1040 <closedir@plt>
test   %rax,%rax
jns    1247 <check_realpath_recurse+0xd7>
mov    $0xcd,%edi
call   1080 <e@plt>
jmp    1247 <check_realpath_recurse+0xd7>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <check_realpath_recurse+0xe6>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xbe,%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <e@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOTDIR = global i64 0
define void @check_realpath_recurse(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* @PATH_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i8*, i8** %3
%16 = call i32 @check_realpath_step_by_step(i8* %15, i32 0)
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 1, i32* %9
br label %56
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %5
%23 = icmp ne i32* %22, null
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOTDIR
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @e(i32 190)
br label %30
30: ; preds = %28, %24
store i32 1, i32* %9
br label %56
31: ; preds = %20
br label %32
32: ; preds = %46, %44, %31
%33 = load i32*, i32** %5
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %6
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %49
36: ; preds = %32
%37 = trunc i64 %12 to i32
%38 = load i8*, i8** %3
%39 = load %struct.dirent*, %struct.dirent** %6
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @pathncat(i8* %14, i32 %37, i8* %38, i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %36
%45 = call i32 @e(i32 198)
br label %32
46: ; preds = %36
%47 = load i32, i32* %4
%48 = sub nsw i32 %47, 1
br label %32
49: ; preds = %32
%50 = load i32*, i32** %5
%51 = call i64 @closedir(i32* %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @e(i32 205)
br label %55
55: ; preds = %53, %49
store i32 0, i32* %9
br label %56
56: ; preds = %55, %30, %19
%57 = load i8*, i8** %7
%58 = load i32, i32* %9
switch i32 %58, label %60 [
i32 0, label %59
i32 1, label %59
]
59: ; preds = %56, %56
ret void
60: ; preds = %56
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOTDIR = global i64 0
define void @check_realpath_recurse(i8* %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32 @check_realpath_step_by_step(i8* %0, i32 0)
%8 = icmp slt i32 %1, 1
br i1 %8, label %40, label %9
9: ; preds = %2
%10 = tail call i32* @opendir(i8* %0)
%11 = icmp eq i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = call %struct.dirent* @readdir(i32* %10)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %35, label %15
15: ; preds = %12
%16 = add nsw i32 %1, -1
br label %23
17: ; preds = %9
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOTDIR
%20 = icmp eq i64 %18, %19
br i1 %20, label %40, label %21
21: ; preds = %17
%22 = tail call i32 @e(i32 190)
br label %40
23: ; preds = %15, %31
%24 = phi %struct.dirent* [ %13, %15 ], [ %32, %31 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @pathncat(i8* %6, i32 %4, i8* %0, i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = call i32 @e(i32 198)
br label %31
31: ; preds = %29, %34
%32 = call %struct.dirent* @readdir(i32* %10)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %35, label %23
34: ; preds = %23
br label %31
35: ; preds = %31, %12
%36 = call i64 @closedir(i32* %10)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @e(i32 205)
br label %40
40: ; preds = %35, %38, %17, %21, %2
ret void
}