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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ system(char *);
void activate_ondemand(void) {
  DIR *dir;
  struct dirent *dirent;
  FILE *file;
  char filename[PATH_MAX];
  system("/sbin/modprobe cpufreq_ondemand > /dev/null 2>&1");
  dir = opendir("/sys/devices/system/cpu");
  if (!dir)
    return;
  while ((dirent = readdir(dir))) {
    if (dirent->d_name[0] == '.')
      continue;
    sprintf(filename, "/sys/devices/system/cpu/%s/cpufreq/scaling_governor",
            dirent->d_name);
    file = fopen(filename, "w");
    if (!file)
      continue;
    fprintf(file, "ondemand\n");
    fclose(file);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<activate_ondemand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe45(%rip),%rdi
call   1050 <system@plt>
lea    0xe6a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e7 <activate_ondemand+0x67>
movl   $0x1,-0x2c(%rbp)
jmp    1284 <activate_ondemand+0x104>
jmp    11ec <activate_ondemand+0x6c>
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1274 <activate_ondemand+0xf4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    121b <activate_ondemand+0x9b>
jmp    11ec <activate_ondemand+0x6c>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1c(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1256 <activate_ondemand+0xd6>
jmp    11ec <activate_ondemand+0x6c>
mov    -0x18(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11ec <activate_ondemand+0x6c>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<activate_ondemand>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe51(%rip),%rdi
call   1050 <system@plt>
lea    0xe76(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     125c <activate_ondemand+0xdc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1246 <activate_ondemand+0xc6>
lea    0xe69(%rip),%r15
lea    0xe96(%rip),%r12
jmp    11fd <activate_ondemand+0x7d>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1246 <activate_ondemand+0xc6>
mov    (%rax),%rdx
cmpb   $0x2e,(%rdx)
je     11f0 <activate_ondemand+0x70>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <sprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f0 <activate_ondemand+0x70>
mov    %rax,%r13
mov    $0x9,%esi
mov    $0x1,%edx
lea    0xe4b(%rip),%rdi
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
jmp    11f0 <activate_ondemand+0x70>
mov    %rbx,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [49 x i8] c"/sbin/modprobe cpufreq_ondemand > /dev/null 2>&1\00"
@.str.1 = [24 x i8] c"/sys/devices/system/cpu\00"
@.str.2 = [52 x i8] c"/sys/devices/system/cpu/%s/cpufreq/scaling_governor\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [10 x i8] c"ondemand\0A\00"
define void @activate_ondemand() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = call i32 @system(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%12 = call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %1
%13 = load i32*, i32** %1
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %0
store i32 1, i32* %6
br label %47
16: ; preds = %0
br label %17
17: ; preds = %39, %38, %29, %16
%18 = load i32*, i32** %1
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %2
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %2
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 46
br i1 %28, label %29, label %30
29: ; preds = %21
br label %17
30: ; preds = %21
%31 = load %struct.dirent*, %struct.dirent** %2
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %3
%36 = load i32*, i32** %3
%37 = icmp ne i32* %36, null
br i1 %37, label %39, label %38
38: ; preds = %30
br label %17
39: ; preds = %30
%40 = load i32*, i32** %3
%41 = call i32 @fprintf(i32* %40, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
br label %17
44: ; preds = %17
%45 = load i32*, i32** %1
%46 = call i32 @closedir(i32* %45)
store i32 0, i32* %6
br label %47
47: ; preds = %44, %15
%48 = load i8*, i8** %4
%49 = load i32, i32* %6
switch i32 %49, label %51 [
i32 0, label %50
i32 1, label %50
]
50: ; preds = %47, %47
ret void
51: ; preds = %47
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [49 x i8] c"/sbin/modprobe cpufreq_ondemand > /dev/null 2>&1\00"
@.str.1 = [24 x i8] c"/sys/devices/system/cpu\00"
@.str.2 = [52 x i8] c"/sys/devices/system/cpu/%s/cpufreq/scaling_governor\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [10 x i8] c"ondemand\0A\00"
define void @activate_ondemand() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32 @system(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %28, label %7
7: ; preds = %0
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %26, label %10
10: ; preds = %7, %23
%11 = phi %struct.dirent* [ %24, %23 ], [ %8, %7 ]
%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 %23, label %16
16: ; preds = %10
%17 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%18 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = tail call i64 @fwrite(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 9, i64 1, i32* %18)
%22 = tail call i32 @fclose(i32* %18)
br label %23
23: ; preds = %20, %10, %16
%24 = tail call %struct.dirent* @readdir(i32* %5)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %10
26: ; preds = %23, %7
%27 = tail call i32 @closedir(i32* %5)
br label %28
28: ; preds = %0, %26
ret void
}