#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*/ _SC_NPROCESSORS_CONF;
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ isspace(char);
scalar_t__ strncmp(char *, char *, int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ warning(char *);
int count_cpus(void) {
  FILE *fp;
  char buf[1024];
  int cpus = 0;
  char *pbuf;
  size_t *pn;
  size_t n;
  int r;
  cpus = sysconf(_SC_NPROCESSORS_CONF);
  if (cpus > 0)
    return cpus;
  warning("sysconf could not determine number of CPUS");
  n = 1024;
  pn = &n;
  pbuf = buf;
  fp = fopen("/proc/cpuinfo", "r");
  if (!fp)
    die("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)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jle    11c4 <count_cpus+0x44>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f7 <count_cpus+0x177>
lea    0xe35(%rip),%rdi
call   1040 <warning@plt>
movq   $0x400,-0x430(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    0xe2d(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1225 <count_cpus+0xa5>
lea    0xe19(%rip),%rdi
call   1080 <die@plt>
jmp    122a <count_cpus+0xaa>
mov    -0x428(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x420(%rbp),%rdi
call   10a0 <getline@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
jl     12e5 <count_cpus+0x165>
lea    -0x410(%rbp),%rdi
lea    0xdf0(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1277 <count_cpus+0xf7>
jmp    122a <count_cpus+0xaa>
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     12be <count_cpus+0x13e>
jmp    12a7 <count_cpus+0x127>
mov    -0x440(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x440(%rbp)
jmp    1289 <count_cpus+0x109>
mov    -0x440(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    12e0 <count_cpus+0x160>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    122a <count_cpus+0xaa>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%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
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
test   %eax,%eax
jg     1296 <count_cpus+0x116>
mov    %eax,0x4(%rsp)
lea    0xe4e(%rip),%rdi
call   1040 <warning@plt>
movq   $0x400,0x8(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0xe5a(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f1 <count_cpus+0x71>
lea    0xe4d(%rip),%rdi
call   1080 <die@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %r12,%rdx
call   10a0 <getline@plt>
test   %eax,%eax
js     128a <count_cpus+0x10a>
lea    0xe3c(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r13
lea    0x8(%rsp),%rbx
mov    %r12,0x18(%rsp)
jmp    1242 <count_cpus+0xc2>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10a0 <getline@plt>
test   %eax,%eax
js     128a <count_cpus+0x10a>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1230 <count_cpus+0xb0>
mov    %r14,%rbp
lea    0x29(%rsp),%r14
nop
movsbl (%r14),%edi
call   1060 <isspace@plt>
add    $0x1,%r14
test   %rax,%rax
jne    1260 <count_cpus+0xe0>
xor    %eax,%eax
cmpb   $0x3a,-0x1(%r14)
sete   %al
add    %eax,0x4(%rsp)
mov    %rbp,%r14
mov    0x18(%rsp),%r12
jmp    1230 <count_cpus+0xb0>
mov    %r12,%rdi
call   1050 <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):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [43 x i8] c"sysconf could not determine number of CPUS\00"
@.str.1 = [14 x i8] c"/proc/cpuinfo\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Can not read cpuinfo\00"
@.str.4 = [10 x i8] c"processor\00"
define i32 @count_cpus() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %4
%10 = load i32, i32* @_SC_NPROCESSORS_CONF
%11 = call i32 @sysconf(i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = load i32, i32* %4
store i32 %15, i32* %1
br label %60
16: ; preds = %0
%17 = call i32 @warning(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i64 1024, i64* %7
store i64* %7, i64** %6
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8* %18, i8** %5
%19 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
br label %25
25: ; preds = %55, %34, %24
%26 = load i64*, i64** %6
%27 = load i32*, i32** %2
%28 = call i32 @getline(i8** %5, i64* %26, i32* %27)
store i32 %28, i32* %8
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %56
30: ; preds = %25
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%32 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 9)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
br label %25
35: ; preds = %30
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%37 = getelementptr inbounds i8, i8* %36, i64 9
store i8* %37, i8** %9
br label %38
38: ; preds = %44, %35
%39 = load i8*, i8** %9
%40 = load i8, i8* %39
%41 = call i64 @isspace(i8 signext %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
br label %44
44: ; preds = %43
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %9
br label %38
47: ; preds = %38
%48 = load i8*, i8** %9
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 58
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %4
br label %55
55: ; preds = %52, %47
br label %25
56: ; preds = %25
%57 = load i32*, i32** %2
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %4
store i32 %59, i32* %1
br label %60
60: ; preds = %56, %14
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [43 x i8] c"sysconf could not determine number of CPUS\00"
@.str.1 = [14 x i8] c"/proc/cpuinfo\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Can not read cpuinfo\00"
@.str.4 = [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*
%7 = load i32, i32* @_SC_NPROCESSORS_CONF
%8 = tail call i32 @sysconf(i32 %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %43, label %10
10: ; preds = %0
%11 = tail call i32 @warning(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i64 1024, i64* %3
store i8* %4, i8** %2
%12 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = call i32 @getline(i8** %2, i64* %3, i32* %12)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %40
19: ; preds = %16
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 9
br label %21
21: ; preds = %19, %36
%22 = phi i32 [ %8, %19 ], [ %37, %36 ]
%23 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 9)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %21, %25
%26 = phi i8* [ %30, %25 ], [ %20, %21 ]
%27 = load i8, i8* %26
%28 = call i64 @isspace(i8 signext %27)
%29 = icmp eq i64 %28, 0
%30 = getelementptr inbounds i8, i8* %26, i64 1
br i1 %29, label %31, label %25
31: ; preds = %25
%32 = load i8, i8* %26
%33 = icmp eq i8 %32, 58
%34 = zext i1 %33 to i32
%35 = add nsw i32 %22, %34
br label %36
36: ; preds = %21, %31
%37 = phi i32 [ %35, %31 ], [ %22, %21 ]
%38 = call i32 @getline(i8** %2, i64* %3, i32* %12)
%39 = icmp sgt i32 %38, -1
br i1 %39, label %21, label %40
40: ; preds = %36, %16
%41 = phi i32 [ %8, %16 ], [ %37, %36 ]
%42 = call i32 @fclose(i32* %12)
br label %43
43: ; preds = %0, %40
%44 = phi i32 [ %41, %40 ], [ %8, %0 ]
ret i32 %44
}