#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 FTS_NOCHDIR;
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 & (FTS_NOCHDIR)))
    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
mov    0x2e66(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1181 <fts_safe_changedir+0x41>
movl   $0x0,-0x4(%rbp)
jmp    1255 <fts_safe_changedir+0x115>
cmpl   $0x0,-0x1c(%rbp)
jge    11b7 <fts_safe_changedir+0x77>
mov    -0x28(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11b7 <fts_safe_changedir+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <fts_safe_changedir+0x115>
mov    -0x34(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     11d9 <fts_safe_changedir+0x99>
movl   $0xffffffff,-0x2c(%rbp)
jmp    1225 <fts_safe_changedir+0xe5>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x48(%rbp),%rax
jne    11fc <fts_safe_changedir+0xbc>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0x40(%rbp),%rax
je     121a <fts_safe_changedir+0xda>
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcc(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x2c(%rbp)
jmp    1225 <fts_safe_changedir+0xe5>
mov    -0x34(%rbp),%edi
call   1040 <fchdir@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1243 <fts_safe_changedir+0x103>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%ecx
mov    0x2d8b(%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
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
xor    %ebp,%ebp
test   %eax,(%rdi)
jne    11e5 <fts_safe_changedir+0xa5>
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edx,%r14d
test   %edx,%edx
jns    1185 <fts_safe_changedir+0x45>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    %rcx,%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11f4 <fts_safe_changedir+0xb4>
mov    %rsp,%rsi
mov    %r14d,%edi
call   1060 <fstat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11cc <fts_safe_changedir+0x8c>
mov    (%r15),%rax
cmp    (%rsp),%rax
jne    11ba <fts_safe_changedir+0x7a>
mov    0x8(%r15),%rax
cmp    0x8(%rsp),%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)
mov    0x2e05(%rip),%r15
mov    (%r15),%r12d
test   %ebx,%ebx
jns    11e2 <fts_safe_changedir+0xa2>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %r12d,(%r15)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    11e5 <fts_safe_changedir+0xa5>

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

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 %44
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, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %44, label %18
18: ; preds = %14, %12
%19 = phi i32 [ %16, %14 ], [ %2, %12 ]
%20 = call i64 @fstat(i32 %19, %struct.stat* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %1, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.stat, %struct.stat* %5, 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.TYPE_5__, %struct.TYPE_5__* %1, i64 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, 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, %34 ], [ %37, %36 ], [ -1, %18 ]
%40 = load i32, i32* @errno
br i1 %13, label %41, label %43
41: ; preds = %38
%42 = tail call i32 @close(i32 %19)
br label %43
43: ; preds = %41, %38
store i32 %40, i32* @errno
br label %44
44: ; preds = %14, %4, %43
%45 = phi i32 [ %39, %43 ], [ 0, %4 ], [ -1, %14 ]
ret i32 %45
}