#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 PATH_MAX;
int /*<<< orphan*/ cachunk_devs();
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ update_devstats_pci(char *, char *);
int /*<<< orphan*/ update_devstats_spi(char *, char *);
void count_device_pm(void) {
  DIR *dir;
  struct dirent *dirent;
  char pathname[PATH_MAX];
  dir = opendir("/sys/bus/pci/devices");
  if (!dir)
    return;
  cachunk_devs();
  while ((dirent = readdir(dir))) {
    if (dirent->d_name[0] == '.')
      continue;
    sprintf(pathname, "/sys/bus/pci/devices/%s", dirent->d_name);
    update_devstats_pci(pathname, dirent->d_name);
  }
  closedir(dir);
  dir = opendir("/sys/bus/spi/devices");
  if (!dir)
    return;
  cachunk_devs();
  while ((dirent = readdir(dir))) {
    if (dirent->d_name[0] == '.')
      continue;
    sprintf(pathname, "/sys/bus/spi/devices/%s", dirent->d_name);
    update_devstats_spi(pathname, dirent->d_name);
  }
  closedir(dir);
  dir = opendir("/sys/bus/i2c/devices");
  if (!dir)
    return;
  cachunk_devs();
  while ((dirent = readdir(dir))) {
    if (dirent->d_name[0] == '.')
      continue;
    sprintf(pathname, "/sys/bus/i2c/devices/%s", dirent->d_name);
    update_devstats_spi(pathname, dirent->d_name);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<count_device_pm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe55(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11cb <count_device_pm+0x5b>
movl   $0x1,-0x24(%rbp)
jmp    1361 <count_device_pm+0x1f1>
mov    $0x0,%al
call   1050 <cachunk_devs@plt>
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     122d <count_device_pm+0xbd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1201 <count_device_pm+0x91>
jmp    11d2 <count_device_pm+0x62>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe02(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1080 <update_devstats_pci@plt>
jmp    11d2 <count_device_pm+0x62>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
lea    0xdf0(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    125d <count_device_pm+0xed>
movl   $0x1,-0x24(%rbp)
jmp    1361 <count_device_pm+0x1f1>
mov    $0x0,%al
call   1050 <cachunk_devs@plt>
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12bf <count_device_pm+0x14f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1293 <count_device_pm+0x123>
jmp    1264 <count_device_pm+0xf4>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd9d(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <update_devstats_spi@plt>
jmp    1264 <count_device_pm+0xf4>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
lea    0xd8b(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    12ef <count_device_pm+0x17f>
movl   $0x1,-0x24(%rbp)
jmp    1361 <count_device_pm+0x1f1>
mov    $0x0,%al
call   1050 <cachunk_devs@plt>
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1351 <count_device_pm+0x1e1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1325 <count_device_pm+0x1b5>
jmp    12f6 <count_device_pm+0x186>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd38(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <update_devstats_spi@plt>
jmp    12f6 <count_device_pm+0x186>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_device_pm>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe64(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12e8 <count_device_pm+0x178>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <cachunk_devs@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1200 <count_device_pm+0x90>
mov    %rax,%rbx
lea    0xe4a(%rip),%r15
jmp    11e0 <count_device_pm+0x70>
nopl   (%rax)
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1200 <count_device_pm+0x90>
mov    (%rbx),%rdx
cmpb   $0x2e,(%rdx)
je     11d0 <count_device_pm+0x60>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <sprintf@plt>
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1080 <update_devstats_pci@plt>
jmp    11d0 <count_device_pm+0x60>
mov    %r12,%rdi
call   1060 <closedir@plt>
lea    0xe1e(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12e8 <count_device_pm+0x178>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <cachunk_devs@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1270 <count_device_pm+0x100>
mov    %rax,%rbx
lea    0xe04(%rip),%r15
jmp    1250 <count_device_pm+0xe0>
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1270 <count_device_pm+0x100>
mov    (%rbx),%rdx
cmpb   $0x2e,(%rdx)
je     1240 <count_device_pm+0xd0>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <sprintf@plt>
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1040 <update_devstats_spi@plt>
jmp    1240 <count_device_pm+0xd0>
mov    %r12,%rdi
call   1060 <closedir@plt>
lea    0xddb(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12e8 <count_device_pm+0x178>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <cachunk_devs@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     12e0 <count_device_pm+0x170>
mov    %rax,%rbx
lea    0xdc5(%rip),%r15
jmp    12c0 <count_device_pm+0x150>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12e0 <count_device_pm+0x170>
mov    (%rbx),%rdx
cmpb   $0x2e,(%rdx)
je     12b0 <count_device_pm+0x140>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <sprintf@plt>
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1040 <update_devstats_spi@plt>
jmp    12b0 <count_device_pm+0x140>
mov    %r12,%rdi
call   1060 <closedir@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"/sys/bus/pci/devices\00"
@.str.1 = [24 x i8] c"/sys/bus/pci/devices/%s\00"
@.str.2 = [21 x i8] c"/sys/bus/spi/devices\00"
@.str.3 = [24 x i8] c"/sys/bus/spi/devices/%s\00"
@.str.4 = [21 x i8] c"/sys/bus/i2c/devices\00"
@.str.5 = [24 x i8] c"/sys/bus/i2c/devices/%s\00"
define void @count_device_pm() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %3
%9 = alloca i8, i64 %7
store i64 %7, i64* %4
%10 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %1
%11 = load i32*, i32** %1
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %0
store i32 1, i32* %5
br label %103
14: ; preds = %0
%15 = call i32 (...) @cachunk_devs()
br label %16
16: ; preds = %29, %28, %14
%17 = load i32*, i32** %1
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %2
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %38
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %2
%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 %16
29: ; preds = %20
%30 = load %struct.dirent*, %struct.dirent** %2
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load %struct.dirent*, %struct.dirent** %2
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @update_devstats_pci(i8* %9, i8* %36)
br label %16
38: ; preds = %16
%39 = load i32*, i32** %1
%40 = call i32 @closedir(i32* %39)
%41 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %1
%42 = load i32*, i32** %1
%43 = icmp ne i32* %42, null
br i1 %43, label %45, label %44
44: ; preds = %38
store i32 1, i32* %5
br label %103
45: ; preds = %38
%46 = call i32 (...) @cachunk_devs()
br label %47
47: ; preds = %60, %59, %45
%48 = load i32*, i32** %1
%49 = call %struct.dirent* @readdir(i32* %48)
store %struct.dirent* %49, %struct.dirent** %2
%50 = icmp ne %struct.dirent* %49, null
br i1 %50, label %51, label %69
51: ; preds = %47
%52 = load %struct.dirent*, %struct.dirent** %2
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = getelementptr inbounds i8, i8* %54, i64 0
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 46
br i1 %58, label %59, label %60
59: ; preds = %51
br label %47
60: ; preds = %51
%61 = load %struct.dirent*, %struct.dirent** %2
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = load %struct.dirent*, %struct.dirent** %2
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 @update_devstats_spi(i8* %9, i8* %67)
br label %47
69: ; preds = %47
%70 = load i32*, i32** %1
%71 = call i32 @closedir(i32* %70)
%72 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32* %72, i32** %1
%73 = load i32*, i32** %1
%74 = icmp ne i32* %73, null
br i1 %74, label %76, label %75
75: ; preds = %69
store i32 1, i32* %5
br label %103
76: ; preds = %69
%77 = call i32 (...) @cachunk_devs()
br label %78
78: ; preds = %91, %90, %76
%79 = load i32*, i32** %1
%80 = call %struct.dirent* @readdir(i32* %79)
store %struct.dirent* %80, %struct.dirent** %2
%81 = icmp ne %struct.dirent* %80, null
br i1 %81, label %82, label %100
82: ; preds = %78
%83 = load %struct.dirent*, %struct.dirent** %2
%84 = getelementptr inbounds %struct.dirent, %struct.dirent* %83, i32 0, i32 0
%85 = load i8*, i8** %84
%86 = getelementptr inbounds i8, i8* %85, i64 0
%87 = load i8, i8* %86
%88 = sext i8 %87 to i32
%89 = icmp eq i32 %88, 46
br i1 %89, label %90, label %91
90: ; preds = %82
br label %78
91: ; preds = %82
%92 = load %struct.dirent*, %struct.dirent** %2
%93 = getelementptr inbounds %struct.dirent, %struct.dirent* %92, i32 0, i32 0
%94 = load i8*, i8** %93
%95 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %94)
%96 = load %struct.dirent*, %struct.dirent** %2
%97 = getelementptr inbounds %struct.dirent, %struct.dirent* %96, i32 0, i32 0
%98 = load i8*, i8** %97
%99 = call i32 @update_devstats_spi(i8* %9, i8* %98)
br label %78
100: ; preds = %78
%101 = load i32*, i32** %1
%102 = call i32 @closedir(i32* %101)
store i32 0, i32* %5
br label %103
103: ; preds = %100, %75, %44, %13
%104 = load i8*, i8** %3
%105 = load i32, i32* %5
switch i32 %105, label %107 [
i32 0, label %106
i32 1, label %106
]
106: ; preds = %103, %103
ret void
107: ; preds = %103
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"/sys/bus/pci/devices\00"
@.str.1 = [24 x i8] c"/sys/bus/pci/devices/%s\00"
@.str.2 = [21 x i8] c"/sys/bus/spi/devices\00"
@.str.3 = [24 x i8] c"/sys/bus/spi/devices/%s\00"
@.str.4 = [21 x i8] c"/sys/bus/i2c/devices\00"
@.str.5 = [24 x i8] c"/sys/bus/i2c/devices/%s\00"
define void @count_device_pm() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %67, label %6
6: ; preds = %0
%7 = tail call i32 (...) @cachunk_devs()
%8 = call %struct.dirent* @readdir(i32* %4)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %23, label %10
10: ; preds = %6, %20
%11 = phi %struct.dirent* [ %21, %20 ], [ %8, %6 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 46
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%18 = load i8*, i8** %12
%19 = call i32 @update_devstats_pci(i8* %3, i8* %18)
br label %20
20: ; preds = %16, %10
%21 = call %struct.dirent* @readdir(i32* %4)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %23, label %10
23: ; preds = %20, %6
%24 = call i32 @closedir(i32* %4)
%25 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %67, label %27
27: ; preds = %23
%28 = call i32 (...) @cachunk_devs()
%29 = call %struct.dirent* @readdir(i32* %25)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %44, label %31
31: ; preds = %27, %41
%32 = phi %struct.dirent* [ %42, %41 ], [ %29, %27 ]
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i64 0, i32 0
%34 = load i8*, i8** %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 46
br i1 %36, label %41, label %37
37: ; preds = %31
%38 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%39 = load i8*, i8** %33
%40 = call i32 @update_devstats_spi(i8* %3, i8* %39)
br label %41
41: ; preds = %37, %31
%42 = call %struct.dirent* @readdir(i32* %25)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %31
44: ; preds = %41, %27
%45 = call i32 @closedir(i32* %25)
%46 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%47 = icmp eq i32* %46, null
br i1 %47, label %67, label %48
48: ; preds = %44
%49 = call i32 (...) @cachunk_devs()
%50 = call %struct.dirent* @readdir(i32* %46)
%51 = icmp eq %struct.dirent* %50, null
br i1 %51, label %65, label %52
52: ; preds = %48, %62
%53 = phi %struct.dirent* [ %63, %62 ], [ %50, %48 ]
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i64 0, i32 0
%55 = load i8*, i8** %54
%56 = load i8, i8* %55
%57 = icmp eq i8 %56, 46
br i1 %57, label %62, label %58
58: ; preds = %52
%59 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%60 = load i8*, i8** %54
%61 = call i32 @update_devstats_spi(i8* %3, i8* %60)
br label %62
62: ; preds = %58, %52
%63 = call %struct.dirent* @readdir(i32* %46)
%64 = icmp eq %struct.dirent* %63, null
br i1 %64, label %65, label %52
65: ; preds = %62, %48
%66 = call i32 @closedir(i32* %46)
br label %67
67: ; preds = %44, %23, %0, %65
ret void
}