#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 */
void *malloc(unsigned int const);
int /*<<< orphan*/ memset(void *, char, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ realloc(void *, int);
int /*<<< orphan*/ sleep(int);
int strtoul(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  if (2 == argc) {
    const unsigned size = strtoul(argv[1], NULL, 0) << 20;
    const unsigned block_size = 512 << 10;
    int big_chunk = size - size * 0.05;
    int i = 1, block_nbr = (size - big_chunk) / block_size;
    void *data;
    printf("%d\n", block_nbr);
    printf("%d\n", big_chunk);
    printf("%d\n", block_size);
    data = malloc(block_size + big_chunk);
    for (i = 1; i < block_nbr; i++) {
      data = (char *)realloc(data, i * block_size + big_chunk);
      memset(data, '-', i * block_size + big_chunk);
    }
    printf("loadmem eats %u MB \n", (i * block_size + big_chunk) >> 20);
    while (1)
      sleep(5);
  } else {
    printf("Usage: loadmem <Memory allocation in MB>\n");
    printf("Example: loadmem 100\n");
  }
  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)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
jne    12ac <main+0x14c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <strtoul@plt>
shl    $0x14,%eax
mov    %eax,-0x14(%rbp)
movl   $0x80000,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cvtsi2sd %rax,%xmm1
movaps %xmm1,%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  0xe34(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
shr    $0x13,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf1(%rip),%rdi
mov    $0x80000,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
add    $0x80000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1283 <main+0x123>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
shl    $0x13,%esi
add    -0x1c(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%edx
shl    $0x13,%edx
add    -0x1c(%rbp),%edx
mov    $0x2d,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    123c <main+0xdc>
mov    -0x20(%rbp),%esi
shl    $0x13,%esi
add    -0x1c(%rbp),%esi
shr    $0x14,%esi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
jmp    129d <main+0x13d>
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    127c <main+0x10c>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%r13d
shl    $0x14,%r13d
cvtsi2sd %r13,%xmm0
movsd  0xe5b(%rip),%xmm1
mulsd  %xmm0,%xmm1
addsd  %xmm0,%xmm1
cvttsd2si %xmm1,%ebp
sub    %ebp,%r13d
mov    %r13d,%r12d
shr    $0x13,%r12d
lea    0xe46(%rip),%rbx
mov    %rbx,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x80000,%r15d
mov    %rbx,%rdi
mov    $0x80000,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,(%rsp)
add    $0x80000,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
cmp    $0x100000,%r13d
jb     124b <main+0xdb>
mov    %rax,%r14
cmp    $0x3,%r12d
mov    $0x2,%r15d
cmovae %r12d,%r15d
lea    -0x1(%r15),%ebx
nop
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2d,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
add    $0x80000,%ebp
add    $0xffffffff,%ebx
jne    1220 <main+0xb0>
shl    $0x13,%r15d
add    (%rsp),%r15d
shr    $0x14,%r15d
lea    0xdb2(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x5,%edi
call   1090 <sleep@plt>
jmp    1270 <main+0x100>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
lea    0xdbb(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [21 x i8] c"loadmem eats %u MB \0A\00"
@.str.2 = [42 x i8] c"Usage: loadmem <Memory allocation in MB>\0A\00"
@.str.3 = [22 x i8] c"Example: loadmem 100\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 2, %12
br i1 %13, label %14, label %69
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strtoul(i8* %17, i32* null, i32 0)
%19 = shl i32 %18, 20
store i32 %19, i32* %6
store i32 524288, i32* %7
%20 = load i32, i32* %6
%21 = uitofp i32 %20 to double
%22 = load i32, i32* %6
%23 = uitofp i32 %22 to double
%24 = fneg double %23
%25 = call double @llvm.fmuladd.f64(double %24, double 5.000000e-02, double %21)
%26 = fptosi double %25 to i32
store i32 %26, i32* %8
store i32 1, i32* %9
%27 = load i32, i32* %6
%28 = load i32, i32* %8
%29 = sub i32 %27, %28
%30 = udiv i32 %29, 524288
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 524288)
%36 = load i32, i32* %8
%37 = add i32 524288, %36
%38 = call i8* @malloc(i32 %37)
store i8* %38, i8** %11
store i32 1, i32* %9
br label %39
39: ; preds = %57, %14
%40 = load i32, i32* %9
%41 = load i32, i32* %10
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %60
43: ; preds = %39
%44 = load i8*, i8** %11
%45 = load i32, i32* %9
%46 = mul i32 %45, 524288
%47 = load i32, i32* %8
%48 = add i32 %46, %47
%49 = call i64 @realloc(i8* %44, i32 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %11
%51 = load i8*, i8** %11
%52 = load i32, i32* %9
%53 = mul i32 %52, 524288
%54 = load i32, i32* %8
%55 = add i32 %53, %54
%56 = call i32 @memset(i8* %51, i8 signext 45, i32 %55)
br label %57
57: ; preds = %43
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %39
60: ; preds = %39
%61 = load i32, i32* %9
%62 = mul i32 %61, 524288
%63 = load i32, i32* %8
%64 = add i32 %62, %63
%65 = lshr i32 %64, 20
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %60, %67
%68 = call i32 @sleep(i32 5)
br label %67
69: ; preds = %2
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %69
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [21 x i8] c"loadmem eats %u MB \0A\00"
@str = [41 x i8] c"Usage: loadmem <Memory allocation in MB>\00"
@str.4 = [21 x i8] c"Example: loadmem 100\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %42
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @strtoul(i8* nocapture %6, i32* null, i32 0)
%8 = shl i32 %7, 20
%9 = uitofp i32 %8 to double
%10 = fneg double %9
%11 = tail call double @llvm.fmuladd.f64(double %10, double 5.000000e-02, double %9)
%12 = fptosi double %11 to i32
%13 = sub i32 %8, %12
%14 = lshr i32 %13, 19
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %12)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 524288)
%18 = add i32 %12, 524288
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp ugt i32 %13, 1048575
br i1 %20, label %21, label %35
21: ; preds = %4
%22 = call i32 @llvm.umax.i32(i32 %14, i32 2)
br label %23
23: ; preds = %21, %23
%24 = phi i8* [ %29, %23 ], [ %19, %21 ]
%25 = phi i32 [ %31, %23 ], [ 1, %21 ]
%26 = shl i32 %25, 19
%27 = add i32 %26, %12
%28 = tail call i64 @realloc(i8* %24, i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = tail call i32 @memset(i8* %29, i8 signext 45, i32 %27)
%31 = add nuw nsw i32 %25, 1
%32 = icmp eq i32 %31, %22
br i1 %32, label %33, label %23
33: ; preds = %23
%34 = shl nuw i32 %22, 19
br label %35
35: ; preds = %33, %4
%36 = phi i32 [ 524288, %4 ], [ %34, %33 ]
%37 = add i32 %36, %12
%38 = lshr i32 %37, 20
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %40, %35
%41 = tail call i32 @sleep(i32 5)
br label %40
42: ; preds = %2
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
ret i32 0
}