#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int get_kernel_nr_cpus(void) {
  int nr_cpus = 1;
  FILE *fff = fopen("/sys/devices/system/cpu/kernel_max", "r");
  if (fff == NULL)
    return nr_cpus;
  int num_read = fscanf(fff, "%d", &nr_cpus);
  fclose(fff);
  return (num_read == 1) ? (nr_cpus + 1) : 1;
}


x86 assembly (clang, -O0):
<get_kernel_nr_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x8(%rbp)
lea    0xeba(%rip),%rdi
lea    0xed6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <get_kernel_nr_cpus+0x3c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <get_kernel_nr_cpus+0x87>
mov    -0x10(%rbp),%rdi
lea    0xeae(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x1,-0x14(%rbp)
jne    11a4 <get_kernel_nr_cpus+0x74>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b1 <get_kernel_nr_cpus+0x81>
mov    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b1 <get_kernel_nr_cpus+0x81>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_nr_cpus>:
push   %rbp
push   %rbx
push   %rax
movl   $0x1,0x4(%rsp)
lea    0xebe(%rip),%rdi
lea    0xeda(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1188 <get_kernel_nr_cpus+0x58>
mov    %rax,%rbx
lea    0xec8(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%ecx
add    $0x1,%ecx
cmp    $0x1,%ebp
mov    $0x1,%eax
cmove  %ecx,%eax
jmp    118d <get_kernel_nr_cpus+0x5d>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"/sys/devices/system/cpu/kernel_max\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @get_kernel_nr_cpus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 1, i32* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([35 x i8], [35 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** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* %2
store i32 %9, i32* %1
br label %23
10: ; preds = %0
%11 = load i32*, i32** %3
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
store i32 %12, i32* %4
%13 = load i32*, i32** %3
%14 = call i32 @fclose(i32* %13)
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
br label %21
20: ; preds = %10
br label %21
21: ; preds = %20, %17
%22 = phi i32 [ %19, %17 ], [ 1, %20 ]
store i32 %22, i32* %1
br label %23
23: ; preds = %21, %8
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [35 x i8] c"/sys/devices/system/cpu/kernel_max\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @get_kernel_nr_cpus() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([35 x i8], [35 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 %12, label %5
5: ; preds = %0
%6 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%7 = call i32 @fclose(i32* %3)
%8 = icmp eq i32 %6, 1
%9 = load i32, i32* %1
%10 = add nsw i32 %9, 1
%11 = select i1 %8, i32 %10, i32 1
br label %12
12: ; preds = %0, %5
%13 = phi i32 [ %11, %5 ], [ 1, %0 ]
ret i32 %13
}