#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*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
scalar_t__ strcmp(char *, char *);
int findCpuCount() {
  size_t nread = 256 * sizeof(char);
  char *line = (char *)malloc(nread + 1);
  int cpuCount = 0;
  FILE *f = fopen("/proc/stat", "r");
  while (1) {
    getline(&line, &nread, f);
    line[3] = '\0';
    if (strcmp(line, "cpu") == 0) {
      cpuCount += 1;
    } else {
      break;
    }
  }
  fclose(f);
  free(line);
  return cpuCount;
}


x86 assembly (clang, -O0):
<findCpuCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x100,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe71(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <getline@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11e0 <findCpuCount+0x80>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e5 <findCpuCount+0x85>
jmp    11ea <findCpuCount+0x8a>
jmp    119f <findCpuCount+0x3f>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findCpuCount>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x100,0x8(%rsp)
mov    $0x101,%edi
call   1060 <malloc@plt>
mov    %rax,(%rsp)
lea    0xe75(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    $0xffffffff,%ebx
mov    %rsp,%r15
lea    0x8(%rsp),%r12
lea    0xe5f(%rip),%r13
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1080 <getline@plt>
mov    (%rsp),%rax
movb   $0x0,0x3(%rax)
mov    (%rsp),%rdi
mov    %r13,%rsi
call   1050 <strcmp@plt>
add    $0x1,%ebx
test   %rax,%rax
je     11b0 <findCpuCount+0x50>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [4 x i8] c"cpu\00"
define i32 @findCpuCount() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i64 256, i64* %1
%5 = load i64, i64* %1
%6 = add i64 %5, 1
%7 = call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %2
store i32 0, i32* %3
%9 = 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* %9, i32** %4
br label %10
10: ; preds = %0, %22
%11 = load i32*, i32** %4
%12 = call i32 @getline(i8** %2, i64* %1, i32* %11)
%13 = load i8*, i8** %2
%14 = getelementptr inbounds i8, i8* %13, i64 3
store i8 0, i8* %14
%15 = load i8*, i8** %2
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %22
21: ; preds = %10
br label %23
22: ; preds = %18
br label %10
23: ; preds = %21
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
%26 = load i8*, i8** %2
%27 = call i32 @free(i8* %26)
%28 = load i32, i32* %3
ret i32 %28
}

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 @findCpuCount() {
%1 = alloca i64
%2 = alloca i8*
%3 = bitcast i64* %1 to i8*
store i64 256, i64* %1
%4 = bitcast i8** %2 to i8*
%5 = tail call i64 @malloc(i64 257)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %2
%7 = 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))
br label %8
8: ; preds = %8, %0
%9 = phi i32 [ 0, %0 ], [ %16, %8 ]
%10 = call i32 @getline(i8** %2, i64* %1, i32* %7)
%11 = load i8*, i8** %2
%12 = getelementptr inbounds i8, i8* %11, i64 3
store i8 0, i8* %12
%13 = load i8*, i8** %2
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
%16 = add nuw nsw i32 %9, 1
br i1 %15, label %8, label %17
17: ; preds = %8
%18 = call i32 @fclose(i32* %7)
%19 = load i8*, i8** %2
%20 = call i32 @free(i8* %19)
ret i32 %9
}