#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*/ *);
scalar_t__ 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_block(void) {
  char base[512];
  DIR *dir;
  struct dirent *dent;
  strncpy(base, "/sys/block", 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));
      if (device_list_insert(dirname) != 0)
        continue;
      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_block>:
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     137b <scan_block+0x20b>
mov    -0x208(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x210(%rbp)
cmpq   $0x0,-0x210(%rbp)
je     136f <scan_block+0x1ff>
mov    -0x210(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11f0 <scan_block+0x80>
jmp    1357 <scan_block+0x1e7>
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   1080 <device_list_insert@plt>
cmp    $0x0,%rax
je     1256 <scan_block+0xe6>
jmp    1357 <scan_block+0x1e7>
lea    -0x410(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     1352 <scan_block+0x1e2>
mov    -0x418(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     1346 <scan_block+0x1d6>
mov    -0x420(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12b3 <scan_block+0x143>
jmp    132e <scan_block+0x1be>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
lea    0xd49(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12d7 <scan_block+0x167>
jmp    132e <scan_block+0x1be>
lea    -0x620(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x620(%rbp),%rdi
lea    0xd0e(%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    128a <scan_block+0x11a>
mov    -0x418(%rbp),%rdi
call   1060 <closedir@plt>
jmp    1357 <scan_block+0x1e7>
mov    -0x208(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x210(%rbp)
jmp    11c7 <scan_block+0x57>
mov    -0x208(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_block>:
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     12e4 <scan_block+0x174>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12dc <scan_block+0x16c>
mov    %rax,%rbx
lea    0x400(%rsp),%r12
mov    %rsp,%r13
lea    0x200(%rsp),%rbp
jmp    11f4 <scan_block+0x84>
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    %rbx,%r13
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12dc <scan_block+0x16c>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     11e0 <scan_block+0x70>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %r12,%rdi
lea    0xdf5(%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   1080 <device_list_insert@plt>
test   %rax,%rax
jne    11e0 <scan_block+0x70>
mov    %r12,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11e0 <scan_block+0x70>
mov    %rax,%r15
mov    %r13,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     11d5 <scan_block+0x65>
mov    %rax,%r13
jmp    1284 <scan_block+0x114>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     11d5 <scan_block+0x65>
mov    0x0(%r13),%rdi
cmpb   $0x2e,(%rdi)
je     1270 <scan_block+0x100>
lea    0xd79(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1270 <scan_block+0x100>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
lea    0xd54(%rip),%rsi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
mov    $0x200,%edx
call   1050 <strncat@plt>
mov    %rbp,%rdi
call   1080 <device_list_insert@plt>
jmp    1270 <scan_block+0x100>
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/block\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"device\00"
define void @scan_block() {
%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 %97
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 = %91, %14
%18 = load %struct.dirent*, %struct.dirent** %3
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %94
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 %91
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 i64 @device_list_insert(i8* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %29
br label %91
44: ; preds = %29
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%46 = call i32* @opendir(i8* %45)
store i32* %46, i32** %5
%47 = load i32*, i32** %5
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %90
49: ; preds = %44
%50 = load i32*, i32** %5
%51 = call %struct.dirent* @readdir(i32* %50)
store %struct.dirent* %51, %struct.dirent** %6
br label %52
52: ; preds = %84, %49
%53 = load %struct.dirent*, %struct.dirent** %6
%54 = icmp ne %struct.dirent* %53, null
br i1 %54, label %55, label %87
55: ; preds = %52
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = getelementptr inbounds i8, i8* %58, i64 0
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 46
br i1 %62, label %63, label %64
63: ; preds = %55
br label %84
64: ; preds = %55
%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 = call i32 @strcmp(i8* %67, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%69 = icmp ne i32 %68, 0
br i1 %69, label %71, label %70
70: ; preds = %64
br label %84
71: ; preds = %64
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%73 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%74 = call i32 @strncpy(i8* %72, i8* %73, i32 512)
%75 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%76 = call i32 @strncat(i8* %75, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%77 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%78 = load %struct.dirent*, %struct.dirent** %6
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %78, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = call i32 @strncat(i8* %77, i8* %80, i32 512)
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%83 = call i64 @device_list_insert(i8* %82)
br label %84
84: ; preds = %71, %70, %63
%85 = load i32*, i32** %5
%86 = call %struct.dirent* @readdir(i32* %85)
store %struct.dirent* %86, %struct.dirent** %6
br label %52
87: ; preds = %52
%88 = load i32*, i32** %5
%89 = call i32 @closedir(i32* %88)
br label %90
90: ; preds = %87, %44
br label %91
91: ; preds = %90, %43, %28
%92 = load i32*, i32** %2
%93 = call %struct.dirent* @readdir(i32* %92)
store %struct.dirent* %93, %struct.dirent** %3
br label %17
94: ; preds = %17
%95 = load i32*, i32** %2
%96 = call i32 @closedir(i32* %95)
br label %97
97: ; preds = %94, %0
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/sys/block\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"device\00"
define void @scan_block() {
%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 %58, label %8
8: ; preds = %0
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %56, 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, %53
%15 = phi %struct.dirent* [ %9, %11 ], [ %54, %53 ]
%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 %53, 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 i64 @device_list_insert(i8* %12)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %20
%28 = call i32* @opendir(i8* %12)
%29 = icmp eq i32* %28, null
br i1 %29, label %53, label %30
30: ; preds = %27
%31 = call %struct.dirent* @readdir(i32* %28)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %51, label %33
33: ; preds = %30, %48
%34 = phi %struct.dirent* [ %49, %48 ], [ %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 %48, label %39
39: ; preds = %33
%40 = call i32 @strcmp(i8* %36, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = call i32 @strncpy(i8* %13, i8* %12, i32 512)
%44 = call i32 @strncat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 512)
%45 = load i8*, i8** %35
%46 = call i32 @strncat(i8* %13, i8* %45, i32 512)
%47 = call i64 @device_list_insert(i8* %13)
br label %48
48: ; preds = %39, %33, %42
%49 = call %struct.dirent* @readdir(i32* %28)
%50 = icmp eq %struct.dirent* %49, null
br i1 %50, label %51, label %33
51: ; preds = %48, %30
%52 = call i32 @closedir(i32* %28)
br label %53
53: ; preds = %27, %51, %20, %14
%54 = call %struct.dirent* @readdir(i32* %6)
%55 = icmp eq %struct.dirent* %54, null
br i1 %55, label %56, label %14
56: ; preds = %53, %8
%57 = call i32 @closedir(i32* %6)
br label %58
58: ; preds = %56, %0
ret void
}