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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
int getNbProcessorsAvailable(void) {
  FILE *output;
  long nbProcessorsAvailable;
  char buf[3];
  char cmd[] = "grep \"CPU\" /proc/cpuinfo | wc -l";
  int status;
  char *ptr, *end;
  output = popen(cmd, "r");
  if (output == NULL) {
    fprintf(stderr, "%s\n", cmd);
    perror("Error: Getting processors number : ");
    exit(EXIT_FAILURE);
  }
  ptr = fgets(buf, sizeof(buf), output);
  assert(ptr != NULL);
  status = pclose(output);
  if (status == -1) {
    fprintf(stderr, "%s\n", buf);
    perror("Error: Closing processors number system call :");
    exit(EXIT_FAILURE);
  }
  nbProcessorsAvailable = strtol(buf, &end, 10);
  if (buf == end) {
    fprintf(stderr, "Error: in getNbProcessorsAvailable strtol call failed.\n");
    exit(EXIT_FAILURE);
  }
  return nbProcessorsAvailable;
}


x86 assembly (clang, -O0):
<getNbProcessorsAvailable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    -0x40(%rbp),%rdi
lea    0xe5d(%rip),%rsi
mov    $0x21,%edx
call   1080 <memcpy@plt>
lea    -0x40(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1201 <getNbProcessorsAvailable+0x71>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe39(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x13(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x3,%esi
call   1040 <fgets@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <pclose@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1274 <getNbProcessorsAvailable+0xe4>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    -0x13(%rbp),%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xdea(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x13(%rbp),%rdi
lea    -0x58(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x10(%rbp)
lea    -0x13(%rbp),%rax
cmp    -0x58(%rbp),%rax
jne    12bd <getNbProcessorsAvailable+0x12d>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNbProcessorsAvailable>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movaps 0xe82(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe66(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x30(%rsp)
lea    0xe76(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     120e <getNbProcessorsAvailable+0x8e>
mov    %rax,%rbx
lea    0x5(%rsp),%r14
mov    %r14,%rdi
mov    $0x3,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <pclose@plt>
cmp    $0xffffffff,%eax
je     1233 <getNbProcessorsAvailable+0xb3>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0xa,%edx
call   1070 <strtol@plt>
cmp    0x8(%rsp),%r14
je     125d <getNbProcessorsAvailable+0xdd>
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xdf6(%rip),%rdi
jmp    1256 <getNbProcessorsAvailable+0xd6>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
lea    0x5(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
jmp    1274 <getNbProcessorsAvailable+0xf4>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.getNbProcessorsAvailable.cmd = [33 x i8] c"grep \22CPU\22 /proc/cpuinfo | wc -l\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [36 x i8] c"Error: Getting processors number : \00"
@EXIT_FAILURE = global i32 0
@.str.3 = [47 x i8] c"Error: Closing processors number system call :\00"
@.str.4 = [56 x i8] c"Error: in getNbProcessorsAvailable strtol call failed.\0A\00"
define i32 @getNbProcessorsAvailable() {
%1 = alloca i32*
%2 = alloca i64
%3 = alloca [3 x i8]
%4 = alloca [33 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast [33 x i8]* %4 to i8*
%9 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %1
%11 = load i32*, i32** %1
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %0
%21 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%22 = load i32*, i32** %1
%23 = call i8* @fgets(i8* %21, i32 3, i32* %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32*, i32** %1
%29 = call i32 @pclose(i32* %28)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %20
%40 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%41 = call i64 @strtol(i8* %40, i8** %7, i32 10)
store i64 %41, i64* %2
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%43 = load i8*, i8** %7
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %39
%51 = load i64, i64* %2
%52 = trunc i64 %51 to i32
ret i32 %52
}

LLVM IR (-O2):
@__const.getNbProcessorsAvailable.cmd = [33 x i8] c"grep \22CPU\22 /proc/cpuinfo | wc -l\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [36 x i8] c"Error: Getting processors number : \00"
@EXIT_FAILURE = global i32 0
@.str.3 = [47 x i8] c"Error: Closing processors number system call :\00"
@.str.4 = [56 x i8] c"Error: in getNbProcessorsAvailable strtol call failed.\0A\00"
define i32 @getNbProcessorsAvailable() {
%1 = alloca [3 x i8]
%2 = alloca [33 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [33 x i8], [33 x i8]* %2, i64 0, i64 0
%6 = bitcast i8** %3 to i8*
%7 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%12 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = call i8* @fgets(i8* %4, i32 3, i32* %7)
%17 = icmp ne i8* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = call i32 @pclose(i32* %7)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%25 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %15
%29 = call i64 @strtol(i8* %4, i8** %3, i32 10)
%30 = load i8*, i8** %3
%31 = icmp eq i8* %4, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %28
%38 = trunc i64 %29 to i32
ret i32 %38
}