#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 *);
char *gettext(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(gettext("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    0xe43(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1215 <count_cpus+0x75>
lea    0xe2e(%rip),%rdi
call   1050 <gettext@plt>
mov    %rax,-0x30(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
lea    0xdfc(%rip),%rsi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <count_cpus+0x13d>
jmp    121a <count_cpus+0x7a>
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1238 <count_cpus+0x98>
jmp    12cb <count_cpus+0x12b>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    1258 <count_cpus+0xb8>
jmp    121a <count_cpus+0x7a>
movb   $0x0,-0x11(%rbp)
lea    -0x1a(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x8,%eax
jge    1273 <count_cpus+0xd3>
jmp    121a <count_cpus+0x7a>
lea    -0x1a(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     1292 <count_cpus+0xf2>
jmp    121a <count_cpus+0x7a>
lea    -0x1a(%rbp),%rdi
lea    0xdad(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     12b4 <count_cpus+0x114>
jmp    121a <count_cpus+0x7a>
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jbe    12c6 <count_cpus+0x126>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    121a <count_cpus+0x7a>
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    0xe43(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11ee <count_cpus+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     1235 <count_cpus+0x95>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x1,%r14d
jmp    1223 <count_cpus+0x83>
lea    0xe18(%rip),%rdi
call   1050 <gettext@plt>
mov    %rax,%rbx
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdee(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1060 <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    0xe01(%rip),%r15
lea    0xdff(%rip),%r12
lea    0x8(%rsp),%r13
jmp    1270 <count_cpus+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11e0 <count_cpus+0x40>
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     1260 <count_cpus+0xc0>
movb   $0x0,0x17(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x8,%eax
jl     1260 <count_cpus+0xc0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
jne    1260 <count_cpus+0xc0>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1260 <count_cpus+0xc0>
mov    0x8(%rsp),%eax
cmp    %r14d,%eax
cmova  %eax,%r14d
jmp    1260 <count_cpus+0xc0>

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 %14, label %9
9: ; preds = %0
%10 = call i8* @gettext(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @printf(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 1, i32* %1
br label %54
14: ; preds = %0
br label %15
15: ; preds = %48, %41, %36, %31, %25, %14
%16 = load i32*, i32** %2
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %49
20: ; preds = %15
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%22 = load i32*, i32** %2
%23 = call i32 @fgets(i8* %21, i32 10, i32* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %20
br label %15
26: ; preds = %20
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 9
store i8 0, i8* %27
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = icmp slt i32 %29, 8
br i1 %30, label %31, label %32
31: ; preds = %26
br label %15
32: ; preds = %26
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%34 = call i64 @strstr(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
br label %15
37: ; preds = %32
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%39 = call i32 @sscanf(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %37
br label %15
42: ; preds = %37
%43 = load i32, i32* %5
%44 = load i32, i32* %4
%45 = icmp ugt i32 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = load i32, i32* %5
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %42
br label %15
49: ; preds = %15
%50 = load i32*, i32** %2
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %4
%53 = add i32 %52, 1
store i32 %53, i32* %1
br label %54
54: ; preds = %49, %9
%55 = load i32, i32* %1
ret i32 %55
}

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