#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*/ strcmp(char *, char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
void scan_class(void) {
  char base[512];
  DIR *dir;
  struct dirent *dent;
  strncpy(base, "/sys/class", 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));
      dir2 = opendir(dirname);
      if (dir2 != NULL) {
        for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
          char dirname2[512];
          if (dent2->d_name[0] == '.')
            continue;
          if (!strcmp(dent2->d_name, "device"))
            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_class>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
lea    -0x200(%rbp),%rdi
lea    0xe77(%rip),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x200(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x208(%rbp)
cmpq   $0x0,-0x208(%rbp)
je     1360 <scan_class+0x1f0>
mov    -0x208(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x210(%rbp)
cmpq   $0x0,-0x210(%rbp)
je     1354 <scan_class+0x1e4>
mov    -0x210(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11f0 <scan_class+0x80>
jmp    133c <scan_class+0x1cc>
lea    -0x410(%rbp),%rdi
lea    -0x200(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xdf5(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x410(%rbp),%rdi
mov    -0x210(%rbp),%rax
mov    (%rax),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x410(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     1337 <scan_class+0x1c7>
mov    -0x418(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     132b <scan_class+0x1bb>
mov    -0x420(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1298 <scan_class+0x128>
jmp    1313 <scan_class+0x1a3>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
lea    0xd64(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12bc <scan_class+0x14c>
jmp    1313 <scan_class+0x1a3>
lea    -0x620(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x620(%rbp),%rdi
lea    0xd29(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x620(%rbp),%rdi
mov    -0x420(%rbp),%rax
mov    (%rax),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
lea    -0x620(%rbp),%rdi
call   1080 <device_list_insert@plt>
mov    -0x418(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x420(%rbp)
jmp    126f <scan_class+0xff>
mov    -0x418(%rbp),%rdi
call   1060 <closedir@plt>
jmp    133c <scan_class+0x1cc>
mov    -0x208(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x210(%rbp)
jmp    11c7 <scan_class+0x57>
mov    -0x208(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_class>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x608,%rsp
lea    0xe78(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ce <scan_class+0x15e>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12c6 <scan_class+0x156>
mov    %rax,%rbx
lea    0x400(%rsp),%r12
lea    0xe3e(%rip),%r13
lea    0x200(%rsp),%rbp
jmp    11ff <scan_class+0x8f>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    %rbx,%r14
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12c6 <scan_class+0x156>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     11eb <scan_class+0x7b>
mov    %r12,%rdi
mov    %rsp,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %r12,%rdi
lea    0xdea(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    (%rbx),%rsi
mov    %r12,%rdi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %r12,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11eb <scan_class+0x7b>
mov    %rax,%r15
mov    %r14,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     11e0 <scan_class+0x70>
mov    %rax,%r14
jmp    1274 <scan_class+0x104>
mov    %r15,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     11e0 <scan_class+0x70>
mov    (%r14),%rdi
cmpb   $0x2e,(%rdi)
je     1260 <scan_class+0xf0>
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1260 <scan_class+0xf0>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
lea    0xd69(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    (%r14),%rsi
mov    %rbp,%rdi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %rbp,%rdi
call   1080 <device_list_insert@plt>
jmp    1260 <scan_class+0xf0>
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 = [11 x i8] c"/sys/class\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"device\00"
define void @scan_class() {
%1 = alloca [512 x i8]
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca [512 x i8]
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca [512 x i8]
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%9 = call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 512)
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %92
14: ; preds = %0
%15 = load i32*, i32** %2
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %3
br label %17
17: ; preds = %86, %14
%18 = load %struct.dirent*, %struct.dirent** %3
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %89
20: ; preds = %17
%21 = load %struct.dirent*, %struct.dirent** %3
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 46
br i1 %27, label %28, label %29
28: ; preds = %20
br label %86
29: ; preds = %20
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%32 = call i32 @strncpy(i8* %30, i8* %31, i32 512)
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%34 = call i32 @strncat(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%36 = load %struct.dirent*, %struct.dirent** %3
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @strncat(i8* %35, i8* %38, i32 512)
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%41 = call i32* @opendir(i8* %40)
store i32* %41, i32** %5
%42 = load i32*, i32** %5
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %85
44: ; preds = %29
%45 = load i32*, i32** %5
%46 = call %struct.dirent* @readdir(i32* %45)
store %struct.dirent* %46, %struct.dirent** %6
br label %47
47: ; preds = %79, %44
%48 = load %struct.dirent*, %struct.dirent** %6
%49 = icmp ne %struct.dirent* %48, null
br i1 %49, label %50, label %82
50: ; preds = %47
%51 = load %struct.dirent*, %struct.dirent** %6
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = getelementptr inbounds i8, i8* %53, i64 0
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 46
br i1 %57, label %58, label %59
58: ; preds = %50
br label %79
59: ; preds = %50
%60 = load %struct.dirent*, %struct.dirent** %6
%61 = getelementptr inbounds %struct.dirent, %struct.dirent* %60, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = call i32 @strcmp(i8* %62, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%64 = icmp ne i32 %63, 0
br i1 %64, label %66, label %65
65: ; preds = %59
br label %79
66: ; preds = %59
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%68 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%69 = call i32 @strncpy(i8* %67, i8* %68, i32 512)
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %7, 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]* %7, i64 0, i64 0
%73 = load %struct.dirent*, %struct.dirent** %6
%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]* %7, i64 0, i64 0
%78 = call i32 @device_list_insert(i8* %77)
br label %79
79: ; preds = %66, %65, %58
%80 = load i32*, i32** %5
%81 = call %struct.dirent* @readdir(i32* %80)
store %struct.dirent* %81, %struct.dirent** %6
br label %47
82: ; preds = %47
%83 = load i32*, i32** %5
%84 = call i32 @closedir(i32* %83)
br label %85
85: ; preds = %82, %29
br label %86
86: ; preds = %85, %28
%87 = load i32*, i32** %2
%88 = call %struct.dirent* @readdir(i32* %87)
store %struct.dirent* %88, %struct.dirent** %3
br label %17
89: ; preds = %17
%90 = load i32*, i32** %2
%91 = call i32 @closedir(i32* %90)
br label %92
92: ; preds = %89, %0
ret void
}

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