#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*/ 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 *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int get_sys_cpu_nr(void) {
  DIR *dir;
  struct dirent *drd;
  struct stat buf;
  char line[1024];
  int proc_nr = 0;
  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, 1024, "%s/%s", "/sys/devices/system/cpu", drd->d_name);
      line[1024 - 1] = '\0';
      if (stat(line, &buf) < 0)
        continue;
      if (S_ISDIR(buf.st_mode)) {
        proc_nr++;
      }
    }
  }
  closedir(dir);
  return proc_nr;
}


x86 assembly (clang, -O0):
<get_sys_cpu_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x424(%rbp)
lea    0xe64(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11bb <get_sys_cpu_nr+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    1298 <get_sys_cpu_nr+0x118>
jmp    11c0 <get_sys_cpu_nr+0x40>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1286 <get_sys_cpu_nr+0x106>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2f(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1281 <get_sys_cpu_nr+0x101>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x3(%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     1281 <get_sys_cpu_nr+0x101>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdec(%rip),%rdx
lea    0xdcd(%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    125b <get_sys_cpu_nr+0xdb>
jmp    11c0 <get_sys_cpu_nr+0x40>
mov    -0x20(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     127c <get_sys_cpu_nr+0xfc>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    1281 <get_sys_cpu_nr+0x101>
jmp    11c0 <get_sys_cpu_nr+0x40>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x424(%rbp),%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    $0x418,%rsp
lea    0xe68(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1259 <get_sys_cpu_nr+0xd9>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
lea    0xe5c(%rip),%r13
lea    0xe41(%rip),%r12
lea    0x10(%rsp),%rbp
lea    0x8(%rsp),%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     124b <get_sys_cpu_nr+0xcb>
mov    %rax,%r15
mov    (%rax),%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11d0 <get_sys_cpu_nr+0x50>
mov    (%r15),%rax
movsbl 0x3(%rax),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     11d0 <get_sys_cpu_nr+0x50>
mov    (%r15),%r8
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xe01(%rip),%rdx
mov    %r12,%rcx
call   1070 <snprintf@plt>
movb   $0x0,0x40f(%rsp)
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <stat@plt>
test   %rax,%rax
js     11d0 <get_sys_cpu_nr+0x50>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x1,%rax
sbbl   $0xffffffff,0x4(%rsp)
jmp    11d0 <get_sys_cpu_nr+0x50>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    0x4(%rsp),%eax
jmp    125b <get_sys_cpu_nr+0xdb>
xor    %eax,%eax
add    $0x418,%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"
define i32 @get_sys_cpu_nr() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 0, i32* %6
%7 = call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 0, i32* %1
br label %54
10: ; preds = %0
br label %11
11: ; preds = %49, %39, %10
%12 = load i32*, i32** %2
%13 = call %struct.dirent* @readdir(i32* %12)
store %struct.dirent* %13, %struct.dirent** %3
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %50
15: ; preds = %11
%16 = load %struct.dirent*, %struct.dirent** %3
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strncmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%20 = icmp ne i32 %19, 0
br i1 %20, label %49, label %21
21: ; preds = %15
%22 = load %struct.dirent*, %struct.dirent** %3
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8, i8* %24, i64 3
%26 = load i8, i8* %25
%27 = call i64 @isdigit(i8 signext %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %49
29: ; preds = %21
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%31 = load %struct.dirent*, %struct.dirent** %3
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %30, 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* %33)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 1023
store i8 0, i8* %35
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%37 = call i64 @stat(i8* %36, %struct.stat* %4)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %29
br label %11
40: ; preds = %29
%41 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @S_ISDIR(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %48
48: ; preds = %45, %40
br label %49
49: ; preds = %48, %21, %15
br label %11
50: ; preds = %11
%51 = load i32*, i32** %2
%52 = call i32 @closedir(i32* %51)
%53 = load i32, i32* %6
store i32 %53, i32* %1
br label %54
54: ; preds = %50, %9
%55 = load i32, i32* %1
ret i32 %55
}

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"
define i32 @get_sys_cpu_nr() {
%1 = alloca %struct.stat
%2 = alloca [1024 x i8]
%3 = bitcast %struct.stat* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %41, label %7
7: ; preds = %0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1023
%9 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %10
10: ; preds = %33, %7
%11 = phi i32 [ %38, %33 ], [ 0, %7 ]
br label %13
12: ; preds = %22, %17
br label %13
13: ; preds = %10, %12
br label %14
14: ; preds = %13, %28
%15 = call %struct.dirent* @readdir(i32* %5)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %39, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @strncmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %17
%23 = load i8*, i8** %18
%24 = getelementptr inbounds i8, i8* %23, i64 3
%25 = load i8, i8* %24
%26 = call i64 @isdigit(i8 signext %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %12, label %28
28: ; preds = %22
%29 = load i8*, i8** %18
%30 = call i32 @snprintf(i8* %4, 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* %29)
store i8 0, i8* %8
%31 = call i64 @stat(i8* %4, %struct.stat* %1)
%32 = icmp slt i64 %31, 0
br i1 %32, label %14, label %33
33: ; preds = %28
%34 = load i32, i32* %9
%35 = call i64 @S_ISDIR(i32 %34)
%36 = icmp ne i64 %35, 0
%37 = zext i1 %36 to i32
%38 = add nuw nsw i32 %11, %37
br label %10
39: ; preds = %14
%40 = call i32 @closedir(i32* %5)
br label %41
41: ; preds = %0, %39
%42 = phi i32 [ %11, %39 ], [ 0, %0 ]
ret i32 %42
}