#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 ssize_t;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, char const *);
int write(int, char const *, size_t);
unsigned int sysfs_cpufreq_write_file(unsigned int cpu, const char *fname,
                                      const char *value, size_t len) {
  char path[255];
  int fd;
  ssize_t numwrite;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/cpufreq/%s",
           cpu, fname);
  fd = open(path, O_WRONLY);
  if (fd == -1)
    return 0;
  numwrite = write(fd, value, len);
  if (numwrite < 1) {
    close(fd);
    return 0;
  }
  close(fd);
  return (unsigned int)numwrite;
}


x86 assembly (clang, -O0):
<sysfs_cpufreq_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0xff,%esi
lea    0xe8c(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11ad <sysfs_cpufreq_write_file+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    11fe <sysfs_cpufreq_write_file+0xbe>
mov    -0x124(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x1,-0x128(%rbp)
jge    11ea <sysfs_cpufreq_write_file+0xaa>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fe <sysfs_cpufreq_write_file+0xbe>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
mov    -0x128(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_cpufreq_write_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xea3(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
call   1040 <snprintf@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a4 <sysfs_cpufreq_write_file+0x64>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
cmovns %ebx,%eax
jmp    11a6 <sysfs_cpufreq_write_file+0x66>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/%s\00"
@O_WRONLY = global i32 0
define i32 @sysfs_cpufreq_write_file(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [255 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%14 = load i32, i32* %6
%15 = load i8*, i8** %7
%16 = call i32 @snprintf(i8* %13, i32 255, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %15)
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 0, i32* %5
br label %37
23: ; preds = %4
%24 = load i32, i32* %11
%25 = load i8*, i8** %8
%26 = load i64, i64* %9
%27 = call i32 @write(i32 %24, i8* %25, i64 %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* %11
%32 = call i32 @close(i32 %31)
store i32 0, i32* %5
br label %37
33: ; preds = %23
%34 = load i32, i32* %11
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %12
store i32 %36, i32* %5
br label %37
37: ; preds = %33, %30, %22
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/%s\00"
@O_WRONLY = global i32 0
define i32 @sysfs_cpufreq_write_file(i32 %0, i8* %1, i8* nocapture %2, i64 %3) {
%5 = alloca [255 x i8]
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = call i32 @write(i32 %9, i8* %2, i64 %3)
%13 = call i32 @close(i32 %9)
%14 = icmp sgt i32 %12, 0
%15 = select i1 %14, i32 %12, i32 0
br label %16
16: ; preds = %11, %4
%17 = phi i32 [ 0, %4 ], [ %15, %11 ]
ret i32 %17
}