#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*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, ...);
int /*<<< orphan*/ strcmp(char *, char *);
int get_dev_part_nr(char *dev_name) {
  DIR *dir;
  struct dirent *drd;
  char dfile[1024], line[1024];
  int part = 0;
  snprintf(dfile, 1024, "%s/%s", "/sys/block", dev_name);
  dfile[1024 - 1] = '\0';
  if ((dir = opendir(dfile)) == NULL)
    return 0;
  while ((drd = readdir(dir)) != NULL) {
    if (!strcmp(drd->d_name, ".") || !strcmp(drd->d_name, ".."))
      continue;
    snprintf(line, 1024, "%s/%s/%s", dfile, drd->d_name, "stat");
    line[1024 - 1] = '\0';
    if (!access(line, R_OK)) {
      part++;
    }
  }
  closedir(dir);
  return part;
}


x86 assembly (clang, -O0):
<get_dev_part_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x824(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    $0x400,%esi
lea    0xe81(%rip),%rdx
lea    0xe69(%rip),%rcx
mov    $0x0,%al
call   1040 <snprintf@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c8 <get_dev_part_nr+0x68>
movl   $0x0,-0x4(%rbp)
jmp    129b <get_dev_part_nr+0x13b>
jmp    11cd <get_dev_part_nr+0x6d>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1289 <get_dev_part_nr+0x129>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     121c <get_dev_part_nr+0xbc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfd(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1221 <get_dev_part_nr+0xc1>
jmp    11cd <get_dev_part_nr+0x6d>
lea    -0x820(%rbp),%rdi
lea    -0x420(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdcc(%rip),%rdx
lea    0xdce(%rip),%r9
mov    $0x0,%al
call   1040 <snprintf@plt>
movb   $0x0,-0x421(%rbp)
lea    -0x820(%rbp),%rdi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    1284 <get_dev_part_nr+0x124>
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
jmp    11cd <get_dev_part_nr+0x6d>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x824(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dev_part_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r8
lea    0xe96(%rip),%rdx
lea    0xe7e(%rip),%rcx
xor    %r14d,%r14d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
movb   $0x0,0x3ff(%rsp)
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     125e <get_dev_part_nr+0xfe>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1253 <get_dev_part_nr+0xf3>
lea    0xe41(%rip),%rbp
lea    0xe39(%rip),%r15
lea    0x400(%rsp),%r12
xor    %r14d,%r14d
jmp    11ed <get_dev_part_nr+0x8d>
nop
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1256 <get_dev_part_nr+0xf6>
mov    (%rax),%r13
mov    %r13,%rdi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <get_dev_part_nr+0x80>
mov    %r13,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <get_dev_part_nr+0x80>
mov    %r12,%rdi
mov    $0x400,%esi
lea    0xdf1(%rip),%rdx
mov    %rsp,%rcx
mov    %r13,%r8
lea    0xded(%rip),%r9
xor    %eax,%eax
call   1040 <snprintf@plt>
movb   $0x0,0x7ff(%rsp)
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <access@plt>
cmp    $0x1,%eax
adc    $0x0,%r14d
jmp    11e0 <get_dev_part_nr+0x80>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %r14d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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