#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ dirname2;
typedef int /*<<< orphan*/ dirname;
typedef int /*<<< orphan*/ base;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ device_list_insert(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
void scan_subsystem(const char *subsys) {
  char base[512];
  DIR *dir;
  struct dirent *dent;
  strncpy(base, "/sys/", sizeof(base));
  strncat(base, subsys, sizeof(base));
  dir = opendir(base);
  if (dir != NULL) {
    for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
      char dirname[512];
      DIR *dir2;
      struct dirent *dent2;
      if (dent->d_name[0] == '.')
        continue;
      strncpy(dirname, base, sizeof(dirname));
      strncat(dirname, "/", sizeof(dirname));
      strncat(dirname, dent->d_name, sizeof(dirname));
      strncat(dirname, "/devices", sizeof(dirname));
      dir2 = opendir(dirname);
      if (dir2 != NULL) {
        for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
          char dirname2[512];
          if (dent2->d_name[0] == '.')
            continue;
          strncpy(dirname2, dirname, sizeof(dirname2));
          strncat(dirname2, "/", sizeof(dirname2));
          strncat(dirname2, dent2->d_name, sizeof(dirname2));
          device_list_insert(dirname2);
        }
        closedir(dir2);
      }
    }
    closedir(dir);
  }
}


x86 assembly (clang, -O0):
<scan_subsystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x630,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
lea    0xe83(%rip),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x210(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     135d <scan_subsystem+0x1fd>
mov    -0x218(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     1351 <scan_subsystem+0x1f1>
mov    -0x220(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11f9 <scan_subsystem+0x99>
jmp    1339 <scan_subsystem+0x1d9>
lea    -0x420(%rbp),%rdi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xde5(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x420(%rbp),%rdi
mov    -0x220(%rbp),%rax
mov    (%rax),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x420(%rbp),%rdi
lea    0xdb4(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x420(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     1334 <scan_subsystem+0x1d4>
mov    -0x428(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
je     1328 <scan_subsystem+0x1c8>
mov    -0x430(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12b9 <scan_subsystem+0x159>
jmp    1310 <scan_subsystem+0x1b0>
lea    -0x630(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x630(%rbp),%rdi
lea    0xd25(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x630(%rbp),%rdi
mov    -0x430(%rbp),%rax
mov    (%rax),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x630(%rbp),%rdi
call   1070 <device_list_insert@plt>
mov    -0x428(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x430(%rbp)
jmp    1290 <scan_subsystem+0x130>
mov    -0x428(%rbp),%rdi
call   1060 <closedir@plt>
jmp    1339 <scan_subsystem+0x1d9>
mov    -0x218(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x220(%rbp)
jmp    11d0 <scan_subsystem+0x70>
mov    -0x218(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x630,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_subsystem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x608,%rsp
mov    %rdi,%rbx
lea    0xe85(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %rbp,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ce <scan_subsystem+0x16e>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12c6 <scan_subsystem+0x166>
mov    %rax,%rbx
lea    0x400(%rsp),%r13
lea    0xe32(%rip),%r12
lea    0x200(%rsp),%rbp
jmp    11fc <scan_subsystem+0x9c>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12c6 <scan_subsystem+0x166>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     11e8 <scan_subsystem+0x88>
mov    %r13,%rdi
mov    %rsp,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    (%rbx),%rsi
mov    %r13,%rdi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %r13,%rdi
lea    0xdc8(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %r13,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11e8 <scan_subsystem+0x88>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11e0 <scan_subsystem+0x80>
mov    %rax,%rbx
jmp    1284 <scan_subsystem+0x124>
xchg   %ax,%ax
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e0 <scan_subsystem+0x80>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     1270 <scan_subsystem+0x110>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    (%rbx),%rsi
mov    %rbp,%rdi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %rbp,%rdi
call   1070 <device_list_insert@plt>
jmp    1270 <scan_subsystem+0x110>
mov    %r14,%rdi
call   1060 <closedir@plt>
add    $0x608,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/sys/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [9 x i8] c"/devices\00"
define void @scan_subsystem(i8* %0) {
%2 = alloca i8*
%3 = alloca [512 x i8]
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca [512 x i8]
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca [512 x i8]
store i8* %0, i8** %2
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%11 = call i32 @strncpy(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 512)
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%13 = load i8*, i8** %2
%14 = call i32 @strncat(i8* %12, i8* %13, i32 512)
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %4
%17 = load i32*, i32** %4
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %92
19: ; preds = %1
%20 = load i32*, i32** %4
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %5
br label %22
22: ; preds = %86, %19
%23 = load %struct.dirent*, %struct.dirent** %5
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %89
25: ; preds = %22
%26 = load %struct.dirent*, %struct.dirent** %5
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 46
br i1 %32, label %33, label %34
33: ; preds = %25
br label %86
34: ; preds = %25
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%37 = call i32 @strncpy(i8* %35, i8* %36, i32 512)
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%39 = call i32 @strncat(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%41 = load %struct.dirent*, %struct.dirent** %5
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @strncat(i8* %40, i8* %43, i32 512)
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%46 = call i32 @strncat(i8* %45, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 512)
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%48 = call i32* @opendir(i8* %47)
store i32* %48, i32** %7
%49 = load i32*, i32** %7
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %85
51: ; preds = %34
%52 = load i32*, i32** %7
%53 = call %struct.dirent* @readdir(i32* %52)
store %struct.dirent* %53, %struct.dirent** %8
br label %54
54: ; preds = %79, %51
%55 = load %struct.dirent*, %struct.dirent** %8
%56 = icmp ne %struct.dirent* %55, null
br i1 %56, label %57, label %82
57: ; preds = %54
%58 = load %struct.dirent*, %struct.dirent** %8
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = getelementptr inbounds i8, i8* %60, i64 0
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 46
br i1 %64, label %65, label %66
65: ; preds = %57
br label %79
66: ; preds = %57
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%68 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%69 = call i32 @strncpy(i8* %67, i8* %68, i32 512)
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%71 = call i32 @strncat(i8* %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%73 = load %struct.dirent*, %struct.dirent** %8
%74 = getelementptr inbounds %struct.dirent, %struct.dirent* %73, i32 0, i32 0
%75 = load i8*, i8** %74
%76 = call i32 @strncat(i8* %72, i8* %75, i32 512)
%77 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%78 = call i32 @device_list_insert(i8* %77)
br label %79
79: ; preds = %66, %65
%80 = load i32*, i32** %7
%81 = call %struct.dirent* @readdir(i32* %80)
store %struct.dirent* %81, %struct.dirent** %8
br label %54
82: ; preds = %54
%83 = load i32*, i32** %7
%84 = call i32 @closedir(i32* %83)
br label %85
85: ; preds = %82, %34
br label %86
86: ; preds = %85, %33
%87 = load i32*, i32** %4
%88 = call %struct.dirent* @readdir(i32* %87)
store %struct.dirent* %88, %struct.dirent** %5
br label %22
89: ; preds = %22
%90 = load i32*, i32** %4
%91 = call i32 @closedir(i32* %90)
br label %92
92: ; preds = %89, %1
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/sys/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [9 x i8] c"/devices\00"
define void @scan_subsystem(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 512)
%7 = call i32 @strncat(i8* %5, i8* %0, i32 512)
%8 = call i32* @opendir(i8* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %55, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %53, label %13
13: ; preds = %10
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
br label %16
16: ; preds = %13, %50
%17 = phi %struct.dirent* [ %11, %13 ], [ %51, %50 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 46
br i1 %21, label %50, label %22
22: ; preds = %16
%23 = call i32 @strncpy(i8* %14, i8* %5, i32 512)
%24 = call i32 @strncat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%25 = load i8*, i8** %18
%26 = call i32 @strncat(i8* %14, i8* %25, i32 512)
%27 = call i32 @strncat(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 512)
%28 = call i32* @opendir(i8* %14)
%29 = icmp eq i32* %28, null
br i1 %29, label %50, label %30
30: ; preds = %22
%31 = call %struct.dirent* @readdir(i32* %28)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %48, label %33
33: ; preds = %30, %45
%34 = phi %struct.dirent* [ %46, %45 ], [ %31, %30 ]
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 46
br i1 %38, label %45, label %39
39: ; preds = %33
%40 = call i32 @strncpy(i8* %15, i8* %14, i32 512)
%41 = call i32 @strncat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%42 = load i8*, i8** %35
%43 = call i32 @strncat(i8* %15, i8* %42, i32 512)
%44 = call i32 @device_list_insert(i8* %15)
br label %45
45: ; preds = %33, %39
%46 = call %struct.dirent* @readdir(i32* %28)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %33
48: ; preds = %45, %30
%49 = call i32 @closedir(i32* %28)
br label %50
50: ; preds = %22, %48, %16
%51 = call %struct.dirent* @readdir(i32* %8)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %53, label %16
53: ; preds = %50, %10
%54 = call i32 @closedir(i32* %8)
br label %55
55: ; preds = %53, %1
ret void
}