#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*/ proc_ptrn;
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 const *, int);
int get_num_cpu(void) {
  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)) {
    char const proc_ptrn[] = "processor";
    if (!strncmp(str, proc_ptrn, sizeof(proc_ptrn) - 1))
      ncpu++;
  }
  fclose(fd);
  return !ncpu ? 1 : ncpu;
}


x86 assembly (clang, -O0):
<get_num_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%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    125c <get_num_cpu+0x10c>
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     1221 <get_num_cpu+0xd1>
mov    0xe43(%rip),%rax
mov    %rax,-0x9e(%rbp)
mov    0xe3d(%rip),%ax
mov    %ax,-0x96(%rbp)
lea    -0x90(%rbp),%rdi
lea    -0x9e(%rbp),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    121c <get_num_cpu+0xcc>
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    1247 <get_num_cpu+0xf7>
mov    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1253 <get_num_cpu+0x103>
mov    -0x94(%rbp),%eax
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num_cpu>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
lea    0xea6(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f4 <get_num_cpu+0xa4>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1210 <get_num_cpu+0xc0>
xor    %ebp,%ebp
movabs $0x6f737365636f7270,%r12
73 73 6f
lea    0x10(%rsp),%rbx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,(%rsp)
movw   $0x72,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%eax
adc    $0x0,%ebp
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <get_num_cpu+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %ebp,%ebp
jne    121d <get_num_cpu+0xcd>
jmp    1218 <get_num_cpu+0xc8>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xdfc(%rip),%rdx
mov    $0xffffffff,%edi
call   1060 <logger@plt>
jmp    1218 <get_num_cpu+0xc8>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
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"
@__const.get_num_cpu.proc_ptrn = [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
%5 = alloca [10 x i8]
store i32 0, i32* %4
%6 = 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* %6, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 @logger(i32 -1, i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %37
11: ; preds = %0
br label %12
12: ; preds = %26, %11
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i64 @fgets(i8* %13, i32 128, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = bitcast [10 x i8]* %5 to i8*
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%21 = call i32 @strncmp(i8* %19, i8* %20, i32 9)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %17
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %17
br label %12
27: ; preds = %12
%28 = load i32*, i32** %2
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27
br label %35
33: ; preds = %27
%34 = load i32, i32* %4
br label %35
35: ; preds = %33, %32
%36 = phi i32 [ 1, %32 ], [ %34, %33 ]
store i32 %36, i32* %1
br label %37
37: ; preds = %35, %8
%38 = load i32, i32* %1
ret i32 %38
}

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