#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*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int count_cpus() {
  FILE *stat = fopen("/proc/stat", "r");
  if (!stat)
    return -1;
  int cpu_count = 0;
  while (fgetc(stat) != '\n')
    ;
  while (fscanf(stat, "cpu%d", &cpu_count) == 1)
    while (fgetc(stat) != '\n')
      ;
  fclose(stat);
  return cpu_count + 1;
}


x86 assembly (clang, -O0):
<count_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <count_cpus+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <count_cpus+0xaf>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     1197 <count_cpus+0x57>
jmp    117d <count_cpus+0x3d>
jmp    119c <count_cpus+0x5c>
mov    -0x10(%rbp),%rdi
lea    0xe66(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    11dd <count_cpus+0x9d>
jmp    11be <count_cpus+0x7e>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     11d8 <count_cpus+0x98>
jmp    11be <count_cpus+0x7e>
jmp    119c <count_cpus+0x5c>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_cpus>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb0(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e0 <count_cpus+0xa0>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%al
jne    1170 <count_cpus+0x30>
lea    0xe8a(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    11cf <count_cpus+0x8f>
lea    0xe71(%rip),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%al
jne    11b0 <count_cpus+0x70>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11b0 <count_cpus+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
add    $0x1,%eax
jmp    11e5 <count_cpus+0xa5>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"cpu%d\00"
define i32 @count_cpus() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = 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* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i32 -1, i32* %1
br label %33
8: ; preds = %0
store i32 0, i32* %3
br label %9
9: ; preds = %14, %8
%10 = load i32*, i32** %2
%11 = call signext i8 @fgetc(i32* %10)
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %15
14: ; preds = %9
br label %9
15: ; preds = %9
br label %16
16: ; preds = %27, %15
%17 = load i32*, i32** %2
%18 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %28
20: ; preds = %16
br label %21
21: ; preds = %26, %20
%22 = load i32*, i32** %2
%23 = call signext i8 @fgetc(i32* %22)
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 10
br i1 %25, label %26, label %27
26: ; preds = %21
br label %21
27: ; preds = %21
br label %16
28: ; preds = %16
%29 = load i32*, i32** %2
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %1
br label %33
33: ; preds = %28, %7
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"cpu%d\00"
define i32 @count_cpus() {
%1 = alloca i32
%2 = 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))
%3 = icmp eq i32* %2, null
br i1 %3, label %23, label %4
4: ; preds = %0
%5 = bitcast i32* %1 to i8*
store i32 0, i32* %1
br label %6
6: ; preds = %6, %4
%7 = tail call signext i8 @fgetc(i32* %2)
%8 = icmp eq i8 %7, 10
br i1 %8, label %9, label %6
9: ; preds = %6
%10 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %15, label %19
12: ; preds = %15
%13 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %18, label %19
15: ; preds = %9, %18
%16 = call signext i8 @fgetc(i32* %2)
%17 = icmp eq i8 %16, 10
br i1 %17, label %12, label %18
18: ; preds = %15, %12
br label %15
19: ; preds = %12, %9
%20 = call i32 @fclose(i32* %2)
%21 = load i32, i32* %1
%22 = add nsw i32 %21, 1
br label %23
23: ; preds = %0, %19
%24 = phi i32 [ %22, %19 ], [ -1, %0 ]
ret i32 %24
}