#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isdigit(char);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int get_sys_cpu_nr(int highest) {
  DIR *dir;
  struct dirent *drd;
  struct stat buf;
  char line[1024];
  int num_proc, proc_nr = -1;
  if ((dir = opendir(""
                     "/sys/devices/system/cpu")) == NULL)
    return 0;
  while ((drd = readdir(dir)) != NULL) {
    if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
      snprintf(line, sizeof(line), "%s/%s",
               ""
               "/sys/devices/system/cpu",
               drd->d_name);
      line[sizeof(line) - 1] = '\0';
      if (stat(line, &buf) < 0)
        continue;
      if (S_ISDIR(buf.st_mode)) {
        if (highest) {
          sscanf(drd->d_name + 3, "%d", &num_proc);
          if (num_proc > proc_nr) {
            proc_nr = num_proc;
          }
        } else {
          proc_nr++;
        }
      }
    }
  }
  closedir(dir);
  return (proc_nr + 1);
}


x86 assembly (clang, -O0):
<get_sys_cpu_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x428(%rbp)
ff ff ff
lea    0xe51(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ce <get_sys_cpu_nr+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    12fe <get_sys_cpu_nr+0x16e>
jmp    11d3 <get_sys_cpu_nr+0x43>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12e9 <get_sys_cpu_nr+0x159>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1c(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12e4 <get_sys_cpu_nr+0x154>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x3(%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     12e4 <get_sys_cpu_nr+0x154>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdd9(%rip),%rdx
lea    0xdba(%rip),%rcx
call   1070 <snprintf@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x420(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    126e <get_sys_cpu_nr+0xde>
jmp    11d3 <get_sys_cpu_nr+0x43>
mov    -0x20(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12df <get_sys_cpu_nr+0x14f>
cmpl   $0x0,-0x8(%rbp)
je     12cb <get_sys_cpu_nr+0x13b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
add    $0x3,%rdi
lea    0xd82(%rip),%rsi
lea    -0x424(%rbp),%rdx
call   10b0 <sscanf@plt>
mov    -0x424(%rbp),%eax
cmp    -0x428(%rbp),%eax
jle    12c6 <get_sys_cpu_nr+0x136>
mov    -0x424(%rbp),%eax
mov    %eax,-0x428(%rbp)
jmp    12da <get_sys_cpu_nr+0x14a>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    12df <get_sys_cpu_nr+0x14f>
jmp    12e4 <get_sys_cpu_nr+0x154>
jmp    11d3 <get_sys_cpu_nr+0x43>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sys_cpu_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edi,%ebx
lea    0xe56(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12ac <get_sys_cpu_nr+0x11c>
mov    %rax,%rbp
mov    %ebx,0x18(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe46(%rip),%rbx
lea    0xe2b(%rip),%r13
lea    0x20(%rsp),%r14
lea    0x10(%rsp),%r15
nop
mov    %rbp,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     129b <get_sys_cpu_nr+0x10b>
mov    %rax,%r12
mov    (%rax),%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11e0 <get_sys_cpu_nr+0x50>
mov    (%r12),%rax
movsbl 0x3(%rax),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     11e0 <get_sys_cpu_nr+0x50>
mov    (%r12),%r8
mov    %r14,%rdi
mov    $0x400,%esi
lea    0xdeb(%rip),%rdx
mov    %r13,%rcx
call   1070 <snprintf@plt>
movb   $0x0,0x41f(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <stat@plt>
test   %rax,%rax
js     11e0 <get_sys_cpu_nr+0x50>
mov    0x10(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     11e0 <get_sys_cpu_nr+0x50>
cmpl   $0x0,0x18(%rsp)
je     1291 <get_sys_cpu_nr+0x101>
mov    (%r12),%rdi
add    $0x3,%rdi
lea    0xdad(%rip),%rsi
lea    0x1c(%rsp),%rdx
call   10b0 <sscanf@plt>
mov    0x1c(%rsp),%eax
mov    0xc(%rsp),%ecx
cmp    %ecx,%eax
cmovg  %eax,%ecx
mov    %ecx,0xc(%rsp)
jmp    11e0 <get_sys_cpu_nr+0x50>
addl   $0x1,0xc(%rsp)
jmp    11e0 <get_sys_cpu_nr+0x50>
mov    %rbp,%rdi
call   1080 <closedir@plt>
mov    0xc(%rsp),%eax
add    $0x1,%eax
jmp    12ae <get_sys_cpu_nr+0x11e>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [24 x i8] c"/sys/devices/system/cpu\00"
@.str.1 = [4 x i8] c"cpu\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @get_sys_cpu_nr(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %9
%10 = call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %74
13: ; preds = %1
br label %14
14: ; preds = %68, %42, %13
%15 = load i32*, i32** %4
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %5
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %69
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %5
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strncmp(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%23 = icmp ne i32 %22, 0
br i1 %23, label %68, label %24
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 3
%29 = load i8, i8* %28
%30 = call i64 @isdigit(i8 signext %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %68
32: ; preds = %24
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%34 = load %struct.dirent*, %struct.dirent** %5
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @snprintf(i8* %33, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 1023
store i8 0, i8* %38
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = call i64 @stat(i8* %39, %struct.stat* %6)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %32
br label %14
43: ; preds = %32
%44 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @S_ISDIR(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %67
48: ; preds = %43
%49 = load i32, i32* %3
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %63
51: ; preds = %48
%52 = load %struct.dirent*, %struct.dirent** %5
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = getelementptr inbounds i8, i8* %54, i64 3
%56 = call i32 @sscanf(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = icmp sgt i32 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %51
%61 = load i32, i32* %8
store i32 %61, i32* %9
br label %62
62: ; preds = %60, %51
br label %66
63: ; preds = %48
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %66
66: ; preds = %63, %62
br label %67
67: ; preds = %66, %43
br label %68
68: ; preds = %67, %24, %18
br label %14
69: ; preds = %14
%70 = load i32*, i32** %4
%71 = call i32 @closedir(i32* %70)
%72 = load i32, i32* %9
%73 = add nsw i32 %72, 1
store i32 %73, i32* %2
br label %74
74: ; preds = %69, %12
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [24 x i8] c"/sys/devices/system/cpu\00"
@.str.1 = [4 x i8] c"cpu\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @get_sys_cpu_nr(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = bitcast %struct.stat* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %56, label %10
10: ; preds = %1
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1023
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = icmp eq i32 %0, 0
br label %14
14: ; preds = %51, %10
%15 = phi i32 [ -1, %10 ], [ %52, %51 ]
br label %16
16: ; preds = %40, %14
br label %17
17: ; preds = %16, %31
%18 = call %struct.dirent* @readdir(i32* %8)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %53, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strncmp(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = load i8*, i8** %21
%27 = getelementptr inbounds i8, i8* %26, i64 3
%28 = load i8, i8* %27
%29 = call i64 @isdigit(i8 signext %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %40, label %31
31: ; preds = %25
%32 = load i8*, i8** %21
%33 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %32)
store i8 0, i8* %11
%34 = call i64 @stat(i8* %6, %struct.stat* %2)
%35 = icmp slt i64 %34, 0
br i1 %35, label %17, label %36
36: ; preds = %31
%37 = load i32, i32* %12
%38 = call i64 @S_ISDIR(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %20, %25, %36
br label %16
41: ; preds = %36
br i1 %13, label %49, label %42
42: ; preds = %41
%43 = load i8*, i8** %21
%44 = getelementptr inbounds i8, i8* %43, i64 3
%45 = call i32 @sscanf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%46 = load i32, i32* %4
%47 = icmp sgt i32 %46, %15
%48 = select i1 %47, i32 %46, i32 %15
br label %51
49: ; preds = %41
%50 = add nsw i32 %15, 1
br label %51
51: ; preds = %49, %42
%52 = phi i32 [ %48, %42 ], [ %50, %49 ]
br label %14
53: ; preds = %17
%54 = call i32 @closedir(i32* %8)
%55 = add nsw i32 %15, 1
br label %56
56: ; preds = %1, %53
%57 = phi i32 [ %55, %53 ], [ 0, %1 ]
ret i32 %57
}