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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(int /*<<< orphan*/, char *, int /*<<< orphan*/);
unsigned int rapl_get_count() {
  unsigned int count = 0;
  struct dirent *entry;
  DIR *dir = opendir("/sys/class/powercap");
  char buf[24];
  if (dir != NULL) {
    snprintf(buf, sizeof(buf), "intel-rapl:%u", count);
    entry = readdir(dir);
    while (entry != NULL) {
      if (!strncmp(entry->d_name, buf, strlen(buf))) {
        count++;
        break;
      }
      entry = readdir(dir);
    }
    closedir(dir);
  }
  return count;
}


x86 assembly (clang, -O0):
<rapl_get_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1214 <rapl_get_count+0xb4>
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x18,%esi
lea    0xe77(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120b <rapl_get_count+0xab>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f9 <rapl_get_count+0x99>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    120b <rapl_get_count+0xab>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
jmp    11af <rapl_get_count+0x4f>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rapl_get_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe8f(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11d9 <rapl_get_count+0x79>
mov    %rax,%r15
lea    0xe8f(%rip),%rdx
mov    %rsp,%rdi
xor    %r14d,%r14d
mov    $0x18,%esi
xor    %ecx,%ecx
call   1060 <snprintf@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11e4 <rapl_get_count+0x84>
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11de <rapl_get_count+0x7e>
mov    %r15,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11b0 <rapl_get_count+0x50>
jmp    11e4 <rapl_get_count+0x84>
xor    %r14d,%r14d
jmp    11ec <rapl_get_count+0x8c>
mov    $0x1,%r14d
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [20 x i8] c"/sys/class/powercap\00"
@.str.1 = [14 x i8] c"intel-rapl:%u\00"
define i32 @rapl_get_count() {
%1 = alloca [24 x i8]
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds [24 x i8], [24 x i8]* %1, i64 0, i64 0
%4 = icmp eq i32* %2, null
br i1 %4, label %22, label %5
5: ; preds = %0
%6 = call i32 @snprintf(i8* %3, i32 24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 0)
%7 = call %struct.dirent* @readdir(i32* %2)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %19, label %9
9: ; preds = %5, %16
%10 = phi %struct.dirent* [ %17, %16 ], [ %7, %5 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strlen(i8* %3)
%14 = call i32 @strncmp(i32 %12, i8* %3, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = call %struct.dirent* @readdir(i32* %2)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %19, label %9
19: ; preds = %16, %9, %5
%20 = phi i32 [ 0, %5 ], [ 1, %9 ], [ 0, %16 ]
%21 = call i32 @closedir(i32* %2)
br label %22
22: ; preds = %19, %0
%23 = phi i32 [ %20, %19 ], [ 0, %0 ]
ret i32 %23
}