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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char const *);
int sys_cpu_setall(const char *control, const char *value) {
  int num = 0;
  char filename[4096];
  FILE *f;
  while (1) {
    snprintf(filename, sizeof(filename),
             "/sys/devices/system/cpu/cpu%i/cpufreq/%s", num, control);
    f = fopen(filename, "w");
    if (!f) {
      return num;
    }
    fprintf(f, "%s\n", value);
    fclose(f);
    num++;
  }
  return -1;
}


x86 assembly (clang, -O0):
<sys_cpu_setall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%r8
mov    $0x1000,%esi
lea    0xe8c(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    11ad <sys_cpu_setall+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret
mov    -0x1028(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe6c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1028(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <sys_cpu_setall+0x1a>

x86 assembly (clang, -O2):
<sys_cpu_setall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xea2(%rip),%rdx
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    $0x1000,%esi
xor    %ecx,%ecx
mov    %r13,%r8
call   1040 <snprintf@plt>
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e9 <sys_cpu_setall+0xa9>
mov    %rax,%r12
xor    %ebp,%ebp
mov    %rsp,%rbx
lea    0xe91(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
lea    0xe81(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x1000,%esi
lea    0xe34(%rip),%rdx
mov    %ebp,%ecx
mov    %r13,%r8
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11a0 <sys_cpu_setall+0x60>
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%i/cpufreq/%s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @sys_cpu_setall(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %8
8: ; preds = %2, %19
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %5
%11 = load i8*, i8** %3
%12 = call i32 @snprintf(i8* %9, i32 4096, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %11)
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %8
%18 = load i32, i32* %5
ret i32 %18
19: ; preds = %8
%20 = load i32*, i32** %7
%21 = load i8*, i8** %4
%22 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %7
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %8
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%i/cpufreq/%s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @sys_cpu_setall(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 0, i8* %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %10, %2
%9 = phi i32 [ 0, %2 ], [ %15, %10 ]
ret i32 %9
10: ; preds = %2, %10
%11 = phi i32* [ %17, %10 ], [ %6, %2 ]
%12 = phi i32 [ %15, %10 ], [ 0, %2 ]
%13 = call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%14 = call i32 @fclose(i32* %11)
%15 = add nuw nsw i32 %12, 1
%16 = call i32 @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %0)
%17 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %8, label %10
}