#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 */

/* Type definitions */
struct udev_enumerate {
  int dummy;
};
struct dirent {
  char *d_name;
  scalar_t__ d_type;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ asprintf(char **, char *, char const *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ parent_add_child(struct udev_enumerate *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int parent_crawl_children(struct udev_enumerate *enumerate, const char *path,
                          int maxdepth) {
  DIR *d;
  struct dirent *dent;
  d = opendir(path);
  if (d == NULL)
    return -errno;
  for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
    char *child;
    if (dent->d_name[0] == '.')
      continue;
    if (dent->d_type != DT_DIR)
      continue;
    if (asprintf(&child, "%s/%s", path, dent->d_name) < 0)
      continue;
    parent_add_child(enumerate, child);
    if (maxdepth > 0)
      parent_crawl_children(enumerate, child, maxdepth - 1);
    free(child);
  }
  closedir(d);
  return 0;
}


x86 assembly (clang, -O0):
<parent_crawl_children>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ae <parent_crawl_children+0x3e>
xor    %eax,%eax
mov    0x2e34(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <parent_crawl_children+0x10a>
mov    -0x28(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     126a <parent_crawl_children+0xfa>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11de <parent_crawl_children+0x6e>
jmp    1258 <parent_crawl_children+0xe8>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
je     11fb <parent_crawl_children+0x8b>
jmp    1258 <parent_crawl_children+0xe8>
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    -0x38(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1090 <asprintf@plt>
cmp    $0x0,%rax
jge    1225 <parent_crawl_children+0xb5>
jmp    1258 <parent_crawl_children+0xe8>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <parent_add_child@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    124f <parent_crawl_children+0xdf>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1050 <parent_crawl_children@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
jmp    11bb <parent_crawl_children+0x4b>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_crawl_children>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1237 <parent_crawl_children+0xc7>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     122b <parent_crawl_children+0xbb>
lea    -0x1(%r14),%ecx
mov    %ecx,0x14(%rsp)
mov    0x2e25(%rip),%rbp
lea    0x8(%rsp),%r13
jmp    11dd <parent_crawl_children+0x6d>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     122b <parent_crawl_children+0xbb>
mov    (%rax),%rcx
cmpb   $0x2e,(%rcx)
je     11d0 <parent_crawl_children+0x60>
mov    0x8(%rax),%rax
cmp    0x0(%rbp),%rax
jne    11d0 <parent_crawl_children+0x60>
mov    %r13,%rdi
lea    0xe07(%rip),%rsi
mov    %r12,%rdx
call   1090 <asprintf@plt>
test   %rax,%rax
js     11d0 <parent_crawl_children+0x60>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <parent_add_child@plt>
test   %r14d,%r14d
jle    11c2 <parent_crawl_children+0x52>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    0x14(%rsp),%edx
call   1050 <parent_crawl_children@plt>
jmp    11c2 <parent_crawl_children+0x52>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    1242 <parent_crawl_children+0xd2>
mov    0x2d9a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev_enumerate = type { i32 }
%struct.dirent = type { i8*, i64 }
@errno = global i32 0
@DT_DIR = global i64 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @parent_crawl_children(%struct.udev_enumerate* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.udev_enumerate*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i8*
store %struct.udev_enumerate* %0, %struct.udev_enumerate** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %4
br label %69
18: ; preds = %3
%19 = load i32*, i32** %8
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %9
br label %21
21: ; preds = %63, %18
%22 = load %struct.dirent*, %struct.dirent** %9
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %66
24: ; preds = %21
%25 = load %struct.dirent*, %struct.dirent** %9
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 46
br i1 %31, label %32, label %33
32: ; preds = %24
br label %63
33: ; preds = %24
%34 = load %struct.dirent*, %struct.dirent** %9
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 1
%36 = load i64, i64* %35
%37 = load i64, i64* @DT_DIR
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
br label %63
40: ; preds = %33
%41 = load i8*, i8** %6
%42 = load %struct.dirent*, %struct.dirent** %9
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @asprintf(i8** %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %41, i8* %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %40
br label %63
48: ; preds = %40
%49 = load %struct.udev_enumerate*, %struct.udev_enumerate** %5
%50 = load i8*, i8** %10
%51 = call i32 @parent_add_child(%struct.udev_enumerate* %49, i8* %50)
%52 = load i32, i32* %7
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %48
%55 = load %struct.udev_enumerate*, %struct.udev_enumerate** %5
%56 = load i8*, i8** %10
%57 = load i32, i32* %7
%58 = sub nsw i32 %57, 1
%59 = call i32 @parent_crawl_children(%struct.udev_enumerate* %55, i8* %56, i32 %58)
br label %60
60: ; preds = %54, %48
%61 = load i8*, i8** %10
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %60, %47, %39, %32
%64 = load i32*, i32** %8
%65 = call %struct.dirent* @readdir(i32* %64)
store %struct.dirent* %65, %struct.dirent** %9
br label %21
66: ; preds = %21
%67 = load i32*, i32** %8
%68 = call i32 @closedir(i32* %67)
store i32 0, i32* %4
br label %69
69: ; preds = %66, %15
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.udev_enumerate = type { i32 }
%struct.dirent = type { i8*, i64 }
@errno = global i32 0
@DT_DIR = global i64 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @parent_crawl_children(%struct.udev_enumerate* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = tail call i32* @opendir(i8* %1)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %45
10: ; preds = %3
%11 = tail call %struct.dirent* @readdir(i32* %5)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %43, label %13
13: ; preds = %10
%14 = bitcast i8** %4 to i8*
%15 = icmp sgt i32 %2, 0
%16 = add nsw i32 %2, -1
br label %17
17: ; preds = %13, %40
%18 = phi %struct.dirent* [ %11, %13 ], [ %41, %40 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
br i1 %22, label %40, label %23
23: ; preds = %17
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 1
%25 = load i64, i64* %24
%26 = load i64, i64* @DT_DIR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %23
%29 = call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %20)
%30 = icmp slt i64 %29, 0
br i1 %30, label %40, label %31
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = call i32 @parent_add_child(%struct.udev_enumerate* %0, i8* %32)
br i1 %15, label %34, label %37
34: ; preds = %31
%35 = load i8*, i8** %4
%36 = call i32 @parent_crawl_children(%struct.udev_enumerate* %0, i8* %35, i32 %16)
br label %37
37: ; preds = %34, %31
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
br label %40
40: ; preds = %28, %23, %17, %37
%41 = call %struct.dirent* @readdir(i32* %5)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %17
43: ; preds = %40, %10
%44 = call i32 @closedir(i32* %5)
br label %45
45: ; preds = %43, %7
%46 = phi i32 [ %9, %7 ], [ 0, %43 ]
ret i32 %46
}