#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)) {
    fgets(value, 10, fp);
    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    12b2 <count_cpus+0x122>
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    12a0 <count_cpus+0x110>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1060 <fgets@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x1a(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x8,%eax
jge    1248 <count_cpus+0xb8>
jmp    11fd <count_cpus+0x6d>
lea    -0x1a(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1267 <count_cpus+0xd7>
jmp    11fd <count_cpus+0x6d>
lea    -0x1a(%rbp),%rdi
lea    0xdd8(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     1289 <count_cpus+0xf9>
jmp    11fd <count_cpus+0x6d>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jbe    129b <count_cpus+0x10b>
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>
movb   $0x0,0x17(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x7,%eax
jle    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 %50
13: ; preds = %0
br label %14
14: ; preds = %44, %37, %32, %27, %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 %45
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 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 9
store i8 0, i8* %23
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = icmp slt i32 %25, 8
br i1 %26, label %27, label %28
27: ; preds = %19
br label %14
28: ; preds = %19
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%30 = call i64 @strstr(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %14
33: ; preds = %28
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %33
br label %14
38: ; preds = %33
%39 = load i32, i32* %5
%40 = load i32, i32* %4
%41 = icmp ugt i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = load i32, i32* %5
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %38
br label %14
45: ; preds = %14
%46 = load i32*, i32** %2
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %4
%49 = add i32 %48, 1
store i32 %49, i32* %1
br label %50
50: ; preds = %45, %9
%51 = load i32, i32* %1
ret i32 %51
}

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 %37
10: ; preds = %7
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 9
br label %35
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 %41
16: ; preds = %35, %20
%17 = call i32 @fgets(i8* %3, i32 10, i32* %5)
store i8 0, i8* %11
%18 = call i32 @strlen(i8* %3)
%19 = icmp slt i32 %18, 8
br i1 %19, label %20, label %23
20: ; preds = %16, %23, %26
%21 = call i32 @feof(i32* %5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %16, label %37
23: ; preds = %16
%24 = call i64 @strstr(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %20
26: ; preds = %23
%27 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %20
29: ; preds = %26
%30 = load i32, i32* %2
%31 = icmp ugt i32 %30, %36
%32 = select i1 %31, i32 %30, i32 %36
%33 = call i32 @feof(i32* %5)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %10, %29
%36 = phi i32 [ 0, %10 ], [ %32, %29 ]
br label %16
37: ; preds = %29, %20, %7
%38 = phi i32 [ 0, %7 ], [ %36, %20 ], [ %32, %29 ]
%39 = call i32 @fclose(i32* %5)
%40 = add i32 %38, 1
br label %41
41: ; preds = %37, %12
%42 = phi i32 [ %40, %37 ], [ 1, %12 ]
ret i32 %42
}