#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_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, char const *);
unsigned int sysfs_read_file(unsigned int cpu, const char *fname, char *buf,
                             size_t buflen) {
  char path[255];
  int fd;
  size_t numread;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/cpufreq/%s",
           cpu, fname);
  if ((fd = open(path, O_RDONLY)) == -1)
    return 0;
  numread = read(fd, buf, buflen - 1);
  if (numread < 1) {
    close(fd);
    return 0;
  }
  buf[numread] = '\0';
  close(fd);
  return numread;
}


x86 assembly (clang, -O0):
<sysfs_read_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   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x124(%rbp)
cmp    $0xffffffff,%eax
jne    11a9 <sysfs_read_file+0x69>
movl   $0x0,-0x4(%rbp)
jmp    1210 <sysfs_read_file+0xd0>
mov    -0x124(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x1,-0x130(%rbp)
01
jae    11ec <sysfs_read_file+0xac>
mov    -0x124(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1210 <sysfs_read_file+0xd0>
mov    -0x18(%rbp),%rax
mov    -0x130(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x124(%rbp),%edi
call   1040 <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_read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xea1(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
call   1030 <snprintf@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
xor    %r15d,%r15d
cmp    $0xffffffff,%eax
je     11bb <sysfs_read_file+0x7b>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
test   %rax,%rax
je     11b4 <sysfs_read_file+0x74>
mov    %rax,%rbx
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%r15d
jmp    11bb <sysfs_read_file+0x7b>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/%s\00"
@O_RDONLY = global i32 0
define i32 @sysfs_read_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_RDONLY
%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 %41
22: ; preds = %4
%23 = load i32, i32* %11
%24 = load i8*, i8** %8
%25 = load i64, i64* %9
%26 = sub i64 %25, 1
%27 = call i64 @read(i32 %23, i8* %24, i64 %26)
store i64 %27, i64* %12
%28 = load i64, i64* %12
%29 = icmp ult i64 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i32, i32* %11
%32 = call i32 @close(i32 %31)
store i32 0, i32* %5
br label %41
33: ; preds = %22
%34 = load i8*, i8** %8
%35 = load i64, i64* %12
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
%37 = load i32, i32* %11
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* %12
%40 = trunc i64 %39 to i32
store i32 %40, i32* %5
br label %41
41: ; preds = %33, %30, %21
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/%s\00"
@O_RDONLY = global i32 0
define i32 @sysfs_read_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_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %21, label %11
11: ; preds = %4
%12 = add i64 %3, -1
%13 = call i64 @read(i32 %9, i8* %2, i64 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @close(i32 %9)
br label %21
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %2, i64 %13
store i8 0, i8* %18
%19 = call i32 @close(i32 %9)
%20 = trunc i64 %13 to i32
br label %21
21: ; preds = %4, %17, %15
%22 = phi i32 [ 0, %15 ], [ %20, %17 ], [ 0, %4 ]
ret i32 %22
}