#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*/ _DIAGASSERT(int /*<<< orphan*/);
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;
  _DIAGASSERT(sp != NULL);
  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)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <_DIAGASSERT@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x20(%rax)
je     1237 <fts_close+0xd7>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    (%rax),%eax
mov    0x2e53(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c0 <fts_close+0x60>
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    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jl     122e <fts_close+0xce>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     120c <fts_close+0xac>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1218 <fts_close+0xb8>
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    11cc <fts_close+0x6c>
mov    -0x20(%rbp),%rdi
call   1050 <fts_free@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1253 <fts_close+0xf3>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <fts_lfree@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     126f <fts_close+0x10f>
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    0x2d3f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12c1 <fts_close+0x161>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    12b5 <fts_close+0x155>
mov    0x2d20(%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     12ec <fts_close+0x18c>
mov    -0x24(%rbp),%ecx
mov    0x2cf2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <fts_close+0x193>
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
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1070 <_DIAGASSERT@plt>
mov    0x20(%r14),%rdi
test   %rdi,%rdi
je     11b9 <fts_close+0x59>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rdi)
je     119a <fts_close+0x3a>
mov    0x20(%rdi),%edi
call   1040 <close@plt>
mov    0x20(%r14),%rdi
mov    0x8(%rdi),%rax
mov    0x2e3b(%rip),%r15
cmp    (%r15),%rax
jge    1245 <fts_close+0xe5>
mov    %rdi,%rbx
mov    %rbx,%rdi
call   1050 <fts_free@plt>
mov    0x18(%r14),%rdi
test   %rdi,%rdi
je     11c7 <fts_close+0x67>
call   1060 <fts_lfree@plt>
mov    0x10(%r14),%rdi
test   %rdi,%rdi
je     11d5 <fts_close+0x75>
call   1030 <free@plt>
mov    0x8(%r14),%rdi
call   1030 <free@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
test   %eax,(%r14)
jne    120f <fts_close+0xaf>
mov    0x4(%r14),%edi
call   1080 <fchdir@plt>
mov    %eax,%ebx
mov    0x2dd2(%rip),%r15
mov    (%r15),%ebp
mov    0x4(%r14),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     1254 <fts_close+0xf4>
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)
xchg   %ax,%ax
call   1050 <fts_free@plt>
mov    0x8(%rbx),%rax
mov    %rbx,%rdi
cmp    (%r15),%rax
jl     11b1 <fts_close+0x51>
mov    0x18(%rdi),%rbx
test   %rbx,%rbx
jne    1230 <fts_close+0xd0>
mov    0x10(%rdi),%rbx
jmp    1230 <fts_close+0xd0>
mov    %r14,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     1217 <fts_close+0xb7>
mov    %ebp,(%r15)
mov    $0xffffffff,%eax
jmp    1219 <fts_close+0xb9>

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

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