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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ strncmp(char *, char *, int);
int getNumCPUs() {
  FILE *fp;
  char *line = NULL;
  size_t dum;
  ssize_t err;
  int count = 0;
  fp = fopen("/proc/stat", "r");
  if (!fp)
    return 0;
  while ((err = getline(&line, &dum, fp)) != -1) {
    if (strncmp(line, "cpu", 3)) {
      free(line);
      break;
    }
    if (line[3] >= '0' && line[3] <= '9')
      count++;
    free(line);
    line = NULL;
  }
  fclose(fp);
  return count;
}


x86 assembly (clang, -O0):
<getNumCPUs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0xe92(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <getNumCPUs+0x45>
movl   $0x0,-0x4(%rbp)
jmp    1234 <getNumCPUs+0xe4>
jmp    119a <getNumCPUs+0x4a>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1225 <getNumCPUs+0xd5>
mov    -0x18(%rbp),%rdi
lea    0xe4b(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11e4 <getNumCPUs+0x94>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    1225 <getNumCPUs+0xd5>
mov    -0x18(%rbp),%rax
movsbl 0x3(%rax),%eax
cmp    $0x30,%eax
jl     120f <getNumCPUs+0xbf>
mov    -0x18(%rbp),%rax
movsbl 0x3(%rax),%eax
cmp    $0x39,%eax
jg     120f <getNumCPUs+0xbf>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    119a <getNumCPUs+0x4a>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNumCPUs>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
lea    0xe95(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f7 <getNumCPUs+0xa7>
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1070 <getline@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
je     1200 <getNumCPUs+0xb0>
lea    0xe70(%rip),%r14
mov    %rsp,%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsp),%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
mov    (%rsp),%rdi
test   %rax,%rax
jne    11fb <getNumCPUs+0xab>
movzbl 0x3(%rdi),%eax
add    $0xd0,%al
cmp    $0xa,%al
adc    $0x0,%ebp
call   1030 <free@plt>
movq   $0x0,(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
jne    11b0 <getNumCPUs+0x60>
jmp    1200 <getNumCPUs+0xb0>
xor    %ebp,%ebp
jmp    1208 <getNumCPUs+0xb8>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"cpu\00"
define i32 @getNumCPUs() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* null, i8** %3
store i32 0, i32* %6
%7 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %0
store i32 0, i32* %1
br label %45
11: ; preds = %0
br label %12
12: ; preds = %38, %11
%13 = load i32*, i32** %2
%14 = call i32 @getline(i8** %3, i64* %4, i32* %13)
store i32 %14, i32* %5
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %41
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = call i32 @free(i8* %21)
br label %41
23: ; preds = %16
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp sge i32 %27, 48
br i1 %28, label %29, label %38
29: ; preds = %23
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 3
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp sle i32 %33, 57
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %38
38: ; preds = %35, %29, %23
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
store i8* null, i8** %3
br label %12
41: ; preds = %20, %12
%42 = load i32*, i32** %2
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %6
store i32 %44, i32* %1
br label %45
45: ; preds = %41, %10
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"cpu\00"
define i32 @getNumCPUs() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = bitcast i64* %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %31, label %7
7: ; preds = %0
%8 = call i32 @getline(i8** %1, i64* %2, i32* %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %28, label %10
10: ; preds = %7, %18
%11 = phi i32 [ %24, %18 ], [ 0, %7 ]
%12 = load i8*, i8** %1
%13 = call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%14 = icmp eq i64 %13, 0
%15 = load i8*, i8** %1
br i1 %14, label %18, label %16
16: ; preds = %10
%17 = call i32 @free(i8* %15)
br label %28
18: ; preds = %10
%19 = getelementptr inbounds i8, i8* %15, i64 3
%20 = load i8, i8* %19
%21 = add i8 %20, -48
%22 = icmp ult i8 %21, 10
%23 = zext i1 %22 to i32
%24 = add nuw nsw i32 %11, %23
%25 = call i32 @free(i8* %15)
store i8* null, i8** %1
%26 = call i32 @getline(i8** %1, i64* %2, i32* %5)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %10
28: ; preds = %18, %7, %16
%29 = phi i32 [ %11, %16 ], [ 0, %7 ], [ %24, %18 ]
%30 = call i32 @fclose(i32* %5)
br label %31
31: ; preds = %0, %28
%32 = phi i32 [ %29, %28 ], [ 0, %0 ]
ret i32 %32
}