#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*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int sscanf(char *, char *, unsigned int *);
int number_of_cores(void) {
  FILE *cmd = popen("grep '^processor' /proc/cpuinfo | wc -l", "r");
  if (cmd == NULL) {
    return 2;
  }
  unsigned nprocs;
  size_t n;
  char buff[8];
  memset(buff, 0x00, sizeof(buff));
  if ((n = fread(buff, 1, sizeof(buff) - 1, cmd)) <= 0) {
    return 2;
  }
  buff[n] = '\0';
  if (sscanf(buff, "%u", &nprocs) != 1) {
    return 2;
  }
  pclose(cmd);
  return nprocs;
}


x86 assembly (clang, -O0):
<number_of_cores>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea1(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1186 <number_of_cores+0x36>
movl   $0x2,-0x4(%rbp)
jmp    1208 <number_of_cores+0xb8>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
ja     11c7 <number_of_cores+0x77>
movl   $0x2,-0x4(%rbp)
jmp    1208 <number_of_cores+0xb8>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x28(%rbp,%rax,1)
lea    -0x28(%rbp),%rdi
lea    0xe4f(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11f9 <number_of_cores+0xa9>
movl   $0x2,-0x4(%rbp)
jmp    1208 <number_of_cores+0xb8>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<number_of_cores>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xea3(%rip),%rdi
lea    0xec4(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11d2 <number_of_cores+0x82>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x7,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    $0x2,%ebp
test   %rax,%rax
je     11d7 <number_of_cores+0x87>
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe7a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11d7 <number_of_cores+0x87>
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    0xc(%rsp),%ebp
jmp    11d7 <number_of_cores+0x87>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"grep '^processor' /proc/cpuinfo | wc -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @number_of_cores() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i64
%5 = alloca [8 x i8]
%6 = call i32* @popen(i8* getelementptr inbounds ([40 x i8], [40 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** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 2, i32* %1
br label %29
10: ; preds = %0
%11 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 8)
%13 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i64 @fread(i8* %13, i32 1, i32 7, i32* %14)
store i64 %15, i64* %4
%16 = icmp ule i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %10
store i32 2, i32* %1
br label %29
18: ; preds = %10
%19 = load i64, i64* %4
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 %19
store i8 0, i8* %20
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 2, i32* %1
br label %29
25: ; preds = %18
%26 = load i32*, i32** %2
%27 = call i32 @pclose(i32* %26)
%28 = load i32, i32* %3
store i32 %28, i32* %1
br label %29
29: ; preds = %25, %24, %17, %9
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str = [40 x i8] c"grep '^processor' /proc/cpuinfo | wc -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @number_of_cores() {
%1 = alloca i32
%2 = alloca [8 x i8]
%3 = tail call i32* @popen(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %20, label %5
5: ; preds = %0
%6 = bitcast i32* %1 to i8*
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 8)
%9 = call i64 @fread(i8* %7, i32 1, i32 7, i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %5
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 %9
store i8 0, i8* %12
%13 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @pclose(i32* %3)
%17 = load i32, i32* %1
br label %18
18: ; preds = %11, %5, %15
%19 = phi i32 [ %17, %15 ], [ 2, %5 ], [ 2, %11 ]
br label %20
20: ; preds = %0, %18
%21 = phi i32 [ %19, %18 ], [ 2, %0 ]
ret i32 %21
}