#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) {
  int r, cs;
  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);
  r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
  return r;
}


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,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1192 <nftw+0x62>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1218 <nftw+0xe8>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%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,-0x44(%rbp)
jmp    1218 <nftw+0xe8>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x50(%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,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%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 i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
%15 = 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
%16 = load i32, i32* @PATH_MAX
%17 = add nsw i32 %16, 1
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %13
%20 = alloca i8, i64 %18
store i64 %18, i64* %14
%21 = load i32, i32* %8
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %4
store i32 0, i32* %5
store i32 1, i32* %15
br label %44
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = call i64 @strlen(i8* %25)
store i64 %26, i64* %12
%27 = load i64, i64* %12
%28 = load i32, i32* @PATH_MAX
%29 = sext i32 %28 to i64
%30 = icmp ugt i64 %27, %29
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = load i32, i32* @ENAMETOOLONG
store i32 %32, i32* @errno
store i32 -1, i32* %5
store i32 1, i32* %15
br label %44
33: ; preds = %24
%34 = load i8*, i8** %6
%35 = load i64, i64* %12
%36 = add i64 %35, 1
%37 = call i32 @memcpy(i8* %20, i8* %34, i64 %36)
%38 = load i32 (i8*, %struct.stat*, i32, %struct.FTW*)*, i32 (i8*, %struct.stat*, i32, %struct.FTW*)** %7
%39 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %38 to i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)*
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = call i32 @do_nftw(i8* %20, i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)* %39, i32 %40, i32 %41, i32* null)
store i32 %42, i32* %10
%43 = load i32, i32* %10
store i32 %43, i32* %5
store i32 1, i32* %15
br label %44
44: ; preds = %33, %31, %23
%45 = load i8*, i8** %13
%46 = load i32, i32* %5
ret i32 %46
}

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
}