#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 stat {
  int st_mode;
  int /*<<< orphan*/ st_rdev;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int S_IFBLK;
int S_IFDIR;
int S_IFLNK;
int S_IFMT;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dev_add(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
void dev_cache_devs(const char *dev_path, const int depth,
                    const int max_depth) {
  DIR *dir;
  struct dirent *dirp;
  dir = opendir(dev_path);
  if (!dir)
    return;
  while ((dirp = readdir(dir)) != NULL) {
    struct stat statbuf;
    char path[PATH_MAX];
    if (dirp->d_name[0] == '.')
      continue;
    snprintf(path, sizeof(path), "%s/%s", dev_path, dirp->d_name);
    if (stat(path, &statbuf) < 0)
      continue;
    if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
      continue;
    if ((depth < max_depth) && ((statbuf.st_mode & S_IFMT) == S_IFDIR)) {
      dev_cache_devs(path, depth + 1, max_depth);
      continue;
    }
    if ((statbuf.st_mode & S_IFMT) != S_IFBLK)
      continue;
    dev_add(path, statbuf.st_rdev);
  }
  (void)closedir(dir);
}


x86 assembly (clang, -O0):
<dev_cache_devs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119f <dev_cache_devs+0x2f>
jmp    131e <dev_cache_devs+0x1ae>
jmp    11a4 <dev_cache_devs+0x34>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1315 <dev_cache_devs+0x1a5>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    120a <dev_cache_devs+0x9a>
movl   $0x2,-0x3c(%rbp)
jmp    12f4 <dev_cache_devs+0x184>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xdda(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    124e <dev_cache_devs+0xde>
movl   $0x2,-0x3c(%rbp)
jmp    12f4 <dev_cache_devs+0x184>
mov    -0x28(%rbp),%eax
mov    0x2d78(%rip),%rcx
and    (%rcx),%eax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%eax
jne    1275 <dev_cache_devs+0x105>
movl   $0x2,-0x3c(%rbp)
jmp    12f4 <dev_cache_devs+0x184>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12ba <dev_cache_devs+0x14a>
mov    -0x28(%rbp),%eax
mov    0x2d45(%rip),%rcx
and    (%rcx),%eax
mov    0x2d24(%rip),%rcx
cmp    (%rcx),%eax
jne    12ba <dev_cache_devs+0x14a>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1090 <dev_cache_devs@plt>
movl   $0x2,-0x3c(%rbp)
jmp    12f4 <dev_cache_devs+0x184>
mov    -0x28(%rbp),%eax
mov    0x2d0c(%rip),%rcx
and    (%rcx),%eax
mov    0x2cfb(%rip),%rcx
cmp    (%rcx),%eax
je     12e1 <dev_cache_devs+0x171>
movl   $0x2,-0x3c(%rbp)
jmp    12f4 <dev_cache_devs+0x184>
mov    -0x48(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <dev_add@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     1310 <dev_cache_devs+0x1a0>
jmp    130b <dev_cache_devs+0x19b>
jmp    11a4 <dev_cache_devs+0x34>
jmp    11a4 <dev_cache_devs+0x34>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dev_cache_devs>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,-0x2c(%rbp)
mov    %rsi,-0x38(%rbp)
mov    %rdi,%r12
call   1030 <opendir@plt>
test   %rax,%rax
je     127e <dev_cache_devs+0x10e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1276 <dev_cache_devs+0x106>
mov    -0x38(%rbp),%rcx
add    $0x1,%ecx
mov    %ecx,-0x30(%rbp)
mov    0x2e32(%rip),%r13
jmp    11e4 <dev_cache_devs+0x74>
mov    %r15,%rdi
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1090 <dev_cache_devs@plt>
xchg   %ax,%ax
mov    %r14,%rsp
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1276 <dev_cache_devs+0x106>
mov    %rsp,%r14
mov    0x0(%r13),%esi
mov    %rsp,%r15
lea    0xf(%rsi),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
mov    (%rax),%r8
cmpb   $0x2e,(%r8)
je     11d0 <dev_cache_devs+0x60>
mov    %r15,%rdi
lea    0xdf1(%rip),%rdx
mov    %r12,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     11d0 <dev_cache_devs+0x60>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
and    -0x40(%rbp),%eax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%eax
je     11d0 <dev_cache_devs+0x60>
mov    -0x38(%rbp),%rcx
cmp    -0x2c(%rbp),%ecx
jge    1257 <dev_cache_devs+0xe7>
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%eax
je     11c0 <dev_cache_devs+0x50>
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%eax
jne    11d0 <dev_cache_devs+0x60>
mov    -0x3c(%rbp),%esi
mov    %r15,%rdi
call   1050 <dev_add@plt>
jmp    11d0 <dev_cache_devs+0x60>
mov    %rbx,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@S_IFDIR = global i32 0
@S_IFBLK = global i32 0
define void @dev_cache_devs(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = load i8*, i8** %4
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %3
br label %88
18: ; preds = %3
br label %19
19: ; preds = %84, %81, %18
%20 = load i32*, i32** %7
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %8
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %85
23: ; preds = %19
%24 = load i32, i32* @PATH_MAX
%25 = zext i32 %24 to i64
%26 = call i8* @llvm.stacksave()
store i8* %26, i8** %10
%27 = alloca i8, i64 %25
store i64 %25, i64* %11
%28 = load %struct.dirent*, %struct.dirent** %8
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 46
br i1 %34, label %35, label %36
35: ; preds = %23
store i32 2, i32* %12
br label %81
36: ; preds = %23
%37 = trunc i64 %25 to i32
%38 = load i8*, i8** %4
%39 = load %struct.dirent*, %struct.dirent** %8
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %27, i32 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %38, i8* %41)
%43 = call i64 @stat(i8* %27, %struct.stat* %9)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %36
store i32 2, i32* %12
br label %81
46: ; preds = %36
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @S_IFMT
%50 = and i32 %48, %49
%51 = load i32, i32* @S_IFLNK
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %46
store i32 2, i32* %12
br label %81
54: ; preds = %46
%55 = load i32, i32* %5
%56 = load i32, i32* %6
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %54
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%60 = load i32, i32* %59
%61 = load i32, i32* @S_IFMT
%62 = and i32 %60, %61
%63 = load i32, i32* @S_IFDIR
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %69
65: ; preds = %58
%66 = load i32, i32* %5
%67 = add nsw i32 %66, 1
%68 = load i32, i32* %6
store i32 2, i32* %12
br label %81
69: ; preds = %58, %54
%70 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%71 = load i32, i32* %70
%72 = load i32, i32* @S_IFMT
%73 = and i32 %71, %72
%74 = load i32, i32* @S_IFBLK
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %77
76: ; preds = %69
store i32 2, i32* %12
br label %81
77: ; preds = %69
%78 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%79 = load i32, i32* %78
%80 = call i32 @dev_add(i8* %27, i32 %79)
store i32 0, i32* %12
br label %81
81: ; preds = %77, %76, %65, %53, %45, %35
%82 = load i8*, i8** %10
%83 = load i32, i32* %12
switch i32 %83, label %89 [
i32 0, label %84
i32 2, label %19
]
84: ; preds = %81
br label %19
85: ; preds = %19
%86 = load i32*, i32** %7
%87 = call i32 @closedir(i32* %86)
br label %88
88: ; preds = %85, %17
ret void
89: ; preds = %81
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@S_IFDIR = global i32 0
@S_IFBLK = global i32 0
define void @dev_cache_devs(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %52, label %7
7: ; preds = %3
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %50, label %10
10: ; preds = %7
%11 = bitcast %struct.stat* %4 to i8*
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%13 = icmp slt i32 %1, %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%15 = add nsw i32 %1, 1
br label %16
16: ; preds = %10, %47
%17 = phi %struct.dirent* [ %8, %10 ], [ %48, %47 ]
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
%21 = alloca i8, i64 %19
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 46
br i1 %25, label %47, label %26
26: ; preds = %16
%27 = call i32 @snprintf(i8* %21, i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %23)
%28 = call i64 @stat(i8* %21, %struct.stat* %4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %47, label %30
30: ; preds = %26
%31 = load i32, i32* %12
%32 = load i32, i32* @S_IFMT
%33 = and i32 %32, %31
%34 = load i32, i32* @S_IFLNK
%35 = icmp eq i32 %33, %34
br i1 %35, label %47, label %36
36: ; preds = %30
%37 = load i32, i32* @S_IFDIR
%38 = icmp eq i32 %33, %37
%39 = select i1 %13, i1 %38, i1 false
br i1 %39, label %40, label %41
40: ; preds = %36
br label %47
41: ; preds = %36
%42 = load i32, i32* @S_IFBLK
%43 = icmp eq i32 %33, %42
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* %14
%46 = call i32 @dev_add(i8* %21, i32 %45)
br label %47
47: ; preds = %41, %30, %26, %16, %44, %40
%48 = call %struct.dirent* @readdir(i32* %5)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %16
50: ; preds = %47, %7
%51 = call i32 @closedir(i32* %5)
br label %52
52: ; preds = %3, %50
ret void
}