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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
double now();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
void disk_bench(int run) {
  char *disks[] = {"/dev/hda", "/dev/sda", NULL};
  char *dev = NULL;
  double t1, t2;
  int i, fd = -1, megs;
  char buf[2048];
  for (i = 0; disks[i] != NULL; i++) {
    if ((fd = open(disks[i], O_RDONLY)) != -1) {
      dev = disks[i];
      break;
    }
  }
  system("/bin/sync");
  sleep(2);
  t1 = now();
  megs = 128 * run;
  for (i = 0; i < 1024 / 2 * megs; i++) {
    if (read(fd, buf, 2048) != 2048) {
      printf("Read error\n");
    }
  }
  close(fd);
  t2 = now();
  printf("%dMB read in %f s from %s = %f MB/s\n", megs, t2 - t1, dev,
         (double)megs / (t2 - t1));
}


x86 assembly (clang, -O0):
<disk_bench>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2c7b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c78(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c75(%rip),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
je     120d <disk_bench+0x9d>
movslq -0x3c(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x40(%rbp)
cmp    $0xffffffff,%eax
je     11fa <disk_bench+0x8a>
movslq -0x3c(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
mov    %rax,-0x28(%rbp)
jmp    120d <disk_bench+0x9d>
jmp    11ff <disk_bench+0x8f>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11b5 <disk_bench+0x45>
lea    0xdfe(%rip),%rdi
call   1030 <system@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1080 <now@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x7,%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x44(%rbp),%ecx
shl    $0x9,%ecx
cmp    %ecx,%eax
jge    1290 <disk_bench+0x120>
mov    -0x40(%rbp),%edi
lea    -0x850(%rbp),%rsi
mov    $0x800,%edx
call   1060 <read@plt>
cmp    $0x800,%eax
je     127d <disk_bench+0x10d>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1282 <disk_bench+0x112>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    123f <disk_bench+0xcf>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1080 <now@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x44(%rbp),%esi
movsd  -0x38(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
mov    -0x28(%rbp),%rdx
cvtsi2sdl -0x44(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm2
subsd  -0x30(%rbp),%xmm2
divsd  %xmm2,%xmm1
lea    0xd59(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disk_bench>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edi,%r12d
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%esi
lea    0xe5c(%rip),%r14
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11d0 <disk_bench+0x50>
mov    (%rbx),%esi
lea    0xe4d(%rip),%r15
mov    %r15,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
cmovne %r15,%r14
lea    0xe3b(%rip),%rdi
call   1040 <system@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1090 <now@plt>
movsd  %xmm0,0x8(%rsp)
mov    %r12d,%r15d
shl    $0x7,%r15d
test   %r12d,%r12d
jle    1245 <disk_bench+0xc5>
shl    $0x10,%r12d
cmp    $0x2,%r12d
mov    $0x1,%ebx
cmovge %r12d,%ebx
lea    0x10(%rsp),%r13
lea    0xe25(%rip),%r12
jmp    1225 <disk_bench+0xa5>
xchg   %ax,%ax
add    $0xffffffff,%ebx
je     1245 <disk_bench+0xc5>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
cmp    $0x800,%eax
je     1220 <disk_bench+0xa0>
mov    %r12,%rdi
call   1030 <puts@plt>
jmp    1220 <disk_bench+0xa0>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1090 <now@plt>
cvtsi2sd %r15d,%xmm1
subsd  0x8(%rsp),%xmm0
divsd  %xmm0,%xmm1
lea    0xdb3(%rip),%rdi
mov    %r15d,%esi
mov    %r14,%rdx
mov    $0x2,%al
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/hda\00"
@.str.1 = [9 x i8] c"/dev/sda\00"
@__const.disk_bench.disks = [3 x i8*] [i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* null]
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"/bin/sync\00"
@.str.3 = [12 x i8] c"Read error\0A\00"
@.str.4 = [37 x i8] c"%dMB read in %f s from %s = %f MB/s\0A\00"
define void @disk_bench(i32 %0) {
%2 = alloca i32
%3 = alloca [3 x i8*]
%4 = alloca i8*
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2048 x i8]
store i32 %0, i32* %2
%11 = bitcast [3 x i8*]* %3 to i8*
store i8* null, i8** %4
store i32 -1, i32* %8
store i32 0, i32* %7
br label %12
12: ; preds = %32, %1
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 %14
%16 = load i8*, i8** %15
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %35
18: ; preds = %12
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 %28
%30 = load i8*, i8** %29
store i8* %30, i8** %4
br label %35
31: ; preds = %18
br label %32
32: ; preds = %31
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %12
35: ; preds = %26, %12
%36 = call i32 @system(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @sleep(i32 2)
%38 = call double (...) @now()
store double %38, double* %5
%39 = load i32, i32* %2
%40 = mul nsw i32 128, %39
store i32 %40, i32* %9
store i32 0, i32* %7
br label %41
41: ; preds = %54, %35
%42 = load i32, i32* %7
%43 = load i32, i32* %9
%44 = mul nsw i32 512, %43
%45 = icmp slt i32 %42, %44
br i1 %45, label %46, label %57
46: ; preds = %41
%47 = load i32, i32* %8
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 2048)
%50 = icmp ne i32 %49, 2048
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
br label %54
54: ; preds = %53
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %41
57: ; preds = %41
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
%60 = call double (...) @now()
store double %60, double* %6
%61 = load i32, i32* %9
%62 = load double, double* %6
%63 = load double, double* %5
%64 = fsub double %62, %63
%65 = load i8*, i8** %4
%66 = load i32, i32* %9
%67 = sitofp i32 %66 to double
%68 = load double, double* %6
%69 = load double, double* %5
%70 = fsub double %68, %69
%71 = fdiv double %67, %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %61, double %64, i8* %65, double %71)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/hda\00"
@.str.1 = [9 x i8] c"/dev/sda\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"/bin/sync\00"
@.str.4 = [37 x i8] c"%dMB read in %f s from %s = %f MB/s\0A\00"
@str = [11 x i8] c"Read error\00"
define void @disk_bench(i32 %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
%11 = select i1 %10, i8* null, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0)
br label %12
12: ; preds = %7, %1
%13 = phi i32 [ %5, %1 ], [ %9, %7 ]
%14 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %1 ], [ %11, %7 ]
%15 = tail call i32 @system(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @sleep(i32 2)
%17 = tail call double (...) @now()
%18 = shl nsw i32 %0, 7
%19 = icmp sgt i32 %0, 0
br i1 %19, label %20, label %32
20: ; preds = %12
%21 = shl i32 %0, 16
%22 = call i32 @llvm.smax.i32(i32 %21, i32 1)
br label %23
23: ; preds = %20, %29
%24 = phi i32 [ %30, %29 ], [ 0, %20 ]
%25 = call i32 @read(i32 %13, i8* %3, i32 2048)
%26 = icmp eq i32 %25, 2048
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %23, %27
%30 = add nuw nsw i32 %24, 1
%31 = icmp eq i32 %30, %22
br i1 %31, label %32, label %23
32: ; preds = %29, %12
%33 = tail call i32 @close(i32 %13)
%34 = tail call double (...) @now()
%35 = fsub double %34, %17
%36 = sitofp i32 %18 to double
%37 = fdiv double %36, %35
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %18, double %35, i8* %14, double %37)
ret void
}