#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_strscpyl(char *, int, int /*<<< orphan*/, char *,
                                 char const *, int /*<<< orphan*/ *);
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_strscpyl(path, sizeof(path), udev_get_sys_path(udev), "/", 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    $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,%edx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe28(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
call   1090 <util_strscpyl@plt>
lea    -0x430(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    120f <scan_dir+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <scan_dir+0x169>
mov    -0x438(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
je     12d6 <scan_dir+0x156>
mov    -0x440(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    124b <scan_dir+0xcb>
jmp    12be <scan_dir+0x13e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x450(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1271 <scan_dir+0xf1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x458(%rbp)
jmp    1282 <scan_dir+0x102>
mov    -0x440(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x458(%rbp)
mov    -0x450(%rbp),%rdi
mov    -0x458(%rbp),%rsi
call   10a0 <match_subsystem@plt>
cmp    $0x0,%eax
jne    12a3 <scan_dir+0x123>
jmp    12be <scan_dir+0x13e>
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    1222 <scan_dir+0xa2>
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>
lea    0xe4f(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %eax,%edx
mov    %r15,%r8
xor    %r9d,%r9d
call   1090 <util_strscpyl@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     123c <scan_dir+0xbc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1230 <scan_dir+0xb0>
mov    %rax,%rbp
jmp    1200 <scan_dir+0x80>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1230 <scan_dir+0xb0>
mov    0x0(%rbp),%rsi
cmpb   $0x2e,(%rsi)
je     11f0 <scan_dir+0x70>
test   %r13,%r13
cmovne %r13,%rsi
mov    %r12,%rdi
call   10a0 <match_subsystem@plt>
test   %eax,%eax
je     11f0 <scan_dir+0x70>
mov    0x0(%rbp),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1060 <scan_dir_and_add_devices@plt>
jmp    11f0 <scan_dir+0x70>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    1241 <scan_dir+0xc1>
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 = load i8*, i8** %7
%20 = call i32 @util_strscpyl(i8* %16, i32 1024, i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %19, i32* null)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %12
%23 = load i32*, i32** %12
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %4
store i32 -1, i32* %5
br label %70
26: ; preds = %4
%27 = load i32*, i32** %12
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %13
br label %29
29: ; preds = %64, %26
%30 = load %struct.dirent*, %struct.dirent** %13
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %67
32: ; preds = %29
%33 = load %struct.dirent*, %struct.dirent** %13
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 46
br i1 %39, label %40, label %41
40: ; preds = %32
br label %64
41: ; preds = %32
%42 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%43 = load i8*, i8** %9
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i8*, i8** %9
br label %51
47: ; preds = %41
%48 = load %struct.dirent*, %struct.dirent** %13
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
br label %51
51: ; preds = %47, %45
%52 = phi i8* [ %46, %45 ], [ %50, %47 ]
%53 = call i32 @match_subsystem(%struct.udev_enumerate* %42, i8* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %51
br label %64
56: ; preds = %51
%57 = load %struct.udev_enumerate*, %struct.udev_enumerate** %6
%58 = load i8*, i8** %7
%59 = load %struct.dirent*, %struct.dirent** %13
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = load i8*, i8** %8
%63 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %57, i8* %58, i8* %61, i8* %62)
br label %64
64: ; preds = %56, %55, %40
%65 = load i32*, i32** %12
%66 = call %struct.dirent* @readdir(i32* %65)
store %struct.dirent* %66, %struct.dirent** %13
br label %29
67: ; preds = %29
%68 = load i32*, i32** %12
%69 = call i32 @closedir(i32* %68)
store i32 0, i32* %5
br label %70
70: ; preds = %67, %25
%71 = load i32, i32* %5
ret i32 %71
}

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_strscpyl(i8* %7, i32 1024, i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %1, i32* null)
%10 = call i32* @opendir(i8* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %35, label %12
12: ; preds = %4
%13 = call %struct.dirent* @readdir(i32* %10)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %33, label %15
15: ; preds = %12
%16 = icmp eq i8* %3, null
br label %17
17: ; preds = %15, %30
%18 = phi %struct.dirent* [ %13, %15 ], [ %31, %30 ]
%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 %30, label %23
23: ; preds = %17
%24 = select i1 %16, i8* %20, i8* %3
%25 = call i32 @match_subsystem(%struct.udev_enumerate* %0, i8* %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i8*, i8** %19
%29 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* %1, i8* %28, i8* %2)
br label %30
30: ; preds = %23, %17, %27
%31 = call %struct.dirent* @readdir(i32* %10)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %33, label %17
33: ; preds = %30, %12
%34 = call i32 @closedir(i32* %10)
br label %35
35: ; preds = %4, %33
%36 = phi i32 [ 0, %33 ], [ -1, %4 ]
ret i32 %36
}