#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_strscpyl(char *, int, int /*<<< orphan*/, char *,
                                 int /*<<< orphan*/ *);
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_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem",
                NULL);
  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    11ba <udev_enumerate_scan_subsystems+0x3a>
xor    %eax,%eax
mov    0x2e30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12c8 <udev_enumerate_scan_subsystems+0x148>
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,%edx
mov    $0x400,%esi
lea    0xe1a(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <util_strscpyl@plt>
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1220 <udev_enumerate_scan_subsystems+0xa0>
lea    0xded(%rip),%rax
mov    %rax,-0x430(%rbp)
jmp    122e <udev_enumerate_scan_subsystems+0xae>
lea    0xde4(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   10a0 <match_subsystem@plt>
cmp    $0x0,%rax
je     1276 <udev_enumerate_scan_subsystems+0xf6>
mov    -0x18(%rbp),%rdi
mov    -0x430(%rbp),%rdx
lea    0xdb5(%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    0xda4(%rip),%rsi
call   10a0 <match_subsystem@plt>
cmp    $0x0,%rax
je     12c1 <udev_enumerate_scan_subsystems+0x141>
mov    -0x18(%rbp),%rdi
mov    -0x430(%rbp),%rdx
lea    0xd8b(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
mov    -0x430(%rbp),%rsi
lea    0xd6c(%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   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r12
call   1080 <udev_enumerate_get_udev@plt>
test   %r12,%r12
je     1258 <udev_enumerate_scan_subsystems+0xd8>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <udev_get_sys_path@plt>
lea    0xe4f(%rip),%rcx
xor    %r14d,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %eax,%edx
xor    %r8d,%r8d
call   1090 <util_strscpyl@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
lea    0xe1f(%rip),%rsi
lea    0xe22(%rip),%rbx
cmove  %rsi,%rbx
mov    %r12,%rdi
call   10a0 <match_subsystem@plt>
test   %rax,%rax
je     121b <udev_enumerate_scan_subsystems+0x9b>
lea    0xe0e(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1050 <udev_log_null@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <scan_dir_and_add_devices@plt>
lea    0xe03(%rip),%rsi
mov    %r12,%rdi
call   10a0 <match_subsystem@plt>
test   %rax,%rax
je     1265 <udev_enumerate_scan_subsystems+0xe5>
lea    0xdf7(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1050 <udev_log_null@plt>
lea    0xddd(%rip),%rcx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rcx,%rdx
call   1030 <scan_dir@plt>
jmp    1265 <udev_enumerate_scan_subsystems+0xe5>
mov    0x2d81(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 %48
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_strscpyl(i8* %16, i32 1024, i32 %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* null)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%21 = call i64 @stat(i8* %20, %struct.stat* %6)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %25
24: ; preds = %15
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %7
br label %25
25: ; preds = %24, %23
%26 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%27 = call i64 @match_subsystem(%struct.udev_enumerate* %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %25
%30 = load %struct.udev*, %struct.udev** %4
%31 = load i8*, i8** %7
%32 = call i32 @udev_log_null(%struct.udev* %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%34 = load i8*, i8** %7
%35 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %33, i8* %34, i32* null, i32* null)
br label %36
36: ; preds = %29, %25
%37 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%38 = call i64 @match_subsystem(%struct.udev_enumerate* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load %struct.udev*, %struct.udev** %4
%42 = load i8*, i8** %7
%43 = call i32 @udev_log_null(%struct.udev* %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %42)
%44 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%45 = load i8*, i8** %7
%46 = call i32 @scan_dir(%struct.udev_enumerate* %44, i8* %45, 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 %47
47: ; preds = %40, %36
store i32 0, i32* %2
br label %48
48: ; preds = %47, %12
%49 = load i32, i32* %2
ret i32 %49
}

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 %28
11: ; preds = %1
%12 = tail call i32 @udev_get_sys_path(%struct.udev* %4)
%13 = call i32 @util_strscpyl(i8* %5, i32 1024, i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* null)
%14 = call i64 @stat(i8* %5, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, 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)
%17 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %11
%20 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%21 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* %16, i32* null, i32* null)
br label %22
22: ; preds = %19, %11
%23 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %16)
%27 = call i32 @scan_dir(%struct.udev_enumerate* %0, i8* %16, 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 %28
28: ; preds = %22, %25, %8
%29 = phi i32 [ %10, %8 ], [ 0, %25 ], [ 0, %22 ]
ret i32 %29
}