#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*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ get_dev_part_nr(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int get_sysfs_dev_nr(int display_partitions) {
  DIR *dir;
  struct dirent *drd;
  char line[1024];
  int dev = 0;
  if ((dir = opendir("/sys/block")) == NULL)
    return 0;
  while ((drd = readdir(dir)) != NULL) {
    if (!strcmp(drd->d_name, ".") || !strcmp(drd->d_name, ".."))
      continue;
    snprintf(line, 1024, "%s/%s/%s", "/sys/block", drd->d_name, "stat");
    line[1024 - 1] = '\0';
    if (!access(line, R_OK)) {
      dev++;
      if (display_partitions) {
        dev += get_dev_part_nr(drd->d_name);
      }
    }
  }
  closedir(dir);
  return dev;
}


x86 assembly (clang, -O0):
<get_sysfs_dev_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x424(%rbp)
lea    0xe71(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ae <get_sysfs_dev_nr+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    12aa <get_sysfs_dev_nr+0x13a>
jmp    11b3 <get_sysfs_dev_nr+0x43>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1298 <get_sysfs_dev_nr+0x128>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe34(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     1202 <get_sysfs_dev_nr+0x92>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe17(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1207 <get_sysfs_dev_nr+0x97>
jmp    11b3 <get_sysfs_dev_nr+0x43>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xded(%rip),%rdx
lea    0xdd8(%rip),%rcx
lea    0xde8(%rip),%r9
call   1040 <snprintf@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x420(%rbp),%rdi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%eax
jne    1293 <get_sysfs_dev_nr+0x123>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     128e <get_sysfs_dev_nr+0x11e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <get_dev_part_nr@plt>
mov    %rax,%rcx
movslq -0x424(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x424(%rbp)
jmp    1293 <get_sysfs_dev_nr+0x123>
jmp    11b3 <get_sysfs_dev_nr+0x43>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x424(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sysfs_dev_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebp
lea    0xe76(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     126c <get_sysfs_dev_nr+0xfc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1271 <get_sysfs_dev_nr+0x101>
mov    %rax,%r13
mov    %ebp,0xc(%rsp)
xor    %r15d,%r15d
lea    0xe4f(%rip),%rbp
lea    0xe47(%rip),%r12
jmp    11e4 <get_sysfs_dev_nr+0x74>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1274 <get_sysfs_dev_nr+0x104>
mov    0x0(%r13),%r14
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11d0 <get_sysfs_dev_nr+0x60>
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11d0 <get_sysfs_dev_nr+0x60>
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
lea    0xdf7(%rip),%rdx
lea    0xde2(%rip),%rcx
mov    %r14,%r8
lea    0xdef(%rip),%r9
call   1040 <snprintf@plt>
movb   $0x0,0x40f(%rsp)
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1090 <access@plt>
test   %eax,%eax
jne    11d0 <get_sysfs_dev_nr+0x60>
add    $0x1,%r15d
cmpl   $0x0,0xc(%rsp)
je     11d0 <get_sysfs_dev_nr+0x60>
mov    0x0(%r13),%rdi
call   1060 <get_dev_part_nr@plt>
add    %eax,%r15d
jmp    11d0 <get_sysfs_dev_nr+0x60>
xor    %r15d,%r15d
jmp    127c <get_sysfs_dev_nr+0x10c>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %r15d,%eax
add    $0x418,%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 = [3 x i8] c"..\00"
@.str.3 = [9 x i8] c"%s/%s/%s\00"
@.str.4 = [5 x i8] c"stat\00"
@R_OK = global i32 0
define i32 @get_sysfs_dev_nr(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %7
%8 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %60
11: ; preds = %1
br label %12
12: ; preds = %55, %28, %11
%13 = load i32*, i32** %4
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %5
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %56
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %5
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load %struct.dirent*, %struct.dirent** %5
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %22, %16
br label %12
29: ; preds = %22
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = load %struct.dirent*, %struct.dirent** %5
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %30, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1023
store i8 0, i8* %35
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = load i32, i32* @R_OK
%38 = call i32 @access(i8* %36, i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %55, label %40
40: ; preds = %29
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
%43 = load i32, i32* %3
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %40
%46 = load %struct.dirent*, %struct.dirent** %5
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i64 @get_dev_part_nr(i8* %48)
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = add nsw i64 %51, %49
%53 = trunc i64 %52 to i32
store i32 %53, i32* %7
br label %54
54: ; preds = %45, %40
br label %55
55: ; preds = %54, %29
br label %12
56: ; preds = %12
%57 = load i32*, i32** %4
%58 = call i32 @closedir(i32* %57)
%59 = load i32, i32* %7
store i32 %59, i32* %2
br label %60
60: ; preds = %56, %10
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/sys/block\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [9 x i8] c"%s/%s/%s\00"
@.str.4 = [5 x i8] c"stat\00"
@R_OK = global i32 0
define i32 @get_sysfs_dev_nr(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %47, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %44, label %9
9: ; preds = %6
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1023
%11 = icmp eq i32 %0, 0
br label %12
12: ; preds = %9, %40
%13 = phi %struct.dirent* [ %7, %9 ], [ %42, %40 ]
%14 = phi i32 [ 0, %9 ], [ %41, %40 ]
br label %15
15: ; preds = %12, %24
%16 = phi %struct.dirent* [ %13, %12 ], [ %25, %24 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strcmp(i8* dereferenceable(1) %18, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = call i32 @strcmp(i8* dereferenceable(1) %18, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21, %15
%25 = call %struct.dirent* @readdir(i32* %4)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %44, label %15
27: ; preds = %21
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%29 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8 0, i8* %10
%30 = load i32, i32* @R_OK
%31 = call i32 @access(i8* %3, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = add nsw i32 %14, 1
br i1 %11, label %40, label %35
35: ; preds = %33
%36 = load i8*, i8** %28
%37 = call i64 @get_dev_part_nr(i8* %36)
%38 = trunc i64 %37 to i32
%39 = add i32 %34, %38
br label %40
40: ; preds = %33, %35, %27
%41 = phi i32 [ %14, %27 ], [ %39, %35 ], [ %34, %33 ]
%42 = call %struct.dirent* @readdir(i32* %4)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %12
44: ; preds = %40, %24, %6
%45 = phi i32 [ 0, %6 ], [ %14, %24 ], [ %41, %40 ]
%46 = call i32 @closedir(i32* %4)
br label %47
47: ; preds = %1, %44
%48 = phi i32 [ %45, %44 ], [ 0, %1 ]
ret i32 %48
}