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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
unsigned long scan_one(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
int read_proc(unsigned long int *const prcs_run,
              unsigned long int *const prcs_blked) {
  char StatBuf[2048], *ptr = &StatBuf[0];
  unsigned long int running, blocked;
  ssize_t len;
  size_t skip;
  FILE *stat;
  *prcs_run = 0;
  *prcs_blked = 0;
  if ((stat = fopen("/proc/stat", "r")) == (FILE *)0) {
    fprintf(stderr, "ERROR: could not open /proc/stat: %s\n", strerror(errno));
    return 1;
  }
  len = sizeof(StatBuf);
  while ((len > 0) && (fgets(ptr, len, stat))) {
    if (ptr[0] != 'p')
      continue;
    skip = strlen(ptr);
    len -= skip;
    ptr += skip;
  }
  fclose(stat);
  running = scan_one(StatBuf, "procs_running");
  blocked = scan_one(StatBuf, "procs_blocked");
  if (running == ~0UL || blocked == ~0UL)
    return 1;
  *prcs_run = running;
  *prcs_blked = blocked;
  return 0;
}


x86 assembly (clang, -O0):
<read_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
lea    0xe52(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x850(%rbp)
cmp    $0x0,%rax
jne    1209 <read_proc+0x99>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x854(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x854(%rbp),%edi
mov    %rax,%rdx
lea    0xe15(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <read_proc+0x1dc>
movq   $0x800,-0x840(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x840(%rbp)
mov    %al,-0x855(%rbp)
jbe    1251 <read_proc+0xe1>
mov    -0x828(%rbp),%rdi
mov    -0x840(%rbp),%rsi
mov    -0x850(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x855(%rbp)
mov    -0x855(%rbp),%al
test   $0x1,%al
jne    1264 <read_proc+0xf4>
jmp    12c1 <read_proc+0x151>
mov    -0x828(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x70,%eax
je     127c <read_proc+0x10c>
jmp    1214 <read_proc+0xa4>
mov    -0x828(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x848(%rbp)
mov    -0x848(%rbp),%rcx
mov    -0x840(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x840(%rbp)
mov    -0x848(%rbp),%rax
add    -0x828(%rbp),%rax
mov    %rax,-0x828(%rbp)
jmp    1214 <read_proc+0xa4>
mov    -0x850(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x820(%rbp),%rdi
lea    0xd58(%rip),%rsi
call   1030 <scan_one@plt>
mov    %rax,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
lea    0xd4c(%rip),%rsi
call   1030 <scan_one@plt>
mov    %rax,-0x838(%rbp)
cmpq   $0xffffffffffffffff,-0x830(%rbp)
ff
je     131d <read_proc+0x1ad>
cmpq   $0xffffffffffffffff,-0x838(%rbp)
ff
jne    1329 <read_proc+0x1b9>
movl   $0x1,-0x4(%rbp)
jmp    134c <read_proc+0x1dc>
mov    -0x830(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x838(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rdi)
movq   $0x0,(%rsi)
lea    0xe65(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1232 <read_proc+0xc2>
mov    %rax,%r12
mov    $0x800,%r13d
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e8 <read_proc+0x78>
cmpb   $0x70,(%rbx)
jne    11c0 <read_proc+0x50>
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %rax,%rbx
sub    %rax,%r13
jne    11c0 <read_proc+0x50>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0xe3c(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
call   1030 <scan_one@plt>
mov    %rax,%rbx
lea    0xe35(%rip),%rsi
mov    %r12,%rdi
call   1030 <scan_one@plt>
mov    %rax,%rcx
mov    $0x1,%eax
cmp    $0xffffffffffffffff,%rbx
je     125f <read_proc+0xef>
cmp    $0xffffffffffffffff,%rcx
je     125f <read_proc+0xef>
mov    %rbx,(%r15)
mov    %rcx,(%r14)
xor    %eax,%eax
jmp    125f <read_proc+0xef>
mov    0x2da7(%rip),%rax
mov    (%rax),%ebx
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdbd(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"ERROR: could not open /proc/stat: %s\0A\00"
@errno = global i32 0
@.str.3 = [14 x i8] c"procs_running\00"
@.str.4 = [14 x i8] c"procs_blocked\00"
define i32 @read_proc(i64* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca [2048 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32*
store i64* %0, i64** %4
store i64* %1, i64** %5
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
store i8* %13, i8** %7
%14 = load i64*, i64** %4
store i64 0, i64* %14
%15 = load i64*, i64** %5
store i64 0, i64* %15
%16 = 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* %16, i32** %12
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
br label %69
23: ; preds = %2
store i64 2048, i64* %10
br label %24
24: ; preds = %42, %41, %23
%25 = load i64, i64* %10
%26 = icmp ugt i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i8*, i8** %7
%29 = load i64, i64* %10
%30 = load i32*, i32** %12
%31 = call i64 @fgets(i8* %28, i64 %29, i32* %30)
%32 = icmp ne i64 %31, 0
br label %33
33: ; preds = %27, %24
%34 = phi i1 [ false, %24 ], [ %32, %27 ]
br i1 %34, label %35, label %51
35: ; preds = %33
%36 = load i8*, i8** %7
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 112
br i1 %40, label %41, label %42
41: ; preds = %35
br label %24
42: ; preds = %35
%43 = load i8*, i8** %7
%44 = call i64 @strlen(i8* %43)
store i64 %44, i64* %11
%45 = load i64, i64* %11
%46 = load i64, i64* %10
%47 = sub i64 %46, %45
store i64 %47, i64* %10
%48 = load i64, i64* %11
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 %48
store i8* %50, i8** %7
br label %24
51: ; preds = %33
%52 = load i32*, i32** %12
%53 = call i32 @fclose(i32* %52)
%54 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%55 = call i64 @scan_one(i8* %54, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i64 %55, i64* %8
%56 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%57 = call i64 @scan_one(i8* %56, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i64 %57, i64* %9
%58 = load i64, i64* %8
%59 = icmp eq i64 %58, -1
br i1 %59, label %63, label %60
60: ; preds = %51
%61 = load i64, i64* %9
%62 = icmp eq i64 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %60, %51
store i32 1, i32* %3
br label %69
64: ; preds = %60
%65 = load i64, i64* %8
%66 = load i64*, i64** %4
store i64 %65, i64* %66
%67 = load i64, i64* %9
%68 = load i64*, i64** %5
store i64 %67, i64* %68
store i32 0, i32* %3
br label %69
69: ; preds = %64, %63, %18
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"ERROR: could not open /proc/stat: %s\0A\00"
@errno = global i32 0
@.str.3 = [14 x i8] c"procs_running\00"
@.str.4 = [14 x i8] c"procs_blocked\00"
define i32 @read_proc(i64* nocapture writeonly %0, i64* nocapture writeonly %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
store i64 0, i64* %0
store i64 0, i64* %1
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %34
12: ; preds = %23, %15
%13 = call i64 @fgets(i8* %24, i64 %25, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = load i8, i8* %24
%17 = icmp eq i8 %16, 112
br i1 %17, label %18, label %12
18: ; preds = %15
%19 = call i64 @strlen(i8* %24)
%20 = sub i64 %25, %19
%21 = getelementptr inbounds i8, i8* %24, i64 %19
%22 = icmp eq i64 %20, 0
br i1 %22, label %26, label %23
23: ; preds = %2, %18
%24 = phi i8* [ %21, %18 ], [ %4, %2 ]
%25 = phi i64 [ %20, %18 ], [ 2048, %2 ]
br label %12
26: ; preds = %18, %12
%27 = call i32 @fclose(i32* %5)
%28 = call i64 @scan_one(i8* %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%29 = call i64 @scan_one(i8* %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i64 %28, -1
%31 = icmp eq i64 %29, -1
%32 = select i1 %30, i1 true, i1 %31
br i1 %32, label %34, label %33
33: ; preds = %26
store i64 %28, i64* %0
store i64 %29, i64* %1
br label %34
34: ; preds = %26, %33, %7
%35 = phi i32 [ 1, %7 ], [ 0, %33 ], [ 1, %26 ]
ret i32 %35
}