#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_8__ TYPE_2__;
typedef struct TYPE_7__ TYPE_1__;

/* Type definitions */
struct TYPE_8__ {
  int fts_options;
  TYPE_1__ *fts_child;
  TYPE_1__ *fts_cur;
};
struct TYPE_7__ {
  int fts_info;
  scalar_t__ fts_level;
  char *fts_accpath;
  struct TYPE_7__ *fts_link;
};
typedef TYPE_1__ FTSENT;
typedef TYPE_2__ FTS;

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fchdir(int);
void *fts_build(TYPE_2__ *, int);
int /*<<< orphan*/ fts_lfree(TYPE_1__ *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
FTSENT *fts_children(FTS *sp, int instr) {
  FTSENT *p;
  int fd;
  if (instr && instr != 0x1000) {
    errno = EINVAL;
    return (NULL);
  }
  p = sp->fts_cur;
  errno = 0;
  if ((sp->fts_options & (0x2000)))
    return (NULL);
  if (p->fts_info == 9)
    return (p->fts_link);
  if (p->fts_info != 1)
    return (NULL);
  if (sp->fts_child)
    fts_lfree(sp->fts_child);
  if (instr == 0x1000) {
    (sp->fts_options |= (0x1000));
    instr = 2;
  } else
    instr = 1;
  if (p->fts_level != 0 || p->fts_accpath[0] == '/' ||
      (sp->fts_options & (0x0004)))
    return (sp->fts_child = fts_build(sp, instr));
  if ((fd = open(".", O_RDONLY, 0)) < 0)
    return (NULL);
  sp->fts_child = fts_build(sp, instr);
  if (fchdir(fd)) {
    (void)close(fd);
    return (NULL);
  }
  (void)close(fd);
  return (sp->fts_child);
}


x86 assembly (clang, -O0):
<fts_children>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1197 <fts_children+0x47>
cmpl   $0x1000,-0x14(%rbp)
je     1197 <fts_children+0x47>
mov    0x2e53(%rip),%rax
mov    (%rax),%rcx
mov    0x2e51(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e2e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0x2000,%eax
cmp    $0x0,%eax
je     11d2 <fts_children+0x82>
movq   $0x0,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    -0x20(%rbp),%rax
cmpl   $0x9,(%rax)
jne    11f0 <fts_children+0xa0>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
je     120a <fts_children+0xba>
movq   $0x0,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1226 <fts_children+0xd6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <fts_lfree@plt>
cmpl   $0x1000,-0x14(%rbp)
jne    124d <fts_children+0xfd>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
or     $0x1000,%ecx
mov    %ecx,(%rax)
movl   $0x2,-0x14(%rbp)
jmp    1254 <fts_children+0x104>
movl   $0x1,-0x14(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1289 <fts_children+0x139>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1289 <fts_children+0x139>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     12a6 <fts_children+0x156>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <fts_build@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,0x8(%rcx)
mov    %rax,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
lea    0xd4a(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    12d6 <fts_children+0x186>
movq   $0x0,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <fts_build@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x24(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0x0,%rax
je     1314 <fts_children+0x1c4>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1328 <fts_children+0x1d8>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fts_children>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   $0xffffefff,%esi
je     117a <fts_children+0x2a>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
mov    %rax,(%rcx)
xor    %ebp,%ebp
jmp    1239 <fts_children+0xe9>
mov    %rdi,%rbx
mov    0x10(%rdi),%r15
mov    0x2e50(%rip),%rax
movq   $0x0,(%rax)
testb  $0x20,0x1(%rdi)
jne    1237 <fts_children+0xe7>
mov    (%r15),%eax
cmp    $0x1,%eax
je     11b3 <fts_children+0x63>
cmp    $0x9,%eax
jne    1237 <fts_children+0xe7>
mov    0x18(%r15),%rbp
jmp    1239 <fts_children+0xe9>
mov    %esi,%ebp
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11c3 <fts_children+0x73>
call   1040 <fts_lfree@plt>
mov    $0x1,%r14d
cmp    $0x1000,%ebp
jne    11db <fts_children+0x8b>
orb    $0x10,0x1(%rbx)
mov    $0x2,%r14d
cmpq   $0x0,0x8(%r15)
jne    1247 <fts_children+0xf7>
mov    0x10(%r15),%rax
cmpb   $0x2f,(%rax)
je     1247 <fts_children+0xf7>
testb  $0x4,(%rbx)
jne    1247 <fts_children+0xf7>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
xor    %ebp,%ebp
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1239 <fts_children+0xe9>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <fts_build@plt>
mov    %rax,0x8(%rbx)
mov    %r15d,%edi
call   1050 <fchdir@plt>
mov    %rax,%rbp
mov    %r15d,%edi
call   1030 <close@plt>
test   %rbp,%rbp
je     125b <fts_children+0x10b>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <fts_build@plt>
mov    %rax,%rbp
mov    %rax,0x8(%rbx)
jmp    1239 <fts_children+0xe9>
mov    0x8(%rbx),%rbp
jmp    1239 <fts_children+0xe9>

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

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32, i64, i8*, %struct.TYPE_7__* }
%struct.TYPE_8__ = type { i32, %struct.TYPE_7__*, %struct.TYPE_7__* }
@EINVAL = global i64 0
@errno = global i64 0
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
define %struct.TYPE_7__* @fts_children(%struct.TYPE_8__* %0, i32 %1) {
%3 = and i32 %1, -4097
%4 = icmp eq i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = load i64, i64* @EINVAL
store i64 %6, i64* @errno
br label %61
7: ; preds = %2
%8 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %0, i64 0, i32 2
%9 = load %struct.TYPE_7__*, %struct.TYPE_7__** %8
store i64 0, i64* @errno
%10 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %0, i64 0, i32 0
%11 = load i32, i32* %10
%12 = and i32 %11, 8192
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %61
14: ; preds = %7
%15 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %9, i64 0, i32 0
%16 = load i32, i32* %15
switch i32 %16, label %61 [
i32 9, label %17
i32 1, label %20
]
17: ; preds = %14
%18 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %9, i64 0, i32 3
%19 = load %struct.TYPE_7__*, %struct.TYPE_7__** %18
br label %61
20: ; preds = %14
%21 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %0, i64 0, i32 1
%22 = load %struct.TYPE_7__*, %struct.TYPE_7__** %21
%23 = icmp eq %struct.TYPE_7__* %22, null
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @fts_lfree(%struct.TYPE_7__* %22)
br label %26
26: ; preds = %24, %20
%27 = icmp eq i32 %1, 4096
br i1 %27, label %28, label %31
28: ; preds = %26
%29 = load i32, i32* %10
%30 = or i32 %29, 4096
store i32 %30, i32* %10
br label %31
31: ; preds = %26, %28
%32 = phi i32 [ 2, %28 ], [ 1, %26 ]
%33 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %9, i64 0, i32 1
%34 = load i64, i64* %33
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %31
%37 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %9, i64 0, i32 2
%38 = load i8*, i8** %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 47
br i1 %40, label %45, label %41
41: ; preds = %36
%42 = load i32, i32* %10
%43 = and i32 %42, 4
%44 = icmp eq i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %41, %36, %31
%46 = tail call i8* @fts_build(%struct.TYPE_8__* %0, i32 %32)
%47 = bitcast i8* %46 to %struct.TYPE_7__*
%48 = bitcast %struct.TYPE_7__** %21 to i8**
store i8* %46, i8** %48
br label %61
49: ; preds = %41
%50 = load i32, i32* @O_RDONLY
%51 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %50, i32 0)
%52 = icmp slt i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %49
%54 = tail call i8* @fts_build(%struct.TYPE_8__* %0, i32 %32)
%55 = bitcast %struct.TYPE_7__** %21 to i8**
store i8* %54, i8** %55
%56 = tail call i64 @fchdir(i32 %51)
%57 = icmp eq i64 %56, 0
%58 = tail call i32 @close(i32 %51)
br i1 %57, label %59, label %61
59: ; preds = %53
%60 = load %struct.TYPE_7__*, %struct.TYPE_7__** %21
br label %61
61: ; preds = %53, %49, %14, %7, %59, %45, %17, %5
%62 = phi %struct.TYPE_7__* [ null, %5 ], [ %19, %17 ], [ %47, %45 ], [ %60, %59 ], [ null, %7 ], [ null, %14 ], [ null, %49 ], [ null, %53 ]
ret %struct.TYPE_7__* %62
}