#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 /*<<< orphan*/ ENAMETOOLONG;
int PATH_MAX;
int do_nftw(char *,
            int (*)(char const *, struct stat const *, int, struct FTW *), int,
            int, int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
int nftw(const char *path,
         int (*fn)(const char *, const struct stat *, int, struct FTW *),
         int fd_limit, int flags) {
  size_t l;
  char pathbuf[PATH_MAX + 1];
  if (fd_limit <= 0)
    return 0;
  l = strlen(path);
  if (l > PATH_MAX) {
    errno = ENAMETOOLONG;
    return -1;
  }
  memcpy(pathbuf, path, l + 1);
  return do_nftw(pathbuf, fn, fd_limit, flags, NULL);
}


x86 assembly (clang, -O0):
<nftw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1192 <nftw+0x62>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1212 <nftw+0xe2>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e46(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jbe    11db <nftw+0xab>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1212 <nftw+0xe2>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <do_nftw@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nftw>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2eab(%rip),%rax
movslq (%rax),%r14
lea    0x1(%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %edx,%edx
jle    1190 <nftw+0x60>
mov    %edx,%r12d
mov    %rdi,%r13
mov    %rsi,%r15
mov    %ecx,-0x2c(%rbp)
call   1030 <strlen@plt>
cmp    %r14,%rax
jbe    1194 <nftw+0x64>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11ba <nftw+0x8a>
xor    %eax,%eax
jmp    11ba <nftw+0x8a>
add    $0x1,%rax
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
mov    -0x2c(%rbp),%ecx
xor    %r8d,%r8d
call   1050 <do_nftw@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type opaque
%struct.FTW = type opaque
%struct.stat.0 = type opaque
%struct.FTW.1 = type opaque
@PATH_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i32 @nftw(i8* %0, i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32 (i8*, %struct.stat*, i32, %struct.FTW*)*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %6
store i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1, i32 (i8*, %struct.stat*, i32, %struct.FTW*)** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i32, i32* @PATH_MAX
%15 = add nsw i32 %14, 1
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %11
%18 = alloca i8, i64 %16
store i64 %16, i64* %12
%19 = load i32, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 0, i32* %5
store i32 1, i32* %13
br label %41
22: ; preds = %4
%23 = load i8*, i8** %6
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = load i32, i32* @PATH_MAX
%27 = sext i32 %26 to i64
%28 = icmp ugt i64 %25, %27
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = load i32, i32* @ENAMETOOLONG
store i32 %30, i32* @errno
store i32 -1, i32* %5
store i32 1, i32* %13
br label %41
31: ; preds = %22
%32 = load i8*, i8** %6
%33 = load i64, i64* %10
%34 = add i64 %33, 1
%35 = call i32 @memcpy(i8* %18, i8* %32, i64 %34)
%36 = load i32 (i8*, %struct.stat*, i32, %struct.FTW*)*, i32 (i8*, %struct.stat*, i32, %struct.FTW*)** %7
%37 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %36 to i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)*
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = call i32 @do_nftw(i8* %18, i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)* %37, i32 %38, i32 %39, i32* null)
store i32 %40, i32* %5
store i32 1, i32* %13
br label %41
41: ; preds = %31, %29, %21
%42 = load i8*, i8** %11
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type opaque
%struct.FTW = type opaque
%struct.stat.0 = type opaque
%struct.FTW.1 = type opaque
@PATH_MAX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i32 @nftw(i8* %0, i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1, i32 %2, i32 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = icmp slt i32 %2, 1
br i1 %9, label %21, label %10
10: ; preds = %4
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = sext i32 %5 to i64
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32, i32* @ENAMETOOLONG
store i32 %15, i32* @errno
br label %21
16: ; preds = %10
%17 = add i64 %11, 1
%18 = call i32 @memcpy(i8* %8, i8* %0, i64 %17)
%19 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1 to i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)*
%20 = call i32 @do_nftw(i8* %8, i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)* %19, i32 %2, i32 %3, i32* null)
br label %21
21: ; preds = %4, %16, %14
%22 = phi i32 [ -1, %14 ], [ %20, %16 ], [ 0, %4 ]
ret i32 %22
}