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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int getnumcpu(void) {
  FILE *fp;
  char linebuf[4096], label[256];
  int cpunum, maxcpu = 0;
  if ((fp = fopen("/proc/stat", "r")) != NULL) {
    while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
      sscanf(linebuf, "%255s", label);
      if (strncmp("cpu", label, 3) == 0 && strlen(label) > 3) {
        cpunum = atoi(&label[3]);
        if (maxcpu < cpunum)
          maxcpu = cpunum;
      }
      if (strncmp("int", label, 3) == 0)
        break;
    }
    fclose(fp);
  }
  return maxcpu + 1;
}


x86 assembly (clang, -O0):
<getnumcpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1120,%rsp
movl   $0x0,-0x1118(%rbp)
lea    0xe74(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     1289 <getnumcpu+0x119>
jmp    11ab <getnumcpu+0x3b>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1280 <getnumcpu+0x110>
lea    -0x1010(%rbp),%rdi
lea    -0x1110(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   1070 <sscanf@plt>
lea    -0x1110(%rbp),%rsi
lea    0xe21(%rip),%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1254 <getnumcpu+0xe4>
lea    -0x1110(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x3,%eax
jle    1254 <getnumcpu+0xe4>
lea    -0x1110(%rbp),%rdi
add    $0x3,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1114(%rbp)
mov    -0x1118(%rbp),%eax
cmp    -0x1114(%rbp),%eax
jge    124f <getnumcpu+0xdf>
mov    -0x1114(%rbp),%eax
mov    %eax,-0x1118(%rbp)
jmp    1254 <getnumcpu+0xe4>
lea    -0x1110(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    127b <getnumcpu+0x10b>
jmp    1280 <getnumcpu+0x110>
jmp    11ab <getnumcpu+0x3b>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1118(%rbp),%eax
add    $0x1,%eax
add    $0x1120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnumcpu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1108,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     123b <getnumcpu+0xcb>
mov    %rax,%rbx
xor    %r15d,%r15d
lea    0x100(%rsp),%r13
mov    %rsp,%rbp
lea    0xe5e(%rip),%r14
lea    0xe5b(%rip),%r12
jmp    11d5 <getnumcpu+0x65>
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1231 <getnumcpu+0xc1>
mov    %r13,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1231 <getnumcpu+0xc1>
mov    %r13,%rdi
lea    0xe19(%rip),%rsi
mov    %rbp,%rdx
call   1070 <sscanf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <getnumcpu+0x50>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jl     11c0 <getnumcpu+0x50>
lea    0x3(%rsp),%rdi
call   1090 <atoi@plt>
cmp    %eax,%r15d
cmovl  %eax,%r15d
jmp    11c0 <getnumcpu+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    123e <getnumcpu+0xce>
xor    %r15d,%r15d
add    $0x1,%r15d
mov    %r15d,%eax
add    $0x1108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%255s\00"
@.str.3 = [4 x i8] c"cpu\00"
@.str.4 = [4 x i8] c"int\00"
define i32 @getnumcpu() {
%1 = alloca [4096 x i8]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%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 %31, label %7
7: ; preds = %0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 3
br label %9
9: ; preds = %7, %24
%10 = phi i32 [ %25, %24 ], [ 0, %7 ]
%11 = call i32* @fgets(i8* %3, i32 4096, i32* %5)
%12 = icmp eq i32* %11, null
br i1 %12, label %28, label %13
13: ; preds = %9
%14 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%15 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = call i32 @strlen(i8* %4)
%19 = icmp sgt i32 %18, 3
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = call i32 @atoi(i8* %8)
%22 = icmp slt i32 %10, %21
%23 = select i1 %22, i32 %21, i32 %10
br label %24
24: ; preds = %20, %17, %13
%25 = phi i32 [ %10, %17 ], [ %10, %13 ], [ %23, %20 ]
%26 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32 3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %9
28: ; preds = %24, %9
%29 = phi i32 [ %25, %24 ], [ %10, %9 ]
%30 = call i32 @fclose(i32* %5)
br label %31
31: ; preds = %28, %0
%32 = phi i32 [ %29, %28 ], [ 0, %0 ]
%33 = add nsw i32 %32, 1
ret i32 %33
}