#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 strlen(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)),
   (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);
      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)),
           (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_block>:
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     1451 <scan_block+0x2d1>
mov    -0x208(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x210(%rbp)
cmpq   $0x0,-0x210(%rbp)
je     1445 <scan_block+0x2c5>
mov    -0x210(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1204 <scan_block+0x84>
jmp    142d <scan_block+0x2ad>
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   1090 <device_list_insert@plt>
cmp    $0x0,%rax
je     12cb <scan_block+0x14b>
jmp    142d <scan_block+0x2ad>
lea    -0x410(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     1428 <scan_block+0x2a8>
mov    -0x418(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     141c <scan_block+0x29c>
mov    -0x420(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1328 <scan_block+0x1a8>
jmp    1404 <scan_block+0x284>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
lea    0xcd4(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    134c <scan_block+0x1cc>
jmp    1404 <scan_block+0x284>
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    0xc67(%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    12ff <scan_block+0x17f>
mov    -0x418(%rbp),%rdi
call   1070 <closedir@plt>
jmp    142d <scan_block+0x2ad>
mov    -0x208(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x210(%rbp)
jmp    11db <scan_block+0x5b>
mov    -0x208(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x650,%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    0xe68(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x1ff(%rsp)
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1346 <scan_block+0x1c6>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     133e <scan_block+0x1be>
mov    %rax,%rbx
lea    0x400(%rsp),%r12
mov    %rsp,%r13
lea    0x200(%rsp),%rbp
jmp    1214 <scan_block+0x94>
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    %rsp,%r13
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     133e <scan_block+0x1be>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     1200 <scan_block+0x80>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x5ff(%rsp)
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %r12,%rdi
lea    0xdbe(%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   1090 <device_list_insert@plt>
test   %rax,%rax
jne    1200 <scan_block+0x80>
mov    %r12,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1200 <scan_block+0x80>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     11ed <scan_block+0x6d>
mov    %rax,%r13
jmp    12c4 <scan_block+0x144>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     11ed <scan_block+0x6d>
mov    0x0(%r13),%rdi
cmpb   $0x2e,(%rdi)
je     12b0 <scan_block+0x130>
lea    0xd39(%rip),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     12b0 <scan_block+0x130>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3ff(%rsp)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %rbp,%rdi
lea    0xcfd(%rip),%rsi
call   1060 <strncat@plt>
mov    0x0(%r13),%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_block+0x130>
mov    %r14,%rdi
call   1070 <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 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 %124
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 = %118, %15
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %121
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 %118
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 i64 @device_list_insert(i8* %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %30
br label %118
58: ; preds = %30
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%60 = call i32* @opendir(i8* %59)
store i32* %60, i32** %5
%61 = load i32*, i32** %5
%62 = icmp ne i32* %61, null
br i1 %62, label %63, label %117
63: ; preds = %58
%64 = load i32*, i32** %5
%65 = call %struct.dirent* @readdir(i32* %64)
store %struct.dirent* %65, %struct.dirent** %6
br label %66
66: ; preds = %111, %63
%67 = load %struct.dirent*, %struct.dirent** %6
%68 = icmp ne %struct.dirent* %67, null
br i1 %68, label %69, label %114
69: ; preds = %66
%70 = load %struct.dirent*, %struct.dirent** %6
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = getelementptr inbounds i8, i8* %72, i64 0
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %75, 46
br i1 %76, label %77, label %78
77: ; preds = %69
br label %111
78: ; preds = %69
%79 = load %struct.dirent*, %struct.dirent** %6
%80 = getelementptr inbounds %struct.dirent, %struct.dirent* %79, i32 0, i32 0
%81 = load i8*, i8** %80
%82 = call i32 @strcmp(i8* %81, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%83 = icmp ne i32 %82, 0
br i1 %83, label %85, label %84
84: ; preds = %78
br label %111
85: ; preds = %78
%86 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%87 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%88 = call i32 @strncpy(i8* %86, i8* %87, i32 512)
%89 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 511
store i8 0, i8* %89
%90 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%91 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%92 = call i32 @strlen(i8* %91)
%93 = sext i32 %92 to i64
%94 = sub i64 512, %93
%95 = sub i64 %94, 1
%96 = trunc i64 %95 to i32
%97 = call i32 @strncat(i8* %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %96)
%98 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%99 = load %struct.dirent*, %struct.dirent** %6
%100 = getelementptr inbounds %struct.dirent, %struct.dirent* %99, i32 0, i32 0
%101 = load i8*, i8** %100
%102 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%103 = call i32 @strlen(i8* %102)
%104 = sext i32 %103 to i64
%105 = sub i64 512, %104
%106 = sub i64 %105, 1
%107 = trunc i64 %106 to i32
%108 = call i32 @strncat(i8* %98, i8* %101, i32 %107)
%109 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%110 = call i64 @device_list_insert(i8* %109)
br label %111
111: ; preds = %85, %84, %77
%112 = load i32*, i32** %5
%113 = call %struct.dirent* @readdir(i32* %112)
store %struct.dirent* %113, %struct.dirent** %6
br label %66
114: ; preds = %66
%115 = load i32*, i32** %5
%116 = call i32 @closedir(i32* %115)
br label %117
117: ; preds = %114, %58
br label %118
118: ; preds = %117, %57, %29
%119 = load i32*, i32** %2
%120 = call %struct.dirent* @readdir(i32* %119)
store %struct.dirent* %120, %struct.dirent** %3
br label %18
121: ; preds = %18
%122 = load i32*, i32** %2
%123 = call i32 @closedir(i32* %122)
br label %124
124: ; preds = %121, %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 = 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 %69, label %9
9: ; preds = %0
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %67, 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, %64
%18 = phi %struct.dirent* [ %10, %12 ], [ %65, %64 ]
%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 %64, 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 i64 @device_list_insert(i8* %13)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %64
34: ; preds = %23
%35 = call i32* @opendir(i8* %13)
%36 = icmp eq i32* %35, null
br i1 %36, label %64, label %37
37: ; preds = %34
%38 = call %struct.dirent* @readdir(i32* %35)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %62, label %40
40: ; preds = %37, %59
%41 = phi %struct.dirent* [ %60, %59 ], [ %38, %37 ]
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = load i8, i8* %43
%45 = icmp eq i8 %44, 46
br i1 %45, label %59, label %46
46: ; preds = %40
%47 = call i32 @strcmp(i8* %43, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp eq i32 %47, 0
br i1 %48, label %59, label %49
49: ; preds = %46
%50 = call i32 @strncpy(i8* %15, i8* %13, i32 512)
store i8 0, i8* %16
%51 = call i32 @strlen(i8* %15)
%52 = sub i32 511, %51
%53 = call i32 @strncat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = load i8*, i8** %42
%55 = call i32 @strlen(i8* %15)
%56 = sub i32 511, %55
%57 = call i32 @strncat(i8* %15, i8* %54, i32 %56)
%58 = call i64 @device_list_insert(i8* %15)
br label %59
59: ; preds = %46, %40, %49
%60 = call %struct.dirent* @readdir(i32* %35)
%61 = icmp eq %struct.dirent* %60, null
br i1 %61, label %62, label %40
62: ; preds = %59, %37
%63 = call i32 @closedir(i32* %35)
br label %64
64: ; preds = %34, %62, %23, %17
%65 = call %struct.dirent* @readdir(i32* %7)
%66 = icmp eq %struct.dirent* %65, null
br i1 %66, label %67, label %17
67: ; preds = %64, %9
%68 = call i32 @closedir(i32* %7)
br label %69
69: ; preds = %67, %0
ret void
}