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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *);
int getcpu() {
  char cmd[256], buff[8] = {0};
  FILE *fd;
  snprintf(cmd, sizeof(cmd), "cat /proc/cpuinfo | grep \"^processor\" | wc -l");
  if ((fd = popen(cmd, "r")) == NULL) {
    pclose(fd);
    return -1;
  }
  if (fgets(buff, sizeof(buff), fd) == NULL) {
    pclose(fd);
    return -1;
  }
  pclose(fd);
  return atoi(buff);
}


x86 assembly (clang, -O0):
<getcpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    -0x118(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xe6f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
jne    11d2 <getcpu+0x72>
mov    -0x120(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <getcpu+0xc7>
lea    -0x118(%rbp),%rdi
mov    -0x120(%rbp),%rdx
mov    $0x8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    120c <getcpu+0xac>
mov    -0x120(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <getcpu+0xc7>
mov    -0x120(%rbp),%rdi
call   1030 <pclose@plt>
lea    -0x118(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcpu>:
push   %r14
push   %rbx
sub    $0x118,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe96(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
call   1060 <popen@plt>
test   %rax,%rax
je     11c1 <getcpu+0x71>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %r14,%r14
je     11c8 <getcpu+0x78>
lea    0x8(%rsp),%rdi
call   1070 <atoi@plt>
jmp    11cd <getcpu+0x7d>
xor    %edi,%edi
call   1030 <pclose@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [46 x i8] c"cat /proc/cpuinfo | grep \22^processor\22 | wc -l\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getcpu() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [8 x i8]
%4 = alloca i32*
%5 = bitcast [8 x i8]* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = call i32* @popen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32*, i32** %4
%13 = call i32 @pclose(i32* %12)
store i32 -1, i32* %1
br label %27
14: ; preds = %0
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %4
%17 = call i32* @fgets(i8* %15, i32 8, i32* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32*, i32** %4
%21 = call i32 @pclose(i32* %20)
store i32 -1, i32* %1
br label %27
22: ; preds = %14
%23 = load i32*, i32** %4
%24 = call i32 @pclose(i32* %23)
%25 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %1
br label %27
27: ; preds = %22, %19, %11
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [46 x i8] c"cat /proc/cpuinfo | grep \22^processor\22 | wc -l\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getcpu() {
%1 = alloca [256 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%6 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @pclose(i32* null)
br label %16
10: ; preds = %0
%11 = call i32* @fgets(i8* %4, i32 8, i32* %6)
%12 = icmp eq i32* %11, null
%13 = call i32 @pclose(i32* %6)
br i1 %12, label %16, label %14
14: ; preds = %10
%15 = call i32 @atoi(i8* %4)
br label %16
16: ; preds = %10, %14, %8
%17 = phi i32 [ -1, %8 ], [ %15, %14 ], [ -1, %10 ]
ret i32 %17
}