#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_ONLN;
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int sysconf(int /*<<< orphan*/);
int numcores(void) {
  int syscores, coreids = 0, res;
  FILE *p;
  syscores = sysconf(_SC_NPROCESSORS_ONLN);
  p = popen("grep '^core id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l", "r");
  if (p) {
    if (fscanf(p, "%d", &coreids) != 1)
      coreids = 0;
    pclose(p);
  }
  if (coreids > 0 && syscores > 0 && coreids == syscores / 2)
    res = coreids;
  else if (coreids > 0 && coreids == syscores)
    res = coreids;
  else if (coreids <= 0 && syscores > 0)
    res = syscores;
  else if (coreids > 0 && syscores <= 0)
    res = coreids;
  else
    res = 8;
  return res;
}


x86 assembly (clang, -O0):
<numcores>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x4(%rbp)
lea    0xe99(%rip),%rdi
lea    0xecc(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11af <numcores+0x6f>
mov    -0x18(%rbp),%rdi
lea    0xeaf(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11a6 <numcores+0x66>
movl   $0x0,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
cmpl   $0x0,-0x8(%rbp)
jle    11ec <numcores+0xac>
cmpl   $0x0,-0x4(%rbp)
jle    11ec <numcores+0xac>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jne    11ec <numcores+0xac>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1261 <numcores+0x121>
cmpl   $0x0,-0x8(%rbp)
jle    120d <numcores+0xcd>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    120d <numcores+0xcd>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    125c <numcores+0x11c>
cmpl   $0x0,-0x8(%rbp)
jg     122c <numcores+0xec>
cmpl   $0x0,-0x4(%rbp)
jle    122c <numcores+0xec>
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1257 <numcores+0x117>
cmpl   $0x0,-0x8(%rbp)
jle    124b <numcores+0x10b>
cmpl   $0x0,-0x4(%rbp)
jg     124b <numcores+0x10b>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1252 <numcores+0x112>
movl   $0x8,-0xc(%rbp)
jmp    1257 <numcores+0x117>
jmp    125c <numcores+0x11c>
jmp    1261 <numcores+0x121>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numcores>:
push   %rbp
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ebp
lea    0xe9e(%rip),%rdi
lea    0xed1(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     11ca <numcores+0x8a>
mov    %rax,%rbx
lea    0xebf(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     1197 <numcores+0x57>
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    0x4(%rsp),%eax
test   %ebp,%ebp
setg   %dl
test   %eax,%eax
setg   %cl
jle    11c0 <numcores+0x80>
test   %ebp,%ebp
jle    11c0 <numcores+0x80>
mov    %ebp,%esi
shr    $0x1f,%esi
add    %ebp,%esi
sar    %esi
cmp    %esi,%eax
je     11ea <numcores+0xaa>
test   %eax,%eax
jle    11d3 <numcores+0x93>
cmp    %ebp,%eax
je     11ec <numcores+0xac>
jmp    11d3 <numcores+0x93>
test   %ebp,%ebp
setg   %dl
xor    %eax,%eax
xor    %ecx,%ecx
test   %eax,%eax
jg     11db <numcores+0x9b>
test   %dl,%dl
jne    11ec <numcores+0xac>
test   %ebp,%ebp
mov    $0x8,%edx
cmovg  %edx,%eax
test   %cl,%cl
cmove  %edx,%eax
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [58 x i8] c"grep '^core id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @numcores() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %2
%5 = load i32, i32* @_SC_NPROCESSORS_ONLN
%6 = call i32 @sysconf(i32 %5)
store i32 %6, i32* %1
%7 = call i32* @popen(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %18
10: ; preds = %0
%11 = load i32*, i32** %4
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 0, i32* %2
br label %15
15: ; preds = %14, %10
%16 = load i32*, i32** %4
%17 = call i32 @pclose(i32* %16)
br label %18
18: ; preds = %15, %0
%19 = load i32, i32* %2
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = load i32, i32* %1
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i32, i32* %2
%26 = load i32, i32* %1
%27 = sdiv i32 %26, 2
%28 = icmp eq i32 %25, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* %2
store i32 %30, i32* %3
br label %60
31: ; preds = %24, %21, %18
%32 = load i32, i32* %2
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i32, i32* %2
%36 = load i32, i32* %1
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32, i32* %2
store i32 %39, i32* %3
br label %59
40: ; preds = %34, %31
%41 = load i32, i32* %2
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* %1
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = load i32, i32* %1
store i32 %47, i32* %3
br label %58
48: ; preds = %43, %40
%49 = load i32, i32* %2
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* %1
%53 = icmp sle i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* %2
store i32 %55, i32* %3
br label %57
56: ; preds = %51, %48
store i32 8, i32* %3
br label %57
57: ; preds = %56, %54
br label %58
58: ; preds = %57, %46
br label %59
59: ; preds = %58, %38
br label %60
60: ; preds = %59, %29
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [58 x i8] c"grep '^core id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @numcores() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = load i32, i32* @_SC_NPROCESSORS_ONLN
%4 = tail call i32 @sysconf(i32 %3)
%5 = tail call i32* @popen(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = icmp sgt i32 %4, 0
br label %25
9: ; preds = %0
%10 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %13, label %12
12: ; preds = %9
store i32 0, i32* %1
br label %13
13: ; preds = %9, %12
%14 = call i32 @pclose(i32* %5)
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 0
%17 = icmp sgt i32 %4, 0
%18 = select i1 %16, i1 %17, i1 false
%19 = sdiv i32 %4, 2
%20 = icmp eq i32 %15, %19
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %35, label %22
22: ; preds = %13
%23 = icmp eq i32 %15, %4
%24 = select i1 %16, i1 %23, i1 false
br i1 %24, label %35, label %25
25: ; preds = %7, %22
%26 = phi i32 [ 0, %7 ], [ %15, %22 ]
%27 = phi i1 [ false, %7 ], [ %16, %22 ]
%28 = phi i1 [ %8, %7 ], [ %17, %22 ]
%29 = icmp slt i32 %26, 1
%30 = select i1 %29, i1 %28, i1 false
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = icmp slt i32 %4, 1
%33 = select i1 %27, i1 %32, i1 false
%34 = select i1 %33, i32 %26, i32 8
br label %35
35: ; preds = %31, %25, %22, %13
%36 = phi i32 [ %15, %13 ], [ %4, %22 ], [ %4, %25 ], [ %34, %31 ]
ret i32 %36
}