#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int main(void) {
  FILE *fp;
  char line[256];
  int disk_reads;
  int disk_reads_total;
  int disk_writes;
  int disk_writes_total;
  int count = 1;
  int disk_activity[2];
  int relative_activity = 0;
  while (count < 3) {
    fp = fopen("/proc/diskstats", "r");
    while (fgets(line, 256, fp)) {
      sscanf(line, "%*[ ]%*d%*[ ]%*d%*[ ]%*s%*[ ]%d", &disk_reads);
      disk_reads_total = disk_reads_total + disk_reads;
      sscanf(line,
             "%*[ ]%*d%*[ ]%*d%*[ ]%*s%*[ ]%*d%*[ ]%*d%*[ ]%*d%*[ ]%*d%*[ ]%d",
             &disk_writes);
      disk_writes_total = disk_writes_total + disk_writes;
    }
    disk_activity[count] = disk_reads_total + disk_writes_total;
    count++;
    fclose(fp);
    sleep(5);
  }
  relative_activity = disk_activity[2] - disk_activity[1];
  printf("\nThe total number of disk reads was: %d \n", disk_reads_total);
  printf("The total number of disk writes was: %d \n", disk_writes_total);
  printf("The relative disk activity was: %d \n", relative_activity);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x124(%rbp)
movl   $0x0,-0x130(%rbp)
cmpl   $0x3,-0x124(%rbp)
jge    1267 <main+0x107>
lea    0xe66(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1226 <main+0xc6>
lea    -0x110(%rbp),%rdi
lea    0xe3b(%rip),%rsi
lea    -0x114(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x118(%rbp),%eax
add    -0x114(%rbp),%eax
mov    %eax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xe2f(%rip),%rsi
lea    -0x11c(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x120(%rbp),%eax
add    -0x11c(%rbp),%eax
mov    %eax,-0x120(%rbp)
jmp    11aa <main+0x4a>
mov    -0x118(%rbp),%ecx
add    -0x120(%rbp),%ecx
movslq -0x124(%rbp),%rax
mov    %ecx,-0x12c(%rbp,%rax,4)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
jmp    1186 <main+0x26>
mov    -0x124(%rbp),%eax
sub    -0x128(%rbp),%eax
mov    %eax,-0x130(%rbp)
mov    -0x118(%rbp),%esi
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
mov    -0x120(%rbp),%esi
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
mov    -0x130(%rbp),%esi
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe88(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f1 <main+0x91>
lea    0x10(%rsp),%r12
lea    0xe88(%rip),%r15
lea    0x8(%rsp),%r13
nop
mov    %r12,%rdi
lea    0xe58(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
add    0xc(%rsp),%ebp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <sscanf@plt>
add    0x8(%rsp),%ebx
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
jmp    11f1 <main+0x91>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xdf6(%rip),%rdi
lea    0xdff(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     128f <main+0x12f>
lea    0x10(%rsp),%r12
lea    0xdf6(%rip),%r15
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
lea    0xdb8(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
add    0xc(%rsp),%ebp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <sscanf@plt>
add    0x8(%rsp),%ebx
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1250 <main+0xf0>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xdca(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0xde6(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/diskstats\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"%*[ ]%*d%*[ ]%*d%*[ ]%*s%*[ ]%d\00"
@.str.3 = [64 x i8] c"%*[ ]%*d%*[ ]%*d%*[ ]%*s%*[ ]%*d%*[ ]%*d%*[ ]%*d%*[ ]%*d%*[ ]%d\00"
@.str.4 = [42 x i8] c"\0AThe total number of disk reads was: %d \0A\00"
@.str.5 = [42 x i8] c"The total number of disk writes was: %d \0A\00"
@.str.6 = [37 x i8] c"The relative disk activity was: %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %8
store i32 0, i32* %10
br label %11
11: ; preds = %32, %0
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %44
14: ; preds = %11
%15 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %2
br label %16
16: ; preds = %21, %14
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %2
%19 = call i64 @fgets(i8* %17, i32 256, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %16
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%24 = load i32, i32* %5
%25 = load i32, i32* %4
%26 = add nsw i32 %24, %25
store i32 %26, i32* %5
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = add nsw i32 %29, %30
store i32 %31, i32* %7
br label %16
32: ; preds = %16
%33 = load i32, i32* %5
%34 = load i32, i32* %7
%35 = add nsw i32 %33, %34
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 %37
store i32 %35, i32* %38
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = load i32*, i32** %2
%42 = call i32 @fclose(i32* %41)
%43 = call i32 @sleep(i32 5)
br label %11
44: ; preds = %11
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 2
%46 = load i32, i32* %45
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%48 = load i32, i32* %47
%49 = sub nsw i32 %46, %48
store i32 %49, i32* %10
%50 = load i32, i32* %5
%51 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %7
%53 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %10
%55 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/diskstats\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"%*[ ]%*d%*[ ]%*d%*[ ]%*s%*[ ]%d\00"
@.str.3 = [64 x i8] c"%*[ ]%*d%*[ ]%*d%*[ ]%*s%*[ ]%*d%*[ ]%*d%*[ ]%*d%*[ ]%*d%*[ ]%d\00"
@.str.4 = [42 x i8] c"\0AThe total number of disk reads was: %d \0A\00"
@.str.5 = [42 x i8] c"The total number of disk writes was: %d \0A\00"
@.str.6 = [37 x i8] c"The relative disk activity was: %d \0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call i64 @fgets(i8* %4, i32 256, i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %0, %10
%11 = phi i32 [ %18, %10 ], [ undef, %0 ]
%12 = phi i32 [ %15, %10 ], [ undef, %0 ]
%13 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%14 = load i32, i32* %2
%15 = add nsw i32 %14, %12
%16 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%17 = load i32, i32* %3
%18 = add nsw i32 %17, %11
%19 = call i64 @fgets(i8* %4, i32 256, i32* %7)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %10
21: ; preds = %10, %0
%22 = phi i32 [ undef, %0 ], [ %15, %10 ]
%23 = phi i32 [ undef, %0 ], [ %18, %10 ]
%24 = call i32 @fclose(i32* %7)
%25 = call i32 @sleep(i32 5)
%26 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = call i64 @fgets(i8* %4, i32 256, i32* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %40, label %29
29: ; preds = %21, %29
%30 = phi i32 [ %37, %29 ], [ %23, %21 ]
%31 = phi i32 [ %34, %29 ], [ %22, %21 ]
%32 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%33 = load i32, i32* %2
%34 = add nsw i32 %33, %31
%35 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%36 = load i32, i32* %3
%37 = add nsw i32 %36, %30
%38 = call i64 @fgets(i8* %4, i32 256, i32* %26)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %29
40: ; preds = %29, %21
%41 = phi i32 [ %22, %21 ], [ %34, %29 ]
%42 = phi i32 [ %23, %21 ], [ %37, %29 ]
%43 = call i32 @fclose(i32* %26)
%44 = call i32 @sleep(i32 5)
%45 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%46 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %42)
%47 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 poison)
ret i32 0
}