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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, char *, unsigned long *, unsigned long *,
           unsigned long *, unsigned long *);
scalar_t__ strncmp(char *, char *, int);
void dump_cpu_stats(void) {
  FILE *f = fopen("/proc/stat", "r");
  if (!f)
    return;
  printf("  \"cpu\":\n  [\n");
  bool terminate_prev_line = false;
  while (!feof(f)) {
    char line[256];
    char cpu[8];
    long unsigned t_usr = 0;
    long unsigned t_nice = 0;
    long unsigned t_sys = 0;
    long unsigned t_idle = 0;
    fgets(line, sizeof(line), f);
    if (strncmp(line, "cpu", 3) != 0 || line[3] == ' ')
      continue;
    if (sscanf(line, "%s %lu %lu %lu %lu", cpu, &t_usr, &t_nice, &t_sys,
               &t_idle) != 5) {
      continue;
    }
    if (terminate_prev_line)
      printf(",\n");
    terminate_prev_line = true;
    printf("    {\"usr\": %lu, \"sys\": %lu, \"idle\": %lu}", t_usr + t_nice,
           t_sys, t_idle);
  }
  fclose(f);
  printf("\n  ]");
}


x86 assembly (clang, -O0):
<dump_cpu_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a2 <dump_cpu_stats+0x32>
jmp    12fa <dump_cpu_stats+0x18a>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d5 <dump_cpu_stats+0x65>
jmp    12e3 <dump_cpu_stats+0x173>
movq   $0x0,-0x120(%rbp)
movq   $0x0,-0x128(%rbp)
movq   $0x0,-0x130(%rbp)
movq   $0x0,-0x138(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
lea    -0x110(%rbp),%rdi
lea    0xdf7(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1248 <dump_cpu_stats+0xd8>
movsbl -0x10d(%rbp),%eax
cmp    $0x20,%eax
jne    124d <dump_cpu_stats+0xdd>
jmp    11b7 <dump_cpu_stats+0x47>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rdx
lea    0xdbd(%rip),%rsi
lea    -0x120(%rbp),%rcx
lea    -0x128(%rbp),%r8
lea    -0x130(%rbp),%r9
lea    -0x138(%rbp),%rax
mov    %rax,(%rsp)
call   1080 <sscanf@plt>
cmp    $0x5,%eax
je     1295 <dump_cpu_stats+0x125>
jmp    11b7 <dump_cpu_stats+0x47>
cmpl   $0x0,-0xc(%rbp)
je     12ad <dump_cpu_stats+0x13d>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x120(%rbp),%rsi
add    -0x128(%rbp),%rsi
mov    -0x130(%rbp),%rdx
mov    -0x138(%rbp),%rcx
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11b7 <dump_cpu_stats+0x47>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_cpu_stats>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
lea    0xe68(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11da <dump_cpu_stats+0x5a>
mov    %rax,%rbx
lea    0xea0(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     11ec <dump_cpu_stats+0x6c>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x30(%rsp),%r14
lea    0xe15(%rip),%r15
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
xor    %ebp,%ebp
jmp    1243 <dump_cpu_stats+0xc3>
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rsp),%rsi
add    0x20(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%rcx
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11c4 <dump_cpu_stats+0x44>
movq   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1237 <dump_cpu_stats+0xb7>
cmpb   $0x20,0x33(%rsp)
je     1237 <dump_cpu_stats+0xb7>
lea    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %r14,%rdi
lea    0xd6b(%rip),%rsi
lea    0x28(%rsp),%rdx
lea    0x20(%rsp),%rcx
mov    %r12,%r8
mov    %r13,%r9
call   1090 <sscanf@plt>
cmp    $0x5,%eax
jne    1237 <dump_cpu_stats+0xb7>
test   %ebp,%ebp
je     1210 <dump_cpu_stats+0x90>
lea    0xd8d(%rip),%rdi
call   1040 <puts@plt>
jmp    1210 <dump_cpu_stats+0x90>

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c" \22cpu\22:\0A [\0A\00"
@.str.3 = [4 x i8] c"cpu\00"
@.str.4 = [19 x i8] c"%s %lu %lu %lu %lu\00"
@.str.5 = [3 x i8] c",\0A\00"
@.str.6 = [42 x i8] c" {\22usr\22: %lu, \22sys\22: %lu, \22idle\22: %lu}\00"
@.str.7 = [5 x i8] c"\0A ]\00"
define void @dump_cpu_stats() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [8 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = 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* %9, i32** %1
%10 = load i32*, i32** %1
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %0
br label %55
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %15
15: ; preds = %44, %38, %32, %13
%16 = load i32*, i32** %1
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %51
20: ; preds = %15
store i64 0, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
store i64 0, i64* %8
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%22 = load i32*, i32** %1
%23 = call i32 @fgets(i8* %21, i32 256, i32* %22)
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%25 = call i64 @strncmp(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%26 = icmp ne i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %20
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 3
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 32
br i1 %31, label %32, label %33
32: ; preds = %27, %20
br label %15
33: ; preds = %27
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%35 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%36 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %35, i64* %5, i64* %6, i64* %7, i64* %8)
%37 = icmp ne i32 %36, 5
br i1 %37, label %38, label %39
38: ; preds = %33
br label %15
39: ; preds = %33
%40 = load i32, i32* %2
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
store i32 1, i32* %2
%45 = load i64, i64* %5
%46 = load i64, i64* %6
%47 = add i64 %45, %46
%48 = load i64, i64* %7
%49 = load i64, i64* %8
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i64 %47, i64 %48, i64 %49)
br label %15
51: ; preds = %15
%52 = load i32*, i32** %1
%53 = call i32 @fclose(i32* %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %55
55: ; preds = %51, %12
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"cpu\00"
@.str.4 = [19 x i8] c"%s %lu %lu %lu %lu\00"
@.str.6 = [42 x i8] c" {\22usr\22: %lu, \22sys\22: %lu, \22idle\22: %lu}\00"
@.str.7 = [5 x i8] c"\0A ]\00"
@str = [13 x i8] c" \22cpu\22:\0A [\00"
@str.8 = [2 x i8] c",\00"
define void @dump_cpu_stats() {
%1 = alloca [256 x i8]
%2 = alloca [8 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %50, label %9
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%11 = call i32 @feof(i32* %7)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %47
13: ; preds = %9
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%16 = bitcast i64* %3 to i8*
%17 = bitcast i64* %4 to i8*
%18 = bitcast i64* %5 to i8*
%19 = bitcast i64* %6 to i8*
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 3
br label %21
21: ; preds = %13, %43
%22 = phi i32 [ 0, %13 ], [ %44, %43 ]
store i64 0, i64* %3
store i64 0, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
%23 = call i32 @fgets(i8* %14, i32 256, i32* %7)
%24 = call i64 @strncmp(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%25 = icmp ne i64 %24, 0
%26 = load i8, i8* %20
%27 = icmp eq i8 %26, 32
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %43, label %29
29: ; preds = %21
%30 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %15, i64* %3, i64* %4, i64* %5, i64* %6)
%31 = icmp eq i32 %30, 5
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = icmp eq i32 %22, 0
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.8, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
%37 = load i64, i64* %3
%38 = load i64, i64* %4
%39 = add i64 %38, %37
%40 = load i64, i64* %5
%41 = load i64, i64* %6
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i64 %39, i64 %40, i64 %41)
br label %43
43: ; preds = %29, %21, %36
%44 = phi i32 [ 1, %36 ], [ %22, %21 ], [ %22, %29 ]
%45 = call i32 @feof(i32* %7)
%46 = icmp eq i32 %45, 0
br i1 %46, label %21, label %47
47: ; preds = %43, %9
%48 = call i32 @fclose(i32* %7)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %0, %47
ret void
}