#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int get_proc_cpu_nr(void) {
  FILE *fp;
  char line[16];
  int num_proc, proc_nr = -1;
  if ((fp = fopen("/proc/stat", "r")) == NULL) {
    fprintf(stderr, ("Cannot open %s: %s\n"), "/proc/stat", strerror(errno));
    exit(1);
  }
  while (fgets(line, 16, fp) != NULL) {
    if (strncmp(line, "cpu ", 4) && !strncmp(line, "cpu", 3)) {
      sscanf(line + 3, "%d", &num_proc);
      if (num_proc > proc_nr) {
        proc_nr = num_proc;
      }
    }
  }
  fclose(fp);
  return (proc_nr + 1);
}


x86 assembly (clang, -O0):
<get_proc_cpu_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x28(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11ed <get_proc_cpu_nr+0x6d>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rcx
lea    0xe36(%rip),%rsi
lea    0xe22(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11f2 <get_proc_cpu_nr+0x72>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x10,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1280 <get_proc_cpu_nr+0x100>
lea    -0x20(%rbp),%rdi
lea    0xe08(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     127b <get_proc_cpu_nr+0xfb>
lea    -0x20(%rbp),%rdi
lea    0xdee(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    127b <get_proc_cpu_nr+0xfb>
lea    -0x20(%rbp),%rdi
add    $0x3,%rdi
lea    0xdcf(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    1276 <get_proc_cpu_nr+0xf6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    127b <get_proc_cpu_nr+0xfb>
jmp    11f2 <get_proc_cpu_nr+0x72>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_cpu_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe6b(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     126e <get_proc_cpu_nr+0xee>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     1250 <get_proc_cpu_nr+0xd0>
lea    0xe4c(%rip),%r15
lea    0x10(%rsp),%rbp
lea    0xe45(%rip),%r13
lea    0xc(%rsp),%r14
jmp    1205 <get_proc_cpu_nr+0x85>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1250 <get_proc_cpu_nr+0xd0>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11f0 <get_proc_cpu_nr+0x70>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11f0 <get_proc_cpu_nr+0x70>
lea    0x13(%rsp),%rdi
lea    0xdef(%rip),%rsi
mov    %r14,%rdx
call   1070 <sscanf@plt>
mov    0xc(%rsp),%eax
cmp    %r12d,%eax
cmovg  %eax,%r12d
jmp    11f0 <get_proc_cpu_nr+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x1,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd81(%rip),%rsi
lea    0xd6d(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"cpu \00"
@.str.4 = [4 x i8] c"cpu\00"
@.str.5 = [3 x i8] c"%d\00"
define i32 @get_proc_cpu_nr() {
%1 = alloca i32*
%2 = alloca [16 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 -1, i32* %4
%5 = 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))
store i32* %5, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
br label %14
14: ; preds = %37, %13
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%16 = load i32*, i32** %1
%17 = call i32* @fgets(i8* %15, i32 16, i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %38
19: ; preds = %14
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%25 = call i64 @strncmp(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%26 = icmp ne i64 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %23
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%29 = getelementptr inbounds i8, i8* %28, i64 3
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = icmp sgt i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = load i32, i32* %3
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %27
br label %37
37: ; preds = %36, %23, %19
br label %14
38: ; preds = %14
%39 = load i32*, i32** %1
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
ret i32 %42
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"cpu \00"
@.str.4 = [4 x i8] c"cpu\00"
@.str.5 = [3 x i8] c"%d\00"
define i32 @get_proc_cpu_nr() {
%1 = alloca [16 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %0
%8 = call i32* @fgets(i8* %3, i32 16, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %34, label %10
10: ; preds = %7
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 3
br label %18
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10, %30
%19 = phi i32 [ -1, %10 ], [ %31, %30 ]
%20 = call i64 @strncmp(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %18
%23 = call i64 @strncmp(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %2)
%27 = load i32, i32* %2
%28 = icmp sgt i32 %27, %19
%29 = select i1 %28, i32 %27, i32 %19
br label %30
30: ; preds = %25, %22, %18
%31 = phi i32 [ %19, %22 ], [ %19, %18 ], [ %29, %25 ]
%32 = call i32* @fgets(i8* %3, i32 16, i32* %5)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %18
34: ; preds = %30, %7
%35 = phi i32 [ -1, %7 ], [ %31, %30 ]
%36 = call i32 @fclose(i32* %5)
%37 = add nsw i32 %35, 1
ret i32 %37
}