#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 {
  int d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *strchr(int, char);
scalar_t__ strncmp(int, char *, int);
unsigned int rapl_zone_count(void) {
  unsigned int count = 0;
  int err_save;
  struct dirent *entry;
  DIR *dir = opendir("/sys/class/powercap");
  if (dir == NULL) {
    perror("/sys/class/powercap");
    return 0;
  }
  for (errno = 0; (entry = readdir(dir)) != NULL;) {
    if (strncmp(entry->d_name, "intel-rapl:", sizeof("intel-rapl:") - 1) == 0 &&
        strchr(entry->d_name + sizeof("intel-rapl:") - 1, ':') == NULL) {
      count++;
    }
  }
  err_save = errno;
  if (closedir(dir)) {
    perror("/sys/class/powercap");
  }
  errno = err_save;
  return errno ? 0 : count;
}


x86 assembly (clang, -O0):
<rapl_zone_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x8(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <rapl_zone_count+0x42>
lea    0xe6f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1277 <rapl_zone_count+0x117>
mov    0x2e37(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     121a <rapl_zone_count+0xba>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    $0xb,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1215 <rapl_zone_count+0xb5>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    $0xc,%rax
sub    $0x1,%rax
mov    %eax,%edi
mov    $0x3a,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    1215 <rapl_zone_count+0xb5>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11af <rapl_zone_count+0x4f>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0x0,%rax
je     1245 <rapl_zone_count+0xe5>
lea    0xdc0(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%ecx
mov    0x2d91(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d88(%rip),%rax
cmpl   $0x0,(%rax)
je     126b <rapl_zone_count+0x10b>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1271 <rapl_zone_count+0x111>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rapl_zone_count>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xe91(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1213 <rapl_zone_count+0xb3>
mov    %rax,%r15
mov    0x2e59(%rip),%r12
movl   $0x0,(%r12)
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11ec <rapl_zone_count+0x8c>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0xe6c(%rip),%r14
jmp    11c0 <rapl_zone_count+0x60>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ee <rapl_zone_count+0x8e>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0xb,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <rapl_zone_count+0x50>
mov    (%rbx),%edi
add    $0xb,%edi
mov    $0x3a,%esi
call   1050 <strchr@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
jmp    11b0 <rapl_zone_count+0x50>
xor    %ebp,%ebp
mov    (%r12),%ebx
mov    %r15,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
jne    1223 <rapl_zone_count+0xc3>
mov    %ebx,(%r12)
xor    %eax,%eax
test   %ebx,%ebx
cmove  %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    120a <rapl_zone_count+0xaa>
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
jmp    11ff <rapl_zone_count+0x9f>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [20 x i8] c"/sys/class/powercap\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"intel-rapl:\00"
define i32 @rapl_zone_count() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.dirent*
%5 = alloca i32*
store i32 0, i32* %2
%6 = call i32* @opendir(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %52
11: ; preds = %0
store i32 0, i32* @errno
br label %12
12: ; preds = %35, %11
%13 = load i32*, i32** %5
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %4
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @strncmp(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 11)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %16
%23 = load %struct.dirent*, %struct.dirent** %4
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sext i32 %25 to i64
%27 = add i64 %26, 12
%28 = sub i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i32* @strchr(i32 %29, i8 signext 58)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = load i32, i32* %2
%34 = add i32 %33, 1
store i32 %34, i32* %2
br label %35
35: ; preds = %32, %22, %16
br label %12
36: ; preds = %12
%37 = load i32, i32* @errno
store i32 %37, i32* %3
%38 = load i32*, i32** %5
%39 = call i64 @closedir(i32* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i32, i32* %3
store i32 %44, i32* @errno
%45 = load i32, i32* @errno
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
br label %50
48: ; preds = %43
%49 = load i32, i32* %2
br label %50
50: ; preds = %48, %47
%51 = phi i32 [ 0, %47 ], [ %49, %48 ]
store i32 %51, i32* %1
br label %52
52: ; preds = %50, %9
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [20 x i8] c"/sys/class/powercap\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"intel-rapl:\00"
define i32 @rapl_zone_count() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %36
5: ; preds = %0
store i32 0, i32* @errno
%6 = tail call %struct.dirent* @readdir(i32* %1)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %26, label %8
8: ; preds = %5, %22
%9 = phi %struct.dirent* [ %24, %22 ], [ %6, %5 ]
%10 = phi i32 [ %23, %22 ], [ 0, %5 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @strncmp(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 11)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %8
%16 = load i32, i32* %11
%17 = add i32 %16, 11
%18 = tail call i32* @strchr(i32 %17, i8 signext 58)
%19 = icmp eq i32* %18, null
%20 = zext i1 %19 to i32
%21 = add i32 %10, %20
br label %22
22: ; preds = %15, %8
%23 = phi i32 [ %10, %8 ], [ %21, %15 ]
%24 = tail call %struct.dirent* @readdir(i32* %1)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %8
26: ; preds = %22, %5
%27 = phi i32 [ 0, %5 ], [ %23, %22 ]
%28 = load i32, i32* @errno
%29 = tail call i64 @closedir(i32* %1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
store i32 %28, i32* @errno
%34 = icmp eq i32 %28, 0
%35 = select i1 %34, i32 %27, i32 0
br label %36
36: ; preds = %33, %3
%37 = phi i32 [ 0, %3 ], [ %35, %33 ]
ret i32 %37
}