#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_dev;
  scalar_t__ st_ino;
};
struct TYPE_6__ {
  int fts_options;
};
struct TYPE_5__ {
  scalar_t__ fts_dev;
  scalar_t__ fts_ino;
};
typedef TYPE_1__ FTSENT;
typedef TYPE_2__ FTS;

/* Variables and functions */
int ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
scalar_t__ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int fts_safe_changedir(FTS *sp, FTSENT *p, int fd, char *path) {
  int ret, oerrno, newfd;
  struct stat sb;
  newfd = fd;
  if ((sp->fts_options & (0x0004)))
    return (0);
  if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
    return (-1);
  if (fstat(newfd, &sb)) {
    ret = -1;
    goto bail;
  }
  if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
    errno = ENOENT;
    ret = -1;
    goto bail;
  }
  ret = fchdir(newfd);
bail:
  oerrno = errno;
  if (fd < 0)
    (void)close(newfd);
  errno = oerrno;
  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,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     117b <fts_safe_changedir+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    124f <fts_safe_changedir+0x10f>
cmpl   $0x0,-0x1c(%rbp)
jge    11b1 <fts_safe_changedir+0x71>
mov    -0x28(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11b1 <fts_safe_changedir+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <fts_safe_changedir+0x10f>
mov    -0x34(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     11d3 <fts_safe_changedir+0x93>
movl   $0xffffffff,-0x2c(%rbp)
jmp    121f <fts_safe_changedir+0xdf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x48(%rbp),%rax
jne    11f6 <fts_safe_changedir+0xb6>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0x40(%rbp),%rax
je     1214 <fts_safe_changedir+0xd4>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x2c(%rbp)
jmp    121f <fts_safe_changedir+0xdf>
mov    -0x34(%rbp),%edi
call   1040 <fchdir@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123d <fts_safe_changedir+0xfd>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%ecx
mov    0x2d91(%rip),%rax
mov    %ecx,(%rax)
mov    -0x2c(%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   %r12
push   %rbx
sub    $0x10,%rsp
xor    %ebp,%ebp
testb  $0x4,(%rdi)
jne    11df <fts_safe_changedir+0x9f>
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edx,%r14d
test   %edx,%edx
jns    117e <fts_safe_changedir+0x3e>
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    %rcx,%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11ee <fts_safe_changedir+0xae>
mov    %rsp,%rsi
mov    %r14d,%edi
call   1060 <fstat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11c5 <fts_safe_changedir+0x85>
mov    (%r15),%rax
cmp    (%rsp),%rax
jne    11b3 <fts_safe_changedir+0x73>
mov    0x8(%r15),%rax
cmp    0x8(%rsp),%rax
jne    11b3 <fts_safe_changedir+0x73>
mov    %r14d,%edi
call   1040 <fchdir@plt>
mov    %eax,%ebp
jmp    11c5 <fts_safe_changedir+0x85>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    0x2e15(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e0c(%rip),%r15
mov    (%r15),%ebx
test   %r12d,%r12d
jns    11dc <fts_safe_changedir+0x9c>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebx,(%r15)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    11df <fts_safe_changedir+0x9f>

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

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