#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 *, char *, char *);
int /*<<< orphan*/ scan_dir_and_add_devices(struct udev_enumerate *, char *,
                                            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 *);
int /*<<< orphan*/ util_strlcat(char *, char *, int);
int /*<<< orphan*/ util_strlcpy(char *, int /*<<< orphan*/, int);
int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate) {
  struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
  char base[1024];
  struct stat statbuf;
  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) {
    udev_log_null(udev, "searching '/subsystem/*/devices/*' dir\n");
    scan_dir(udev_enumerate, "subsystem", "devices", NULL);
  } else {
    udev_log_null(udev, "searching '/bus/*/devices/*' dir\n");
    scan_dir(udev_enumerate, "bus", "devices", NULL);
    udev_log_null(udev, "searching '/class/*' dir\n");
    scan_dir(udev_enumerate, "class", NULL, NULL);
    util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
    util_strlcat(base, "/class/block", sizeof(base));
    if (stat(base, &statbuf) != 0) {
      if (match_subsystem(udev_enumerate, "block")) {
        udev_log_null(udev, "searching '/block/*' dir\n");
        scan_dir_and_add_devices(udev_enumerate, "block", NULL, NULL);
        udev_log_null(udev, "searching '/block/*/*' dir\n");
        scan_dir(udev_enumerate, "block", NULL, "block");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<udev_enumerate_scan_devices>:
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_devices+0x3a>
xor    %eax,%eax
mov    0x2e20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1383 <udev_enumerate_scan_devices+0x1f3>
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <udev_get_sys_path@plt>
mov    -0x430(%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    1259 <udev_enumerate_scan_devices+0xc9>
mov    -0x18(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    0xdbd(%rip),%rsi
lea    0xde8(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <scan_dir@plt>
jmp    137c <udev_enumerate_scan_devices+0x1ec>
mov    -0x18(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    0xde9(%rip),%rsi
lea    0xdb8(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <scan_dir@plt>
mov    -0x18(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    0xddc(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1030 <scan_dir@plt>
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    0xd9e(%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
je     1377 <udev_enumerate_scan_devices+0x1e7>
mov    -0x10(%rbp),%rdi
lea    0xd73(%rip),%rsi
call   1090 <match_subsystem@plt>
cmp    $0x0,%rax
je     1372 <udev_enumerate_scan_devices+0x1e2>
mov    -0x18(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    0xd49(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <scan_dir_and_add_devices@plt>
mov    -0x18(%rbp),%rdi
lea    0xd52(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    0xd22(%rip),%rcx
xor    %eax,%eax
mov    %eax,%edx
mov    %rcx,%rsi
call   1030 <scan_dir@plt>
jmp    1377 <udev_enumerate_scan_devices+0x1e7>
jmp    137c <udev_enumerate_scan_devices+0x1ec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_enumerate_scan_devices>:
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     12e2 <udev_enumerate_scan_devices+0x152>
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    0xe2b(%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
je     12f1 <udev_enumerate_scan_devices+0x161>
lea    0xe3c(%rip),%rsi
mov    %r14,%rdi
call   1050 <udev_log_null@plt>
lea    0xe4f(%rip),%rsi
lea    0xe1e(%rip),%rdx
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %ecx,%ecx
call   1030 <scan_dir@plt>
lea    0xe38(%rip),%rsi
mov    %r14,%rdi
call   1050 <udev_log_null@plt>
lea    0xe43(%rip),%rsi
mov    %r12,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <scan_dir@plt>
mov    %r14,%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    0xe1a(%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
je     131b <udev_enumerate_scan_devices+0x18b>
lea    0xdf7(%rip),%rsi
mov    %r12,%rdi
call   1090 <match_subsystem@plt>
test   %rax,%rax
je     131b <udev_enumerate_scan_devices+0x18b>
lea    0xde9(%rip),%rsi
mov    %r14,%rdi
call   1050 <udev_log_null@plt>
lea    0xdd4(%rip),%rbx
xor    %r15d,%r15d
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <scan_dir_and_add_devices@plt>
lea    0xddb(%rip),%rsi
mov    %r14,%rdi
call   1050 <udev_log_null@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
mov    %rbx,%rcx
jmp    1316 <udev_enumerate_scan_devices+0x186>
mov    0x2cf7(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
jmp    131b <udev_enumerate_scan_devices+0x18b>
lea    0xd13(%rip),%rsi
mov    %r14,%rdi
call   1050 <udev_log_null@plt>
lea    0xcfa(%rip),%rsi
lea    0xd25(%rip),%rdx
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %ecx,%ecx
call   1030 <scan_dir@plt>
mov    %r15d,%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 = [40 x i8] c"searching '/subsystem/*/devices/*' dir\0A\00"
@.str.2 = [10 x i8] c"subsystem\00"
@.str.3 = [8 x i8] c"devices\00"
@.str.4 = [34 x i8] c"searching '/bus/*/devices/*' dir\0A\00"
@.str.5 = [4 x i8] c"bus\00"
@.str.6 = [26 x i8] c"searching '/class/*' dir\0A\00"
@.str.7 = [6 x i8] c"class\00"
@.str.8 = [13 x i8] c"/class/block\00"
@.str.9 = [6 x i8] c"block\00"
@.str.10 = [26 x i8] c"searching '/block/*' dir\0A\00"
@.str.11 = [28 x i8] c"searching '/block/*/*' dir\0A\00"
define i32 @udev_enumerate_scan_devices(%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
store %struct.udev_enumerate* %0, %struct.udev_enumerate** %3
%7 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%8 = call %struct.udev* @udev_enumerate_get_udev(%struct.udev_enumerate* %7)
store %struct.udev* %8, %struct.udev** %4
%9 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%10 = icmp eq %struct.udev_enumerate* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %63
14: ; preds = %1
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = load %struct.udev*, %struct.udev** %4
%17 = call i32 @udev_get_sys_path(%struct.udev* %16)
%18 = call i32 @util_strlcpy(i8* %15, i32 %17, i32 1024)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = call i32 @util_strlcat(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 1024)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i64 @stat(i8* %21, %struct.stat* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = load %struct.udev*, %struct.udev** %4
%26 = call i32 @udev_log_null(%struct.udev* %25, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%27 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%28 = call i32 @scan_dir(%struct.udev_enumerate* %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %62
29: ; preds = %14
%30 = load %struct.udev*, %struct.udev** %4
%31 = call i32 @udev_log_null(%struct.udev* %30, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%32 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%33 = call i32 @scan_dir(%struct.udev_enumerate* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* null)
%34 = load %struct.udev*, %struct.udev** %4
%35 = call i32 @udev_log_null(%struct.udev* %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%36 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%37 = call i32 @scan_dir(%struct.udev_enumerate* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* null, i8* null)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = load %struct.udev*, %struct.udev** %4
%40 = call i32 @udev_get_sys_path(%struct.udev* %39)
%41 = call i32 @util_strlcpy(i8* %38, i32 %40, i32 1024)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%43 = call i32 @util_strlcat(i8* %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 1024)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = call i64 @stat(i8* %44, %struct.stat* %6)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %61
47: ; preds = %29
%48 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%49 = call i64 @match_subsystem(%struct.udev_enumerate* %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %47
%52 = load %struct.udev*, %struct.udev** %4
%53 = call i32 @udev_log_null(%struct.udev* %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0))
%54 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%55 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32* null, i32* null)
%56 = load %struct.udev*, %struct.udev** %4
%57 = call i32 @udev_log_null(%struct.udev* %56, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0))
%58 = load %struct.udev_enumerate*, %struct.udev_enumerate** %3
%59 = call i32 @scan_dir(%struct.udev_enumerate* %58, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i8* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %60
60: ; preds = %51, %47
br label %61
61: ; preds = %60, %29
br label %62
62: ; preds = %61, %24
store i32 0, i32* %2
br label %63
63: ; preds = %62, %11
%64 = load i32, i32* %2
ret i32 %64
}

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 = [40 x i8] c"searching '/subsystem/*/devices/*' dir\0A\00"
@.str.2 = [10 x i8] c"subsystem\00"
@.str.3 = [8 x i8] c"devices\00"
@.str.4 = [34 x i8] c"searching '/bus/*/devices/*' dir\0A\00"
@.str.5 = [4 x i8] c"bus\00"
@.str.6 = [26 x i8] c"searching '/class/*' dir\0A\00"
@.str.7 = [6 x i8] c"class\00"
@.str.8 = [13 x i8] c"/class/block\00"
@.str.9 = [6 x i8] c"block\00"
@.str.10 = [26 x i8] c"searching '/block/*' dir\0A\00"
@.str.11 = [28 x i8] c"searching '/block/*/*' dir\0A\00"
define i32 @udev_enumerate_scan_devices(%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 %38
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
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @scan_dir(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %38
20: ; preds = %11
%21 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @scan_dir(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* null)
%23 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%24 = call i32 @scan_dir(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* null, i8* null)
%25 = call i32 @udev_get_sys_path(%struct.udev* %4)
%26 = call i32 @util_strlcpy(i8* %5, i32 %25, i32 1024)
%27 = call i32 @util_strlcat(i8* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 1024)
%28 = call i64 @stat(i8* %5, %struct.stat* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %20
%31 = call i64 @match_subsystem(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0))
%35 = call i32 @scan_dir_and_add_devices(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32* null, i32* null)
%36 = call i32 @udev_log_null(%struct.udev* %4, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0))
%37 = call i32 @scan_dir(%struct.udev_enumerate* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i8* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
br label %38
38: ; preds = %17, %30, %33, %20, %8
%39 = phi i32 [ %10, %8 ], [ 0, %20 ], [ 0, %33 ], [ 0, %30 ], [ 0, %17 ]
ret i32 %39
}