#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;
  fp = fopen(fcpu, "r");
  if (!fp) {
    printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
    exit(1);
  }
  while (fgets(buff, sizeof(buff), fp)) {
    if (sscanf(buff, "%u-%u", &start, &end) == 2) {
      possible_cpus = start == 0 ? end + 1 : 0;
      break;
    }
  }
  fclose(fp);
  if (!possible_cpus) {
    printf("Failed to retrieve # possible CPUs!\n");
    exit(1);
  }
  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,-0xa0(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xa0(%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     126e <bpf_num_possible_cpus+0xfe>
lea    -0x90(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    -0x4(%rbp),%rdx
lea    -0x8(%rbp),%rcx
call   1060 <sscanf@plt>
cmp    $0x2,%eax
jne    1269 <bpf_num_possible_cpus+0xf9>
cmpl   $0x0,-0x4(%rbp)
jne    124e <bpf_num_possible_cpus+0xde>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    125b <bpf_num_possible_cpus+0xeb>
xor    %eax,%eax
mov    %eax,-0xa4(%rbp)
jmp    125b <bpf_num_possible_cpus+0xeb>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    126e <bpf_num_possible_cpus+0xfe>
jmp    11ed <bpf_num_possible_cpus+0x7d>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0xc(%rbp)
jne    129c <bpf_num_possible_cpus+0x12c>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_num_possible_cpus>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x90,%rsp
lea    0xe69(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1246 <bpf_num_possible_cpus+0xc6>
mov    %rax,%r14
lea    0x10(%rsp),%rbx
lea    0xe82(%rip),%r15
lea    0xc(%rsp),%r12
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1228 <bpf_num_possible_cpus+0xa8>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1070 <sscanf@plt>
cmp    $0x2,%eax
jne    11d0 <bpf_num_possible_cpus+0x50>
cmpl   $0x0,0xc(%rsp)
jne    1228 <bpf_num_possible_cpus+0xa8>
mov    0x8(%rsp),%ebx
add    $0x1,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %ebx,%ebx
je     1230 <bpf_num_possible_cpus+0xb0>
mov    %ebx,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc8(%rip),%rdi
lea    0xd9e(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@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*
store i32 0, i32* %3
%6 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %0
%11 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %35, %16
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %5
%20 = call i64 @fgets(i8* %18, i32 128, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%24 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %1, i32* %2)
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i32, i32* %1
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %2
%31 = add i32 %30, 1
br label %33
32: ; preds = %26
br label %33
33: ; preds = %32, %29
%34 = phi i32 [ %31, %29 ], [ 0, %32 ]
store i32 %34, i32* %3
br label %36
35: ; preds = %22
br label %17
36: ; preds = %33, %17
%37 = load i32*, i32** %5
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %3
%40 = icmp ne i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %36
%45 = load i32, i32* %3
ret i32 %45
}

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, %17
%15 = call i64 @fgets(i8* %6, i32 128, i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, 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)
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %14
20: ; preds = %17
%21 = load i32, i32* %1
%22 = icmp eq i32 %21, 0
%23 = load i32, i32* %2
%24 = add i32 %23, 1
br i1 %22, label %27, label %25
25: ; preds = %14, %20
%26 = call i32 @fclose(i32* %7)
br label %30
27: ; preds = %20
%28 = call i32 @fclose(i32* %7)
%29 = icmp eq i32 %24, 0
br i1 %29, label %30, label %33
30: ; preds = %25, %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
ret i32 %24
}