#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__ ENOENT;
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 != ENOENT && errno != ENOTDIR)
      (e(231));
    return;
  }
  while (dirent = readdir(dir)) {
    if (!pathncat(pathsub, sizeof(pathsub), path, dirent->d_name)) {
      (e(239));
      continue;
    }
    check_realpath_recurse(pathsub, depth - 1);
  }
  if (closedir(dir) < 0)
    (e(246));
}


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    12b9 <check_realpath_recurse+0x149>
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1234 <check_realpath_recurse+0xc4>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
je     1228 <check_realpath_recurse+0xb8>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
je     1228 <check_realpath_recurse+0xb8>
mov    $0xe7,%edi
call   1080 <e@plt>
movl   $0x1,-0x34(%rbp)
jmp    12b9 <check_realpath_recurse+0x149>
jmp    1239 <check_realpath_recurse+0xc9>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1295 <check_realpath_recurse+0x125>
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    1281 <check_realpath_recurse+0x111>
mov    $0xef,%edi
call   1080 <e@plt>
jmp    1239 <check_realpath_recurse+0xc9>
mov    -0x40(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1050 <check_realpath_recurse@plt>
jmp    1239 <check_realpath_recurse+0xc9>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
cmp    $0x0,%rax
jge    12b2 <check_realpath_recurse+0x142>
mov    $0xf6,%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    126b <check_realpath_recurse+0xfb>
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    11f7 <check_realpath_recurse+0x87>
nopw   0x0(%rax,%rax,1)
mov    $0xef,%edi
call   1080 <e@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
je     11e0 <check_realpath_recurse+0x70>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <check_realpath_recurse@plt>
jmp    11ea <check_realpath_recurse+0x7a>
mov    %r13,%rdi
call   1040 <closedir@plt>
test   %rax,%rax
jns    126b <check_realpath_recurse+0xfb>
mov    $0xf6,%edi
call   1080 <e@plt>
jmp    126b <check_realpath_recurse+0xfb>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
je     126b <check_realpath_recurse+0xfb>
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
je     126b <check_realpath_recurse+0xfb>
mov    $0xe7,%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <e@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = 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 %60
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 %35, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOTDIR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @e(i32 231)
br label %34
34: ; preds = %32, %28, %24
store i32 1, i32* %9
br label %60
35: ; preds = %20
br label %36
36: ; preds = %50, %48, %35
%37 = load i32*, i32** %5
%38 = call %struct.dirent* @readdir(i32* %37)
store %struct.dirent* %38, %struct.dirent** %6
%39 = icmp ne %struct.dirent* %38, null
br i1 %39, label %40, label %53
40: ; preds = %36
%41 = trunc i64 %12 to i32
%42 = load i8*, i8** %3
%43 = load %struct.dirent*, %struct.dirent** %6
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @pathncat(i8* %14, i32 %41, i8* %42, i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %40
%49 = call i32 @e(i32 239)
br label %36
50: ; preds = %40
%51 = load i32, i32* %4
%52 = sub nsw i32 %51, 1
br label %36
53: ; preds = %36
%54 = load i32*, i32** %5
%55 = call i64 @closedir(i32* %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @e(i32 246)
br label %59
59: ; preds = %57, %53
store i32 0, i32* %9
br label %60
60: ; preds = %59, %34, %19
%61 = load i8*, i8** %7
%62 = load i32, i32* %9
switch i32 %62, label %64 [
i32 0, label %63
i32 1, label %63
]
63: ; preds = %60, %60
ret void
64: ; preds = %60
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = 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 %43, 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 %38, label %15
15: ; preds = %12
%16 = add nsw i32 %1, -1
br label %26
17: ; preds = %9
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
%21 = load i64, i64* @ENOTDIR
%22 = icmp eq i64 %18, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %43, label %24
24: ; preds = %17
%25 = tail call i32 @e(i32 231)
br label %43
26: ; preds = %15, %34
%27 = phi %struct.dirent* [ %13, %15 ], [ %35, %34 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @pathncat(i8* %6, i32 %4, i8* %0, i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = call i32 @e(i32 239)
br label %34
34: ; preds = %32, %37
%35 = call %struct.dirent* @readdir(i32* %10)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %38, label %26
37: ; preds = %26
br label %34
38: ; preds = %34, %12
%39 = call i64 @closedir(i32* %10)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @e(i32 246)
br label %43
43: ; preds = %38, %41, %17, %24, %2
ret void
}