#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*/ msg(int, int, char *, ...);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int sysconf(int /*<<< orphan*/);
int getsystemcores(int explain) {
  int syscores, coreids = 0, res;
  FILE *p;
  syscores = sysconf(_SC_NPROCESSORS_ONLN);
  if (explain) {
    if (syscores > 0)
      msg(-1, 1, "'sysconf' reports %d processors online", syscores);
    else
      msg(-1, 1, "'sysconf' fails to determine number of online processors");
  }
  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;
    if (explain) {
      if (coreids > 0)
        msg(-1, 1, "found %d unique core ids in '/proc/cpuinfo'", coreids);
      else
        msg(-1, 1, "failed to extract core ids from '/proc/cpuinfo'");
    }
    pclose(p);
  }
  if ((coreids > 0 && syscores > 0 && coreids == syscores / 2) ||
      (coreids > 0 && coreids == syscores)) {
  COREIDS:
    if (explain)
      msg(-1, 1, "assuming cores = extracted number of core ids = %d", coreids);
    res = coreids;
  } else if (syscores > 0) {
    if (explain)
      msg(-1, 1, "assuming cores = reported number of processors = %d",
          syscores);
    res = syscores;
  } else if (coreids > 0)
    goto COREIDS;
  else {
    if (explain)
      msg(-1, 1, "using compiled in default value of %d workers", 8);
    res = 8;
  }
  return res;
}


x86 assembly (clang, -O0):
<getsystemcores>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11c4 <getsystemcores+0x74>
cmpl   $0x0,-0x8(%rbp)
jle    11a7 <getsystemcores+0x57>
mov    -0x8(%rbp),%ecx
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe65(%rip),%rdx
mov    $0x0,%al
call   1070 <msg@plt>
jmp    11bf <getsystemcores+0x6f>
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe6f(%rip),%rdx
mov    $0x0,%al
call   1070 <msg@plt>
jmp    11c4 <getsystemcores+0x74>
lea    0xe95(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1264 <getsystemcores+0x114>
mov    -0x18(%rbp),%rdi
lea    0xf37(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     120a <getsystemcores+0xba>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     125b <getsystemcores+0x10b>
cmpl   $0x0,-0xc(%rbp)
jle    123e <getsystemcores+0xee>
mov    -0xc(%rbp),%ecx
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe6a(%rip),%rdx
mov    $0x0,%al
call   1070 <msg@plt>
jmp    1256 <getsystemcores+0x106>
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe79(%rip),%rdx
mov    $0x0,%al
call   1070 <msg@plt>
jmp    125b <getsystemcores+0x10b>
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
cmpl   $0x0,-0xc(%rbp)
jle    1296 <getsystemcores+0x146>
cmpl   $0x0,-0x8(%rbp)
jle    1296 <getsystemcores+0x146>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
je     12ac <getsystemcores+0x15c>
cmpl   $0x0,-0xc(%rbp)
jle    12e1 <getsystemcores+0x191>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    12e1 <getsystemcores+0x191>
jmp    12b1 <getsystemcores+0x161>
cmpl   $0x0,-0x4(%rbp)
je     12d6 <getsystemcores+0x186>
mov    -0xc(%rbp),%ecx
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe29(%rip),%rdx
mov    $0x0,%al
call   1070 <msg@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1362 <getsystemcores+0x212>
cmpl   $0x0,-0x8(%rbp)
jle    131b <getsystemcores+0x1cb>
cmpl   $0x0,-0x4(%rbp)
je     1310 <getsystemcores+0x1c0>
mov    -0x8(%rbp),%ecx
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe22(%rip),%rdx
mov    $0x0,%al
call   1070 <msg@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    135d <getsystemcores+0x20d>
cmpl   $0x0,-0xc(%rbp)
jle    132a <getsystemcores+0x1da>
jmp    12b1 <getsystemcores+0x161>
cmpl   $0x0,-0x4(%rbp)
je     1351 <getsystemcores+0x201>
mov    $0xffffffff,%edi
mov    $0x1,%esi
lea    0xe1a(%rip),%rdx
mov    $0x8,%ecx
mov    $0x0,%al
call   1070 <msg@plt>
movl   $0x8,-0x10(%rbp)
jmp    135d <getsystemcores+0x20d>
jmp    1362 <getsystemcores+0x212>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsystemcores>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
movl   $0x0,0xc(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ebx
test   %r14d,%r14d
je     11b0 <getsystemcores+0x60>
test   %ebx,%ebx
jle    1198 <getsystemcores+0x48>
lea    0xe7d(%rip),%rdx
mov    $0xffffffff,%edi
mov    $0x1,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1070 <msg@plt>
jmp    11b0 <getsystemcores+0x60>
lea    0xe88(%rip),%rdx
mov    $0xffffffff,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <msg@plt>
lea    0xea9(%rip),%rdi
lea    0xedc(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     1213 <getsystemcores+0xc3>
mov    %rax,%rbp
lea    0xf56(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11ec <getsystemcores+0x9c>
movl   $0x0,0xc(%rsp)
test   %r14d,%r14d
je     125c <getsystemcores+0x10c>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
jle    1244 <getsystemcores+0xf4>
lea    0xe9c(%rip),%rdx
mov    $0xffffffff,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <msg@plt>
jmp    125c <getsystemcores+0x10c>
test   %ebx,%ebx
jg     12b0 <getsystemcores+0x160>
test   %r14d,%r14d
je     12da <getsystemcores+0x18a>
lea    0xf34(%rip),%rdx
mov    $0x8,%ebx
mov    $0xffffffff,%edi
mov    $0x1,%esi
mov    $0x8,%ecx
jmp    12c8 <getsystemcores+0x178>
lea    0xe7d(%rip),%rdx
mov    $0xffffffff,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <msg@plt>
mov    %rbp,%rdi
call   1030 <pclose@plt>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
jle    127d <getsystemcores+0x12d>
test   %ebx,%ebx
jle    127d <getsystemcores+0x12d>
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
cmp    %eax,%ecx
je     1285 <getsystemcores+0x135>
test   %ecx,%ecx
jle    12ac <getsystemcores+0x15c>
cmp    %ebx,%ecx
jne    12ac <getsystemcores+0x15c>
test   %r14d,%r14d
je     12a8 <getsystemcores+0x158>
lea    0xe67(%rip),%rdx
mov    $0xffffffff,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <msg@plt>
mov    0xc(%rsp),%ebx
jmp    12cf <getsystemcores+0x17f>
mov    %ecx,%ebx
jmp    12cf <getsystemcores+0x17f>
test   %ebx,%ebx
jle    12e1 <getsystemcores+0x191>
test   %r14d,%r14d
je     12cf <getsystemcores+0x17f>
lea    0xe6f(%rip),%rdx
mov    $0xffffffff,%edi
mov    $0x1,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1070 <msg@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x8,%ebx
jmp    12cf <getsystemcores+0x17f>
test   %ecx,%ecx
jg     1285 <getsystemcores+0x135>
jmp    121b <getsystemcores+0xcb>

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [39 x i8] c"'sysconf' reports %d processors online\00"
@.str.1 = [57 x i8] c"'sysconf' fails to determine number of online processors\00"
@.str.2 = [58 x i8] c"grep '^core id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [44 x i8] c"found %d unique core ids in '/proc/cpuinfo'\00"
@.str.6 = [48 x i8] c"failed to extract core ids from '/proc/cpuinfo'\00"
@.str.7 = [51 x i8] c"assuming cores = extracted number of core ids = %d\00"
@.str.8 = [52 x i8] c"assuming cores = reported number of processors = %d\00"
@.str.9 = [46 x i8] c"using compiled in default value of %d workers\00"
define i32 @getsystemcores(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 %0, i32* %2
store i32 0, i32* %4
%7 = load i32, i32* @_SC_NPROCESSORS_ONLN
%8 = call i32 @sysconf(i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %1
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %3
%16 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %19
17: ; preds = %11
%18 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
br label %20
20: ; preds = %19, %1
%21 = call i32* @popen(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %44
24: ; preds = %20
%25 = load i32*, i32** %6
%26 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %4)
%27 = icmp ne i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 0, i32* %4
br label %29
29: ; preds = %28, %24
%30 = load i32, i32* %2
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %29
%33 = load i32, i32* %4
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %4
%37 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %36)
br label %40
38: ; preds = %32
%39 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
br label %41
41: ; preds = %40, %29
%42 = load i32*, i32** %6
%43 = call i32 @pclose(i32* %42)
br label %44
44: ; preds = %41, %20
%45 = load i32, i32* %4
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %44
%48 = load i32, i32* %3
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* %4
%52 = load i32, i32* %3
%53 = sdiv i32 %52, 2
%54 = icmp eq i32 %51, %53
br i1 %54, label %62, label %55
55: ; preds = %50, %47, %44
%56 = load i32, i32* %4
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %71
58: ; preds = %55
%59 = load i32, i32* %4
%60 = load i32, i32* %3
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %71
62: ; preds = %58, %50
br label %63
63: ; preds = %85, %62
%64 = load i32, i32* %2
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32, i32* %4
%68 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.7, i64 0, i64 0), i32 %67)
br label %69
69: ; preds = %66, %63
%70 = load i32, i32* %4
store i32 %70, i32* %5
br label %94
71: ; preds = %58, %55
%72 = load i32, i32* %3
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %82
74: ; preds = %71
%75 = load i32, i32* %2
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i32, i32* %3
%79 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.8, i64 0, i64 0), i32 %78)
br label %80
80: ; preds = %77, %74
%81 = load i32, i32* %3
store i32 %81, i32* %5
br label %93
82: ; preds = %71
%83 = load i32, i32* %4
%84 = icmp sgt i32 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %82
br label %63
86: ; preds = %82
%87 = load i32, i32* %2
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.9, i64 0, i64 0), i32 8)
br label %91
91: ; preds = %89, %86
store i32 8, i32* %5
br label %92
92: ; preds = %91
br label %93
93: ; preds = %92, %80
br label %94
94: ; preds = %93, %69
%95 = load i32, i32* %5
ret i32 %95
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [39 x i8] c"'sysconf' reports %d processors online\00"
@.str.1 = [57 x i8] c"'sysconf' fails to determine number of online processors\00"
@.str.2 = [58 x i8] c"grep '^core id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [44 x i8] c"found %d unique core ids in '/proc/cpuinfo'\00"
@.str.6 = [48 x i8] c"failed to extract core ids from '/proc/cpuinfo'\00"
@.str.7 = [51 x i8] c"assuming cores = extracted number of core ids = %d\00"
@.str.8 = [52 x i8] c"assuming cores = reported number of processors = %d\00"
@.str.9 = [46 x i8] c"using compiled in default value of %d workers\00"
define i32 @getsystemcores(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i32, i32* @_SC_NPROCESSORS_ONLN
%5 = tail call i32 @sysconf(i32 %4)
%6 = icmp eq i32 %0, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = icmp sgt i32 %5, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %13
11: ; preds = %7
%12 = tail call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %9, %11, %1
%14 = tail call i32* @popen(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = icmp sgt i32 %5, 0
br i1 %17, label %47, label %51
18: ; preds = %13
%19 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%20 = icmp eq i32 %19, 1
br i1 %20, label %22, label %21
21: ; preds = %18
store i32 0, i32* %2
br label %22
22: ; preds = %21, %18
br i1 %6, label %30, label %23
23: ; preds = %22
%24 = load i32, i32* %2
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %24)
br label %30
28: ; preds = %23
%29 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
br label %30
30: ; preds = %22, %28, %26
%31 = call i32 @pclose(i32* %14)
%32 = load i32, i32* %2
%33 = icmp sgt i32 %32, 0
%34 = icmp sgt i32 %5, 0
%35 = select i1 %33, i1 %34, i1 false
%36 = sdiv i32 %5, 2
%37 = icmp eq i32 %32, %36
%38 = select i1 %35, i1 %37, i1 false
br i1 %38, label %42, label %39
39: ; preds = %30
%40 = icmp eq i32 %32, %5
%41 = select i1 %33, i1 %40, i1 false
br i1 %41, label %42, label %46
42: ; preds = %39, %30, %50
br i1 %6, label %54, label %43
43: ; preds = %42
%44 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.7, i64 0, i64 0), i32 %32)
%45 = load i32, i32* %2
br label %54
46: ; preds = %39
br i1 %34, label %47, label %50
47: ; preds = %16, %46
br i1 %6, label %54, label %48
48: ; preds = %47
%49 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.8, i64 0, i64 0), i32 %5)
br label %54
50: ; preds = %46
br i1 %33, label %42, label %51
51: ; preds = %16, %50
br i1 %6, label %54, label %52
52: ; preds = %51
%53 = call i32 (i32, i32, i8*, ...) @msg(i32 -1, i32 1, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.9, i64 0, i64 0), i32 8)
br label %54
54: ; preds = %42, %43, %51, %52, %47, %48
%55 = phi i32 [ %5, %48 ], [ %5, %47 ], [ 8, %52 ], [ 8, %51 ], [ %45, %43 ], [ %32, %42 ]
ret i32 %55
}