#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 BUFSIZ;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strncmp(char *, char *, int);
int cpu_online(int cpu) {
  char buf[BUFSIZ];
  char *cp;
  FILE *fp;
  int n;
  if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
    return -1;
  while (fgets(buf, sizeof buf, fp)) {
    if (strncmp(buf, "processor\t:", 11) != 0)
      continue;
    cp = buf + 12;
    n = atoi(cp);
    if (n == cpu) {
      fclose(fp);
      return 1;
    }
    if (n >= cpu) {
      fclose(fp);
      return -1;
    }
  }
  fclose(fp);
  return -1;
}


x86 assembly (clang, -O0):
<cpu_online>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    0xe6e(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11bf <cpu_online+0x6f>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1289 <cpu_online+0x139>
jmp    11c4 <cpu_online+0x74>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1272 <cpu_online+0x122>
mov    -0x38(%rbp),%rdi
lea    0xe24(%rip),%rsi
mov    $0xb,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1205 <cpu_online+0xb5>
jmp    11c4 <cpu_online+0x74>
mov    -0x38(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1245 <cpu_online+0xf5>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1289 <cpu_online+0x139>
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     126d <cpu_online+0x11d>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1289 <cpu_online+0x139>
jmp    11c4 <cpu_online+0x74>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_online>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,-0x2c(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe7d(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ff <cpu_online+0xaf>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     120d <cpu_online+0xbd>
lea    0xc(%rbx),%r13
lea    0xe55(%rip),%r14
jmp    11d3 <cpu_online+0x83>
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f7 <cpu_online+0xa7>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xb,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <cpu_online+0x70>
mov    %r13,%rdi
call   1070 <atoi@plt>
cmp    -0x2c(%rbp),%eax
je     1207 <cpu_online+0xb7>
jl     11c0 <cpu_online+0x70>
mov    $0xffffffff,%r14d
jmp    120d <cpu_online+0xbd>
mov    $0xffffffff,%r14d
jmp    1215 <cpu_online+0xc5>
mov    $0x1,%r14d
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"processor\09:\00"
define i32 @cpu_online(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %7
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %9
br label %48
17: ; preds = %1
br label %18
18: ; preds = %44, %26, %17
%19 = trunc i64 %11 to i32
%20 = load i32*, i32** %7
%21 = call i64 @fgets(i8* %13, i32 %19, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %45
23: ; preds = %18
%24 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
br label %18
27: ; preds = %23
%28 = getelementptr inbounds i8, i8* %13, i64 12
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = load i32, i32* %3
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32*, i32** %7
%36 = call i32 @fclose(i32* %35)
store i32 1, i32* %2
store i32 1, i32* %9
br label %48
37: ; preds = %27
%38 = load i32, i32* %8
%39 = load i32, i32* %3
%40 = icmp sge i32 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %48
44: ; preds = %37
br label %18
45: ; preds = %18
%46 = load i32*, i32** %7
%47 = call i32 @fclose(i32* %46)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %48
48: ; preds = %45, %41, %34, %16
%49 = load i8*, i8** %4
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"processor\09:\00"
define i32 @cpu_online(i32 %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %26, label %7
7: ; preds = %1
%8 = call i64 @fgets(i8* %4, i32 %2, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = getelementptr inbounds i8, i8* %4, i64 12
br label %12
12: ; preds = %10, %15
%13 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12, %21
%16 = call i64 @fgets(i8* %4, i32 %2, i32* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %12
18: ; preds = %12
%19 = call i32 @atoi(i8* %11)
%20 = icmp eq i32 %19, %0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = icmp slt i32 %19, %0
br i1 %22, label %15, label %23
23: ; preds = %15, %21, %18, %7
%24 = phi i32 [ -1, %7 ], [ 1, %18 ], [ -1, %21 ], [ -1, %15 ]
%25 = call i32 @fclose(i32* %5)
br label %26
26: ; preds = %23, %1
%27 = phi i32 [ -1, %1 ], [ %24, %23 ]
ret i32 %27
}