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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int cmd_file_read(int argc, char **argv) {
  int fd;
  static char buffer[(1024)];
  char *ptr;
  int bytes_num;
  int counter = 0;
  time_t time_start, time_end;
  unsigned int speed;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s frome_file_name\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDONLY)) == -1) {
    fprintf(stderr, "Open file [%s] error [%s]\n", argv[1], strerror(errno));
    exit(1);
  }
  printf("Begin to Read File: %s\n", argv[1]);
  counter = 0;
  time_start = time(NULL);
  while (bytes_num = read(fd, buffer, (1024))) {
    counter += bytes_num;
  }
  time_end = time(NULL);
  close(fd);
  speed = counter / difftime(time_end, time_start) / (1024);
  printf("File Read Speed : %d KB/s\n", speed);
  return 0;
}


x86 assembly (clang, -O0):
<cmd_file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11d8 <cmd_file_read+0x48>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1242 <cmd_file_read+0xb2>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    0x2e0b(%rip),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1299 <cmd_file_read+0x109>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    126b <cmd_file_read+0xdb>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <difftime@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cltd
idiv   %ecx
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_file_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    1254 <cmd_file_read+0xc4>
mov    0x8(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1279 <cmd_file_read+0xe9>
mov    %eax,%r12d
mov    0x8(%rbp),%rsi
lea    0xe69(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%r14d
lea    0x2e99(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ebx
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
lea    (%rax,%rbx,1),%ebp
test   %eax,%eax
jne    11f0 <cmd_file_read+0x60>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%r15d
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %r14d,%esi
call   1040 <difftime@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
lea    0x3ff(%rax),%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0xa,%esi
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd98(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rbp),%rbp
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd80(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@cmd_file_read.buffer = internal global [1024 x i8] zeroinitializer
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s frome_file_name\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"Open file [%s] error [%s]\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Begin to Read File: %s\0A\00"
@.str.3 = [27 x i8] c"File Read Speed : %d KB/s\0A\00"
define i32 @cmd_file_read(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %8
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = load i8**, i8*** %4
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %21
%38 = load i8**, i8*** %4
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %40)
store i32 0, i32* %8
%42 = call i32 @time(i32* null)
store i32 %42, i32* %9
br label %43
43: ; preds = %47, %37
%44 = load i32, i32* %5
%45 = call i32 @read(i32 %44, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @cmd_file_read.buffer, i64 0, i64 0), i32 1024)
store i32 %45, i32* %7
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i32, i32* %7
%49 = load i32, i32* %8
%50 = add nsw i32 %49, %48
store i32 %50, i32* %8
br label %43
51: ; preds = %43
%52 = call i32 @time(i32* null)
store i32 %52, i32* %10
%53 = load i32, i32* %5
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %8
%56 = load i32, i32* %10
%57 = load i32, i32* %9
%58 = call i32 @difftime(i32 %56, i32 %57)
%59 = sdiv i32 %55, %58
%60 = sdiv i32 %59, 1024
store i32 %60, i32* %11
%61 = load i32, i32* %11
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %61)
ret i32 0
}

LLVM IR (-O2):
@cmd_file_read.buffer = internal global [1024 x i8] zeroinitializer
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s frome_file_name\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"Open file [%s] error [%s]\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Begin to Read File: %s\0A\00"
@.str.3 = [27 x i8] c"File Read Speed : %d KB/s\0A\00"
define i32 @cmd_file_read(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %10
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9
%23 = load i8*, i8** %10
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = tail call i32 @time(i32* null)
br label %26
26: ; preds = %26, %22
%27 = phi i32 [ 0, %22 ], [ %30, %26 ]
%28 = tail call i32 @read(i32 %13, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @cmd_file_read.buffer, i64 0, i64 0), i32 1024)
%29 = icmp eq i32 %28, 0
%30 = add nsw i32 %28, %27
br i1 %29, label %31, label %26
31: ; preds = %26
%32 = tail call i32 @time(i32* null)
%33 = tail call i32 @close(i32 %13)
%34 = tail call i32 @difftime(i32 %32, i32 %25)
%35 = sdiv i32 %27, %34
%36 = sdiv i32 %35, 1024
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %36)
ret i32 0
}