#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 */
typedef struct TYPE_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct stat {
  scalar_t__ st_ino;
  scalar_t__ st_dev;
};
struct TYPE_6__ {
  int fts_options;
};
struct TYPE_5__ {
  scalar_t__ fts_ino;
  scalar_t__ fts_dev;
};
typedef TYPE_1__ FTSENT;
typedef TYPE_2__ FTS;

/* Variables and functions */
int ENOENT;
int FTS_NOCHDIR;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int fstat(int, struct stat *);
int open(char const *, int);
int fts_safe_changedir(const FTS *sp, const FTSENT *p, int fd,
                       const char *path) {
  int oldfd = fd, ret = -1;
  struct stat sb;
  if ((sp->fts_options & (FTS_NOCHDIR)))
    return 0;
  if (oldfd < 0 && (fd = open(path, O_RDONLY | 0)) == -1)
    return -1;
  if (fstat(fd, &sb) == -1)
    goto bail;
  if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
    errno = ENOENT;
    goto bail;
  }
  ret = fchdir(fd);
bail:
  if (oldfd < 0) {
    int save_errno = errno;
    (void)close(fd);
    errno = save_errno;
  }
  return ret;
}


x86 assembly (clang, -O0):
<fts_safe_changedir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e5f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1188 <fts_safe_changedir+0x48>
movl   $0x0,-0x4(%rbp)
jmp    124e <fts_safe_changedir+0x10e>
cmpl   $0x0,-0x2c(%rbp)
jge    11bf <fts_safe_changedir+0x7f>
mov    -0x28(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11bf <fts_safe_changedir+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <fts_safe_changedir+0x10e>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11d9 <fts_safe_changedir+0x99>
jmp    121e <fts_safe_changedir+0xde>
mov    -0x40(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jne    11fc <fts_safe_changedir+0xbc>
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     1213 <fts_safe_changedir+0xd3>
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcc(%rip),%rax
mov    %ecx,(%rax)
jmp    121e <fts_safe_changedir+0xde>
mov    -0x1c(%rbp),%edi
call   1040 <fchdir@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1248 <fts_safe_changedir+0x108>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x44(%rbp),%ecx
mov    0x2d92(%rip),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fts_safe_changedir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
xor    %ebp,%ebp
test   %eax,(%rdi)
jne    11ec <fts_safe_changedir+0xac>
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edx,%r14d
test   %edx,%edx
jns    1182 <fts_safe_changedir+0x42>
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    %rcx,%rdi
call   1050 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     11e7 <fts_safe_changedir+0xa7>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   1060 <fstat@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11cc <fts_safe_changedir+0x8c>
mov    0x8(%rsp),%rax
cmp    (%r15),%rax
jne    11ba <fts_safe_changedir+0x7a>
mov    0x10(%rsp),%rax
cmp    0x8(%r15),%rax
jne    11ba <fts_safe_changedir+0x7a>
mov    %r14d,%edi
call   1040 <fchdir@plt>
mov    %eax,%ebp
jmp    11cc <fts_safe_changedir+0x8c>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    0x2e0e(%rip),%rcx
mov    %eax,(%rcx)
test   %ebx,%ebx
jns    11ec <fts_safe_changedir+0xac>
mov    0x2e01(%rip),%r15
mov    (%r15),%ebx
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebx,(%r15)
jmp    11ec <fts_safe_changedir+0xac>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_5__ = type { i64, i64 }
%struct.stat = type { i64, i64 }
@FTS_NOCHDIR = global i32 0
@O_RDONLY = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
define i32 @fts_safe_changedir(%struct.TYPE_6__* %0, %struct.TYPE_5__* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.TYPE_6__*
%7 = alloca %struct.TYPE_5__*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca i32
store %struct.TYPE_6__* %0, %struct.TYPE_6__** %6
store %struct.TYPE_5__* %1, %struct.TYPE_5__** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%14 = load i32, i32* %8
store i32 %14, i32* %10
store i32 -1, i32* %11
%15 = load %struct.TYPE_6__*, %struct.TYPE_6__** %6
%16 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @FTS_NOCHDIR
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 0, i32* %5
br label %66
22: ; preds = %4
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i8*, i8** %9
%27 = load i32, i32* @O_RDONLY
%28 = or i32 %27, 0
%29 = call i32 @open(i8* %26, i32 %28)
store i32 %29, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %5
br label %66
32: ; preds = %25, %22
%33 = load i32, i32* %8
%34 = call i32 @fstat(i32 %33, %struct.stat* %12)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %32
br label %56
37: ; preds = %32
%38 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%39 = load i64, i64* %38
%40 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%41 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = icmp ne i64 %39, %42
br i1 %43, label %51, label %44
44: ; preds = %37
%45 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%46 = load i64, i64* %45
%47 = load %struct.TYPE_5__*, %struct.TYPE_5__** %7
%48 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = icmp ne i64 %46, %49
br i1 %50, label %51, label %53
51: ; preds = %44, %37
%52 = load i32, i32* @ENOENT
store i32 %52, i32* @errno
br label %56
53: ; preds = %44
%54 = load i32, i32* %8
%55 = call i32 @fchdir(i32 %54)
store i32 %55, i32* %11
br label %56
56: ; preds = %53, %51, %36
%57 = load i32, i32* %10
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32, i32* @errno
store i32 %60, i32* %13
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %13
store i32 %63, i32* @errno
br label %64
64: ; preds = %59, %56
%65 = load i32, i32* %11
store i32 %65, i32* %5
br label %66
66: ; preds = %64, %31, %21
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_5__ = type { i64, i64 }
%struct.stat = type { i64, i64 }
@FTS_NOCHDIR = global i32 0
@O_RDONLY = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
define i32 @fts_safe_changedir(%struct.TYPE_6__* nocapture %0, %struct.TYPE_5__* nocapture %1, i32 %2, i8* nocapture %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @FTS_NOCHDIR
%10 = and i32 %9, %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %4
%13 = icmp slt i32 %2, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %3, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %43, label %18
18: ; preds = %14, %12
%19 = phi i32 [ %16, %14 ], [ %2, %12 ]
%20 = call i32 @fstat(i32 %19, %struct.stat* %5)
%21 = icmp eq i32 %20, -1
br i1 %21, label %38, label %22
22: ; preds = %18
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %1, i64 0, i32 0
%26 = load i64, i64* %25
%27 = icmp eq i64 %24, %26
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %1, i64 0, i32 1
%32 = load i64, i64* %31
%33 = icmp eq i64 %30, %32
br i1 %33, label %36, label %34
34: ; preds = %28, %22
%35 = load i32, i32* @ENOENT
store i32 %35, i32* @errno
br label %38
36: ; preds = %28
%37 = tail call i32 @fchdir(i32 %19)
br label %38
38: ; preds = %18, %36, %34
%39 = phi i32 [ -1, %18 ], [ -1, %34 ], [ %37, %36 ]
br i1 %13, label %40, label %43
40: ; preds = %38
%41 = load i32, i32* @errno
%42 = tail call i32 @close(i32 %19)
store i32 %41, i32* @errno
br label %43
43: ; preds = %38, %40, %14, %4
%44 = phi i32 [ 0, %4 ], [ -1, %14 ], [ %39, %40 ], [ %39, %38 ]
ret i32 %44
}