#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_7__ TYPE_2__;
typedef struct TYPE_6__ TYPE_1__;

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

/* Variables and functions */
int FTS_NOCHDIR;
scalar_t__ FTS_ROOTLEVEL;
int FTS_SYMFOLLOW;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int errno;
int fchdir(int /*<<< orphan*/);
int /*<<< orphan*/ free(TYPE_2__ *);
int /*<<< orphan*/ fts_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) {
    if (sp->fts_cur->fts_flags & FTS_SYMFOLLOW)
      (void)close(sp->fts_cur->fts_symfd);
    for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
      freep = p;
      p = p->fts_link ? p->fts_link : p->fts_parent;
      fts_free(freep);
    }
    fts_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))) {
    if (fchdir(sp->fts_rfd) == -1)
      saved_errno = errno;
    (void)close(sp->fts_rfd);
  }
  free(sp);
  if (saved_errno) {
    errno = saved_errno;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fts_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x20(%rax)
je     1215 <fts_close+0xc5>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    (%rax),%eax
mov    0x2e75(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     119e <fts_close+0x4e>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    0x20(%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jl     120c <fts_close+0xbc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     11ea <fts_close+0x9a>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11f6 <fts_close+0xa6>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fts_free@plt>
jmp    11aa <fts_close+0x5a>
mov    -0x20(%rbp),%rdi
call   1050 <fts_free@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1231 <fts_close+0xe1>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <fts_lfree@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     124d <fts_close+0xfd>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2d61(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    129f <fts_close+0x14f>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    1293 <fts_close+0x143>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x24(%rbp)
je     12ca <fts_close+0x17a>
mov    -0x24(%rbp),%ecx
mov    0x2d14(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <fts_close+0x181>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fts_close>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x20(%rdi),%rdi
test   %rdi,%rdi
je     119b <fts_close+0x4b>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rdi)
je     117c <fts_close+0x2c>
mov    0x20(%rdi),%edi
call   1040 <close@plt>
mov    0x20(%r14),%rdi
mov    0x8(%rdi),%rax
mov    0x2e59(%rip),%r15
cmp    (%r15),%rax
jge    1225 <fts_close+0xd5>
mov    %rdi,%rbx
mov    %rbx,%rdi
call   1050 <fts_free@plt>
mov    0x18(%r14),%rdi
test   %rdi,%rdi
je     11a9 <fts_close+0x59>
call   1060 <fts_lfree@plt>
mov    0x10(%r14),%rdi
test   %rdi,%rdi
je     11b7 <fts_close+0x67>
call   1030 <free@plt>
mov    0x8(%r14),%rdi
call   1030 <free@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
test   %eax,(%r14)
jne    11f1 <fts_close+0xa1>
mov    0x4(%r14),%edi
call   1070 <fchdir@plt>
mov    %eax,%ebx
mov    0x2df0(%rip),%r15
mov    (%r15),%ebp
mov    0x4(%r14),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     1234 <fts_close+0xe4>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
call   1050 <fts_free@plt>
mov    0x8(%rbx),%rax
mov    %rbx,%rdi
cmp    (%r15),%rax
jl     1193 <fts_close+0x43>
mov    0x18(%rdi),%rbx
test   %rbx,%rbx
jne    1210 <fts_close+0xc0>
mov    0x10(%rdi),%rbx
jmp    1210 <fts_close+0xc0>
mov    %r14,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     11f9 <fts_close+0xa9>
mov    %ebp,(%r15)
mov    $0xffffffff,%eax
jmp    11fb <fts_close+0xab>

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

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