#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 *);
double elapsed_time_hr(struct timespec, struct timespec, char *);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
int mem_write_random(unsigned int size) {
  int i, n;
  unsigned int t;
  int nbytes = size * 1048576;
  int nelements = nbytes / sizeof(int);
  double oh, rt = 0.0;
  struct timespec start, end;
  t = (int)time(NULL);
  int *array = (int *)malloc(nbytes);
  if (array == NULL) {
    printf("Out Of Memory: could not allocate space for the array.\n");
    return 0;
  }
  srand(t);
  clock_gettime(CLOCK_MONOTONIC, &start);
  for (i = 0; i < nelements; i++) {
    n = rand() % (nelements);
  }
  clock_gettime(CLOCK_MONOTONIC, &end);
  oh = elapsed_time_hr(start, end, "Overhead for random write");
  printf("Keep result: n = %d\n", n);
  srand(t);
  int data = rand();
  clock_gettime(CLOCK_MONOTONIC, &start);
  for (i = 0; i < nelements; i++) {
    n = rand() % (nelements);
    array[n] = data;
  }
  clock_gettime(CLOCK_MONOTONIC, &end);
  rt = elapsed_time_hr(start, end, "Write random");
  printf("Runtime: %f\n", (rt - oh));
  printf("Finished allocating %d MB memory (%d ints) and "
         "filling the array in a random manner.\n\n",
         size, nelements);
  free(array);
  return 0;
}


x86 assembly (clang, -O0):
<mem_write_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x14,%eax
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11e4 <mem_write_random+0x64>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132d <mem_write_random+0x1ad>
mov    -0x14(%rbp),%edi
call   1070 <srand@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1050 <clock_gettime@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    122d <mem_write_random+0xad>
mov    $0x0,%al
call   10a0 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1205 <mem_write_random+0x85>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x38(%rbp),%edi
mov    -0x40(%rbp),%esi
lea    0xdec(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%esi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <srand@plt>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1050 <clock_gettime@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12c8 <mem_write_random+0x148>
mov    $0x0,%al
call   10a0 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x4c(%rbp),%edx
mov    -0x48(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1292 <mem_write_random+0x112>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x38(%rbp),%edi
mov    -0x40(%rbp),%esi
lea    0xd80(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
lea    0xd72(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_write_random>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %edi,%ebp
shl    $0x14,%ebp
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1214 <mem_write_random+0x84>
mov    %rax,%r13
mov    %ebx,0xc(%rsp)
mov    %ebp,%r15d
sar    $0x2,%r15d
mov    %r12d,%edi
call   1080 <srand@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <clock_gettime@plt>
test   %ebp,%ebp
jle    1225 <mem_write_random+0x95>
cmp    $0x2,%r15d
mov    $0x1,%ebx
cmovge %r15d,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10b0 <rand@plt>
add    $0xffffffff,%ebx
jne    1200 <mem_write_random+0x70>
cltd
idiv   %r15d
mov    %edx,%ebx
jmp    1225 <mem_write_random+0x95>
lea    0xe85(%rip),%rdi
call   1050 <puts@plt>
jmp    12fd <mem_write_random+0x16d>
mov    0x2dbc(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
mov    0x8(%rsp),%edi
mov    (%rsp),%esi
lea    0xdbb(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  %xmm0,0x10(%rsp)
lea    0xdc3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12d,%edi
call   1080 <srand@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
mov    %eax,%ebx
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1060 <clock_gettime@plt>
test   %ebp,%ebp
jle    12a8 <mem_write_random+0x118>
cmp    $0x2,%r15d
mov    $0x1,%ebp
cmovge %r15d,%ebp
nop
xor    %eax,%eax
call   10b0 <rand@plt>
cltd
idiv   %r15d
movslq %edx,%rax
mov    %ebx,0x0(%r13,%rax,4)
add    $0xffffffff,%ebp
jne    1290 <mem_write_random+0x100>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
mov    0x8(%rsp),%edi
mov    (%rsp),%esi
lea    0xd68(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
subsd  0x10(%rsp),%xmm0
lea    0xd63(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
lea    0xd62(%rip),%rdi
mov    0xc(%rsp),%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [56 x i8] c"Out Of Memory: could not allocate space for the array.\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [26 x i8] c"Overhead for random write\00"
@.str.2 = [21 x i8] c"Keep result: n = %d\0A\00"
@.str.3 = [13 x i8] c"Write random\00"
@.str.4 = [13 x i8] c"Runtime: %f\0A\00"
@.str.5 = [87 x i8] c"Finished allocating %d MB memory (%d ints) and filling the array in a random manner.\0A\0A\00"
define i32 @mem_write_random(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca %struct.timespec
%12 = alloca %struct.timespec
%13 = alloca i32*
%14 = alloca i32
store i32 %0, i32* %3
%15 = load i32, i32* %3
%16 = mul i32 %15, 1048576
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = udiv i64 %18, 4
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
store double 0.000000e+00, double* %10
%21 = call i64 @time(i32* null)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %6
%23 = load i32, i32* %7
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %13
%26 = load i32*, i32** %13
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %1
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %94
30: ; preds = %1
%31 = load i32, i32* %6
%32 = call i32 @srand(i32 %31)
%33 = load i32, i32* @CLOCK_MONOTONIC
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %11)
store i32 0, i32* %4
br label %35
35: ; preds = %43, %30
%36 = load i32, i32* %4
%37 = load i32, i32* %8
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = call i32 (...) @rand()
%41 = load i32, i32* %8
%42 = srem i32 %40, %41
store i32 %42, i32* %5
br label %43
43: ; preds = %39
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %35
46: ; preds = %35
%47 = load i32, i32* @CLOCK_MONOTONIC
%48 = call i32 @clock_gettime(i32 %47, %struct.timespec* %12)
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call double @elapsed_time_hr(i32 %50, i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store double %53, double* %9
%54 = load i32, i32* %5
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %6
%57 = call i32 @srand(i32 %56)
%58 = call i32 (...) @rand()
store i32 %58, i32* %14
%59 = load i32, i32* @CLOCK_MONOTONIC
%60 = call i32 @clock_gettime(i32 %59, %struct.timespec* %11)
store i32 0, i32* %4
br label %61
61: ; preds = %74, %46
%62 = load i32, i32* %4
%63 = load i32, i32* %8
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %77
65: ; preds = %61
%66 = call i32 (...) @rand()
%67 = load i32, i32* %8
%68 = srem i32 %66, %67
store i32 %68, i32* %5
%69 = load i32, i32* %14
%70 = load i32*, i32** %13
%71 = load i32, i32* %5
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %70, i64 %72
store i32 %69, i32* %73
br label %74
74: ; preds = %65
%75 = load i32, i32* %4
%76 = add nsw i32 %75, 1
store i32 %76, i32* %4
br label %61
77: ; preds = %61
%78 = load i32, i32* @CLOCK_MONOTONIC
%79 = call i32 @clock_gettime(i32 %78, %struct.timespec* %12)
%80 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%81 = load i32, i32* %80
%82 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%83 = load i32, i32* %82
%84 = call double @elapsed_time_hr(i32 %81, i32 %83, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store double %84, double* %10
%85 = load double, double* %10
%86 = load double, double* %9
%87 = fsub double %85, %86
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), double %87)
%89 = load i32, i32* %3
%90 = load i32, i32* %8
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.5, i64 0, i64 0), i32 %89, i32 %90)
%92 = load i32*, i32** %13
%93 = call i32 @free(i32* %92)
store i32 0, i32* %2
br label %94
94: ; preds = %77, %28
%95 = load i32, i32* %2
ret i32 %95
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [26 x i8] c"Overhead for random write\00"
@.str.2 = [21 x i8] c"Keep result: n = %d\0A\00"
@.str.3 = [13 x i8] c"Write random\00"
@.str.4 = [13 x i8] c"Runtime: %f\0A\00"
@.str.5 = [87 x i8] c"Finished allocating %d MB memory (%d ints) and filling the array in a random manner.\0A\0A\00"
@str = [55 x i8] c"Out Of Memory: could not allocate space for the array.\00"
define i32 @mem_write_random(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = shl i32 %0, 20
%5 = ashr exact i32 %4, 2
%6 = bitcast %struct.timespec* %2 to i8*
%7 = bitcast %struct.timespec* %3 to i8*
%8 = tail call i64 @time(i32* null)
%9 = trunc i64 %8 to i32
%10 = tail call i64 @malloc(i32 %4)
%11 = inttoptr i64 %10 to i32*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
br label %63
15: ; preds = %1
%16 = tail call i32 @srand(i32 %9)
%17 = load i32, i32* @CLOCK_MONOTONIC
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %2)
%19 = icmp sgt i32 %4, 0
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = call i32 @llvm.smax.i32(i32 %5, i32 1)
br label %22
22: ; preds = %20, %22
%23 = phi i32 [ %25, %22 ], [ 0, %20 ]
%24 = call i32 (...) @rand()
%25 = add nuw nsw i32 %23, 1
%26 = icmp eq i32 %25, %21
br i1 %26, label %27, label %22
27: ; preds = %22
%28 = srem i32 %24, %5
br label %29
29: ; preds = %27, %15
%30 = phi i32 [ %28, %27 ], [ undef, %15 ]
%31 = load i32, i32* @CLOCK_MONOTONIC
%32 = call i32 @clock_gettime(i32 %31, %struct.timespec* %3)
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call double @elapsed_time_hr(i32 %34, i32 %36, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%39 = call i32 @srand(i32 %9)
%40 = call i32 (...) @rand()
%41 = load i32, i32* @CLOCK_MONOTONIC
%42 = call i32 @clock_gettime(i32 %41, %struct.timespec* %2)
br i1 %19, label %43, label %53
43: ; preds = %29
%44 = call i32 @llvm.smax.i32(i32 %5, i32 1)
br label %45
45: ; preds = %43, %45
%46 = phi i32 [ %51, %45 ], [ 0, %43 ]
%47 = call i32 (...) @rand()
%48 = srem i32 %47, %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %11, i64 %49
store i32 %40, i32* %50
%51 = add nuw nsw i32 %46, 1
%52 = icmp eq i32 %51, %44
br i1 %52, label %53, label %45
53: ; preds = %45, %29
%54 = load i32, i32* @CLOCK_MONOTONIC
%55 = call i32 @clock_gettime(i32 %54, %struct.timespec* %3)
%56 = load i32, i32* %33
%57 = load i32, i32* %35
%58 = call double @elapsed_time_hr(i32 %56, i32 %57, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%59 = fsub double %58, %37
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), double %59)
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([87 x i8], [87 x i8]* @.str.5, i64 0, i64 0), i32 %0, i32 %5)
%62 = call i32 @free(i32* %11)
br label %63
63: ; preds = %53, %13
ret i32 0
}