#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*/ *);
int /*<<< orphan*/ 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*/);
int strlen(char *);
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 len, n, i, j = 0;
  fp = fopen(fcpu, "r");
  if (!fp) {
    printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
    exit(1);
  }
  if (!fgets(buff, sizeof(buff), fp)) {
    printf("Failed to read %s!\n", fcpu);
    exit(1);
  }
  len = strlen(buff);
  for (i = 0; i <= len; i++) {
    if (buff[i] == ',' || buff[i] == '\0') {
      buff[i] = '\0';
      n = sscanf(&buff[j], "%u-%u", &start, &end);
      if (n <= 0) {
        printf("Failed to retrieve # possible CPUs!\n");
        exit(1);
      } else if (n == 1) {
        end = start;
      }
      possible_cpus += end - start + 1;
      j = i + 1;
    }
  }
  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)
movl   $0x0,-0xa8(%rbp)
mov    0x2ebd(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    1202 <bpf_num_possible_cpus+0x82>
mov    0x2e95(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xb0(%rbp),%rsi
mov    %rax,%rdx
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    1242 <bpf_num_possible_cpus+0xc2>
mov    0x2e36(%rip),%rsi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x9c(%rbp)
movl   $0x0,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0x9c(%rbp),%eax
jg     134e <bpf_num_possible_cpus+0x1ce>
movslq -0xa4(%rbp),%rax
movsbl -0x90(%rbp,%rax,1),%eax
ff
cmp    $0x2c,%eax
je     12a0 <bpf_num_possible_cpus+0x120>
movslq -0xa4(%rbp),%rax
movsbl -0x90(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
jne    1335 <bpf_num_possible_cpus+0x1b5>
movslq -0xa4(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
movslq -0xa8(%rbp),%rax
lea    -0x90(%rbp),%rdi
add    %rax,%rdi
lea    0xd8a(%rip),%rsi
lea    -0x4(%rbp),%rdx
lea    -0x8(%rbp),%rcx
call   1070 <sscanf@plt>
mov    %eax,-0xa0(%rbp)
cmpl   $0x0,-0xa0(%rbp)
jg     12ff <bpf_num_possible_cpus+0x17f>
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x1,-0xa0(%rbp)
jne    1312 <bpf_num_possible_cpus+0x192>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1317 <bpf_num_possible_cpus+0x197>
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
add    $0x1,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    133a <bpf_num_possible_cpus+0x1ba>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    125e <bpf_num_possible_cpus+0xde>
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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xe58(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12b2 <bpf_num_possible_cpus+0x122>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     12e2 <bpf_num_possible_cpus+0x152>
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
test   %eax,%eax
js     127c <bpf_num_possible_cpus+0xec>
mov    %eax,%ebx
add    $0x1,%ebx
xor    %r14d,%r14d
lea    0xe51(%rip),%r15
lea    0x4(%rsp),%r12
mov    %rsp,%r13
xor    %eax,%eax
xor    %ebp,%ebp
jmp    1219 <bpf_num_possible_cpus+0x89>
xchg   %ax,%ax
add    $0x1,%r14
cmp    %rbx,%r14
je     127e <bpf_num_possible_cpus+0xee>
movzbl 0x10(%rsp,%r14,1),%ecx
test   %cl,%cl
je     1228 <bpf_num_possible_cpus+0x98>
cmp    $0x2c,%cl
jne    1210 <bpf_num_possible_cpus+0x80>
movb   $0x0,0x10(%rsp,%r14,1)
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1080 <sscanf@plt>
test   %eax,%eax
jle    129c <bpf_num_possible_cpus+0x10c>
cmp    $0x1,%eax
jne    1260 <bpf_num_possible_cpus+0xd0>
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %eax,%ecx
jmp    1267 <bpf_num_possible_cpus+0xd7>
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%ecx
mov    0x4(%rsp),%eax
add    %ecx,%ebp
sub    %eax,%ebp
add    $0x1,%ebp
add    $0x1,%r14
mov    %r14d,%eax
cmp    %rbx,%r14
jne    1219 <bpf_num_possible_cpus+0x89>
jmp    127e <bpf_num_possible_cpus+0xee>
xor    %ebp,%ebp
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd5c(%rip),%rdi
lea    0xd32(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd54(%rip),%rdi
lea    0xd10(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <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 = [20 x i8] c"Failed to read %s!\0A\00"
@.str.4 = [6 x i8] c"%u-%u\00"
@.str.5 = [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
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 0, i32* %9
%10 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %20, label %14
14: ; preds = %0
%15 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%22 = load i32*, i32** %5
%23 = call i32 @fgets(i8* %21, i32 128, i32* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i8*, i8** @bpf_num_possible_cpus.fcpu
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
store i32 %31, i32* %6
store i32 0, i32* %8
br label %32
32: ; preds = %79, %29
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = icmp sle i32 %33, %34
br i1 %35, label %36, label %82
36: ; preds = %32
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 44
br i1 %42, label %50, label %43
43: ; preds = %36
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %78
50: ; preds = %43, %36
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %52
store i8 0, i8* %53
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %55
%57 = call i32 @sscanf(i8* %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %1, i32* %2)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp sle i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %50
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %50
%64 = load i32, i32* %7
%65 = icmp eq i32 %64, 1
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = load i32, i32* %1
store i32 %67, i32* %2
br label %68
68: ; preds = %66, %63
br label %69
69: ; preds = %68
%70 = load i32, i32* %2
%71 = load i32, i32* %1
%72 = sub i32 %70, %71
%73 = add i32 %72, 1
%74 = load i32, i32* %3
%75 = add i32 %74, %73
store i32 %75, i32* %3
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %9
br label %78
78: ; preds = %69, %43
br label %79
79: ; preds = %78
%80 = load i32, i32* %8
%81 = add nsw i32 %80, 1
store i32 %81, i32* %8
br label %32
82: ; preds = %32
%83 = load i32*, i32** %5
%84 = call i32 @fclose(i32* %83)
%85 = load i32, i32* %3
ret i32 %85
}

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 = [20 x i8] c"Failed to read %s!\0A\00"
@.str.4 = [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 i32 @fgets(i8* %6, i32 128, i32* %7)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = call i32 @strlen(i8* %6)
%22 = icmp slt i32 %21, 0
br i1 %22, label %62, label %23
23: ; preds = %20
%24 = add nuw i32 %21, 1
%25 = zext i32 %24 to i64
br label %26
26: ; preds = %23, %57
%27 = phi i64 [ 0, %23 ], [ %58, %57 ]
%28 = phi i32 [ 0, %23 ], [ %60, %57 ]
%29 = phi i32 [ 0, %23 ], [ %59, %57 ]
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %27
%31 = load i8, i8* %30
switch i8 %31, label %32 [
i8 44, label %34
i8 0, label %34
]
32: ; preds = %26
%33 = add nuw nsw i64 %27, 1
br label %57
34: ; preds = %26, %26
store i8 0, i8* %30
%35 = sext i32 %28 to i64
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %35
%37 = call i32 @sscanf(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %1, i32* %2)
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
%43 = icmp eq i32 %37, 1
br i1 %43, label %47, label %44
44: ; preds = %42
%45 = load i32, i32* %2
%46 = load i32, i32* %1
br label %49
47: ; preds = %42
%48 = load i32, i32* %1
store i32 %48, i32* %2
br label %49
49: ; preds = %44, %47
%50 = phi i32 [ %46, %44 ], [ %48, %47 ]
%51 = phi i32 [ %45, %44 ], [ %48, %47 ]
%52 = add i32 %29, 1
%53 = add i32 %52, %51
%54 = sub i32 %53, %50
%55 = add nuw nsw i64 %27, 1
%56 = trunc i64 %55 to i32
br label %57
57: ; preds = %32, %49
%58 = phi i64 [ %33, %32 ], [ %55, %49 ]
%59 = phi i32 [ %29, %32 ], [ %54, %49 ]
%60 = phi i32 [ %28, %32 ], [ %56, %49 ]
%61 = icmp eq i64 %58, %25
br i1 %61, label %62, label %26
62: ; preds = %57, %20
%63 = phi i32 [ 0, %20 ], [ %59, %57 ]
%64 = call i32 @fclose(i32* %7)
ret i32 %63
}