#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 uint64_t;
struct stat {
  int st_size;
};
typedef int int64_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ free(struct stat *);
struct stat *malloc(int);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int rdtsc();
int read(int, char *, int);
int /*<<< orphan*/ stat(char *, struct stat *);
float read_time(char *file) {
  int fd, rd;
  struct stat *buf;
  buf = malloc(sizeof(struct stat));
  fd = (int)open(file, O_RDONLY);
  if (fd > 1) {
    stat(file, buf);
    int size = buf->st_size;
    free(buf);
    char buff[size];
    uint64_t start = rdtsc();
    rd = read(fd, buff, size);
    uint64_t stop = rdtsc();
    unsigned int lo = start & (((uint64_t)2 << 32) - 1);
    unsigned int lo2 = stop & (((int64_t)2 << 32) - 1);
    int cycles = lo2 - lo;
    float t = ((float)(cycles));
    float factor = 3591338000.0 * .000001;
    t = t / factor;
    return t;
  } else {
    printf("Error with open");
    free(buf);
    return -1;
  }
}


x86 assembly (clang, -O0):
<read_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    1265 <read_time+0xf5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1050 <rdtsc@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <rdtsc@plt>
mov    %eax,-0x40(%rbp)
and    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
and    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
sub    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
cvtsi2ssl -0x4c(%rbp),%xmm0
movss  %xmm0,-0x50(%rbp)
movss  0xdc9(%rip),%xmm0
movss  %xmm0,-0x54(%rbp)
movss  -0x50(%rbp),%xmm0
divss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x50(%rbp)
movss  -0x50(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
jmp    1287 <read_time+0x117>
lea    0xd9c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movss  0xd7e(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_time>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
mov    %rax,%r14
cmp    $0x2,%r14d
jl     11f3 <read_time+0x83>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <stat@plt>
mov    (%rbx),%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rsp,%r12
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1050 <rdtsc@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
xor    %eax,%eax
call   1050 <rdtsc@plt>
mov    %r12,%rsp
jmp    1207 <read_time+0x97>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
movss  0xdf1(%rip),%xmm0
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [16 x i8] c"Error with open\00"
define float @read_time(i8* %0) {
%2 = alloca float
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca float
%16 = alloca float
store i8* %0, i8** %3
%17 = call %struct.stat* @malloc(i32 4)
store %struct.stat* %17, %struct.stat** %6
%18 = load i8*, i8** %3
%19 = load i32, i32* @O_RDONLY
%20 = call i64 @open(i8* %18, i32 %19)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %56
24: ; preds = %1
%25 = load i8*, i8** %3
%26 = load %struct.stat*, %struct.stat** %6
%27 = call i32 @stat(i8* %25, %struct.stat* %26)
%28 = load %struct.stat*, %struct.stat** %6
%29 = getelementptr inbounds %struct.stat, %struct.stat* %28, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %7
%31 = load %struct.stat*, %struct.stat** %6
%32 = call i32 @free(%struct.stat* %31)
%33 = load i32, i32* %7
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
store i8* %35, i8** %8
%36 = alloca i8, i64 %34
store i64 %34, i64* %9
%37 = call i32 (...) @rdtsc()
store i32 %37, i32* %10
%38 = load i32, i32* %4
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %38, i8* %36, i32 %39)
store i32 %40, i32* %5
%41 = call i32 (...) @rdtsc()
store i32 %41, i32* %11
%42 = load i32, i32* %10
%43 = and i32 %42, poison
store i32 %43, i32* %12
%44 = load i32, i32* %11
%45 = and i32 %44, poison
store i32 %45, i32* %13
%46 = load i32, i32* %13
%47 = load i32, i32* %12
%48 = sub i32 %46, %47
store i32 %48, i32* %14
%49 = load i32, i32* %14
%50 = sitofp i32 %49 to float
store float %50, float* %15
store float 0x40AC0EAD00000000, float* %16
%51 = load float, float* %15
%52 = load float, float* %16
%53 = fdiv float %51, %52
store float %53, float* %15
%54 = load float, float* %15
store float %54, float* %2
%55 = load i8*, i8** %8
br label %60
56: ; preds = %1
%57 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%58 = load %struct.stat*, %struct.stat** %6
%59 = call i32 @free(%struct.stat* %58)
store float -1.000000e+00, float* %2
br label %60
60: ; preds = %56, %24
%61 = load float, float* %2
ret float %61
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [16 x i8] c"Error with open\00"
define float @read_time(i8* nocapture %0) {
%2 = tail call %struct.stat* @malloc(i32 4)
%3 = load i32, i32* @O_RDONLY
%4 = tail call i64 @open(i8* %0, i32 %3)
%5 = trunc i64 %4 to i32
%6 = icmp sgt i32 %5, 1
br i1 %6, label %7, label %18
7: ; preds = %1
%8 = tail call i32 @stat(i8* %0, %struct.stat* %2)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @free(%struct.stat* %2)
%12 = zext i32 %10 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = tail call i32 (...) @rdtsc()
%16 = call i32 @read(i32 %5, i8* %14, i32 %10)
%17 = tail call i32 (...) @rdtsc()
tail call void @llvm.stackrestore(i8* %13)
br label %21
18: ; preds = %1
%19 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @free(%struct.stat* %2)
br label %21
21: ; preds = %18, %7
ret float -1.000000e+00
}