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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int get_num_cpu() {
  FILE *fd;
  char str[128];
  int ncpu = 0;
  if ((fd = fopen("/proc/cpuinfo", "r")) == NULL) {
    logger(-1, errno, "Cannot open /proc/cpuinfo");
    return 1;
  }
  while (fgets(str, sizeof(str), fd)) {
    if (!strncmp(str, "processor", 9))
      ncpu++;
  }
  fclose(fd);
  return !ncpu ? 1 : ncpu;
}


x86 assembly (clang, -O0):
<get_num_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x94(%rbp)
lea    0xea0(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ac <get_num_cpu+0x5c>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
lea    0xe65(%rip),%rdx
call   1060 <logger@plt>
movl   $0x1,-0x4(%rbp)
jmp    1240 <get_num_cpu+0xf0>
jmp    11b1 <get_num_cpu+0x61>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1205 <get_num_cpu+0xb5>
lea    -0x90(%rbp),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1200 <get_num_cpu+0xb0>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
jmp    11b1 <get_num_cpu+0x61>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x94(%rbp)
jne    122b <get_num_cpu+0xdb>
mov    $0x1,%eax
mov    %eax,-0x98(%rbp)
jmp    1237 <get_num_cpu+0xe7>
mov    -0x94(%rbp),%eax
mov    %eax,-0x98(%rbp)
mov    -0x98(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num_cpu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
lea    0xea8(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d9 <get_num_cpu+0x89>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f5 <get_num_cpu+0xa5>
xor    %ebp,%ebp
lea    0xe84(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%eax
adc    $0x0,%ebp
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11a0 <get_num_cpu+0x50>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %ebp,%ebp
jne    1202 <get_num_cpu+0xb2>
jmp    11fd <get_num_cpu+0xad>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdx
mov    $0xffffffff,%edi
call   1060 <logger@plt>
jmp    11fd <get_num_cpu+0xad>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x88,%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"
@errno = global i32 0
@.str.2 = [26 x i8] c"Cannot open /proc/cpuinfo\00"
@.str.3 = [10 x i8] c"processor\00"
define i32 @get_num_cpu() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [128 x i8]
%4 = alloca i32
store i32 0, i32* %4
%5 = 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* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = call i32 @logger(i32 -1, i32 %8, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %34
10: ; preds = %0
br label %11
11: ; preds = %23, %10
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i64 @fgets(i8* %12, i32 128, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%18 = call i32 @strncmp(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %23
23: ; preds = %20, %16
br label %11
24: ; preds = %11
%25 = load i32*, i32** %2
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %24
br label %32
30: ; preds = %24
%31 = load i32, i32* %4
br label %32
32: ; preds = %30, %29
%33 = phi i32 [ 1, %29 ], [ %31, %30 ]
store i32 %33, i32* %1
br label %34
34: ; preds = %32, %7
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Cannot open /proc/cpuinfo\00"
@.str.3 = [10 x i8] c"processor\00"
define i32 @get_num_cpu() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 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 %10, label %5
5: ; preds = %0
%6 = call i64 @fgets(i8* %2, i32 128, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = call i32 @fclose(i32* %3)
br label %24
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = tail call i32 @logger(i32 -1, i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %25
13: ; preds = %5, %13
%14 = phi i32 [ %18, %13 ], [ 0, %5 ]
%15 = call i32 @strncmp(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%16 = icmp eq i32 %15, 0
%17 = zext i1 %16 to i32
%18 = add nuw nsw i32 %14, %17
%19 = call i64 @fgets(i8* %2, i32 128, i32* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %13
21: ; preds = %13
%22 = call i32 @fclose(i32* %3)
%23 = icmp eq i32 %18, 0
br i1 %23, label %24, label %25
24: ; preds = %8, %21
br label %25
25: ; preds = %24, %21, %10
%26 = phi i32 [ 1, %10 ], [ 1, %24 ], [ %18, %21 ]
ret i32 %26
}