#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*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, unsigned int *, unsigned int *);
char *strerror(int /*<<< orphan*/);
unsigned int bpf_num_possible_cpus(void) {
  static const char *fcpu = "/sys/devices/system/cpu/possible";
  unsigned int start, end, possible_cpus = 0;
  char buff[128];
  FILE *fp;
  int n;
  fp = fopen(fcpu, "r");
  if (!fp) {
    printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
    exit(1);
  }
  while (fgets(buff, sizeof(buff), fp)) {
    n = sscanf(buff, "%u-%u", &start, &end);
    if (n == 0) {
      printf("Failed to retrieve # possible CPUs!\n");
      exit(1);
    } else if (n == 1) {
      end = start;
    }
    possible_cpus = start == 0 ? end + 1 : 0;
    break;
  }
  fclose(fp);
  return possible_cpus;
}


x86 assembly (clang, -O0):
<bpf_num_possible_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0xc(%rbp)
mov    0x2ecf(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11e8 <bpf_num_possible_cpus+0x78>
mov    0x2ea7(%rip),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xa8(%rbp),%rsi
mov    %rax,%rdx
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11ed <bpf_num_possible_cpus+0x7d>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     129e <bpf_num_possible_cpus+0x12e>
lea    -0x90(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    -0x4(%rbp),%rdx
lea    -0x8(%rbp),%rcx
call   1060 <sscanf@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
jne    1255 <bpf_num_possible_cpus+0xe5>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x1,-0x9c(%rbp)
jne    1268 <bpf_num_possible_cpus+0xf8>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    126d <bpf_num_possible_cpus+0xfd>
cmpl   $0x0,-0x4(%rbp)
jne    1288 <bpf_num_possible_cpus+0x118>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xac(%rbp)
jmp    1295 <bpf_num_possible_cpus+0x125>
xor    %eax,%eax
mov    %eax,-0xac(%rbp)
jmp    1295 <bpf_num_possible_cpus+0x125>
mov    -0xac(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_num_possible_cpus>:
push   %rbp
push   %rbx
sub    $0x98,%rsp
lea    0xe70(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1215 <bpf_num_possible_cpus+0x95>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e9 <bpf_num_possible_cpus+0x69>
lea    0xe7b(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     11ed <bpf_num_possible_cpus+0x6d>
test   %eax,%eax
je     1245 <bpf_num_possible_cpus+0xc5>
mov    0xc(%rsp),%ecx
mov    0x8(%rsp),%eax
jmp    11f7 <bpf_num_possible_cpus+0x77>
xor    %ebp,%ebp
jmp    1201 <bpf_num_possible_cpus+0x81>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %eax,%ecx
add    $0x1,%eax
xor    %ebp,%ebp
test   %ecx,%ecx
cmove  %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x98,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf9(%rip),%rdi
lea    0xdcf(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdf7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@bpf_num_possible_cpus.fcpu = internal global i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i32 0, i32 0)
@.str = [33 x i8] c"/sys/devices/system/cpu/possible\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"Failed to open %s: '%s'!\0A\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"%u-%u\00"
@.str.4 = [37 x i8] c"Failed to retrieve # possible CPUs!\0A\00"
define i32 @bpf_num_possible_cpus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32*
%6 = alloca i32
store i32 0, i32* %3
%7 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %17, label %11
11: ; preds = %0
%12 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
br label %18
18: ; preds = %17
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%20 = load i32*, i32** %5
%21 = call i64 @fgets(i8* %19, i32 128, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %46
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %1, i32* %2)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* %1
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %31
br label %37
37: ; preds = %36
%38 = load i32, i32* %1
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* %2
%42 = add i32 %41, 1
br label %44
43: ; preds = %37
br label %44
44: ; preds = %43, %40
%45 = phi i32 [ %42, %40 ], [ 0, %43 ]
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %18
%47 = load i32*, i32** %5
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/devices/system/cpu/possible\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"Failed to open %s: '%s'!\0A\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"%u-%u\00"
@str = [36 x i8] c"Failed to retrieve # possible CPUs!\00"
define i32 @bpf_num_possible_cpus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i64 @fgets(i8* %6, i32 128, i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %14
%18 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %1, i32* %2)
switch i32 %18, label %19 [
i32 0, label %22
i32 1, label %25
]
19: ; preds = %17
%20 = load i32, i32* %1
%21 = load i32, i32* %2
br label %27
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = load i32, i32* %1
store i32 %26, i32* %2
br label %27
27: ; preds = %19, %25
%28 = phi i32 [ %21, %19 ], [ %26, %25 ]
%29 = phi i32 [ %20, %19 ], [ %26, %25 ]
%30 = icmp eq i32 %29, 0
%31 = add i32 %28, 1
%32 = select i1 %30, i32 %31, i32 0
br label %33
33: ; preds = %27, %14
%34 = phi i32 [ %32, %27 ], [ 0, %14 ]
%35 = call i32 @fclose(i32* %7)
ret i32 %34
}