#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 udev {
  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 *);
struct udev *udev_enumerate_get_udev(struct udev_enumerate *);
int /*<<< orphan*/ udev_get_sys_path(struct udev *);
int /*<<< orphan*/ util_strlcat(char *, char const *, int);
int /*<<< orphan*/ util_strlcpy(char *, int /*<<< orphan*/, int);
int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir,
             const char *subdir, const char *subsystem) {
  struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
  char path[1024];
  DIR *dir;
  struct dirent *dent;
  util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
  util_strlcat(path, "/", sizeof(path));
  util_strlcat(path, basedir, sizeof(path));
  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    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <udev_enumerate_get_udev@plt>
mov    %rax,-0x30(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <udev_get_sys_path@plt>
mov    -0x448(%rbp),%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10a0 <util_strlcpy@plt>
lea    -0x430(%rbp),%rdi
lea    0xe10(%rip),%rsi
mov    $0x400,%edx
call   10b0 <util_strlcat@plt>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   10b0 <util_strlcat@plt>
lea    -0x430(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    123c <scan_dir+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <scan_dir+0x186>
mov    -0x438(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
je     1303 <scan_dir+0x173>
mov    -0x440(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1278 <scan_dir+0xe8>
jmp    12eb <scan_dir+0x15b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x450(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     129e <scan_dir+0x10e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x458(%rbp)
jmp    12af <scan_dir+0x11f>
mov    -0x440(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x458(%rbp)
mov    -0x450(%rbp),%rdi
mov    -0x458(%rbp),%rsi
call   1090 <match_subsystem@plt>
cmp    $0x0,%eax
jne    12d0 <scan_dir+0x140>
jmp    12eb <scan_dir+0x15b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x440(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <scan_dir_and_add_devices@plt>
mov    -0x438(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x440(%rbp)
jmp    124f <scan_dir+0xbf>
mov    -0x438(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%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
call   1080 <udev_enumerate_get_udev@plt>
mov    %rax,%rdi
call   1030 <udev_get_sys_path@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10a0 <util_strlcpy@plt>
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   10b0 <util_strlcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x400,%edx
call   10b0 <util_strlcat@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     126c <scan_dir+0xdc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1260 <scan_dir+0xd0>
mov    %rax,%rbp
jmp    1230 <scan_dir+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1260 <scan_dir+0xd0>
mov    0x0(%rbp),%rsi
cmpb   $0x2e,(%rsi)
je     1220 <scan_dir+0x90>
test   %r13,%r13
cmovne %r13,%rsi
mov    %r12,%rdi
call   1090 <match_subsystem@plt>
test   %eax,%eax
je     1220 <scan_dir+0x90>
mov    0x0(%rbp),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1060 <scan_dir_and_add_devices@plt>
jmp    1220 <scan_dir+0x90>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    1271 <scan_dir+0xe1>
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.udev = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c"/\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 %struct.udev*
%11 = alloca [1024 x i8]
%12 = alloca i32*
%13 = 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
%14 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%15 = call %struct.udev* @udev_enumerate_get_udev(%struct.udev_enumerate* %14)
store %struct.udev* %15, %struct.udev** %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%17 = load %struct.udev*, %struct.udev** %10
%18 = call i32 @udev_get_sys_path(%struct.udev* %17)
%19 = call i32 @util_strlcpy(i8* %16, i32 %18, i32 1024)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%21 = call i32 @util_strlcat(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1024)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%23 = load i8*, i8** %7
%24 = call i32 @util_strlcat(i8* %22, i8* %23, i32 1024)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %12
%27 = load i32*, i32** %12
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %4
store i32 -1, i32* %5
br label %74
30: ; preds = %4
%31 = load i32*, i32** %12
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %13
br label %33
33: ; preds = %68, %30
%34 = load %struct.dirent*, %struct.dirent** %13
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %71
36: ; preds = %33
%37 = load %struct.dirent*, %struct.dirent** %13
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 46
br i1 %43, label %44, label %45
44: ; preds = %36
br label %68
45: ; preds = %36
%46 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%47 = load i8*, i8** %9
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = load i8*, i8** %9
br label %55
51: ; preds = %45
%52 = load %struct.dirent*, %struct.dirent** %13
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
br label %55
55: ; preds = %51, %49
%56 = phi i8* [ %50, %49 ], [ %54, %51 ]
%57 = call i32 @match_subsystem(%struct.udev_enumerate* %46, i8* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %55
br label %68
60: ; preds = %55
%61 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%62 = load i8*, i8** %7
%63 = load %struct.dirent*, %struct.dirent** %13
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = load i8*, i8** %8
%67 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %61, i8* %62, i8* %65, i8* %66)
br label %68
68: ; preds = %60, %59, %44
%69 = load i32*, i32** %12
%70 = call %struct.dirent* @readdir(i32* %69)
store %struct.dirent* %70, %struct.dirent** %13
br label %33
71: ; preds = %33
%72 = load i32*, i32** %12
%73 = call i32 @closedir(i32* %72)
store i32 0, i32* %5
br label %74
74: ; preds = %71, %29
%75 = load i32, i32* %5
ret i32 %75
}

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