#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 */

/* Variables and functions */
long parse_cpuinfo(char *, unsigned int);
int readfile(char *, char **, unsigned int *, unsigned int *);
long get_num_cpus() {
  int err = 0;
  char *cpuinfo = NULL;
  unsigned int size = 0;
  unsigned int max = 0;
  err = readfile("/proc/cpuinfo", &cpuinfo, &size, &max);
  if (err || !cpuinfo) {
    return err;
  }
  return parse_cpuinfo(cpuinfo, size);
}


x86 assembly (clang, -O0):
<get_num_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xeb4(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <readfile@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1175 <get_num_cpus+0x55>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <get_num_cpus+0x62>
movslq -0xc(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1192 <get_num_cpus+0x72>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <parse_cpuinfo@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num_cpus>:
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xebc(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1040 <readfile@plt>
test   %eax,%eax
jne    1174 <get_num_cpus+0x54>
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
je     1174 <get_num_cpus+0x54>
mov    0x8(%rsp),%esi
call   1030 <parse_cpuinfo@plt>
add    $0x18,%rsp
ret
cltq
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
define i64 @get_num_cpus() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %2
store i8* null, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
%6 = call i32 @readfile(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %3, i32* %4, i32* %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9, %0
%13 = load i32, i32* %2
%14 = sext i32 %13 to i64
store i64 %14, i64* %1
br label %19
15: ; preds = %9
%16 = load i8*, i8** %3
%17 = load i32, i32* %4
%18 = call i64 @parse_cpuinfo(i8* %16, i32 %17)
store i64 %18, i64* %1
br label %19
19: ; preds = %15, %12
%20 = load i64, i64* %1
ret i64 %20
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
define i64 @get_num_cpus() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = call i32 @readfile(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %1, i32* %2, i32* %3)
%8 = icmp eq i32 %7, 0
%9 = load i8*, i8** %1
%10 = icmp ne i8* %9, null
%11 = select i1 %8, i1 %10, i1 false
br i1 %11, label %14, label %12
12: ; preds = %0
%13 = sext i32 %7 to i64
br label %17
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i64 @parse_cpuinfo(i8* %9, i32 %15)
br label %17
17: ; preds = %14, %12
%18 = phi i64 [ %16, %14 ], [ %13, %12 ]
ret i64 %18
}