#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 /*<<< orphan*/ free(int *);
int *malloc(unsigned long long);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, unsigned long long *);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Usage: %s size(megabytes)\n", argv[0]);
    return 1;
  }
  unsigned long long size;
  if (sscanf(argv[1], "%lu", &size) != 1) {
    printf("Expected integer size in megabytes\n");
    return 1;
  }
  int *top;
  size = size * 1024 * 1024;
  while (size != 0) {
    top = malloc(size);
    if (top == NULL)
      size = (3 * size) / 4;
    else
      break;
  }
  printf("Hogging %llu bytes (%llu mb)\n", size, size / (1024 * 1024));
  unsigned long long cycles = 0;
  for (int i = 0;; i = (i + 4096 / sizeof(int)) % (size / sizeof(int))) {
    top[i]++;
  }
  free(top);
  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)
cmpl   $0x2,-0x8(%rbp)
je     1171 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1256 <main+0x126>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe9b(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11ac <main+0x7c>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1256 <main+0x126>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
shl    $0xa,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fb <main+0xcb>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f1 <main+0xc1>
imul   $0x3,-0x18(%rbp),%rax
shr    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    11f6 <main+0xc6>
jmp    11fb <main+0xcb>
jmp    11bc <main+0x8c>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shr    $0x14,%rdx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
movslq -0x2c(%rbp),%rax
add    $0x400,%rax
mov    -0x18(%rbp),%rcx
shr    $0x2,%rcx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x2c(%rbp)
jmp    1224 <main+0xf4>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    11a0 <main+0x60>
mov    0x8(%rsi),%rdi
lea    0xec2(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11b3 <main+0x73>
mov    0x8(%rsp),%r14
shl    $0x14,%r14
je     11ce <main+0x8e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
jne    11d6 <main+0x96>
lea    (%r14,%r14,2),%rax
mov    %rax,%r14
shr    $0x2,%r14
cmp    $0x3,%rax
ja     1180 <main+0x40>
jmp    11d1 <main+0x91>
mov    (%rsi),%rsi
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11bf <main+0x7f>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    11d9 <main+0x99>
mov    %rax,%r15
mov    %r14,0x8(%rsp)
mov    %r14,%rdx
shr    $0x14,%rdx
lea    0xe33(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rcx
shr    $0x2,%rcx
jmp    1218 <main+0xd8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edx,%edx
div    %rcx
mov    %rdx,%rbx
movslq %ebx,%rax
addl   $0x1,(%r15,%rax,4)
add    $0x400,%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    1210 <main+0xd0>
xor    %edx,%edx
div    %ecx
mov    %edx,%ebx
jmp    1218 <main+0xd8>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: %s size(megabytes)\0A\00"
@.str.1 = [4 x i8] c"%lu\00"
@.str.2 = [36 x i8] c"Expected integer size in megabytes\0A\00"
@.str.3 = [30 x i8] c"Hogging %llu bytes (%llu mb)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %63
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %6)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %63
25: ; preds = %17
%26 = load i64, i64* %6
%27 = mul i64 %26, 1024
%28 = mul i64 %27, 1024
store i64 %28, i64* %6
br label %29
29: ; preds = %42, %25
%30 = load i64, i64* %6
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i64, i64* %6
%34 = call i32* @malloc(i64 %33)
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i64, i64* %6
%39 = mul i64 3, %38
%40 = udiv i64 %39, 4
store i64 %40, i64* %6
br label %42
41: ; preds = %32
br label %43
42: ; preds = %37
br label %29
43: ; preds = %41, %29
%44 = load i64, i64* %6
%45 = load i64, i64* %6
%46 = udiv i64 %45, 1048576
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64 %44, i64 %46)
store i64 0, i64* %8
store i32 0, i32* %9
br label %48
48: ; preds = %55, %43
%49 = load i32*, i32** %7
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1
store i32 %54, i32* %52
br label %55
55: ; preds = %48
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = add i64 %57, 1024
%59 = load i64, i64* %6
%60 = udiv i64 %59, 4
%61 = urem i64 %58, %60
%62 = trunc i64 %61 to i32
store i32 %62, i32* %9
br label %48
63: ; preds = %23, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [27 x i8] c"Usage: %s size(megabytes)\0A\00"
@.str.1 = [4 x i8] c"%lu\00"
@.str.3 = [30 x i8] c"Hogging %llu bytes (%llu mb)\0A\00"
@str = [35 x i8] c"Expected integer size in megabytes\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %44
8: ; preds = %2
%9 = bitcast i64* %3 to i8*
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%13 = icmp eq i32 %12, 1
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %44
16: ; preds = %8
%17 = load i64, i64* %3
%18 = shl i64 %17, 20
%19 = icmp eq i64 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %16, %24
%21 = phi i64 [ %26, %24 ], [ %18, %16 ]
%22 = call i32* @malloc(i64 %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = mul i64 %21, 3
%26 = lshr i64 %25, 2
%27 = icmp ult i64 %25, 4
br i1 %27, label %28, label %20
28: ; preds = %24, %20, %16
%29 = phi i64 [ 0, %16 ], [ %21, %20 ], [ %26, %24 ]
%30 = phi i32* [ null, %16 ], [ %22, %20 ], [ null, %24 ]
store i64 %29, i64* %3
%31 = lshr i64 %29, 20
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i64 %29, i64 %31)
%33 = load i64, i64* %3
%34 = lshr i64 %33, 2
br label %35
35: ; preds = %35, %28
%36 = phi i64 [ 0, %28 ], [ %43, %35 ]
%37 = shl i64 %36, 32
%38 = ashr exact i64 %37, 32
%39 = getelementptr inbounds i32, i32* %30, i64 %38
%40 = load i32, i32* %39
%41 = add nsw i32 %40, 1
store i32 %41, i32* %39
%42 = add nsw i64 %38, 1024
%43 = urem i64 %42, %34
br label %35
44: ; preds = %14, %5
ret i32 1
}