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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncmp(char *, char *, int);
unsigned long strtoul(char *, char **, int);
unsigned long list_cpus_sys_dev(cpu_set_t *set, size_t setsize) {
  unsigned long cpu_max = 0;
  DIR *dir;
  struct dirent *dent;
  dir = opendir("/sys/devices/system/cpu");
  if (!dir) {
    perror("opendir(/sys/devices/system/cpu)");
    return 0;
  }
  while ((dent = readdir(dir)) != NULL) {
    if (!strncmp(dent->d_name, "cpu", 3) && dent->d_name[3] >= '0' &&
        dent->d_name[3] <= '9') {
      unsigned long cpu;
      char *endptr = dent->d_name + 3;
      cpu = strtoul(endptr, &endptr, 10);
      if (*endptr) {
        fprintf(stderr, "strtoul: failed to parse %s\n", dent->d_name);
        continue;
      }
      if (cpu_max < cpu) {
        cpu_max = cpu;
      }
      if (set) {
        do {
          (void)setsize;
          CPU_SET((cpu), (set));
        } while (0);
      }
    }
  }
  closedir(dir);
  return cpu_max + 1;
}


x86 assembly (clang, -O0):
<list_cpus_sys_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe61(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cc <list_cpus_sys_dev+0x4c>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <list_cpus_sys_dev+0x15b>
jmp    11d1 <list_cpus_sys_dev+0x51>
mov    -0x28(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12c6 <list_cpus_sys_dev+0x146>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1e(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12c1 <list_cpus_sys_dev+0x141>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl 0x3(%rax),%eax
cmp    $0x30,%eax
jl     12c1 <list_cpus_sys_dev+0x141>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl 0x3(%rax),%eax
cmp    $0x39,%eax
jg     12c1 <list_cpus_sys_dev+0x141>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
add    $0x3,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
cmpb   $0x0,(%rax)
je     1284 <list_cpus_sys_dev+0x104>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
lea    0xdbf(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11d1 <list_cpus_sys_dev+0x51>
mov    -0x20(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    129a <list_cpus_sys_dev+0x11a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12bc <list_cpus_sys_dev+0x13c>
jmp    12aa <list_cpus_sys_dev+0x12a>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <CPU_SET@plt>
jmp    12bc <list_cpus_sys_dev+0x13c>
jmp    12c1 <list_cpus_sys_dev+0x141>
jmp    11d1 <list_cpus_sys_dev+0x51>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_cpus_sys_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
lea    0xe68(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1299 <list_cpus_sys_dev+0x119>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1274 <list_cpus_sys_dev+0xf4>
mov    %rax,%r15
xor    %eax,%eax
mov    %rax,(%rsp)
lea    0xe45(%rip),%r12
lea    0x8(%rsp),%r13
mov    0x2e05(%rip),%r14
lea    0xe57(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%rdi
mov    %r12,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    123f <list_cpus_sys_dev+0xbf>
mov    (%r15),%rdi
movzbl 0x3(%rdi),%eax
add    $0xd0,%al
cmp    $0x9,%al
ja     123f <list_cpus_sys_dev+0xbf>
add    $0x3,%rdi
mov    %rdi,0x8(%rsp)
mov    %r13,%rsi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1251 <list_cpus_sys_dev+0xd1>
mov    (%r14),%edi
mov    (%r15),%rdx
mov    %rbp,%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11f0 <list_cpus_sys_dev+0x70>
jmp    127a <list_cpus_sys_dev+0xfa>
mov    (%rsp),%rcx
cmp    %rax,%rcx
cmovb  %rax,%rcx
mov    %rcx,(%rsp)
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     123f <list_cpus_sys_dev+0xbf>
mov    %rax,%rdi
call   10a0 <CPU_SET@plt>
jmp    123f <list_cpus_sys_dev+0xbf>
xor    %eax,%eax
mov    %rax,(%rsp)
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    (%rsp),%rax
add    $0x1,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd78(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    128a <list_cpus_sys_dev+0x10a>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [24 x i8] c"/sys/devices/system/cpu\00"
@.str.1 = [33 x i8] c"opendir(/sys/devices/system/cpu)\00"
@.str.2 = [4 x i8] c"cpu\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"strtoul: failed to parse %s\0A\00"
define i64 @list_cpus_sys_dev(i32* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i64
%10 = alloca i8*
store i32* %0, i32** %4
store i64 %1, i64* %5
store i64 0, i64* %6
%11 = call i32* @opendir(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %3
br label %82
16: ; preds = %2
br label %17
17: ; preds = %76, %53, %16
%18 = load i32*, i32** %7
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %8
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %77
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %8
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @strncmp(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%26 = icmp ne i32 %25, 0
br i1 %26, label %76, label %27
27: ; preds = %21
%28 = load %struct.dirent*, %struct.dirent** %8
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 3
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp sge i32 %33, 48
br i1 %34, label %35, label %76
35: ; preds = %27
%36 = load %struct.dirent*, %struct.dirent** %8
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = getelementptr inbounds i8, i8* %38, i64 3
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp sle i32 %41, 57
br i1 %42, label %43, label %76
43: ; preds = %35
%44 = load %struct.dirent*, %struct.dirent** %8
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = getelementptr inbounds i8, i8* %46, i64 3
store i8* %47, i8** %10
%48 = load i8*, i8** %10
%49 = call i64 @strtoul(i8* %48, i8** %10, i32 10)
store i64 %49, i64* %9
%50 = load i8*, i8** %10
%51 = load i8, i8* %50
%52 = icmp ne i8 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %43
%54 = load i32, i32* @stderr
%55 = load %struct.dirent*, %struct.dirent** %8
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %57)
br label %17
59: ; preds = %43
%60 = load i64, i64* %6
%61 = load i64, i64* %9
%62 = icmp ult i64 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = load i64, i64* %9
store i64 %64, i64* %6
br label %65
65: ; preds = %63, %59
%66 = load i32*, i32** %4
%67 = icmp ne i32* %66, null
br i1 %67, label %68, label %75
68: ; preds = %65
br label %69
69: ; preds = %68
%70 = load i64, i64* %5
%71 = load i64, i64* %9
%72 = load i32*, i32** %4
%73 = call i32 @CPU_SET(i64 %71, i32* %72)
br label %74
74: ; preds = %69
br label %75
75: ; preds = %74, %65
br label %76
76: ; preds = %75, %35, %27, %21
br label %17
77: ; preds = %17
%78 = load i32*, i32** %7
%79 = call i32 @closedir(i32* %78)
%80 = load i64, i64* %6
%81 = add i64 %80, 1
store i64 %81, i64* %3
br label %82
82: ; preds = %77, %14
%83 = load i64, i64* %3
ret i64 %83
}

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