#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 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 /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int /*<<< orphan*/ free(TYPE_1__ *);
int /*<<< orphan*/ fts_lfree(scalar_t__);
int fts_close(FTS *sp) {
  FTSENT *freep, *p;
  int rfd, error = 0;
  if (sp->fts_cur) {
    for (p = sp->fts_cur; p->fts_level >= 0;) {
      freep = p;
      p = p->fts_link ? p->fts_link : p->fts_parent;
      free(freep);
    }
    free(p);
  }
  rfd = (sp->fts_options & (0x0004)) ? -1 : sp->fts_rfd;
  if (sp->fts_child)
    fts_lfree(sp->fts_child);
  if (sp->fts_array)
    free(sp->fts_array);
  free(sp->fts_path);
  free(sp);
  if (rfd != -1) {
    int saved_errno;
    error = fchdir(rfd);
    saved_errno = errno;
    (void)close(rfd);
    errno = saved_errno;
  }
  return (error);
}


x86 assembly (clang, -O0):
<fts_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x38(%rax)
je     11cf <fts_close+0x8f>
mov    -0x8(%rbp),%rax
mov    0x38(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jl     11c6 <fts_close+0x86>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x30(%rax)
je     11a4 <fts_close+0x64>
mov    -0x18(%rbp),%rax
mov    0x30(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b0 <fts_close+0x70>
mov    -0x18(%rbp),%rax
mov    0x28(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    116e <fts_close+0x2e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     11ef <fts_close+0xaf>
mov    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    11f9 <fts_close+0xb9>
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x20(%rax)
je     121b <fts_close+0xdb>
mov    -0x8(%rbp),%rax
mov    0x20(%rax),%rdi
call   1050 <fts_lfree@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1237 <fts_close+0xf7>
mov    -0x8(%rbp),%rax
mov    0x18(%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     1282 <fts_close+0x142>
mov    -0x1c(%rbp),%edi
call   1060 <fchdir@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d60(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fts_close>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x38(%rdi),%rdi
test   %rdi,%rdi
je     1168 <fts_close+0x28>
cmpq   $0x0,(%rdi)
jns    11e3 <fts_close+0xa3>
mov    %rdi,%rbp
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
testb  $0x4,0x8(%rbx)
jne    1176 <fts_close+0x36>
mov    0xc(%rbx),%ebp
mov    0x20(%rbx),%rdi
test   %rdi,%rdi
je     1184 <fts_close+0x44>
call   1050 <fts_lfree@plt>
mov    0x18(%rbx),%rdi
test   %rdi,%rdi
je     1192 <fts_close+0x52>
call   1030 <free@plt>
mov    0x10(%rbx),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
je     11f2 <fts_close+0xb2>
mov    %ebp,%edi
call   1060 <fchdir@plt>
mov    %eax,%r15d
mov    0x2e27(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,(%r14)
mov    %r15d,%eax
jmp    11f4 <fts_close+0xb4>
nopl   0x0(%rax,%rax,1)
call   1030 <free@plt>
cmpq   $0x0,0x0(%rbp)
mov    %rbp,%rdi
js     1160 <fts_close+0x20>
mov    0x30(%rdi),%rbp
test   %rbp,%rbp
jne    11d0 <fts_close+0x90>
mov    0x28(%rdi),%rbp
jmp    11d0 <fts_close+0x90>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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__* }
@errno = global i32 0
define i32 @fts_close(%struct.TYPE_5__* %0) {
%2 = alloca %struct.TYPE_5__*
%3 = alloca %struct.TYPE_5__*
%4 = alloca %struct.TYPE_5__*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.TYPE_5__* %0, %struct.TYPE_5__** %2
store i32 0, i32* %6
%8 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%9 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %8, i32 0, i32 8
%10 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%11 = icmp ne %struct.TYPE_5__* %10, null
br i1 %11, label %12, label %42
12: ; preds = %1
%13 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %13, i32 0, i32 8
%15 = load %struct.TYPE_5__*, %struct.TYPE_5__** %14
store %struct.TYPE_5__* %15, %struct.TYPE_5__** %4
br label %16
16: ; preds = %35, %12
%17 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%18 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = icmp sge i64 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %16
%22 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
store %struct.TYPE_5__* %22, %struct.TYPE_5__** %3
%23 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%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__** %4
%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__** %4
%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__** %4
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%38 = call i32 @free(%struct.TYPE_5__* %37)
br label %16
39: ; preds = %16
%40 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%41 = call i32 @free(%struct.TYPE_5__* %40)
br label %42
42: ; preds = %39, %1
%43 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%44 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = and i32 %45, 4
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %42
br label %53
49: ; preds = %42
%50 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%51 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %50, i32 0, i32 2
%52 = load i32, i32* %51
br label %53
53: ; preds = %49, %48
%54 = phi i32 [ -1, %48 ], [ %52, %49 ]
store i32 %54, i32* %5
%55 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%56 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %55, i32 0, i32 5
%57 = load i64, i64* %56
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %53
%60 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%61 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %60, i32 0, i32 5
%62 = load i64, i64* %61
%63 = call i32 @fts_lfree(i64 %62)
br label %64
64: ; preds = %59, %53
%65 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%66 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %65, i32 0, i32 4
%67 = load %struct.TYPE_5__*, %struct.TYPE_5__** %66
%68 = icmp ne %struct.TYPE_5__* %67, null
br i1 %68, label %69, label %74
69: ; preds = %64
%70 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%71 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %70, i32 0, i32 4
%72 = load %struct.TYPE_5__*, %struct.TYPE_5__** %71
%73 = call i32 @free(%struct.TYPE_5__* %72)
br label %74
74: ; preds = %69, %64
%75 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%76 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %75, i32 0, i32 3
%77 = load %struct.TYPE_5__*, %struct.TYPE_5__** %76
%78 = call i32 @free(%struct.TYPE_5__* %77)
%79 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%80 = call i32 @free(%struct.TYPE_5__* %79)
%81 = load i32, i32* %5
%82 = icmp ne i32 %81, -1
br i1 %82, label %83, label %90
83: ; preds = %74
%84 = load i32, i32* %5
%85 = call i32 @fchdir(i32 %84)
store i32 %85, i32* %6
%86 = load i32, i32* @errno
store i32 %86, i32* %7
%87 = load i32, i32* %5
%88 = call i32 @close(i32 %87)
%89 = load i32, i32* %7
store i32 %89, i32* @errno
br label %90
90: ; preds = %83, %74
%91 = load i32, i32* %6
ret i32 %91
}

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__* }
@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 %26, 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 = icmp sgt i64 %7, -1
br i1 %8, label %9, label %23
9: ; preds = %5, %17
%10 = phi %struct.TYPE_5__* [ %18, %17 ], [ %3, %5 ]
%11 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i64 0, i32 7
%12 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%13 = icmp eq %struct.TYPE_5__* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i64 0, i32 6
%16 = load %struct.TYPE_5__*, %struct.TYPE_5__** %15
br label %17
17: ; preds = %9, %14
%18 = phi %struct.TYPE_5__* [ %16, %14 ], [ %12, %9 ]
%19 = tail call i32 @free(%struct.TYPE_5__* %10)
%20 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %18, i64 0, i32 0
%21 = load i64, i64* %20
%22 = icmp sgt i64 %21, -1
br i1 %22, label %9, label %23
23: ; preds = %17, %5
%24 = phi %struct.TYPE_5__* [ %3, %5 ], [ %18, %17 ]
%25 = tail call i32 @free(%struct.TYPE_5__* %24)
br label %26
26: ; preds = %23, %1
%27 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 1
%28 = load i32, i32* %27
%29 = and i32 %28, 4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 2
%33 = load i32, i32* %32
br label %34
34: ; preds = %26, %31
%35 = phi i32 [ %33, %31 ], [ -1, %26 ]
%36 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 5
%37 = load i64, i64* %36
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = tail call i32 @fts_lfree(i64 %37)
br label %41
41: ; preds = %39, %34
%42 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 4
%43 = load %struct.TYPE_5__*, %struct.TYPE_5__** %42
%44 = icmp eq %struct.TYPE_5__* %43, null
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = tail call i32 @free(%struct.TYPE_5__* %43)
br label %47
47: ; preds = %45, %41
%48 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 3
%49 = load %struct.TYPE_5__*, %struct.TYPE_5__** %48
%50 = tail call i32 @free(%struct.TYPE_5__* %49)
%51 = tail call i32 @free(%struct.TYPE_5__* %0)
%52 = icmp eq i32 %35, -1
br i1 %52, label %57, label %53
53: ; preds = %47
%54 = tail call i32 @fchdir(i32 %35)
%55 = load i32, i32* @errno
%56 = tail call i32 @close(i32 %35)
store i32 %55, i32* @errno
br label %57
57: ; preds = %53, %47
%58 = phi i32 [ %54, %53 ], [ 0, %47 ]
ret i32 %58
}