#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int is_online(int cpuid) {
  FILE *filp;
  int state;
  int retval, rc;
  char path[4096];
  retval = -1;
  sprintf(path, "/sys/devices/system/cpu/cpu%d/online", cpuid);
  if (access(path, R_OK) == 0) {
    filp = fopen(path, "r");
    rc = fscanf(filp, "%d", &state);
    if (rc == 1) {
      if (state == 1)
        retval = 1;
      if (state == 0)
        retval = 0;
      fclose(filp);
    }
  }
  return retval;
}


x86 assembly (clang, -O0):
<is_online>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8a(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
jne    1202 <is_online+0xb2>
lea    -0x1020(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6b(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    11fd <is_online+0xad>
cmpl   $0x1,-0x14(%rbp)
jne    11e3 <is_online+0x93>
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11f4 <is_online+0xa4>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1202 <is_online+0xb2>
mov    -0x18(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_online>:
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%edx
lea    0xe9d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11d7 <is_online+0x87>
lea    0xe93(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0xe81(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    11d7 <is_online+0x87>
mov    0xc(%rsp),%eax
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
lea    (%rcx,%rcx,1),%ebx
add    $0xffffffff,%ebx
test   %eax,%eax
cmove  %eax,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/devices/system/cpu/cpu%d/online\00"
@R_OK = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @is_online(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
store i32 %0, i32* %2
store i32 -1, i32* %5
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %2
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* @R_OK
%13 = call i64 @access(i8* %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %1
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %33
22: ; preds = %15
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 1, i32* %5
br label %26
26: ; preds = %25, %22
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 0, i32* %5
br label %30
30: ; preds = %29, %26
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
br label %33
33: ; preds = %30, %15
br label %34
34: ; preds = %33, %1
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/devices/system/cpu/cpu%d/online\00"
@R_OK = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @is_online(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @R_OK
%8 = call i64 @access(i8* %5, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %1
%11 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 1
%17 = select i1 %16, i32 1, i32 -1
%18 = icmp eq i32 %15, 0
%19 = select i1 %18, i32 0, i32 %17
%20 = call i32 @fclose(i32* %11)
br label %21
21: ; preds = %10, %14, %1
%22 = phi i32 [ %19, %14 ], [ -1, %10 ], [ -1, %1 ]
ret i32 %22
}