#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 FTS_NOCHDIR;
scalar_t__ FTS_ROOTLEVEL;
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 >= FTS_ROOTLEVEL;) {
      freep = p;
      p = p->fts_link ? p->fts_link : p->fts_parent;
      free(freep);
    }
    free(p);
  }
  rfd = (sp->fts_options & (FTS_NOCHDIR)) ? -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     11d8 <fts_close+0x98>
mov    -0x8(%rbp),%rax
mov    0x38(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jl     11cf <fts_close+0x8f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x30(%rax)
je     11ad <fts_close+0x6d>
mov    -0x18(%rbp),%rax
mov    0x30(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b9 <fts_close+0x79>
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
mov    0x2dea(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11fe <fts_close+0xbe>
mov    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1208 <fts_close+0xc8>
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     122a <fts_close+0xea>
mov    -0x8(%rbp),%rax
mov    0x20(%rax),%rdi
call   1050 <fts_lfree@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1246 <fts_close+0x106>
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     1291 <fts_close+0x151>
mov    -0x1c(%rbp),%edi
call   1060 <fchdir@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d49(%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,%r14
mov    0x38(%rdi),%rdi
test   %rdi,%rdi
je     1171 <fts_close+0x31>
mov    (%rdi),%rax
mov    0x2e8b(%rip),%rbx
cmp    (%rbx),%rax
jge    11f5 <fts_close+0xb5>
mov    %rdi,%rbp
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    $0xffffffff,%ebp
test   %eax,0x8(%r14)
jne    1189 <fts_close+0x49>
mov    0xc(%r14),%ebp
mov    0x20(%r14),%rdi
test   %rdi,%rdi
je     1197 <fts_close+0x57>
call   1050 <fts_lfree@plt>
mov    0x18(%r14),%rdi
test   %rdi,%rdi
je     11a5 <fts_close+0x65>
call   1030 <free@plt>
mov    0x10(%r14),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
je     1204 <fts_close+0xc4>
mov    %ebp,%edi
call   1060 <fchdir@plt>
mov    %eax,%r15d
mov    0x2e0c(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,(%r14)
mov    %r15d,%eax
jmp    1206 <fts_close+0xc6>
xchg   %ax,%ax
call   1030 <free@plt>
mov    0x0(%rbp),%rax
mov    %rbp,%rdi
cmp    (%rbx),%rax
jl     1169 <fts_close+0x29>
mov    0x30(%rdi),%rbp
test   %rbp,%rbp
jne    11e0 <fts_close+0xa0>
mov    0x28(%rdi),%rbp
jmp    11e0 <fts_close+0xa0>
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__* }
@FTS_ROOTLEVEL = global i64 0
@FTS_NOCHDIR = global i32 0
@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 %43
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 = %36, %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 = load i64, i64* @FTS_ROOTLEVEL
%21 = icmp sge i64 %19, %20
br i1 %21, label %22, label %40
22: ; preds = %16
%23 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
store %struct.TYPE_5__* %23, %struct.TYPE_5__** %3
%24 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %24, i32 0, i32 7
%26 = load %struct.TYPE_5__*, %struct.TYPE_5__** %25
%27 = icmp ne %struct.TYPE_5__* %26, null
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%30 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %29, i32 0, i32 7
%31 = load %struct.TYPE_5__*, %struct.TYPE_5__** %30
br label %36
32: ; preds = %22
%33 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%34 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %33, i32 0, i32 6
%35 = load %struct.TYPE_5__*, %struct.TYPE_5__** %34
br label %36
36: ; preds = %32, %28
%37 = phi %struct.TYPE_5__* [ %31, %28 ], [ %35, %32 ]
store %struct.TYPE_5__* %37, %struct.TYPE_5__** %4
%38 = load %struct.TYPE_5__*, %struct.TYPE_5__** %3
%39 = call i32 @free(%struct.TYPE_5__* %38)
br label %16
40: ; preds = %16
%41 = load %struct.TYPE_5__*, %struct.TYPE_5__** %4
%42 = call i32 @free(%struct.TYPE_5__* %41)
br label %43
43: ; preds = %40, %1
%44 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%45 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %44, i32 0, i32 1
%46 = load i32, i32* %45
%47 = load i32, i32* @FTS_NOCHDIR
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %43
br label %55
51: ; preds = %43
%52 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%53 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %52, i32 0, i32 2
%54 = load i32, i32* %53
br label %55
55: ; preds = %51, %50
%56 = phi i32 [ -1, %50 ], [ %54, %51 ]
store i32 %56, i32* %5
%57 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%58 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %57, i32 0, i32 5
%59 = load i64, i64* %58
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%63 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %62, i32 0, i32 5
%64 = load i64, i64* %63
%65 = call i32 @fts_lfree(i64 %64)
br label %66
66: ; preds = %61, %55
%67 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%68 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %67, i32 0, i32 4
%69 = load %struct.TYPE_5__*, %struct.TYPE_5__** %68
%70 = icmp ne %struct.TYPE_5__* %69, null
br i1 %70, label %71, label %76
71: ; preds = %66
%72 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%73 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %72, i32 0, i32 4
%74 = load %struct.TYPE_5__*, %struct.TYPE_5__** %73
%75 = call i32 @free(%struct.TYPE_5__* %74)
br label %76
76: ; preds = %71, %66
%77 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%78 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %77, i32 0, i32 3
%79 = load %struct.TYPE_5__*, %struct.TYPE_5__** %78
%80 = call i32 @free(%struct.TYPE_5__* %79)
%81 = load %struct.TYPE_5__*, %struct.TYPE_5__** %2
%82 = call i32 @free(%struct.TYPE_5__* %81)
%83 = load i32, i32* %5
%84 = icmp ne i32 %83, -1
br i1 %84, label %85, label %92
85: ; preds = %76
%86 = load i32, i32* %5
%87 = call i32 @fchdir(i32 %86)
store i32 %87, i32* %6
%88 = load i32, i32* @errno
store i32 %88, i32* %7
%89 = load i32, i32* %5
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %7
store i32 %91, i32* @errno
br label %92
92: ; preds = %85, %76
%93 = load i32, i32* %6
ret i32 %93
}

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 1
%30 = load i32, i32* %29
%31 = load i32, i32* @FTS_NOCHDIR
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 2
%36 = load i32, i32* %35
br label %37
37: ; preds = %28, %34
%38 = phi i32 [ %36, %34 ], [ -1, %28 ]
%39 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 5
%40 = load i64, i64* %39
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %37
%43 = tail call i32 @fts_lfree(i64 %40)
br label %44
44: ; preds = %42, %37
%45 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 4
%46 = load %struct.TYPE_5__*, %struct.TYPE_5__** %45
%47 = icmp eq %struct.TYPE_5__* %46, null
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = tail call i32 @free(%struct.TYPE_5__* %46)
br label %50
50: ; preds = %48, %44
%51 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 3
%52 = load %struct.TYPE_5__*, %struct.TYPE_5__** %51
%53 = tail call i32 @free(%struct.TYPE_5__* %52)
%54 = tail call i32 @free(%struct.TYPE_5__* %0)
%55 = icmp eq i32 %38, -1
br i1 %55, label %60, label %56
56: ; preds = %50
%57 = tail call i32 @fchdir(i32 %38)
%58 = load i32, i32* @errno
%59 = tail call i32 @close(i32 %38)
store i32 %58, i32* @errno
br label %60
60: ; preds = %56, %50
%61 = phi i32 [ %57, %56 ], [ 0, %50 ]
ret i32 %61
}