#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcspn(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
unsigned int get_disk_io_nr(void) {
  FILE *fp;
  char line[8192];
  unsigned int dsk = 0;
  int pos;
  if ((fp = fopen("/proc/stat", "r")) == NULL) {
    fprintf(stderr, ("Cannot open %s: %s\n"), "/proc/stat", strerror(errno));
    exit(2);
  }
  while (fgets(line, 8192, fp) != NULL) {
    if (!strncmp(line, "disk_io: ", 9)) {
      for (pos = 9; pos < strlen(line) - 1;
           pos += strcspn(line + pos, " ") + 1) {
        dsk++;
      }
    }
  }
  fclose(fp);
  return dsk;
}


x86 assembly (clang, -O0):
<get_disk_io_nr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
movl   $0x0,-0x2014(%rbp)
lea    0xe54(%rip),%rdi
lea    0xe58(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1209 <get_disk_io_nr+0x79>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x201c(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x201c(%rbp),%edi
mov    %rax,%rcx
lea    0xe1a(%rip),%rsi
lea    0xe06(%rip),%rdx
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
jmp    120e <get_disk_io_nr+0x7e>
lea    -0x2010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x2000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12d5 <get_disk_io_nr+0x145>
lea    -0x2010(%rbp),%rdi
lea    0xde6(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12d0 <get_disk_io_nr+0x140>
movl   $0x9,-0x2018(%rbp)
mov    -0x2018(%rbp),%eax
mov    %eax,-0x2020(%rbp)
lea    -0x2010(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x2020(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12cb <get_disk_io_nr+0x13b>
mov    -0x2014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2014(%rbp)
lea    -0x2010(%rbp),%rdi
movslq -0x2018(%rbp),%rax
add    %rax,%rdi
lea    0xd7f(%rip),%rsi
call   1060 <strcspn@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x2018(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2018(%rbp)
jmp    1258 <get_disk_io_nr+0xc8>
jmp    12d0 <get_disk_io_nr+0x140>
jmp    120e <get_disk_io_nr+0x7e>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2014(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_disk_io_nr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
lea    0xe58(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1274 <get_disk_io_nr+0xe4>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x2000,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1258 <get_disk_io_nr+0xc8>
lea    0xe3f(%rip),%r14
mov    %rsp,%r13
lea    0xe3d(%rip),%r12
jmp    1205 <get_disk_io_nr+0x75>
xchg   %ax,%ax
mov    %r13,%rdi
mov    $0x2000,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1258 <get_disk_io_nr+0xc8>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11f0 <get_disk_io_nr+0x60>
mov    %r13,%rdi
call   1050 <strlen@plt>
cmp    $0xb,%eax
jl     11f0 <get_disk_io_nr+0x60>
mov    $0x9,%ebx
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
movslq %ebx,%rbx
lea    (%rsp,%rbx,1),%rdi
mov    %r12,%rsi
call   1060 <strcspn@plt>
add    %eax,%ebx
add    $0x1,%ebx
mov    %r13,%rdi
call   1050 <strlen@plt>
add    $0xffffffff,%eax
cmp    %eax,%ebx
jl     1230 <get_disk_io_nr+0xa0>
jmp    11f0 <get_disk_io_nr+0x60>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d65(%rip),%rax
mov    (%rax),%ebx
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd7b(%rip),%rsi
lea    0xd67(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

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 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [10 x i8] c"disk_io: \00"
@.str.4 = [2 x i8] c" \00"
define i32 @get_disk_io_nr() {
%1 = alloca i32*
%2 = alloca [8192 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %3
%5 = 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* %5, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 2)
unreachable
13: ; preds = %0
br label %14
14: ; preds = %45, %13
%15 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%16 = load i32*, i32** %1
%17 = call i32* @fgets(i8* %15, i32 8192, i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %46
19: ; preds = %14
%20 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%21 = call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%22 = icmp ne i32 %21, 0
br i1 %22, label %45, label %23
23: ; preds = %19
store i32 9, i32* %4
br label %24
24: ; preds = %33, %23
%25 = load i32, i32* %4
%26 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = icmp slt i32 %25, %28
br i1 %29, label %30, label %44
30: ; preds = %24
%31 = load i32, i32* %3
%32 = add i32 %31, 1
store i32 %32, i32* %3
br label %33
33: ; preds = %30
%34 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = call i64 @strcspn(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = add nsw i64 %38, 1
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = add nsw i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
br label %24
44: ; preds = %24
br label %45
45: ; preds = %44, %19
br label %14
46: ; preds = %14
%47 = load i32*, i32** %1
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %3
ret i32 %49
}

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 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [10 x i8] c"disk_io: \00"
@.str.4 = [2 x i8] c" \00"
define i32 @get_disk_io_nr() {
%1 = alloca [8192 x i8]
%2 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 0
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %2, i32 8192, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %38, label %14
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %5, %34
%15 = phi i32 [ %35, %34 ], [ 0, %5 ]
%16 = call i32 @strncmp(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 9)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %14
%19 = call i32 @strlen(i8* %2)
%20 = icmp sgt i32 %19, 10
br i1 %20, label %21, label %34
21: ; preds = %18, %21
%22 = phi i32 [ %30, %21 ], [ 9, %18 ]
%23 = phi i32 [ %24, %21 ], [ %15, %18 ]
%24 = add i32 %23, 1
%25 = sext i32 %22 to i64
%26 = getelementptr inbounds [8192 x i8], [8192 x i8]* %1, i64 0, i64 %25
%27 = call i64 @strcspn(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = trunc i64 %27 to i32
%29 = add nsw i32 %22, 1
%30 = add i32 %29, %28
%31 = call i32 @strlen(i8* %2)
%32 = add nsw i32 %31, -1
%33 = icmp slt i32 %30, %32
br i1 %33, label %21, label %34
34: ; preds = %21, %18, %14
%35 = phi i32 [ %15, %14 ], [ %15, %18 ], [ %24, %21 ]
%36 = call i32* @fgets(i8* %2, i32 8192, i32* %3)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %14
38: ; preds = %34, %5
%39 = phi i32 [ 0, %5 ], [ %35, %34 ]
%40 = call i32 @fclose(i32* %3)
ret i32 %39
}