#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 stat {
  int /*<<< orphan*/ st_dev;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int _nftwRec(char const *,
             int (*)(char const *, struct stat const *, int, struct FTW *), int,
             int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
             int /*<<< orphan*/, struct stat *);
int fchdir(int);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int _nftw(const char *dirpath,
          int (*fn)(const char *fpath, const struct stat *sb, int typeflag,
                    struct FTW *ftwbuf),
          int nopenfd, int flags) {
  int cwdfd, status;
  struct stat sBuf;
  cwdfd = open(".", O_RDONLY);
  if (cwdfd == -1) {
    return -1;
  }
  if (fstat(cwdfd, &sBuf) == -1) {
    return -1;
  }
  int fdCount = 0;
  status =
      _nftwRec(dirpath, fn, nopenfd, flags, 0, 0, &fdCount, sBuf.st_dev, &sBuf);
  if (fchdir(cwdfd) == -1) {
    return -1;
  }
  return status;
}


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    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1184 <_nftw+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <_nftw+0xc5>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11a5 <_nftw+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <_nftw+0xc5>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x30(%rbp),%r10d
xor    %r9d,%r9d
lea    -0x34(%rbp),%r11
lea    -0x30(%rbp),%rax
mov    %r9d,%r8d
mov    %r11,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1030 <_nftwRec@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11ff <_nftw+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <_nftw+0xc5>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nftw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r13
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1050 <open@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ce <_nftw+0x8e>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ce <_nftw+0x8e>
movl   $0x0,0x4(%rsp)
mov    (%rsp),%eax
sub    $0x8,%rsp
lea    0xc(%rsp),%r10
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
lea    0x8(%rsp),%rbp
push   %rbp
push   %rax
push   %r10
call   1030 <_nftwRec@plt>
add    $0x20,%rsp
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <fchdir@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.FTW = type opaque
%struct.FTW.0 = type opaque
@.str = [2 x i8] c".\00"
@O_RDONLY = 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 %struct.stat
%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* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %39
19: ; preds = %4
%20 = load i32, i32* %10
%21 = call i32 @fstat(i32 %20, %struct.stat* %12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %5
br label %39
24: ; preds = %19
store i32 0, i32* %13
%25 = load i8*, i8** %6
%26 = load i32 (i8*, %struct.stat*, i32, %struct.FTW*)*, i32 (i8*, %struct.stat*, i32, %struct.FTW*)** %7
%27 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %26 to i32 (i8*, %struct.stat*, i32, %struct.FTW.0*)*
%28 = load i32, i32* %8
%29 = load i32, i32* %9
%30 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @_nftwRec(i8* %25, i32 (i8*, %struct.stat*, i32, %struct.FTW.0*)* %27, i32 %28, i32 %29, i32 0, i32 0, i32* %13, i32 %31, %struct.stat* %12)
store i32 %32, i32* %11
%33 = load i32, i32* %10
%34 = call i32 @fchdir(i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %24
store i32 -1, i32* %5
br label %39
37: ; preds = %24
%38 = load i32, i32* %11
store i32 %38, i32* %5
br label %39
39: ; preds = %37, %36, %23, %18
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.FTW = type opaque
%struct.FTW.0 = type opaque
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
define i32 @_nftw(i8* %0, i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1, i32 %2, i32 %3) {
%5 = alloca %struct.stat
%6 = alloca i32
%7 = bitcast %struct.stat* %5 to i8*
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %23, label %11
11: ; preds = %4
%12 = call i32 @fstat(i32 %9, %struct.stat* %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%16 = bitcast i32 (i8*, %struct.stat*, i32, %struct.FTW*)* %1 to i32 (i8*, %struct.stat*, i32, %struct.FTW.0*)*
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @_nftwRec(i8* %0, i32 (i8*, %struct.stat*, i32, %struct.FTW.0*)* %16, i32 %2, i32 %3, i32 0, i32 0, i32* %6, i32 %18, %struct.stat* %5)
%20 = call i32 @fchdir(i32 %9)
%21 = icmp eq i32 %20, -1
%22 = select i1 %21, i32 -1, i32 %19
br label %23
23: ; preds = %11, %4, %14
%24 = phi i32 [ %22, %14 ], [ -1, %4 ], [ -1, %11 ]
ret i32 %24
}