#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;
};
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ match_subsystem(struct udev_enumerate *, char const *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ scan_dir_and_add_devices(struct udev_enumerate *,
                                            char const *, char const *,
                                            char const *);
int /*<<< orphan*/ strscpyl(char *, int, char *, char const *,
                            int /*<<< orphan*/ *);
int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir,
             const char *subdir, const char *subsystem) {
  char path[1024];
  DIR *dir;
  struct dirent *dent;
  strscpyl(path, sizeof(path), "/sys/", basedir, NULL);
  dir = opendir(path);
  if (dir == NULL)
    return -1;
  for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
    if (dent->d_name[0] == '.')
      continue;
    if (!match_subsystem(udev_enumerate,
                         subsystem != NULL ? subsystem : dent->d_name))
      continue;
    scan_dir_and_add_devices(udev_enumerate, basedir, dent->d_name, subdir);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<scan_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x400,%esi
lea    0xe6e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <strscpyl@plt>
lea    -0x430(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    11c9 <scan_dir+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <scan_dir+0x143>
mov    -0x438(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
je     1290 <scan_dir+0x130>
mov    -0x440(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1205 <scan_dir+0xa5>
jmp    1278 <scan_dir+0x118>
mov    -0x10(%rbp),%rax
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     122b <scan_dir+0xcb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x450(%rbp)
jmp    123c <scan_dir+0xdc>
mov    -0x440(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x450(%rbp)
mov    -0x448(%rbp),%rdi
mov    -0x450(%rbp),%rsi
call   1080 <match_subsystem@plt>
cmp    $0x0,%eax
jne    125d <scan_dir+0xfd>
jmp    1278 <scan_dir+0x118>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x440(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <scan_dir_and_add_devices@plt>
mov    -0x438(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x440(%rbp)
jmp    11dc <scan_dir+0x7c>
mov    -0x438(%rbp),%rdi
call   1040 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe7c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rcx
xor    %r8d,%r8d
call   1070 <strscpyl@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     120c <scan_dir+0xac>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1200 <scan_dir+0xa0>
mov    %rax,%rbp
jmp    11d0 <scan_dir+0x70>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1200 <scan_dir+0xa0>
mov    0x0(%rbp),%rsi
cmpb   $0x2e,(%rsi)
je     11c0 <scan_dir+0x60>
test   %r13,%r13
cmovne %r13,%rsi
mov    %r12,%rdi
call   1080 <match_subsystem@plt>
test   %eax,%eax
je     11c0 <scan_dir+0x60>
mov    0x0(%rbp),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1050 <scan_dir_and_add_devices@plt>
jmp    11c0 <scan_dir+0x60>
mov    %rbx,%rdi
call   1040 <closedir@plt>
xor    %eax,%eax
jmp    1211 <scan_dir+0xb1>
mov    $0xffffffff,%eax
add    $0x408,%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* }
@.str = [6 x i8] c"/sys/\00"
define i32 @scan_dir(%struct.udev_enumerate* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.udev_enumerate*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [1024 x i8]
%11 = alloca i32*
%12 = alloca %struct.dirent*
store %struct.udev_enumerate* %0, %struct.udev_enumerate** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %7
%15 = call i32 @strscpyl(i8* %13, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i32* null)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %11
%18 = load i32*, i32** %11
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %65
21: ; preds = %4
%22 = load i32*, i32** %11
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %12
br label %24
24: ; preds = %59, %21
%25 = load %struct.dirent*, %struct.dirent** %12
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %62
27: ; preds = %24
%28 = load %struct.dirent*, %struct.dirent** %12
%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 = %27
br label %59
36: ; preds = %27
%37 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%38 = load i8*, i8** %9
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i8*, i8** %9
br label %46
42: ; preds = %36
%43 = load %struct.dirent*, %struct.dirent** %12
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
br label %46
46: ; preds = %42, %40
%47 = phi i8* [ %41, %40 ], [ %45, %42 ]
%48 = call i32 @match_subsystem(%struct.udev_enumerate* %37, i8* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %46
br label %59
51: ; preds = %46
%52 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%53 = load i8*, i8** %7
%54 = load %struct.dirent*, %struct.dirent** %12
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = load i8*, i8** %8
%58 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %52, i8* %53, i8* %56, i8* %57)
br label %59
59: ; preds = %51, %50, %35
%60 = load i32*, i32** %11
%61 = call %struct.dirent* @readdir(i32* %60)
store %struct.dirent* %61, %struct.dirent** %12
br label %24
62: ; preds = %24
%63 = load i32*, i32** %11
%64 = call i32 @closedir(i32* %63)
store i32 0, i32* %5
br label %65
65: ; preds = %62, %20
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
%struct.udev_enumerate = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/sys/\00"
define i32 @scan_dir(%struct.udev_enumerate* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @strscpyl(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i32* null)
%8 = call i32* @opendir(i8* %6)
%9 = icmp eq i32* %8, null
br i1 %9, label %33, label %10
10: ; preds = %4
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %31, label %13
13: ; preds = %10
%14 = icmp eq i8* %3, null
br label %15
15: ; preds = %13, %28
%16 = phi %struct.dirent* [ %11, %13 ], [ %29, %28 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 46
br i1 %20, label %28, label %21
21: ; preds = %15
%22 = select i1 %14, i8* %18, i8* %3
%23 = call i32 @match_subsystem(%struct.udev_enumerate* %0, i8* %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i8*, i8** %17
%27 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* %1, i8* %26, i8* %2)
br label %28
28: ; preds = %21, %15, %25
%29 = call %struct.dirent* @readdir(i32* %8)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %15
31: ; preds = %28, %10
%32 = call i32 @closedir(i32* %8)
br label %33
33: ; preds = %4, %31
%34 = phi i32 [ 0, %31 ], [ -1, %4 ]
ret i32 %34
}