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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strncmp(char *, char *, int);
bool cpu_online(int i) {
  bool ret = false;
  char fn[100];
  sprintf(fn, "/sys/devices/system/cpu/cpu%d/online", i);
  int fd = open(fn, O_RDONLY);
  if (fd >= 0) {
    char buf[128];
    int n = read(fd, buf, 128);
    if (n > 0 && !strncmp(buf, "1", 1))
      ret = true;
    close(fd);
  }
  return ret;
}


x86 assembly (clang, -O0):
<cpu_online>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jl     11ee <cpu_online+0x9e>
mov    -0x74(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0x104(%rbp)
cmpl   $0x0,-0x104(%rbp)
jle    11e6 <cpu_online+0x96>
lea    -0x100(%rbp),%rdi
lea    0xe59(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11e6 <cpu_online+0x96>
movl   $0x1,-0x8(%rbp)
mov    -0x74(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_online>:
push   %rbp
push   %rbx
sub    $0xe8,%rsp
mov    %edi,%edx
lea    0xe9e(%rip),%rsi
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11ba <cpu_online+0x6a>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11be <cpu_online+0x6e>
lea    0xe82(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1,%edx
call   1030 <strncmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
jmp    11c0 <cpu_online+0x70>
xor    %ebp,%ebp
jmp    11c7 <cpu_online+0x77>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"/sys/devices/system/cpu/cpu%d/online\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"1\00"
define i32 @cpu_online(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, 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 [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %1
%17 = load i32, i32* %5
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 128)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%24 = call i32 @strncmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
store i32 1, i32* %3
br label %27
27: ; preds = %26, %22, %16
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %1
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [37 x i8] c"/sys/devices/system/cpu/cpu%d/online\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"1\00"
define i32 @cpu_online(i32 %0) {
%2 = alloca [100 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %20
9: ; preds = %1
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%11 = call i32 @read(i32 %7, i8* %10, i32 128)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = call i32 @strncmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
br label %17
17: ; preds = %13, %9
%18 = phi i32 [ 0, %9 ], [ %16, %13 ]
%19 = call i32 @close(i32 %7)
br label %20
20: ; preds = %17, %1
%21 = phi i32 [ %18, %17 ], [ 0, %1 ]
ret i32 %21
}