#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*/);
int rapl_is_energy_supported(unsigned int zone) {
  struct dirent *entry;
  char buf[64];
  snprintf(buf, sizeof(buf),
           "/sys/class/powercap"
           "/intel-rapl:%u",
           zone);
  DIR *dir = opendir(buf);
  int ret = 0;
  if (dir != NULL) {
    entry = readdir(dir);
    while (entry != NULL) {
      if (!strncmp(entry->d_name, "energy_uj", strlen("energy_uj"))) {
        ret = 1;
        break;
      }
      entry = readdir(dir);
    }
    closedir(dir);
  }
  return ret;
}


x86 assembly (clang, -O0):
<rapl_is_energy_supported>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xe82(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1210 <rapl_is_energy_supported+0xb0>
mov    -0x58(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1207 <rapl_is_energy_supported+0xa7>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
lea    0xe58(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%edi
mov    %eax,%edx
lea    0xe47(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11f5 <rapl_is_energy_supported+0x95>
movl   $0x1,-0x5c(%rbp)
jmp    1207 <rapl_is_energy_supported+0xa7>
mov    -0x58(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
jmp    11af <rapl_is_energy_supported+0x4f>
mov    -0x58(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x5c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rapl_is_energy_supported>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ecx
lea    0xe8d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11d9 <rapl_is_energy_supported+0x79>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11e4 <rapl_is_energy_supported+0x84>
lea    0xe78(%rip),%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_is_energy_supported+0x7e>
mov    %r15,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11b0 <rapl_is_energy_supported+0x50>
jmp    11e4 <rapl_is_energy_supported+0x84>
xor    %r14d,%r14d
jmp    11ec <rapl_is_energy_supported+0x8c>
mov    $0x1,%r14d
mov    %r15,%rdi
call   1070 <closedir@plt>
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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