#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 stat {
  int dummy;
};
typedef int /*<<< orphan*/ base;

/* Variables and functions */
int EINVAL;
scalar_t__ match_subsystem(struct udev_enumerate *, char *);
int /*<<< orphan*/ scan_dir(struct udev_enumerate *, char const *, char *,
                            char *);
int /*<<< orphan*/ scan_dir_and_add_devices(struct udev_enumerate *,
                                            char const *, int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
struct udev *udev_enumerate_get_udev(struct udev_enumerate *);
int /*<<< orphan*/ udev_get_sys_path(struct udev *);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char const *);
int /*<<< orphan*/ util_strlcat(char *, char *, int);
int /*<<< orphan*/ util_strlcpy(char *, int /*<<< orphan*/, int);
int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate) {
  struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
  char base[1024];
  struct stat statbuf;
  const char *subsysdir;
  if (udev_enumerate == NULL)
    return -EINVAL;
  util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
  util_strlcat(base, "/subsystem", sizeof(base));
  if (stat(base, &statbuf) == 0)
    subsysdir = "subsystem";
  else
    subsysdir = "bus";
  if (match_subsystem(udev_enumerate, "subsystem")) {
    udev_log_null(udev, "searching '%s/*' dir\n", subsysdir);
    scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL);
  }
  if (match_subsystem(udev_enumerate, "drivers")) {
    udev_log_null(udev, "searching '%s/*/drivers/*' dir\n", subsysdir);
    scan_dir(udev_enumerate, subsysdir, "drivers", "drivers");
  }
  return 0;
}


x86 assembly (clang, -O0):
<udev_enumerate_scan_subsystems>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <udev_enumerate_get_udev@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ca <udev_enumerate_scan_subsystems+0x3a>
xor    %eax,%eax
mov    0x2e20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12e4 <udev_enumerate_scan_subsystems+0x154>
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <udev_get_sys_path@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10a0 <util_strlcpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x400,%edx
call   10b0 <util_strlcat@plt>
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    123c <udev_enumerate_scan_subsystems+0xac>
lea    0xdd1(%rip),%rax
mov    %rax,-0x430(%rbp)
jmp    124a <udev_enumerate_scan_subsystems+0xba>
lea    0xdc8(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1090 <match_subsystem@plt>
cmp    $0x0,%rax
je     1292 <udev_enumerate_scan_subsystems+0x102>
mov    -0x18(%rbp),%rdi
mov    -0x430(%rbp),%rdx
lea    0xd99(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
mov    -0x430(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <scan_dir_and_add_devices@plt>
mov    -0x10(%rbp),%rdi
lea    0xd88(%rip),%rsi
call   1090 <match_subsystem@plt>
cmp    $0x0,%rax
je     12dd <udev_enumerate_scan_subsystems+0x14d>
mov    -0x18(%rbp),%rdi
mov    -0x430(%rbp),%rdx
lea    0xd6f(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
mov    -0x430(%rbp),%rsi
lea    0xd50(%rip),%rcx
mov    %rcx,%rdx
call   1030 <scan_dir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_enumerate_scan_subsystems>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r15
call   1080 <udev_enumerate_get_udev@plt>
test   %r15,%r15
je     1270 <udev_enumerate_scan_subsystems+0xe0>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <udev_get_sys_path@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10a0 <util_strlcpy@plt>
lea    0xe2c(%rip),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   10b0 <util_strlcat@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
lea    0xe09(%rip),%rsi
lea    0xe0c(%rip),%rbx
cmove  %rsi,%rbx
mov    %r15,%rdi
call   1090 <match_subsystem@plt>
test   %rax,%rax
je     1231 <udev_enumerate_scan_subsystems+0xa1>
lea    0xdf8(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1050 <udev_log_null@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <scan_dir_and_add_devices@plt>
lea    0xded(%rip),%rsi
mov    %r15,%rdi
call   1090 <match_subsystem@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     127b <udev_enumerate_scan_subsystems+0xeb>
lea    0xddf(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1050 <udev_log_null@plt>
lea    0xdc5(%rip),%rcx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rcx,%rdx
call   1030 <scan_dir@plt>
jmp    127b <udev_enumerate_scan_subsystems+0xeb>
mov    0x2d69(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev_enumerate = type { i32 }
%struct.udev = type { i32 }
%struct.stat = type { i32 }
@EINVAL = global i32 0
@.str = [11 x i8] c"/subsystem\00"
@.str.1 = [10 x i8] c"subsystem\00"
@.str.2 = [4 x i8] c"bus\00"
@.str.3 = [22 x i8] c"searching '%s/*' dir\0A\00"
@.str.4 = [8 x i8] c"drivers\00"
@.str.5 = [32 x i8] c"searching '%s/*/drivers/*' dir\0A\00"
define i32 @udev_enumerate_scan_subsystems(%struct.udev_enumerate* %0) {
%2 = alloca i32
%3 = alloca %struct.udev_enumerate*
%4 = alloca %struct.udev*
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
%7 = alloca i8*
store %struct.udev_enumerate* %0, %struct.udev_enumerate** %3
%8 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%9 = call %struct.udev* @udev_enumerate_get_udev(%struct.udev_enumerate* %8)
store %struct.udev* %9, %struct.udev** %4
%10 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%11 = icmp eq %struct.udev_enumerate* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %50
15: ; preds = %1
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%17 = load %struct.udev*, %struct.udev** %4
%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]* %5, i64 0, i64 0
%21 = call i32 @util_strlcat(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 1024)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i64 @stat(i8* %22, %struct.stat* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %27
26: ; preds = %15
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %7
br label %27
27: ; preds = %26, %25
%28 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%29 = call i64 @match_subsystem(%struct.udev_enumerate* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load %struct.udev*, %struct.udev** %4
%33 = load i8*, i8** %7
%34 = call i32 @udev_log_null(%struct.udev* %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%36 = load i8*, i8** %7
%37 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %35, i8* %36, i32* null, i32* null)
br label %38
38: ; preds = %31, %27
%39 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%40 = call i64 @match_subsystem(%struct.udev_enumerate* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = load %struct.udev*, %struct.udev** %4
%44 = load i8*, i8** %7
%45 = call i32 @udev_log_null(%struct.udev* %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%47 = load i8*, i8** %7
%48 = call i32 @scan_dir(%struct.udev_enumerate* %46, i8* %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %42, %38
store i32 0, i32* %2
br label %50
50: ; preds = %49, %12
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.udev_enumerate = type { i32 }
%struct.stat = type { i32 }
%struct.udev = type { i32 }
@EINVAL = global i32 0
@.str = [11 x i8] c"/subsystem\00"
@.str.1 = [10 x i8] c"subsystem\00"
@.str.2 = [4 x i8] c"bus\00"
@.str.3 = [22 x i8] c"searching '%s/*' dir\0A\00"
@.str.4 = [8 x i8] c"drivers\00"
@.str.5 = [32 x i8] c"searching '%s/*/drivers/*' dir\0A\00"
define i32 @udev_enumerate_scan_subsystems(%struct.udev_enumerate* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = tail call %struct.udev* @udev_enumerate_get_udev(%struct.udev_enumerate* %0)
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.stat* %3 to i8*
%7 = icmp eq %struct.udev_enumerate* %0, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %29
11: ; preds = %1
%12 = tail call i32 @udev_get_sys_path(%struct.udev* %4)
%13 = call i32 @util_strlcpy(i8* %5, i32 %12, i32 1024)
%14 = call i32 @util_strlcat(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 1024)
%15 = call i64 @stat(i8* %5, %struct.stat* %3)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%18 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %11
%21 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%22 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* %17, i32* null, i32* null)
br label %23
23: ; preds = %20, %11
%24 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %17)
%28 = call i32 @scan_dir(%struct.udev_enumerate* %0, i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %23, %26, %8
%30 = phi i32 [ %10, %8 ], [ 0, %26 ], [ 0, %23 ]
ret i32 %30
}