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

/* Variables and functions */
int EINVAL;
int EIO;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
scalar_t__ stat(char *, struct stat *);
unsigned long long strtoull(char *, char **, int /*<<< orphan*/);
int cpupower_is_cpu_online(unsigned int cpu) {
  char path[255];
  int fd;
  ssize_t numread;
  unsigned long long value;
  char linebuf[4096];
  char *endp;
  struct stat statbuf;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u",
           cpu);
  if (stat(path, &statbuf) != 0)
    return 0;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/online",
           cpu);
  if (stat(path, &statbuf) != 0)
    return 1;
  fd = open(path, O_RDONLY);
  if (fd == -1)
    return -errno;
  numread = read(fd, linebuf, 4096 - 1);
  if (numread < 1) {
    close(fd);
    return -EIO;
  }
  linebuf[numread] = '\0';
  close(fd);
  value = strtoull(linebuf, &endp, 0);
  if (value > 1)
    return -EINVAL;
  return value;
}


x86 assembly (clang, -O0):
<cpupower_is_cpu_online>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1130,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe7c(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x1130(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11b2 <cpupower_is_cpu_online+0x52>
movl   $0x0,-0x4(%rbp)
jmp    12da <cpupower_is_cpu_online+0x17a>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe56(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x1130(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11f6 <cpupower_is_cpu_online+0x96>
movl   $0x1,-0x4(%rbp)
jmp    12da <cpupower_is_cpu_online+0x17a>
lea    -0x110(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    1231 <cpupower_is_cpu_online+0xd1>
xor    %eax,%eax
mov    0x2da9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <cpupower_is_cpu_online+0x17a>
mov    -0x114(%rbp),%edi
lea    -0x1120(%rbp),%rsi
mov    $0xfff,%edx
call   1050 <read@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x1,-0x118(%rbp)
jge    1279 <cpupower_is_cpu_online+0x119>
mov    -0x114(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    0x2d69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <cpupower_is_cpu_online+0x17a>
movslq -0x118(%rbp),%rax
movb   $0x0,-0x1120(%rbp,%rax,1)
mov    -0x114(%rbp),%edi
call   1040 <close@plt>
lea    -0x1120(%rbp),%rdi
lea    -0x1128(%rbp),%rsi
xor    %edx,%edx
call   1060 <strtoull@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x1,-0x120(%rbp)
01
jbe    12d0 <cpupower_is_cpu_online+0x170>
xor    %eax,%eax
mov    0x2d02(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <cpupower_is_cpu_online+0x17a>
mov    -0x120(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpupower_is_cpu_online>:
push   %rbp
push   %rbx
sub    $0x1118,%rsp
mov    %edi,%ebp
lea    0xe8e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    1259 <cpupower_is_cpu_online+0xf9>
lea    0xe7b(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    1259 <cpupower_is_cpu_online+0xf9>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     123a <cpupower_is_cpu_online+0xda>
mov    %eax,%ebx
lea    0x110(%rsp),%rsi
mov    %eax,%edi
mov    $0xfff,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1243 <cpupower_is_cpu_online+0xe3>
mov    %eax,%eax
movb   $0x0,0x110(%rsp,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
lea    0x110(%rsp),%rdi
lea    0x8(%rsp),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1060 <strtoull@plt>
cmp    $0x2,%rax
jb     1257 <cpupower_is_cpu_online+0xf7>
mov    0x2d92(%rip),%rax
sub    (%rax),%ebx
jmp    1259 <cpupower_is_cpu_online+0xf9>
mov    0x2d8f(%rip),%rax
jmp    1251 <cpupower_is_cpu_online+0xf1>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d87(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    1259 <cpupower_is_cpu_online+0xf9>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x1118,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [30 x i8] c"/sys/devices/system/cpu/cpu%u\00"
@.str.1 = [37 x i8] c"/sys/devices/system/cpu/cpu%u/online\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@EIO = global i32 0
@EINVAL = global i32 0
define i32 @cpupower_is_cpu_online(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca [4096 x i8]
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 %0, i32* %3
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* %3
%13 = call i32 @snprintf(i8* %11, i32 255, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%15 = call i64 @stat(i8* %14, %struct.stat* %10)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
br label %62
18: ; preds = %1
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* %3
%21 = call i32 @snprintf(i8* %19, i32 255, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%23 = call i64 @stat(i8* %22, %struct.stat* %10)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 1, i32* %2
br label %62
26: ; preds = %18
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
store i32 %34, i32* %2
br label %62
35: ; preds = %26
%36 = load i32, i32* %5
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 4095)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 1
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* @EIO
%45 = sub nsw i32 0, %44
store i32 %45, i32* %2
br label %62
46: ; preds = %35
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %48
store i8 0, i8* %49
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%53 = call i64 @strtoull(i8* %52, i8** %9, i32 0)
store i64 %53, i64* %7
%54 = load i64, i64* %7
%55 = icmp ugt i64 %54, 1
br i1 %55, label %56, label %59
56: ; preds = %46
%57 = load i32, i32* @EINVAL
%58 = sub nsw i32 0, %57
store i32 %58, i32* %2
br label %62
59: ; preds = %46
%60 = load i64, i64* %7
%61 = trunc i64 %60 to i32
store i32 %61, i32* %2
br label %62
62: ; preds = %59, %56, %41, %32, %25, %17
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [30 x i8] c"/sys/devices/system/cpu/cpu%u\00"
@.str.1 = [37 x i8] c"/sys/devices/system/cpu/cpu%u/online\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@EIO = global i32 0
@EINVAL = global i32 0
define i32 @cpupower_is_cpu_online(i32 %0) {
%2 = alloca [255 x i8]
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = bitcast i8** %4 to i8*
%9 = bitcast %struct.stat* %5 to i8*
%10 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = call i64 @stat(i8* %6, %struct.stat* %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %42
13: ; preds = %1
%14 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%15 = call i64 @stat(i8* %6, %struct.stat* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %42
17: ; preds = %13
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %6, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
br label %42
24: ; preds = %17
%25 = call i32 @read(i32 %19, i8* %7, i32 4095)
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 @close(i32 %19)
%29 = load i32, i32* @EIO
%30 = sub nsw i32 0, %29
br label %42
31: ; preds = %24
%32 = zext i32 %25 to i64
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 @close(i32 %19)
%35 = call i64 @strtoull(i8* %7, i8** %4, i32 0)
%36 = icmp ugt i64 %35, 1
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i32, i32* @EINVAL
%39 = sub nsw i32 0, %38
br label %42
40: ; preds = %31
%41 = trunc i64 %35 to i32
br label %42
42: ; preds = %13, %1, %40, %37, %27, %21
%43 = phi i32 [ %23, %21 ], [ %30, %27 ], [ %39, %37 ], [ %41, %40 ], [ 0, %1 ], [ 1, %13 ]
ret i32 %43
}