#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 PTHREAD_CANCEL_DISABLE;
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);
int /*<<< orphan*/ pthread_setcancelstate(int, int *);
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);
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
  pthread_setcancelstate(cs, 0);
  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    0x2e93(%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     11a2 <nftw+0x62>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1246 <nftw+0x106>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e36(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jbe    11eb <nftw+0xab>
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
mov    0x2e02(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1246 <nftw+0x106>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <pthread_setcancelstate@plt>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <do_nftw@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_setcancelstate@plt>
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
sub    $0x18,%rsp
mov    %rsp,%r14
mov    0x2e95(%rip),%r15
mov    (%r15),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %edx,%edx
jle    11a9 <nftw+0x69>
mov    %edx,%r12d
mov    %rdi,%r13
mov    %rsi,-0x38(%rbp)
mov    %ecx,-0x2c(%rbp)
call   1030 <strlen@plt>
movslq (%r15),%rcx
cmp    %rcx,%rax
jbe    11ad <nftw+0x6d>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    11f2 <nftw+0xb2>
xor    %ebx,%ebx
jmp    11f2 <nftw+0xb2>
add    $0x1,%rax
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1040 <pthread_setcancelstate@plt>
mov    %rbx,%rdi
mov    -0x38(%rbp),%rsi
mov    %r12d,%edx
mov    -0x2c(%rbp),%ecx
xor    %r8d,%r8d
call   1060 <do_nftw@plt>
mov    %eax,%ebx
mov    -0x30(%rbp),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
mov    %r14,%rsp
mov    %ebx,%eax
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
@PTHREAD_CANCEL_DISABLE = 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 %48
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 %48
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, i32* @PTHREAD_CANCEL_DISABLE
%39 = call i32 @pthread_setcancelstate(i32 %38, i32* %11)
%40 = load i32 (i8*, %struct.stat*, i32, %struct.FTW*)*, i32 (i8*, %struct.stat*, i32, %struct.FTW*)** %7
%41 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %40 to i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)*
%42 = load i32, i32* %8
%43 = load i32, i32* %9
%44 = call i32 @do_nftw(i8* %20, i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)* %41, i32 %42, i32 %43, i32* null)
store i32 %44, i32* %10
%45 = load i32, i32* %11
%46 = call i32 @pthread_setcancelstate(i32 %45, i32* null)
%47 = load i32, i32* %10
store i32 %47, i32* %5
store i32 1, i32* %15
br label %48
48: ; preds = %33, %31, %23
%49 = load i8*, i8** %13
%50 = load i32, i32* %5
ret i32 %50
}

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
@PTHREAD_CANCEL_DISABLE = global i32 0
define i32 @nftw(i8* %0, i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = icmp slt i32 %2, 1
br i1 %12, label %29, label %13
13: ; preds = %4
%14 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%15 = load i32, i32* @PATH_MAX
%16 = sext i32 %15 to i64
%17 = icmp ugt i64 %14, %16
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* @ENAMETOOLONG
store i32 %19, i32* @errno
br label %29
20: ; preds = %13
%21 = add i64 %14, 1
%22 = call i32 @memcpy(i8* %11, i8* %0, i64 %21)
%23 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%24 = call i32 @pthread_setcancelstate(i32 %23, i32* %5)
%25 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1 to i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)*
%26 = call i32 @do_nftw(i8* %11, i32 (i8*, %struct.stat.0*, i32, %struct.FTW.1*)* %25, i32 %2, i32 %3, i32* null)
%27 = load i32, i32* %5
%28 = call i32 @pthread_setcancelstate(i32 %27, i32* null)
br label %29
29: ; preds = %4, %20, %18
%30 = phi i32 [ -1, %18 ], [ %26, %20 ], [ 0, %4 ]
ret i32 %30
}