#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strncmp(char *, char *, int);
int get_numCPUs() {
  int cpucount = 0;
  char line[1024];
  FILE *fp = fopen("/proc/cpuinfo", "r");
  if (fp != NULL) {
    while (fgets(line, 1024, fp)) {
      if (strncmp(line, "processor", 9) == 0) {
        cpucount++;
      }
    }
    fclose(fp);
  }
  return cpucount;
}


x86 assembly (clang, -O0):
<get_numCPUs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xeb6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     11dd <get_numCPUs+0x9d>
jmp    117f <get_numCPUs+0x3f>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11d1 <get_numCPUs+0x91>
lea    -0x410(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11cc <get_numCPUs+0x8c>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    117f <get_numCPUs+0x3f>
mov    -0x418(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_numCPUs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xeac(%rip),%rdi
lea    0xebb(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c6 <get_numCPUs+0x86>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11bc <get_numCPUs+0x7c>
lea    0xe88(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <get_numCPUs+0x50>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11c8 <get_numCPUs+0x88>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"processor\00"
define i32 @get_numCPUs() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %24
7: ; preds = %0
br label %8
8: ; preds = %20, %7
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%10 = load i32*, i32** %3
%11 = call i64 @fgets(i8* %9, i32 1024, i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%15 = call i64 @strncmp(i8* %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %1
%19 = add nsw i32 %18, 1
store i32 %19, i32* %1
br label %20
20: ; preds = %17, %13
br label %8
21: ; preds = %8
%22 = load i32*, i32** %3
%23 = call i32 @fclose(i32* %22)
br label %24
24: ; preds = %21, %0
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"processor\00"
define i32 @get_numCPUs() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %19, label %5
5: ; preds = %0
%6 = call i64 @fgets(i8* %2, i32 1024, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %5, %8
%9 = phi i32 [ %13, %8 ], [ 0, %5 ]
%10 = call i64 @strncmp(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 9)
%11 = icmp eq i64 %10, 0
%12 = zext i1 %11 to i32
%13 = add nuw nsw i32 %9, %12
%14 = call i64 @fgets(i8* %2, i32 1024, i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %5
%17 = phi i32 [ 0, %5 ], [ %13, %8 ]
%18 = call i32 @fclose(i32* %3)
br label %19
19: ; preds = %16, %0
%20 = phi i32 [ %17, %16 ], [ 0, %0 ]
ret i32 %20
}