#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_5__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  scalar_t__ fts_level;
  int fts_options;
  int /*<<< orphan*/ fts_rfd;
  struct TYPE_5__ *fts_path;
  struct TYPE_5__ *fts_array;
  scalar_t__ fts_child;
  struct TYPE_5__ *fts_parent;
  struct TYPE_5__ *fts_link;
  struct TYPE_5__ *fts_cur;
};
typedef TYPE_1__ FTSENT;
typedef TYPE_1__ FTS;

/* Variables and functions */
int FTS_NOCHDIR;
scalar_t__ FTS_ROOTLEVEL;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int errno;
scalar_t__ fchdir(int /*<<< orphan*/);
int /*<<< orphan*/ free(TYPE_1__ *);
int /*<<< orphan*/ fts_lfree(scalar_t__);
int fts_close(FTS *sp) {
  FTSENT *freep, *p;
  int saved_errno = 0;
  if (sp->fts_cur) {
    for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
      freep = p;
      p = p->fts_link ? p->fts_link : p->fts_parent;
      free(freep);
    }
    free(p);
  }
  if (sp->fts_child)
    fts_lfree(sp->fts_child);
  if (sp->fts_array)
    free(sp->fts_array);
  free(sp->fts_path);
  if (!(sp->fts_options & (FTS_NOCHDIR))) {
    saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
    (void)close(sp->fts_rfd);
  }
  free(sp);
  if (!(sp->fts_options & (FTS_NOCHDIR)) && saved_errno) {
    errno = saved_errno;
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<fts_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x38(%rax)
je     11d8 <fts_close+0x98>
mov    -0x10(%rbp),%rax
mov    0x38(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jl     11cf <fts_close+0x8f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x30(%rax)
je     11ad <fts_close+0x6d>
mov    -0x20(%rbp),%rax
mov    0x30(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b9 <fts_close+0x79>
mov    -0x20(%rbp),%rax
mov    0x28(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    116e <fts_close+0x2e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x20(%rax)
je     11f4 <fts_close+0xb4>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1050 <fts_lfree@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1210 <fts_close+0xd0>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2da5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1279 <fts_close+0x139>
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edi
call   1060 <fchdir@plt>
cmp    $0x0,%rax
je     125d <fts_close+0x11d>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1267 <fts_close+0x127>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1267 <fts_close+0x127>
mov    -0x34(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2d40(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12bd <fts_close+0x17d>
cmpl   $0x0,-0x24(%rbp)
je     12bd <fts_close+0x17d>
mov    -0x24(%rbp),%ecx
mov    0x2d29(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <fts_close+0x184>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fts_close>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r12
mov    0x38(%rdi),%rdi
test   %rdi,%rdi
je     1172 <fts_close+0x32>
mov    (%rdi),%rax
mov    0x2e8a(%rip),%rbx
cmp    (%rbx),%rax
jge    1225 <fts_close+0xe5>
mov    %rdi,%rbp
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x20(%r12),%rdi
test   %rdi,%rdi
je     1181 <fts_close+0x41>
call   1050 <fts_lfree@plt>
mov    0x18(%r12),%rdi
test   %rdi,%rdi
je     1190 <fts_close+0x50>
call   1030 <free@plt>
mov    0x10(%r12),%rdi
call   1030 <free@plt>
mov    0x2e2f(%rip),%r14
mov    (%r14),%eax
xor    %r15d,%r15d
mov    $0x0,%ebp
test   %eax,0x8(%r12)
jne    11d7 <fts_close+0x97>
mov    0xc(%r12),%edi
call   1060 <fchdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11cd <fts_close+0x8d>
mov    0x2e0d(%rip),%rax
mov    (%rax),%ebp
mov    0xc(%r12),%edi
call   1040 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    (%r14),%eax
test   %eax,0x8(%r12)
jne    11fc <fts_close+0xbc>
test   %ebp,%ebp
je     11fc <fts_close+0xbc>
mov    0x2de4(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   0x0(%rax,%rax,1)
call   1030 <free@plt>
mov    0x0(%rbp),%rax
mov    %rbp,%rdi
cmp    (%rbx),%rax
jl     116a <fts_close+0x2a>
mov    0x30(%rdi),%rbp
test   %rbp,%rbp
jne    1210 <fts_close+0xd0>
mov    0x28(%rdi),%rbp
jmp    1210 <fts_close+0xd0>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, i32, i32, %struct.TYPE_5__*, %struct.TYPE_5__*, i64, %struct.TYPE_5__*, %struct.TYPE_5__*, %struct.TYPE_5__* }
@FTS_ROOTLEVEL = global i64 0
@FTS_NOCHDIR = global i32 0
@errno = global i32 0
define i32 @fts_close(%struct.TYPE_5__* %0) {
%2 = alloca i32
%3 = alloca %struct.TYPE_5__*
%4 = alloca %struct.TYPE_5__*
%5 = alloca %struct.TYPE_5__*
%6 = alloca i32
store %struct.TYPE_5__* %0, %struct.TYPE_5__** %3
store i32 0, i32* %6
%7 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%8 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i32 0, i32 8
%9 = load %struct.TYPE_5__*, %struct.TYPE_5__** %8
%10 = icmp ne %struct.TYPE_5__* %9, null
br i1 %10, label %11, label %42
11: ; preds = %1
%12 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%13 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 8
%14 = load %struct.TYPE_5__*, %struct.TYPE_5__** %13
store %struct.TYPE_5__* %14, %struct.TYPE_5__** %5
br label %15
15: ; preds = %35, %11
%16 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%17 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @FTS_ROOTLEVEL
%20 = icmp sge i64 %18, %19
br i1 %20, label %21, label %39
21: ; preds = %15
%22 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
store %struct.TYPE_5__* %22, %struct.TYPE_5__** %4
%23 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%24 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %23, i32 0, i32 7
%25 = load %struct.TYPE_5__*, %struct.TYPE_5__** %24
%26 = icmp ne %struct.TYPE_5__* %25, null
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%29 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %28, i32 0, i32 7
%30 = load %struct.TYPE_5__*, %struct.TYPE_5__** %29
br label %35
31: ; preds = %21
%32 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%33 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %32, i32 0, i32 6
%34 = load %struct.TYPE_5__*, %struct.TYPE_5__** %33
br label %35
35: ; preds = %31, %27
%36 = phi %struct.TYPE_5__* [ %30, %27 ], [ %34, %31 ]
store %struct.TYPE_5__* %36, %struct.TYPE_5__** %5
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%38 = call i32 @free(%struct.TYPE_5__* %37)
br label %15
39: ; preds = %15
%40 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%41 = call i32 @free(%struct.TYPE_5__* %40)
br label %42
42: ; preds = %39, %1
%43 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%44 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %43, i32 0, i32 5
%45 = load i64, i64* %44
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%49 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %48, i32 0, i32 5
%50 = load i64, i64* %49
%51 = call i32 @fts_lfree(i64 %50)
br label %52
52: ; preds = %47, %42
%53 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%54 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %53, i32 0, i32 4
%55 = load %struct.TYPE_5__*, %struct.TYPE_5__** %54
%56 = icmp ne %struct.TYPE_5__* %55, null
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%59 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %58, i32 0, i32 4
%60 = load %struct.TYPE_5__*, %struct.TYPE_5__** %59
%61 = call i32 @free(%struct.TYPE_5__* %60)
br label %62
62: ; preds = %57, %52
%63 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%64 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %63, i32 0, i32 3
%65 = load %struct.TYPE_5__*, %struct.TYPE_5__** %64
%66 = call i32 @free(%struct.TYPE_5__* %65)
%67 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%68 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %67, i32 0, i32 1
%69 = load i32, i32* %68
%70 = load i32, i32* @FTS_NOCHDIR
%71 = and i32 %69, %70
%72 = icmp ne i32 %71, 0
br i1 %72, label %88, label %73
73: ; preds = %62
%74 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%75 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %74, i32 0, i32 2
%76 = load i32, i32* %75
%77 = call i64 @fchdir(i32 %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %73
%80 = load i32, i32* @errno
br label %82
81: ; preds = %73
br label %82
82: ; preds = %81, %79
%83 = phi i32 [ %80, %79 ], [ 0, %81 ]
store i32 %83, i32* %6
%84 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%85 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %84, i32 0, i32 2
%86 = load i32, i32* %85
%87 = call i32 @close(i32 %86)
br label %88
88: ; preds = %82, %62
%89 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%90 = call i32 @free(%struct.TYPE_5__* %89)
%91 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%92 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %91, i32 0, i32 1
%93 = load i32, i32* %92
%94 = load i32, i32* @FTS_NOCHDIR
%95 = and i32 %93, %94
%96 = icmp ne i32 %95, 0
br i1 %96, label %102, label %97
97: ; preds = %88
%98 = load i32, i32* %6
%99 = icmp ne i32 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %97
%101 = load i32, i32* %6
store i32 %101, i32* @errno
store i32 -1, i32* %2
br label %103
102: ; preds = %97, %88
store i32 0, i32* %2
br label %103
103: ; preds = %102, %100
%104 = load i32, i32* %2
ret i32 %104
}

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