#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*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strstr(char *, char *);
int countProcessors() {
  FILE *fp = fopen("/proc/stat", "r");
  char buffer[256];
  int procs = -1, processorCount, i;
  do {
    procs++;
    fgets(buffer, 255, fp);
  } while ((strstr((buffer), ("cpu")) == (buffer)));
  processorCount = procs - 1;
  fclose(fp);
  return processorCount;
}


x86 assembly (clang, -O0):
<countProcessors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    0xeae(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x8(%rbp)
movl   $0xffffffff,-0x114(%rbp)
ff ff ff
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0xff,%esi
call   1040 <fgets@plt>
lea    -0x110(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1060 <strstr@plt>
lea    -0x110(%rbp),%rcx
cmp    %rcx,%rax
je     116c <countProcessors+0x2c>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x118(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x118(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countProcessors>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
lea    0xebc(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    $0xfffffffe,%ebx
mov    %rsp,%r15
mov    $0x7063,%ebp
mov    %r15,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
mov    (%rsp),%eax
xor    %ebp,%eax
movzbl 0x2(%rsp),%ecx
xor    $0x75,%ecx
add    $0x1,%ebx
or     %ax,%cx
je     1160 <countProcessors+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
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 @countProcessors() {
%1 = alloca i32*
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%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** %1
store i32 -1, i32* %3
br label %7
7: ; preds = %13, %0
%8 = load i32, i32* %3
%9 = add nsw i32 %8, 1
store i32 %9, i32* %3
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = load i32*, i32** %1
%12 = call i32 @fgets(i8* %10, i32 255, i32* %11)
br label %13
13: ; preds = %7
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%15 = call i8* @strstr(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%17 = icmp eq i8* %15, %16
br i1 %17, label %7, label %18
18: ; preds = %13
%19 = load i32, i32* %3
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %4
%21 = load i32*, i32** %1
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %4
ret i32 %23
}

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 @countProcessors() {
%1 = alloca [256 x i8]
%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 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
br label %4
4: ; preds = %4, %0
%5 = phi i32 [ -1, %0 ], [ %6, %4 ]
%6 = add nsw i32 %5, 1
%7 = call i32 @fgets(i8* %3, i32 255, i32* %2)
%8 = call i32 @bcmp(i8* dereferenceable(3) %3, i8* dereferenceable(3) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %4, label %10
10: ; preds = %4
%11 = call i32 @fclose(i32* %2)
ret i32 %5
}