#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 getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ isspace(char);
int /*<<< orphan*/ perr(char *);
scalar_t__ strncmp(char *, char *, int);
int count_cpus(void) {
  FILE *fp;
  char buf[1024];
  int cpus = 0;
  char *pbuf;
  size_t *pn;
  size_t n;
  int r;
  n = 1024;
  pn = &n;
  pbuf = buf;
  fp = fopen("/proc/cpuinfo", "r");
  if (!fp)
    perr("Can not read cpuinfo");
  while ((r = getline(&pbuf, pn, fp)) >= 0) {
    char *p;
    if (strncmp(buf, "processor", 9) != 0)
      continue;
    for (p = buf + 9; isspace(*p); p++)
      ;
    if (*p == ':')
      cpus++;
  }
  fclose(fp);
  return cpus;
}


x86 assembly (clang, -O0):
<count_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x414(%rbp)
movq   $0x400,-0x430(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    0xe5d(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11ca <count_cpus+0x6a>
lea    0xe49(%rip),%rdi
call   1050 <perr@plt>
jmp    11cf <count_cpus+0x6f>
mov    -0x428(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    -0x420(%rbp),%rdi
call   1080 <getline@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
jl     128a <count_cpus+0x12a>
lea    -0x410(%rbp),%rdi
lea    0xe20(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     121c <count_cpus+0xbc>
jmp    11cf <count_cpus+0x6f>
lea    -0x410(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
je     1263 <count_cpus+0x103>
jmp    124c <count_cpus+0xec>
mov    -0x440(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x440(%rbp)
jmp    122e <count_cpus+0xce>
mov    -0x440(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1285 <count_cpus+0x125>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    11cf <count_cpus+0x6f>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x414(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_cpus>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
movq   $0x400,0x8(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0xe75(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11ab <count_cpus+0x4b>
lea    0xe68(%rip),%rdi
call   1050 <perr@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %r12,%rdx
call   1080 <getline@plt>
test   %eax,%eax
js     124a <count_cpus+0xea>
movl   $0x0,0x4(%rsp)
lea    0xe4f(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r13
lea    0x8(%rsp),%rbx
mov    %r12,0x18(%rsp)
jmp    1202 <count_cpus+0xa2>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <getline@plt>
test   %eax,%eax
js     1252 <count_cpus+0xf2>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11f0 <count_cpus+0x90>
mov    %r14,%rbp
lea    0x29(%rsp),%r14
nop
movsbl (%r14),%edi
call   1060 <isspace@plt>
add    $0x1,%r14
test   %rax,%rax
jne    1220 <count_cpus+0xc0>
xor    %eax,%eax
cmpb   $0x3a,-0x1(%r14)
sete   %al
add    %eax,0x4(%rsp)
mov    %rbp,%r14
mov    0x18(%rsp),%r12
jmp    11f0 <count_cpus+0x90>
movl   $0x0,0x4(%rsp)
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"Can not read cpuinfo\00"
@.str.3 = [10 x i8] c"processor\00"
define i32 @count_cpus() {
%1 = alloca i32*
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i64 1024, i64* %6
store i64* %6, i64** %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
store i8* %9, i8** %4
%10 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %1
%11 = load i32*, i32** %1
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %0
%14 = call i32 @perr(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
br label %16
16: ; preds = %46, %25, %15
%17 = load i64*, i64** %5
%18 = load i32*, i32** %1
%19 = call i32 @getline(i8** %4, i64* %17, i32* %18)
store i32 %19, i32* %7
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %47
21: ; preds = %16
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%23 = call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %16
26: ; preds = %21
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%28 = getelementptr inbounds i8, i8* %27, i64 9
store i8* %28, i8** %8
br label %29
29: ; preds = %35, %26
%30 = load i8*, i8** %8
%31 = load i8, i8* %30
%32 = call i64 @isspace(i8 signext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
br label %35
35: ; preds = %34
%36 = load i8*, i8** %8
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %8
br label %29
38: ; preds = %29
%39 = load i8*, i8** %8
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 58
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
store i32 %45, i32* %3
br label %46
46: ; preds = %43, %38
br label %16
47: ; preds = %16
%48 = load i32*, i32** %1
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"Can not read cpuinfo\00"
@.str.3 = [10 x i8] c"processor\00"
define i32 @count_cpus() {
%1 = alloca [1024 x i8]
%2 = alloca i8*
%3 = alloca i64
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = bitcast i8** %2 to i8*
%6 = bitcast i64* %3 to i8*
store i64 1024, i64* %3
store i8* %4, i8** %2
%7 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perr(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 @getline(i8** %2, i64* %3, i32* %7)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 9
br label %16
16: ; preds = %14, %31
%17 = phi i32 [ 0, %14 ], [ %32, %31 ]
%18 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %16, %20
%21 = phi i8* [ %25, %20 ], [ %15, %16 ]
%22 = load i8, i8* %21
%23 = call i64 @isspace(i8 signext %22)
%24 = icmp eq i64 %23, 0
%25 = getelementptr inbounds i8, i8* %21, i64 1
br i1 %24, label %26, label %20
26: ; preds = %20
%27 = load i8, i8* %21
%28 = icmp eq i8 %27, 58
%29 = zext i1 %28 to i32
%30 = add nsw i32 %17, %29
br label %31
31: ; preds = %16, %26
%32 = phi i32 [ %30, %26 ], [ %17, %16 ]
%33 = call i32 @getline(i8** %2, i64* %3, i32* %7)
%34 = icmp sgt i32 %33, -1
br i1 %34, label %16, label %35
35: ; preds = %31, %11
%36 = phi i32 [ 0, %11 ], [ %32, %31 ]
%37 = call i32 @fclose(i32* %7)
ret i32 %36
}