#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*/ 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 *);
size_t write(int, char const *, size_t);
unsigned int sysfs_write_file(unsigned int cpu, const char *fname,
                              const char *value, size_t len) {
  char path[255];
  int fd;
  size_t numwrite;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/cpufreq/%s",
           cpu, fname);
  if ((fd = open(path, O_WRONLY)) == -1)
    return 0;
  numwrite = write(fd, value, len);
  if (numwrite < 1) {
    close(fd);
    return 0;
  }
  close(fd);
  return numwrite;
}


x86 assembly (clang, -O0):
<sysfs_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)
cmp    $0xffffffff,%eax
jne    11a9 <sysfs_write_file+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11fd <sysfs_write_file+0xbd>
mov    -0x124(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x1,-0x130(%rbp)
01
jae    11e8 <sysfs_write_file+0xa8>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <sysfs_write_file+0xbd>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
mov    -0x130(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_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     119e <sysfs_write_file+0x5e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11a0 <sysfs_write_file+0x60>
xor    %ebx,%ebx
mov    %ebx,%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_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 i64
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 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 0, i32* %5
br label %37
22: ; preds = %4
%23 = load i32, i32* %11
%24 = load i8*, i8** %8
%25 = load i64, i64* %9
%26 = call i64 @write(i32 %23, i8* %24, i64 %25)
store i64 %26, i64* %12
%27 = load i64, i64* %12
%28 = icmp ult i64 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %11
%31 = call i32 @close(i32 %30)
store i32 0, i32* %5
br label %37
32: ; preds = %22
%33 = load i32, i32* %11
%34 = call i32 @close(i32 %33)
%35 = load i64, i64* %12
%36 = trunc i64 %35 to i32
store i32 %36, i32* %5
br label %37
37: ; preds = %32, %29, %21
%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_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 %15, label %11
11: ; preds = %4
%12 = call i64 @write(i32 %9, i8* %2, i64 %3)
%13 = call i32 @close(i32 %9)
%14 = trunc i64 %12 to i32
br label %15
15: ; preds = %11, %4
%16 = phi i32 [ 0, %4 ], [ %14, %11 ]
ret i32 %16
}