#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int sscanf(char *, char *, unsigned int *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
scalar_t__ strstr(char *, char *);
unsigned int count_cpus(void) {
  FILE *fp;
  char value[10];
  unsigned int ret = 0;
  unsigned int cpunr = 0;
  fp = fopen("/proc/stat", "r");
  if (!fp) {
    printf("Couldn't count the number of CPUs (%s: %s), assuming 1\n",
           "/proc/stat", strerror(errno));
    return 1;
  }
  while (!feof(fp)) {
    if (!fgets(value, 10, fp))
      continue;
    value[10 - 1] = '\0';
    if (strlen(value) < (10 - 2))
      continue;
    if (strstr(value, "cpu "))
      continue;
    if (sscanf(value, "cpu%d ", &cpunr) != 1)
      continue;
    if (cpunr > ret)
      ret = cpunr;
  }
  fclose(fp);
  return (ret + 1);
}


x86 assembly (clang, -O0):
<count_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe53(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f8 <count_cpus+0x68>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdx
lea    0xe2d(%rip),%rdi
lea    0xe19(%rip),%rsi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c0 <count_cpus+0x130>
jmp    11fd <count_cpus+0x6d>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121b <count_cpus+0x8b>
jmp    12ae <count_cpus+0x11e>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    123b <count_cpus+0xab>
jmp    11fd <count_cpus+0x6d>
movb   $0x0,-0x11(%rbp)
lea    -0x1a(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x8,%eax
jge    1256 <count_cpus+0xc6>
jmp    11fd <count_cpus+0x6d>
lea    -0x1a(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1275 <count_cpus+0xe5>
jmp    11fd <count_cpus+0x6d>
lea    -0x1a(%rbp),%rdi
lea    0xdca(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     1297 <count_cpus+0x107>
jmp    11fd <count_cpus+0x6d>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jbe    12a9 <count_cpus+0x119>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11fd <count_cpus+0x6d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_cpus>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x8(%rsp)
lea    0xe53(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11de <count_cpus+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     121a <count_cpus+0x8a>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x1,%r14d
jmp    1208 <count_cpus+0x78>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe1a(%rip),%rdi
lea    0xe06(%rip),%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
lea    0xe(%rsp),%rbp
lea    0xe1c(%rip),%r15
lea    0xe1a(%rip),%r12
lea    0x8(%rsp),%r13
jmp    124c <count_cpus+0xbc>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11d0 <count_cpus+0x40>
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %eax,%eax
je     1240 <count_cpus+0xb0>
movb   $0x0,0x17(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x8,%eax
jl     1240 <count_cpus+0xb0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
jne    1240 <count_cpus+0xb0>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    1240 <count_cpus+0xb0>
mov    0x8(%rsp),%eax
cmp    %r14d,%eax
cmova  %eax,%r14d
jmp    1240 <count_cpus+0xb0>

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [56 x i8] c"Couldn't count the number of CPUs (%s: %s), assuming 1\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"cpu \00"
@.str.4 = [7 x i8] c"cpu%d \00"
define i32 @count_cpus() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %4
store i32 0, i32* %5
%6 = 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* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 1, i32* %1
br label %53
13: ; preds = %0
br label %14
14: ; preds = %47, %40, %35, %30, %24, %13
%15 = load i32*, i32** %2
%16 = call i32 @feof(i32* %15)
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %48
19: ; preds = %14
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%21 = load i32*, i32** %2
%22 = call i32 @fgets(i8* %20, i32 10, i32* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %19
br label %14
25: ; preds = %19
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 9
store i8 0, i8* %26
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = icmp slt i32 %28, 8
br i1 %29, label %30, label %31
30: ; preds = %25
br label %14
31: ; preds = %25
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%33 = call i64 @strstr(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %14
36: ; preds = %31
%37 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %36
br label %14
41: ; preds = %36
%42 = load i32, i32* %5
%43 = load i32, i32* %4
%44 = icmp ugt i32 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i32, i32* %5
store i32 %46, i32* %4
br label %47
47: ; preds = %45, %41
br label %14
48: ; preds = %14
%49 = load i32*, i32** %2
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %4
%52 = add i32 %51, 1
store i32 %52, i32* %1
br label %53
53: ; preds = %48, %9
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [56 x i8] c"Couldn't count the number of CPUs (%s: %s), assuming 1\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"cpu \00"
@.str.4 = [7 x i8] c"cpu%d \00"
define i32 @count_cpus() {
%1 = alloca [10 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%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 %12, label %7
7: ; preds = %0
%8 = call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %39
10: ; preds = %7
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 9
br label %37
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %43
16: ; preds = %37, %19
%17 = call i32 @fgets(i8* %3, i32 10, i32* %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16, %22, %25, %28
%20 = call i32 @feof(i32* %5)
%21 = icmp eq i32 %20, 0
br i1 %21, label %16, label %39
22: ; preds = %16
store i8 0, i8* %11
%23 = call i32 @strlen(i8* %3)
%24 = icmp slt i32 %23, 8
br i1 %24, label %19, label %25
25: ; preds = %22
%26 = call i64 @strstr(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %19
28: ; preds = %25
%29 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %19
31: ; preds = %28
%32 = load i32, i32* %2
%33 = icmp ugt i32 %32, %38
%34 = select i1 %33, i32 %32, i32 %38
%35 = call i32 @feof(i32* %5)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %10, %31
%38 = phi i32 [ 0, %10 ], [ %34, %31 ]
br label %16
39: ; preds = %31, %19, %7
%40 = phi i32 [ 0, %7 ], [ %38, %19 ], [ %34, %31 ]
%41 = call i32 @fclose(i32* %5)
%42 = add i32 %40, 1
br label %43
43: ; preds = %39, %12
%44 = phi i32 [ %42, %39 ], [ 1, %12 ]
ret i32 %44
}