#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 */
struct timespec {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ elapsed_time_hr(struct timespec, struct timespec, char *);
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ srand(int);
scalar_t__ time(int /*<<< orphan*/ *);
int mem_read_ram(unsigned long reps) {
  struct timespec start, end;
  int i, a;
  volatile int b, c;
  srand((int)time(NULL));
  b = rand();
  c = rand();
  clock_gettime(CLOCK_MONOTONIC, &start);
  for (i = 0; i < reps; i++) {
    a = b + c;
  }
  clock_gettime(CLOCK_MONOTONIC, &end);
  elapsed_time_hr(start, end, "RAM reads");
  printf("Finished %lu RAM reads.\n\n", (2 * reps));
  return 0;
}


x86 assembly (clang, -O0):
<mem_read_ram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x0,%al
call   1080 <rand@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11d0 <mem_read_ram+0x70>
mov    -0x24(%rbp),%eax
mov    -0x28(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a9 <mem_read_ram+0x49>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    0xe11(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
mov    -0x8(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_read_ram>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   1080 <rand@plt>
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1080 <rand@plt>
mov    %eax,(%rsp)
mov    0x2e54(%rip),%r14
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rbx,%rbx
je     121d <mem_read_ram+0xbd>
lea    -0x1(%rbx),%rcx
mov    %ebx,%eax
and    $0x7,%eax
cmp    $0x7,%rcx
jb     11fe <mem_read_ram+0x9e>
mov    %rbx,%rcx
and    $0xfffffffffffffff8,%rcx
nopl   0x0(%rax)
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
mov    0x4(%rsp),%edx
mov    (%rsp),%edx
add    $0xfffffffffffffff8,%rcx
jne    11c0 <mem_read_ram+0x60>
test   %rax,%rax
je     121d <mem_read_ram+0xbd>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x4(%rsp),%ecx
mov    (%rsp),%ecx
add    $0xffffffffffffffff,%rax
jne    1210 <mem_read_ram+0xb0>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%edi
mov    0x8(%rsp),%esi
lea    0xdc7(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
add    %ebx,%ebx
lea    0xdc3(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [10 x i8] c"RAM reads\00"
@.str.1 = [26 x i8] c"Finished %lu RAM reads.\0A\0A\00"
define i32 @mem_read_ram(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i64 %0, i64* %2
%9 = call i64 @time(i32* null)
%10 = trunc i64 %9 to i32
%11 = call i32 @srand(i32 %10)
%12 = call i32 (...) @rand()
store volatile i32 %12, i32* %7
%13 = call i32 (...) @rand()
store volatile i32 %13, i32* %8
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %3)
store i32 0, i32* %5
br label %16
16: ; preds = %25, %1
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = load i64, i64* %2
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load volatile i32, i32* %7
%23 = load volatile i32, i32* %8
%24 = add nsw i32 %22, %23
store i32 %24, i32* %6
br label %25
25: ; preds = %21
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %16
28: ; preds = %16
%29 = load i32, i32* @CLOCK_MONOTONIC
%30 = call i32 @clock_gettime(i32 %29, %struct.timespec* %4)
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @elapsed_time_hr(i32 %32, i32 %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%36 = load i64, i64* %2
%37 = mul i64 2, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %38)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [10 x i8] c"RAM reads\00"
@.str.1 = [26 x i8] c"Finished %lu RAM reads.\0A\0A\00"
define i32 @mem_read_ram(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.timespec* %2 to i8*
%7 = bitcast %struct.timespec* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = tail call i64 @time(i32* null)
%11 = trunc i64 %10 to i32
%12 = tail call i32 @srand(i32 %11)
%13 = tail call i32 (...) @rand()
store volatile i32 %13, i32* %4
%14 = tail call i32 (...) @rand()
store volatile i32 %14, i32* %5
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %2)
%17 = icmp eq i64 %0, 0
br i1 %17, label %52, label %18
18: ; preds = %1
%19 = add i64 %0, -1
%20 = and i64 %0, 7
%21 = icmp ult i64 %19, 7
br i1 %21, label %44, label %22
22: ; preds = %18
%23 = and i64 %0, -8
br label %24
24: ; preds = %24, %22
%25 = phi i64 [ 0, %22 ], [ %42, %24 ]
%26 = load volatile i32, i32* %4
%27 = load volatile i32, i32* %5
%28 = load volatile i32, i32* %4
%29 = load volatile i32, i32* %5
%30 = load volatile i32, i32* %4
%31 = load volatile i32, i32* %5
%32 = load volatile i32, i32* %4
%33 = load volatile i32, i32* %5
%34 = load volatile i32, i32* %4
%35 = load volatile i32, i32* %5
%36 = load volatile i32, i32* %4
%37 = load volatile i32, i32* %5
%38 = load volatile i32, i32* %4
%39 = load volatile i32, i32* %5
%40 = load volatile i32, i32* %4
%41 = load volatile i32, i32* %5
%42 = add i64 %25, 8
%43 = icmp eq i64 %42, %23
br i1 %43, label %44, label %24
44: ; preds = %24, %18
%45 = icmp eq i64 %20, 0
br i1 %45, label %52, label %46
46: ; preds = %44, %46
%47 = phi i64 [ %50, %46 ], [ 0, %44 ]
%48 = load volatile i32, i32* %4
%49 = load volatile i32, i32* %5
%50 = add i64 %47, 1
%51 = icmp eq i64 %50, %20
br i1 %51, label %52, label %46
52: ; preds = %44, %46, %1
%53 = load i32, i32* @CLOCK_MONOTONIC
%54 = call i32 @clock_gettime(i32 %53, %struct.timespec* %3)
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @elapsed_time_hr(i32 %56, i32 %58, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%60 = trunc i64 %0 to i32
%61 = shl i32 %60, 1
%62 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %61)
ret i32 0
}