#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*/ err_exit(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, ...);
unsigned long get_online_cpus(void) {
  FILE *fp;
  unsigned long cpu_num;
  int cpu_id, ret;
  char *proc_stat = "/proc/stat";
  fp = fopen(proc_stat, "r");
  if (!fp)
    err_exit("Open failed /proc/stat");
  ret = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
  if (ret)
    err_exit("Failed to parse format");
  while (1) {
    ret =
        fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_id);
    if (ret != 1)
      break;
    cpu_num++;
  }
  fclose(fp);
  return cpu_num;
}


x86 assembly (clang, -O0):
<get_online_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xebf(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117e <get_online_cpus+0x3e>
lea    0xe89(%rip),%rdi
call   1050 <err_exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11a9 <get_online_cpus+0x69>
lea    0xea2(%rip),%rdi
call   1050 <err_exit@plt>
jmp    11ae <get_online_cpus+0x6e>
mov    -0x8(%rbp),%rdi
lea    0xea4(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     11d6 <get_online_cpus+0x96>
jmp    11e7 <get_online_cpus+0xa7>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11ae <get_online_cpus+0x6e>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_online_cpus>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xebf(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    116f <get_online_cpus+0x2f>
lea    0xe98(%rip),%rdi
call   1050 <err_exit@plt>
lea    0xea3(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
test   %eax,%eax
je     1190 <get_online_cpus+0x50>
lea    0xebb(%rip),%rdi
call   1050 <err_exit@plt>
lea    0xec6(%rip),%r15
lea    0x4(%rsp),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
add    $0x1,%rbx
cmp    $0x1,%eax
je     11a0 <get_online_cpus+0x60>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Open failed /proc/stat\00"
@.str.3 = [45 x i8] c"cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\0A\00"
@.str.4 = [23 x i8] c"Failed to parse format\00"
@.str.5 = [47 x i8] c"cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\0A\00"
define i64 @get_online_cpus() {
%1 = alloca i32*
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %1
%8 = load i32*, i32** %1
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @err_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32*, i32** %1
%14 = call i32 (i32*, i8*, ...) @fscanf(i32* %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @err_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
br label %20
20: ; preds = %19, %26
%21 = load i32*, i32** %1
%22 = call i32 (i32*, i8*, ...) @fscanf(i32* %21, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i32* %3)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %20
br label %29
26: ; preds = %20
%27 = load i64, i64* %2
%28 = add i64 %27, 1
store i64 %28, i64* %2
br label %20
29: ; preds = %25
%30 = load i32*, i32** %1
%31 = call i32 @fclose(i32* %30)
%32 = load i64, i64* %2
ret i64 %32
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Open failed /proc/stat\00"
@.str.3 = [45 x i8] c"cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\0A\00"
@.str.4 = [23 x i8] c"Failed to parse format\00"
@.str.5 = [47 x i8] c"cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\0A\00"
define i64 @get_online_cpus() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 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 %5, label %7
5: ; preds = %0
%6 = tail call i32 @err_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i32 (i32*, i8*, ...) @fscanf(i32* %3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @err_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
br label %13
13: ; preds = %12, %13
%14 = phi i64 [ %17, %13 ], [ undef, %12 ]
%15 = call i32 (i32*, i8*, ...) @fscanf(i32* %3, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%16 = icmp eq i32 %15, 1
%17 = add i64 %14, 1
br i1 %16, label %13, label %18
18: ; preds = %13
%19 = call i32 @fclose(i32* %3)
ret i64 %14
}