#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 atoi(char *);
void *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ nice(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  int footprint_mb = (1024);
  if (argc > 1) {
    footprint_mb = atoi(argv[1]);
  }
  nice(10);
  printf("Pre-allocating %dMB...\n", footprint_mb);
  char **buffers = malloc(sizeof(char *) * footprint_mb);
  for (int i = 0; i < footprint_mb; i++) {
    buffers[i] = malloc((1024 * 1024));
  }
  printf("Done. Starting to touch that memory randomly... press ctrl+C to "
         "exit.\n");
  srand(time(NULL));
  int iter = 0;
  for (;;) {
    int buffer_num = rand() % footprint_mb;
    memset(buffers[buffer_num], 0, (1024 * 1024));
    if (++iter >= 64) {
      printf("Wrote buffer %5d (and %d others); %dMB total touched.\n",
             buffer_num, iter - 1, iter);
      sleep(1);
      iter = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x400,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c7 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0xa,%edi
call   1080 <nice@plt>
mov    -0x14(%rbp),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    122f <main+0x9f>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fc <main+0x6c>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmp    $0x40,%eax
jl     12b7 <main+0x127>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
sub    $0x1,%edx
mov    -0x28(%rbp),%ecx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movl   $0x0,-0x28(%rbp)
jmp    1254 <main+0xc4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x400,%r14d
cmp    $0x2,%edi
jl     11bf <main+0x1f>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    $0xa,%edi
call   1090 <nice@plt>
lea    0xe30(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x0(,%r14,8),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    1217 <main+0x77>
mov    %r14d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x100000,%edi
call   1080 <malloc@plt>
mov    %rax,(%r12,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1200 <main+0x60>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xde0(%rip),%r15
nopl   0x0(%rax,%rax,1)
xor    %ecx,%ecx
mov    %ecx,%ebx
xor    %eax,%eax
call   10c0 <rand@plt>
cltd
idiv   %r14d
mov    %edx,%ebp
movslq %edx,%rax
mov    (%r12,%rax,8),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
lea    0x1(%rbx),%ecx
cmp    $0x3f,%ebx
jl     1242 <main+0xa2>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1240 <main+0xa0>

LLVM IR (-O0):
@.str = [24 x i8] c"Pre-allocating %dMB...\0A\00"
@.str.1 = [71 x i8] c"Done. Starting to touch that memory randomly... press ctrl+C to exit.\0A\00"
@.str.2 = [55 x i8] c"Wrote buffer %5d (and %d others); %dMB total touched.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1024, i32* %6
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %6
br label %18
18: ; preds = %13, %2
%19 = call i32 @nice(i32 10)
%20 = load i32, i32* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = mul i64 8, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
%27 = bitcast i8* %26 to i8**
store i8** %27, i8*** %7
store i32 0, i32* %8
br label %28
28: ; preds = %38, %18
%29 = load i32, i32* %8
%30 = load i32, i32* %6
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = call i8* @malloc(i32 1048576)
%34 = load i8**, i8*** %7
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
store i8* %33, i8** %37
br label %38
38: ; preds = %32
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %28
41: ; preds = %28
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @time(i32* null)
%44 = call i32 @srand(i32 %43)
store i32 0, i32* %9
br label %45
45: ; preds = %65, %41
%46 = call i32 (...) @rand()
%47 = load i32, i32* %6
%48 = srem i32 %46, %47
store i32 %48, i32* %10
%49 = load i8**, i8*** %7
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8*, i8** %49, i64 %51
%53 = load i8*, i8** %52
%54 = call i32 @memset(i8* %53, i32 0, i32 1048576)
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
store i32 %56, i32* %9
%57 = icmp sge i32 %56, 64
br i1 %57, label %58, label %65
58: ; preds = %45
%59 = load i32, i32* %10
%60 = load i32, i32* %9
%61 = sub nsw i32 %60, 1
%62 = load i32, i32* %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %59, i32 %61, i32 %62)
%64 = call i32 @sleep(i32 1)
store i32 0, i32* %9
br label %65
65: ; preds = %58, %45
br label %45
}

LLVM IR (-O2):
@.str = [24 x i8] c"Pre-allocating %dMB...\0A\00"
@.str.2 = [55 x i8] c"Wrote buffer %5d (and %d others); %dMB total touched.\0A\00"
@str = [70 x i8] c"Done. Starting to touch that memory randomly... press ctrl+C to exit.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
br label %8
8: ; preds = %4, %2
%9 = phi i32 [ %7, %4 ], [ 1024, %2 ]
%10 = tail call i32 @nice(i32 10)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %9)
%12 = shl i32 %9, 3
%13 = tail call i8* @malloc(i32 %12)
%14 = bitcast i8* %13 to i8**
%15 = icmp sgt i32 %9, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = zext i32 %9 to i64
br label %22
18: ; preds = %22, %8
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @time(i32* null)
%21 = tail call i32 @srand(i32 %20)
br label %28
22: ; preds = %16, %22
%23 = phi i64 [ 0, %16 ], [ %26, %22 ]
%24 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%25 = getelementptr inbounds i8*, i8** %14, i64 %23
store i8* %24, i8** %25
%26 = add nuw nsw i64 %23, 1
%27 = icmp eq i64 %26, %17
br i1 %27, label %18, label %22
28: ; preds = %41, %18
%29 = phi i32 [ 0, %18 ], [ %42, %41 ]
%30 = tail call i32 (...) @rand()
%31 = srem i32 %30, %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %14, i64 %32
%34 = load i8*, i8** %33
%35 = tail call i32 @memset(i8* %34, i32 0, i32 1048576)
%36 = add nsw i32 %29, 1
%37 = icmp sgt i32 %29, 62
br i1 %37, label %38, label %41
38: ; preds = %28
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %29, i32 %36)
%40 = tail call i32 @sleep(i32 1)
br label %41
41: ; preds = %38, %28
%42 = phi i32 [ 0, %38 ], [ %36, %28 ]
br label %28
}