#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, char *);
int /*<<< orphan*/ printf(char *);
int parse_cpucount_sysfs() {
  const char online_filepath[] = "/sys/devices/system/cpu/online";
  int prev = -1;
  int num_cores = 0;
  FILE *file = fopen(online_filepath, "r");
  if (!file) {
    printf("You got problems son. Most likely, you're missing an important "
           "scheduling module.\n");
    return 0;
  }
  for (;;) {
    char sep;
    int cpu;
    int n = fscanf(file, "%u%c", &cpu, &sep);
    if (n <= 0)
      break;
    if (n == 1)
      sep = '\n';
    if (prev >= 0) {
      for (int i = prev; i <= cpu; ++i) {
        ++num_cores;
      }
      prev = -1;
    } else if (sep == '-') {
      prev = cpu;
    } else {
      ++num_cores;
    }
    if (sep == '\n')
      break;
  }
  fclose(file);
  return num_cores;
}


x86 assembly (clang, -O0):
<parse_cpucount_sysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xeb1(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeae(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeab(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xea9(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xea3(%rip),%ax
mov    %ax,-0x14(%rbp)
mov    0xe9b(%rip),%al
mov    %al,-0x12(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11cb <parse_cpucount_sysfs+0x8b>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1293 <parse_cpucount_sysfs+0x153>
jmp    11d0 <parse_cpucount_sysfs+0x90>
mov    -0x40(%rbp),%rdi
lea    0xe99(%rip),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x41(%rbp),%rcx
call   1050 <fscanf@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jg     11fa <parse_cpucount_sysfs+0xba>
jmp    1284 <parse_cpucount_sysfs+0x144>
cmpl   $0x1,-0x4c(%rbp)
jne    1208 <parse_cpucount_sysfs+0xc8>
movb   $0xa,-0x41(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     1247 <parse_cpucount_sysfs+0x107>
mov    -0x34(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x48(%rbp),%eax
jg     123b <parse_cpucount_sysfs+0xfb>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1218 <parse_cpucount_sysfs+0xd8>
movl   $0xffffffff,-0x34(%rbp)
jmp    126d <parse_cpucount_sysfs+0x12d>
movsbl -0x41(%rbp),%eax
cmp    $0x2d,%eax
jne    125f <parse_cpucount_sysfs+0x11f>
mov    -0x48(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    1268 <parse_cpucount_sysfs+0x128>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    126d <parse_cpucount_sysfs+0x12d>
movsbl -0x41(%rbp),%eax
cmp    $0xa,%eax
jne    127f <parse_cpucount_sysfs+0x13f>
jmp    1284 <parse_cpucount_sysfs+0x144>
jmp    11d0 <parse_cpucount_sysfs+0x90>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_cpucount_sysfs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movups 0xeba(%rip),%xmm0
movups %xmm0,0x1f(%rsp)
movaps 0xe9f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xeb2(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     122b <parse_cpucount_sysfs+0xeb>
mov    %rax,%r14
lea    0xe97(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0xb(%rsp),%rcx
mov    %rax,%rdi
call   1050 <fscanf@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    1221 <parse_cpucount_sysfs+0xe1>
mov    $0xffffffff,%ebp
lea    0xe6f(%rip),%r15
lea    0xc(%rsp),%r12
lea    0xb(%rsp),%r13
jmp    11d9 <parse_cpucount_sysfs+0x99>
xchg   %ax,%ax
mov    0xc(%rsp),%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1050 <fscanf@plt>
test   %eax,%eax
jle    1221 <parse_cpucount_sysfs+0xe1>
cmp    $0x1,%eax
jne    11e3 <parse_cpucount_sysfs+0xa3>
movb   $0xa,0xb(%rsp)
test   %ebp,%ebp
jns    1200 <parse_cpucount_sysfs+0xc0>
movzbl 0xb(%rsp),%eax
cmp    $0x2d,%al
je     11c0 <parse_cpucount_sysfs+0x80>
add    $0x1,%ebx
cmp    $0xa,%al
jne    11c4 <parse_cpucount_sysfs+0x84>
jmp    1221 <parse_cpucount_sysfs+0xe1>
nopl   0x0(%rax)
mov    0xc(%rsp),%eax
lea    (%rbx,%rax,1),%ecx
sub    %ebp,%ecx
add    $0x1,%ecx
cmp    %eax,%ebp
cmovg  %ebx,%ecx
mov    $0xffffffff,%ebp
movzbl 0xb(%rsp),%eax
mov    %ecx,%ebx
cmp    $0xa,%al
jne    11c4 <parse_cpucount_sysfs+0x84>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1239 <parse_cpucount_sysfs+0xf9>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.parse_cpucount_sysfs.online_filepath = [31 x i8] c"/sys/devices/system/cpu/online\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [83 x i8] c"You got problems son. Most likely, you're missing an important scheduling module.\0A\00"
@.str.2 = [5 x i8] c"%u%c\00"
define i32 @parse_cpucount_sysfs() {
%1 = alloca i32
%2 = alloca [31 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = bitcast [31 x i8]* %2 to i8*
store i32 -1, i32* %3
store i32 0, i32* %4
%11 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %64
17: ; preds = %0
br label %18
18: ; preds = %59, %17
%19 = load i32*, i32** %5
%20 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %7, i8* %6)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %60
24: ; preds = %18
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %24
store i8 10, i8* %6
br label %28
28: ; preds = %27, %24
%29 = load i32, i32* %3
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %28
%32 = load i32, i32* %3
store i32 %32, i32* %9
br label %33
33: ; preds = %40, %31
%34 = load i32, i32* %9
%35 = load i32, i32* %7
%36 = icmp sle i32 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %4
br label %40
40: ; preds = %37
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %33
43: ; preds = %33
store i32 -1, i32* %3
br label %54
44: ; preds = %28
%45 = load i8, i8* %6
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 45
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %7
store i32 %49, i32* %3
br label %53
50: ; preds = %44
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
br label %53
53: ; preds = %50, %48
br label %54
54: ; preds = %53, %43
%55 = load i8, i8* %6
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 10
br i1 %57, label %58, label %59
58: ; preds = %54
br label %60
59: ; preds = %54
br label %18
60: ; preds = %58, %23
%61 = load i32*, i32** %5
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %4
store i32 %63, i32* %1
br label %64
64: ; preds = %60, %15
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@__const.parse_cpucount_sysfs.online_filepath = [31 x i8] c"/sys/devices/system/cpu/online\00"
@.str = [2 x i8] c"r\00"
@.str.2 = [5 x i8] c"%u%c\00"
@str = [82 x i8] c"You got problems son. Most likely, you're missing an important scheduling module.\00"
define i32 @parse_cpucount_sysfs() {
%1 = alloca [31 x i8]
%2 = alloca i8
%3 = alloca i32
%4 = getelementptr inbounds [31 x i8], [31 x i8]* %1, i64 0, i64 0
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = bitcast i32* %3 to i8*
%9 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %3, i8* %2)
%10 = icmp slt i32 %9, 1
br i1 %10, label %46, label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @str, i64 0, i64 0))
br label %49
13: ; preds = %7, %41
%14 = phi i32 [ %44, %41 ], [ %9, %7 ]
%15 = phi i32 [ %43, %41 ], [ -1, %7 ]
%16 = phi i32 [ %42, %41 ], [ 0, %7 ]
%17 = icmp eq i32 %14, 1
br i1 %17, label %18, label %19
18: ; preds = %13
store i8 10, i8* %2
br label %19
19: ; preds = %18, %13
%20 = icmp sgt i32 %15, -1
br i1 %20, label %21, label %29
21: ; preds = %19
%22 = load i32, i32* %3
%23 = icmp sgt i32 %15, %22
%24 = add i32 %16, 1
%25 = add i32 %24, %22
%26 = sub i32 %25, %15
%27 = select i1 %23, i32 %16, i32 %26
%28 = load i8, i8* %2
br label %36
29: ; preds = %19
%30 = load i8, i8* %2
%31 = icmp eq i8 %30, 45
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* %3
br label %41
34: ; preds = %29
%35 = add nsw i32 %16, 1
br label %36
36: ; preds = %21, %34
%37 = phi i8 [ %28, %21 ], [ %30, %34 ]
%38 = phi i32 [ %27, %21 ], [ %35, %34 ]
%39 = phi i32 [ -1, %21 ], [ %15, %34 ]
%40 = icmp eq i8 %37, 10
br i1 %40, label %46, label %41
41: ; preds = %32, %36
%42 = phi i32 [ %38, %36 ], [ %16, %32 ]
%43 = phi i32 [ %39, %36 ], [ %33, %32 ]
%44 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %3, i8* %2)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %13
46: ; preds = %41, %36, %7
%47 = phi i32 [ 0, %7 ], [ %38, %36 ], [ %42, %41 ]
%48 = call i32 @fclose(i32* %5)
br label %49
49: ; preds = %46, %11
%50 = phi i32 [ %47, %46 ], [ 0, %11 ]
ret i32 %50
}