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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncmp(char *, char *, int);
unsigned long strtoul(char *, char **, int);
unsigned long list_cpus_proc_stat(cpu_set_t *set, size_t setsize) {
  unsigned long cpu_max = 0;
  FILE *file;
  char buffer[4096];
  file = fopen("/proc/stat", "r");
  if (!file) {
    perror("fopen(/proc/stat)");
    return 0;
  }
  while (fgets(buffer, sizeof(buffer), file)) {
    if (!strncmp(buffer, "cpu", 3) && buffer[3] >= '0' && buffer[3] <= '9') {
      unsigned long cpu;
      char *endptr = buffer + 3;
      cpu = strtoul(endptr, &endptr, 10);
      if (*endptr != ' ') {
        fprintf(stderr, "strtoul: failed to parse line: %s\n", buffer);
        continue;
      }
      if (cpu_max < cpu) {
        cpu_max = cpu;
      }
      if (set) {
        do {
          (void)setsize;
          CPU_SET((cpu), (set));
        } while (0);
      }
    }
  }
  fclose(file);
  return cpu_max + 1;
}


x86 assembly (clang, -O0):
<list_cpus_proc_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe5e(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <list_cpus_proc_stat+0x56>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1300 <list_cpus_proc_stat+0x180>
jmp    11db <list_cpus_proc_stat+0x5b>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12eb <list_cpus_proc_stat+0x16b>
lea    -0x1030(%rbp),%rdi
lea    0xe17(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12e6 <list_cpus_proc_stat+0x166>
movsbl -0x102d(%rbp),%eax
cmp    $0x30,%eax
jl     12e6 <list_cpus_proc_stat+0x166>
movsbl -0x102d(%rbp),%eax
cmp    $0x39,%eax
jg     12e6 <list_cpus_proc_stat+0x166>
lea    -0x1030(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x1040(%rbp)
mov    -0x1040(%rbp),%rdi
lea    -0x1040(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x1038(%rbp)
mov    -0x1040(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
je     12a0 <list_cpus_proc_stat+0x120>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    -0x1030(%rbp),%rdx
lea    0xd8d(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11db <list_cpus_proc_stat+0x5b>
mov    -0x20(%rbp),%rax
cmp    -0x1038(%rbp),%rax
jae    12bc <list_cpus_proc_stat+0x13c>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12e1 <list_cpus_proc_stat+0x161>
jmp    12cc <list_cpus_proc_stat+0x14c>
mov    -0x1038(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <CPU_SET@plt>
jmp    12e1 <list_cpus_proc_stat+0x161>
jmp    12e6 <list_cpus_proc_stat+0x166>
jmp    11db <list_cpus_proc_stat+0x5b>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_cpus_proc_stat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rdi,%rbx
lea    0xe65(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12ba <list_cpus_proc_stat+0x13a>
mov    %rax,%r12
lea    0x20(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1290 <list_cpus_proc_stat+0x110>
mov    %rbx,0x18(%rsp)
lea    0x23(%rsp),%rbx
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0xe39(%rip),%r13
lea    0x20(%rsp),%rbp
lea    0x10(%rsp),%r14
lea    0xe2c(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1254 <list_cpus_proc_stat+0xd4>
movzbl 0x23(%rsp),%eax
cmp    $0x30,%al
jl     1254 <list_cpus_proc_stat+0xd4>
cmp    $0x39,%al
jg     1254 <list_cpus_proc_stat+0xd4>
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    0x10(%rsp),%rcx
cmpb   $0x20,(%rcx)
je     126b <list_cpus_proc_stat+0xeb>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1060 <fprintf@plt>
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1200 <list_cpus_proc_stat+0x80>
jmp    1297 <list_cpus_proc_stat+0x117>
mov    0x8(%rsp),%rcx
cmp    %rax,%rcx
cmovb  %rax,%rcx
mov    %rcx,0x8(%rsp)
mov    0x18(%rsp),%rsi
test   %rsi,%rsi
je     1254 <list_cpus_proc_stat+0xd4>
mov    %rax,%rdi
call   10a0 <CPU_SET@plt>
jmp    1254 <list_cpus_proc_stat+0xd4>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
add    $0x1,%rax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4c(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    12a8 <list_cpus_proc_stat+0x128>

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"fopen(/proc/stat)\00"
@.str.3 = [4 x i8] c"cpu\00"
@stderr = global i32 0
@.str.4 = [35 x i8] c"strtoul: failed to parse line: %s\0A\00"
define i64 @list_cpus_proc_stat(i32* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i64
%10 = alloca i8*
store i32* %0, i32** %4
store i64 %1, i64* %5
store i64 0, i64* %6
%11 = 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* %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 ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %3
br label %72
16: ; preds = %2
br label %17
17: ; preds = %66, %45, %16
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%19 = load i32*, i32** %7
%20 = call i64 @fgets(i8* %18, i32 4096, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %67
22: ; preds = %17
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%24 = call i32 @strncmp(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%25 = icmp ne i32 %24, 0
br i1 %25, label %66, label %26
26: ; preds = %22
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 3
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp sge i32 %29, 48
br i1 %30, label %31, label %66
31: ; preds = %26
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp sle i32 %34, 57
br i1 %35, label %36, label %66
36: ; preds = %31
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%38 = getelementptr inbounds i8, i8* %37, i64 3
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = call i64 @strtoul(i8* %39, i8** %10, i32 10)
store i64 %40, i64* %9
%41 = load i8*, i8** %10
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 32
br i1 %44, label %45, label %49
45: ; preds = %36
%46 = load i32, i32* @stderr
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %47)
br label %17
49: ; preds = %36
%50 = load i64, i64* %6
%51 = load i64, i64* %9
%52 = icmp ult i64 %50, %51
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = load i64, i64* %9
store i64 %54, i64* %6
br label %55
55: ; preds = %53, %49
%56 = load i32*, i32** %4
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %65
58: ; preds = %55
br label %59
59: ; preds = %58
%60 = load i64, i64* %5
%61 = load i64, i64* %9
%62 = load i32*, i32** %4
%63 = call i32 @CPU_SET(i64 %61, i32* %62)
br label %64
64: ; preds = %59
br label %65
65: ; preds = %64, %55
br label %66
66: ; preds = %65, %31, %26, %22
br label %17
67: ; preds = %17
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
%70 = load i64, i64* %6
%71 = add i64 %70, 1
store i64 %71, i64* %3
br label %72
72: ; preds = %67, %14
%73 = load i64, i64* %3
ret i64 %73
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"fopen(/proc/stat)\00"
@.str.3 = [4 x i8] c"cpu\00"
@stderr = global i32 0
@.str.4 = [35 x i8] c"strtoul: failed to parse line: %s\0A\00"
define i64 @list_cpus_proc_stat(i32* %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = 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))
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = call i64 @fgets(i8* %5, i32 4096, i32* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %47, label %11
11: ; preds = %8
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 3
%13 = bitcast i8** %4 to i8*
%14 = icmp eq i32* %0, null
br label %45
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %51
17: ; preds = %45, %36
%18 = call i32 @strncmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%19 = icmp eq i32 %18, 0
%20 = load i8, i8* %12
%21 = icmp sgt i8 %20, 47
%22 = select i1 %19, i1 %21, i1 false
%23 = icmp slt i8 %20, 58
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %25, label %41
25: ; preds = %17
store i8* %12, i8** %4
%26 = call i64 @strtoul(i8* %12, i8** %4, i32 10)
%27 = load i8*, i8** %4
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 32
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = icmp ult i64 %46, %26
%32 = select i1 %31, i64 %26, i64 %46
br i1 %14, label %35, label %33
33: ; preds = %30
%34 = call i32 @CPU_SET(i64 %26, i32* %0)
br label %35
35: ; preds = %33, %30
br label %41
36: ; preds = %25
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%39 = call i64 @fgets(i8* %5, i32 4096, i32* %6)
%40 = icmp eq i64 %39, 0
br i1 %40, label %47, label %17
41: ; preds = %17, %35
%42 = phi i64 [ %32, %35 ], [ %46, %17 ]
%43 = call i64 @fgets(i8* %5, i32 4096, i32* %6)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %11, %41
%46 = phi i64 [ 0, %11 ], [ %42, %41 ]
br label %17
47: ; preds = %41, %36, %8
%48 = phi i64 [ 0, %8 ], [ %46, %36 ], [ %42, %41 ]
%49 = call i32 @fclose(i32* %6)
%50 = add i64 %48, 1
br label %51
51: ; preds = %47, %15
%52 = phi i64 [ %50, %47 ], [ 0, %15 ]
ret i64 %52
}