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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *popen(char *, char *);
int ncores() {
  FILE *fp;
  char res[128];
  memset(res, 0, 128);
  fp = popen("/bin/cat /proc/cpuinfo |grep -c '^processor'", "r");
  fread(res, 1, sizeof(res) - 1, fp);
  fclose(fp);
  return atoi(res);
}


x86 assembly (clang, -O0):
<ncores>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe8b(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7f,%edx
call   1030 <fread@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x90(%rbp),%rdi
call   1070 <atoi@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ncores>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe8d(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x7f,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1070 <atoi@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [45 x i8] c"/bin/cat /proc/cpuinfo |grep -c '^processor'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @ncores() {
%1 = alloca i32*
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 128)
%5 = call i32* @popen(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %1
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%7 = load i32*, i32** %1
%8 = call i32 @fread(i8* %6, i32 1, i32 127, i32* %7)
%9 = load i32*, i32** %1
%10 = call i32 @fclose(i32* %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%12 = call i32 @atoi(i8* %11)
ret i32 %12
}

LLVM IR (-O2):
@.str = [45 x i8] c"/bin/cat /proc/cpuinfo |grep -c '^processor'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @ncores() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 128)
%4 = call i32* @popen(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @fread(i8* %2, i32 1, i32 127, i32* %4)
%6 = call i32 @fclose(i32* %4)
%7 = call i32 @atoi(i8* %2)
ret i32 %7
}