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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, int *);
int get_online_cpu_from_cpuinfo(void) {
  FILE *f;
  int cpu = -1;
  char line[4096];
  f = fopen("/proc/cpuinfo", "r");
  if (!f)
    return -1;
  while (!feof(f)) {
    if (!fgets(line, sizeof(line), f))
      return -1;
    if (sscanf(line, "processor%*[^0123456789]%d", &cpu) == 1)
      break;
  }
  fclose(f);
  return cpu;
}


x86 assembly (clang, -O0):
<get_online_cpu_from_cpuinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0xffffffff,-0x14(%rbp)
lea    0xe97(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <get_online_cpu_from_cpuinfo+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <get_online_cpu_from_cpuinfo+0xc6>
jmp    1195 <get_online_cpu_from_cpuinfo+0x45>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b3 <get_online_cpu_from_cpuinfo+0x63>
jmp    1207 <get_online_cpu_from_cpuinfo+0xb7>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    11dd <get_online_cpu_from_cpuinfo+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <get_online_cpu_from_cpuinfo+0xc6>
lea    -0x1020(%rbp),%rdi
lea    0xe25(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1202 <get_online_cpu_from_cpuinfo+0xb2>
jmp    1207 <get_online_cpu_from_cpuinfo+0xb7>
jmp    1195 <get_online_cpu_from_cpuinfo+0x45>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_online_cpu_from_cpuinfo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe92(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11e0 <get_online_cpu_from_cpuinfo+0x90>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
lea    0xe7c(%rip),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    11d3 <get_online_cpu_from_cpuinfo+0x83>
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     11e0 <get_online_cpu_from_cpuinfo+0x90>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11a0 <get_online_cpu_from_cpuinfo+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x1010,%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"
@.str.2 = [27 x i8] c"processor%*[^0123456789]%d\00"
define i32 @get_online_cpu_from_cpuinfo() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [4096 x i8]
store i32 -1, i32* %3
%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 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %9, label %8
8: ; preds = %0
store i32 -1, i32* %1
br label %31
9: ; preds = %0
br label %10
10: ; preds = %26, %9
%11 = load i32*, i32** %2
%12 = call i32 @feof(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %27
15: ; preds = %10
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%17 = load i32*, i32** %2
%18 = call i32 @fgets(i8* %16, i32 4096, i32* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 -1, i32* %1
br label %31
21: ; preds = %15
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %21
br label %27
26: ; preds = %21
br label %10
27: ; preds = %25, %10
%28 = load i32*, i32** %2
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %3
store i32 %30, i32* %1
br label %31
31: ; preds = %27, %20, %8
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"processor%*[^0123456789]%d\00"
define i32 @get_online_cpu_from_cpuinfo() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %0, %13
%8 = call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = call i32 @fgets(i8* %4, i32 4096, i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %7
16: ; preds = %13, %7
%17 = call i32 @fclose(i32* %5)
%18 = load i32, i32* %1
br label %19
19: ; preds = %10, %0, %16
%20 = phi i32 [ %18, %16 ], [ -1, %0 ], [ -1, %10 ]
ret i32 %20
}