#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 */
scalar_t__ calloc(int, size_t const);
size_t fread(char *, int, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int sscanf(char *, char *, unsigned long *, unsigned long *);
char *strchr(char *, char);
char *strtok_r(char *, char *, char **);
long _get_ncpus_sys_devices(FILE *online_cpus_fp) {
  const size_t cpumask_buffer_size = 19370;
  char *cpumask_buffer = (char *)calloc(1, cpumask_buffer_size);
  if (cpumask_buffer == NULL) {
    return -1;
  }
  size_t read_bytes =
      fread(cpumask_buffer, 1, cpumask_buffer_size - 1, online_cpus_fp);
  if (read_bytes == 0) {
    free(cpumask_buffer);
    return -1;
  }
  long ncpus = 0;
  char *saveptr = NULL;
  char *until_token = strtok_r(cpumask_buffer, ",", &saveptr);
  while (until_token != NULL) {
    char *dash_position = strchr(until_token, '-');
    if (dash_position != NULL) {
      unsigned long first_cpu;
      unsigned long last_cpu;
      if (sscanf(until_token, "%lu-%lu", &first_cpu, &last_cpu) != 2) {
        free(cpumask_buffer);
        return -1;
      }
      ncpus += last_cpu - first_cpu + 1;
    } else {
      ncpus++;
    }
    until_token = strtok_r(NULL, ",", &saveptr);
  }
  free(cpumask_buffer);
  return ncpus;
}


x86 assembly (clang, -O0):
<_get_ncpus_sys_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x4baa,-0x18(%rbp)
mov    $0x1,%edi
mov    $0x4baa,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119f <_get_ncpus_sys_devices+0x3f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12b5 <_get_ncpus_sys_devices+0x155>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4ba9,%edx
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11db <_get_ncpus_sys_devices+0x7b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12b5 <_get_ncpus_sys_devices+0x155>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe0a(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12a4 <_get_ncpus_sys_devices+0x144>
mov    -0x40(%rbp),%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     127b <_get_ncpus_sys_devices+0x11b>
mov    -0x40(%rbp),%rdi
lea    0xdcc(%rip),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
je     1262 <_get_ncpus_sys_devices+0x102>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12b5 <_get_ncpus_sys_devices+0x155>
mov    -0x58(%rbp),%rax
sub    -0x50(%rbp),%rax
add    $0x1,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1287 <_get_ncpus_sys_devices+0x127>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd6e(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
jmp    1203 <_get_ncpus_sys_devices+0xa3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_ncpus_sys_devices>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
mov    $0x4baa,%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
je     124d <_get_ncpus_sys_devices+0xed>
mov    %rax,%r14
mov    $0x4ba9,%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1245 <_get_ncpus_sys_devices+0xe5>
movq   $0x0,(%rsp)
lea    0xe40(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1243 <_get_ncpus_sys_devices+0xe3>
mov    %rax,%rbp
xor    %ebx,%ebx
lea    0xe24(%rip),%r15
mov    %rsp,%r12
lea    0x8(%rsp),%r13
jmp    1209 <_get_ncpus_sys_devices+0xa9>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <strtok_r@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1245 <_get_ncpus_sys_devices+0xe5>
mov    %rbp,%rdi
mov    $0x2d,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11f0 <_get_ncpus_sys_devices+0x90>
mov    %rbp,%rdi
lea    0xddd(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %r13,%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jne    125f <_get_ncpus_sys_devices+0xff>
add    0x8(%rsp),%rbx
sub    0x10(%rsp),%rbx
jmp    11f0 <_get_ncpus_sys_devices+0x90>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rbx
jmp    124d <_get_ncpus_sys_devices+0xed>

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
@.str.1 = [8 x i8] c"%lu-%lu\00"
define i64 @_get_ncpus_sys_devices(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i32* %0, i32** %3
store i64 19370, i64* %4
%13 = call i64 @calloc(i32 1, i64 19370)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
store i64 -1, i64* %2
br label %61
18: ; preds = %1
%19 = load i8*, i8** %5
%20 = load i32*, i32** %3
%21 = call i64 @fread(i8* %19, i32 1, i64 19369, i32* %20)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
store i64 -1, i64* %2
br label %61
27: ; preds = %18
store i64 0, i64* %7
store i8* null, i8** %8
%28 = load i8*, i8** %5
%29 = call i8* @strtok_r(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %8)
store i8* %29, i8** %9
br label %30
30: ; preds = %55, %27
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %57
33: ; preds = %30
%34 = load i8*, i8** %9
%35 = call i8* @strchr(i8* %34, i8 signext 45)
store i8* %35, i8** %10
%36 = load i8*, i8** %10
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %52
38: ; preds = %33
%39 = load i8*, i8** %9
%40 = call i32 @sscanf(i8* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64* %11, i64* %12)
%41 = icmp ne i32 %40, 2
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = call i32 @free(i8* %43)
store i64 -1, i64* %2
br label %61
45: ; preds = %38
%46 = load i64, i64* %12
%47 = load i64, i64* %11
%48 = sub i64 %46, %47
%49 = add i64 %48, 1
%50 = load i64, i64* %7
%51 = add i64 %50, %49
store i64 %51, i64* %7
br label %55
52: ; preds = %33
%53 = load i64, i64* %7
%54 = add nsw i64 %53, 1
store i64 %54, i64* %7
br label %55
55: ; preds = %52, %45
%56 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %8)
store i8* %56, i8** %9
br label %30
57: ; preds = %30
%58 = load i8*, i8** %5
%59 = call i32 @free(i8* %58)
%60 = load i64, i64* %7
store i64 %60, i64* %2
br label %61
61: ; preds = %57, %42, %24, %17
%62 = load i64, i64* %2
ret i64 %62
}

LLVM IR (-O2):
@.str = [2 x i8] c",\00"
@.str.1 = [8 x i8] c"%lu-%lu\00"
define i64 @_get_ncpus_sys_devices(i32* nocapture %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = tail call i64 @calloc(i32 1, i64 19370)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %47, label %8
8: ; preds = %1
%9 = tail call i64 @fread(i8* %6, i32 1, i64 19369, i32* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @free(i8* %6)
br label %47
13: ; preds = %8
%14 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%15 = call i8* @strtok_r(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%16 = icmp eq i8* %15, null
br i1 %16, label %42, label %17
17: ; preds = %13
%18 = bitcast i64* %3 to i8*
%19 = bitcast i64* %4 to i8*
br label %20
20: ; preds = %17, %38
%21 = phi i8* [ %15, %17 ], [ %40, %38 ]
%22 = phi i64 [ 0, %17 ], [ %39, %38 ]
%23 = call i8* @strchr(i8* %21, i8 signext 45)
%24 = icmp eq i8* %23, null
br i1 %24, label %36, label %25
25: ; preds = %20
%26 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64* %3, i64* %4)
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* %4
%30 = load i64, i64* %3
%31 = add i64 %22, 1
%32 = add i64 %31, %29
%33 = sub i64 %32, %30
br label %38
34: ; preds = %25
%35 = call i32 @free(i8* %6)
br label %45
36: ; preds = %20
%37 = add nsw i64 %22, 1
br label %38
38: ; preds = %36, %28
%39 = phi i64 [ %37, %36 ], [ %33, %28 ]
%40 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %20
42: ; preds = %38, %13
%43 = phi i64 [ 0, %13 ], [ %39, %38 ]
%44 = call i32 @free(i8* %6)
br label %45
45: ; preds = %34, %42
%46 = phi i64 [ %43, %42 ], [ -1, %34 ]
br label %47
47: ; preds = %11, %45, %1
%48 = phi i64 [ -1, %1 ], [ -1, %11 ], [ %46, %45 ]
ret i64 %48
}