#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 strlen(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)),
   (base)[(sizeof(base)) - 1] = '\0');
  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)),
       (dirname)[(sizeof(dirname)) - 1] = '\0');
      strncat(dirname, "/", (sizeof(dirname)) - strlen(dirname) - 1);
      strncat(dirname, dent->d_name, (sizeof(dirname)) - strlen(dirname) - 1);
      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)),
           (dirname2)[(sizeof(dirname2)) - 1] = '\0');
          strncat(dirname2, "/", (sizeof(dirname2)) - strlen(dirname2) - 1);
          strncat(dirname2, dent2->d_name,
                  (sizeof(dirname2)) - strlen(dirname2) - 1);
          device_list_insert(dirname2);
        }
        closedir(dir2);
      }
    }
    closedir(dir);
  }
}


x86 assembly (clang, -O0):
<scan_class>:
push   %rbp
mov    %rsp,%rbp
sub    $0x650,%rsp
lea    -0x200(%rbp),%rdi
lea    0xe67(%rip),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbp)
lea    -0x200(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x208(%rbp)
cmpq   $0x0,-0x208(%rbp)
je     1436 <scan_class+0x2b6>
mov    -0x208(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x210(%rbp)
cmpq   $0x0,-0x210(%rbp)
je     142a <scan_class+0x2aa>
mov    -0x210(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1204 <scan_class+0x84>
jmp    1412 <scan_class+0x292>
lea    -0x410(%rbp),%rdi
lea    -0x200(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x211(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x638(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x638(%rbp),%rdi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xdaf(%rip),%rsi
call   1060 <strncat@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x630(%rbp)
mov    -0x210(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x628(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x630(%rbp),%rdi
mov    -0x628(%rbp),%rsi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
lea    -0x410(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     140d <scan_class+0x28d>
mov    -0x418(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     1401 <scan_class+0x281>
mov    -0x420(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    130d <scan_class+0x18d>
jmp    13e9 <scan_class+0x269>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
lea    0xcef(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1331 <scan_class+0x1b1>
jmp    13e9 <scan_class+0x269>
lea    -0x620(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x421(%rbp)
lea    -0x620(%rbp),%rax
mov    %rax,-0x650(%rbp)
lea    -0x620(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x650(%rbp),%rdi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xc82(%rip),%rsi
call   1060 <strncat@plt>
lea    -0x620(%rbp),%rax
mov    %rax,-0x648(%rbp)
mov    -0x420(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x640(%rbp)
lea    -0x620(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x648(%rbp),%rdi
mov    -0x640(%rbp),%rsi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
lea    -0x620(%rbp),%rdi
call   1090 <device_list_insert@plt>
mov    -0x418(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x420(%rbp)
jmp    12e4 <scan_class+0x164>
mov    -0x418(%rbp),%rdi
call   1070 <closedir@plt>
jmp    1412 <scan_class+0x292>
mov    -0x208(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x210(%rbp)
jmp    11db <scan_class+0x5b>
mov    -0x208(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x650,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_class>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x618,%rsp
lea    0xe68(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x20f(%rsp)
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1340 <scan_class+0x1c0>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1338 <scan_class+0x1b8>
mov    %rax,%rbx
lea    0x410(%rsp),%r12
lea    0xe24(%rip),%r13
lea    0x210(%rsp),%rbp
mov    %r14,0x8(%rsp)
jmp    1221 <scan_class+0xa1>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    0x8(%rsp),%r14
mov    %r14,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1338 <scan_class+0x1b8>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     120d <scan_class+0x8d>
mov    %r12,%rdi
lea    0x10(%rsp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x60f(%rsp)
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %r12,%rdi
lea    0xdaf(%rip),%rsi
call   1060 <strncat@plt>
mov    (%rbx),%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <strncat@plt>
mov    %r12,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     120d <scan_class+0x8d>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1200 <scan_class+0x80>
mov    %rax,%r14
jmp    12c4 <scan_class+0x144>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1200 <scan_class+0x80>
mov    (%r14),%rdi
cmpb   $0x2e,(%rdi)
je     12b0 <scan_class+0x130>
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     12b0 <scan_class+0x130>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x40f(%rsp)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %rbp,%rdi
lea    0xd02(%rip),%rsi
call   1060 <strncat@plt>
mov    (%r14),%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <strncat@plt>
mov    %rbp,%rdi
call   1090 <device_list_insert@plt>
jmp    12b0 <scan_class+0x130>
mov    %r14,%rdi
call   1070 <closedir@plt>
add    $0x618,%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 511
store i8 0, i8* %10
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %119
15: ; preds = %0
%16 = load i32*, i32** %2
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %3
br label %18
18: ; preds = %113, %15
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %116
21: ; preds = %18
%22 = load %struct.dirent*, %struct.dirent** %3
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 46
br i1 %28, label %29, label %30
29: ; preds = %21
br label %113
30: ; preds = %21
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%33 = call i32 @strncpy(i8* %31, i8* %32, i32 512)
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 511
store i8 0, i8* %34
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = sub i64 512, %38
%40 = sub i64 %39, 1
%41 = trunc i64 %40 to i32
%42 = call i32 @strncat(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%44 = load %struct.dirent*, %struct.dirent** %3
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = sext i32 %48 to i64
%50 = sub i64 512, %49
%51 = sub i64 %50, 1
%52 = trunc i64 %51 to i32
%53 = call i32 @strncat(i8* %43, i8* %46, i32 %52)
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%55 = call i32* @opendir(i8* %54)
store i32* %55, i32** %5
%56 = load i32*, i32** %5
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %112
58: ; preds = %30
%59 = load i32*, i32** %5
%60 = call %struct.dirent* @readdir(i32* %59)
store %struct.dirent* %60, %struct.dirent** %6
br label %61
61: ; preds = %106, %58
%62 = load %struct.dirent*, %struct.dirent** %6
%63 = icmp ne %struct.dirent* %62, null
br i1 %63, label %64, label %109
64: ; preds = %61
%65 = load %struct.dirent*, %struct.dirent** %6
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = getelementptr inbounds i8, i8* %67, i64 0
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 46
br i1 %71, label %72, label %73
72: ; preds = %64
br label %106
73: ; preds = %64
%74 = load %struct.dirent*, %struct.dirent** %6
%75 = getelementptr inbounds %struct.dirent, %struct.dirent* %74, i32 0, i32 0
%76 = load i8*, i8** %75
%77 = call i32 @strcmp(i8* %76, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%78 = icmp ne i32 %77, 0
br i1 %78, label %80, label %79
79: ; preds = %73
br label %106
80: ; preds = %73
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%83 = call i32 @strncpy(i8* %81, i8* %82, i32 512)
%84 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 511
store i8 0, i8* %84
%85 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%86 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%87 = call i32 @strlen(i8* %86)
%88 = sext i32 %87 to i64
%89 = sub i64 512, %88
%90 = sub i64 %89, 1
%91 = trunc i64 %90 to i32
%92 = call i32 @strncat(i8* %85, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %91)
%93 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%94 = load %struct.dirent*, %struct.dirent** %6
%95 = getelementptr inbounds %struct.dirent, %struct.dirent* %94, i32 0, i32 0
%96 = load i8*, i8** %95
%97 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%98 = call i32 @strlen(i8* %97)
%99 = sext i32 %98 to i64
%100 = sub i64 512, %99
%101 = sub i64 %100, 1
%102 = trunc i64 %101 to i32
%103 = call i32 @strncat(i8* %93, i8* %96, i32 %102)
%104 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%105 = call i32 @device_list_insert(i8* %104)
br label %106
106: ; preds = %80, %79, %72
%107 = load i32*, i32** %5
%108 = call %struct.dirent* @readdir(i32* %107)
store %struct.dirent* %108, %struct.dirent** %6
br label %61
109: ; preds = %61
%110 = load i32*, i32** %5
%111 = call i32 @closedir(i32* %110)
br label %112
112: ; preds = %109, %30
br label %113
113: ; preds = %112, %29
%114 = load i32*, i32** %2
%115 = call %struct.dirent* @readdir(i32* %114)
store %struct.dirent* %115, %struct.dirent** %3
br label %18
116: ; preds = %18
%117 = load i32*, i32** %2
%118 = call i32 @closedir(i32* %117)
br label %119
119: ; preds = %116, %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 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 511
store i8 0, i8* %6
%7 = call i32* @opendir(i8* %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %66, label %9
9: ; preds = %0
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %64, label %12
12: ; preds = %9
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 511
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 511
br label %17
17: ; preds = %12, %61
%18 = phi %struct.dirent* [ %10, %12 ], [ %62, %61 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
br i1 %22, label %61, label %23
23: ; preds = %17
%24 = call i32 @strncpy(i8* %13, i8* %4, i32 512)
store i8 0, i8* %14
%25 = call i32 @strlen(i8* %13)
%26 = sub i32 511, %25
%27 = call i32 @strncat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i8*, i8** %19
%29 = call i32 @strlen(i8* %13)
%30 = sub i32 511, %29
%31 = call i32 @strncat(i8* %13, i8* %28, i32 %30)
%32 = call i32* @opendir(i8* %13)
%33 = icmp eq i32* %32, null
br i1 %33, label %61, label %34
34: ; preds = %23
%35 = call %struct.dirent* @readdir(i32* %32)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %59, label %37
37: ; preds = %34, %56
%38 = phi %struct.dirent* [ %57, %56 ], [ %35, %34 ]
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i64 0, i32 0
%40 = load i8*, i8** %39
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 46
br i1 %42, label %56, label %43
43: ; preds = %37
%44 = call i32 @strcmp(i8* %40, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%45 = icmp eq i32 %44, 0
br i1 %45, label %56, label %46
46: ; preds = %43
%47 = call i32 @strncpy(i8* %15, i8* %13, i32 512)
store i8 0, i8* %16
%48 = call i32 @strlen(i8* %15)
%49 = sub i32 511, %48
%50 = call i32 @strncat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %49)
%51 = load i8*, i8** %39
%52 = call i32 @strlen(i8* %15)
%53 = sub i32 511, %52
%54 = call i32 @strncat(i8* %15, i8* %51, i32 %53)
%55 = call i32 @device_list_insert(i8* %15)
br label %56
56: ; preds = %43, %37, %46
%57 = call %struct.dirent* @readdir(i32* %32)
%58 = icmp eq %struct.dirent* %57, null
br i1 %58, label %59, label %37
59: ; preds = %56, %34
%60 = call i32 @closedir(i32* %32)
br label %61
61: ; preds = %23, %59, %17
%62 = call %struct.dirent* @readdir(i32* %7)
%63 = icmp eq %struct.dirent* %62, null
br i1 %63, label %64, label %17
64: ; preds = %61, %9
%65 = call i32 @closedir(i32* %7)
br label %66
66: ; preds = %64, %0
ret void
}