#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 stat {
  int dummy;
};

/* 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 *);
__attribute__((visibility("default"))) int
udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate) {
  struct stat statbuf;
  const char *subsysdir;
  if (udev_enumerate == NULL)
    return -EINVAL;
  if (match_subsystem(udev_enumerate, "module"))
    scan_dir_and_add_devices(udev_enumerate, "module", NULL, NULL);
  if (stat("/sys/subsystem", &statbuf) == 0)
    subsysdir = "subsystem";
  else
    subsysdir = "bus";
  if (match_subsystem(udev_enumerate, "subsystem"))
    scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL);
  if (match_subsystem(udev_enumerate, "drivers"))
    scan_dir(udev_enumerate, subsysdir, "drivers", "drivers");
  return 0;
}


x86 assembly (clang, -O0):
<udev_enumerate_scan_subsystems>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116a <udev_enumerate_scan_subsystems+0x2a>
xor    %eax,%eax
mov    0x2e80(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <udev_enumerate_scan_subsystems+0xf6>
mov    -0x10(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <match_subsystem@plt>
cmp    $0x0,%rax
je     119b <udev_enumerate_scan_subsystems+0x5b>
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <scan_dir_and_add_devices@plt>
lea    0xe65(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    11c5 <udev_enumerate_scan_subsystems+0x85>
lea    0xe50(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11d0 <udev_enumerate_scan_subsystems+0x90>
lea    0xe4a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1060 <match_subsystem@plt>
cmp    $0x0,%rax
je     11fe <udev_enumerate_scan_subsystems+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <scan_dir_and_add_devices@plt>
mov    -0x10(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1060 <match_subsystem@plt>
cmp    $0x0,%rax
je     122f <udev_enumerate_scan_subsystems+0xef>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xdf3(%rip),%rcx
mov    %rcx,%rdx
call   1030 <scan_dir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_enumerate_scan_subsystems>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11e7 <udev_enumerate_scan_subsystems+0xa7>
mov    %rdi,%rbx
lea    0xea5(%rip),%rsi
call   1060 <match_subsystem@plt>
test   %rax,%rax
je     1178 <udev_enumerate_scan_subsystems+0x38>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <scan_dir_and_add_devices@plt>
lea    0xe88(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
lea    0xe79(%rip),%rsi
lea    0xe7c(%rip),%r14
cmove  %rsi,%r14
mov    %rbx,%rdi
call   1060 <match_subsystem@plt>
test   %rax,%rax
je     11ba <udev_enumerate_scan_subsystems+0x7a>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <scan_dir_and_add_devices@plt>
lea    0xe59(%rip),%rsi
mov    %rbx,%rdi
call   1060 <match_subsystem@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11f2 <udev_enumerate_scan_subsystems+0xb2>
lea    0xe43(%rip),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rcx,%rdx
call   1030 <scan_dir@plt>
jmp    11f2 <udev_enumerate_scan_subsystems+0xb2>
mov    0x2df2(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.udev_enumerate = type { i32 }
%struct.stat = type { i32 }
@EINVAL = global i32 0
@.str = [7 x i8] c"module\00"
@.str.1 = [15 x i8] c"/sys/subsystem\00"
@.str.2 = [10 x i8] c"subsystem\00"
@.str.3 = [4 x i8] c"bus\00"
@.str.4 = [8 x i8] c"drivers\00"
define i32 @udev_enumerate_scan_subsystems(%struct.udev_enumerate* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq %struct.udev_enumerate* %0, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @EINVAL
%7 = sub nsw i32 0, %6
br label %26
8: ; preds = %1
%9 = tail call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
br label %13
13: ; preds = %11, %8
%14 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %2)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%17 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* %16, i32* null, i32* null)
br label %21
21: ; preds = %19, %13
%22 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = 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 %26
26: ; preds = %21, %24, %5
%27 = phi i32 [ %7, %5 ], [ 0, %24 ], [ 0, %21 ]
ret i32 %27
}