#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 */
unsigned int INT_MAX;
scalar_t__ calloc(unsigned int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(char *, int, unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int rand();
int scanf(char *, unsigned int *);
int main() {
  unsigned int n = 1;
  int check;
  char *aux;
  while (1) {
    printf("how many 100MB blocks to allocate? 0 to leave >> ");
    check = scanf("%ud", &n);
    if (n == 0 || check != 1) {
      puts("leaving, bye");
      exit(0);
    }
    if (100 * 131072u > INT_MAX / n) {
      printf("integer overflow, try allocating less than %d 100MB blocks\n\n",
             INT_MAX / (100 * 131072u));
    } else {
      aux = (char *)calloc(n * 100 * 131072u, 8);
      if (aux == NULL) {
        puts("Can't allocate this ammount of memory");
      } else {
        memset(aux, rand() % 50 + 1, n * 100 * 1048576u);
        puts("sucessful alloc, press enter to free");
        getchar();
        getchar();
        free(aux);
        puts("memory freed\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe77(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1090 <scanf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11db <main+0x4b>
cmpl   $0x1,-0xc(%rbp)
je     11ee <main+0x5e>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %eax,%ecx
mov    $0xc80000,%eax
cmp    %ecx,%eax
jbe    1232 <main+0xa2>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    $0xc80000,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12c3 <main+0x133>
imul   $0x64,-0x8(%rbp),%edi
shl    $0x11,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1263 <main+0xd3>
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
jmp    12be <main+0x12e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10b0 <rand@plt>
mov    -0x20(%rbp),%rdi
mov    $0x32,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
add    $0x1,%esi
imul   $0x64,-0x8(%rbp),%edx
shl    $0x14,%edx
call   1060 <memset@plt>
lea    0xe10(%rip),%rdi
call   1040 <puts@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
jmp    12c3 <main+0x133>
jmp    11a6 <main+0x16>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x1,0x4(%rsp)
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7a(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1090 <scanf@plt>
cmp    $0x1,%eax
jne    12dd <main+0x14d>
mov    0x4(%rsp),%esi
test   %esi,%esi
je     12dd <main+0x14d>
mov    0x2e0a(%rip),%r13
lea    0xe5e(%rip),%r14
lea    0xe14(%rip),%r15
lea    0xe3f(%rip),%r12
lea    0x4(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%ecx
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
cmp    $0xc7ffff,%eax
ja     1230 <main+0xa0>
mov    %ecx,%eax
imul   $0x51eb851f,%rax,%rsi
shr    $0x36,%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b7 <main+0x127>
nopl   (%rax)
imul   $0xc80000,%esi,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     12ab <main+0x11b>
mov    %rax,%rbp
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x24,%rcx
add    %edx,%ecx
imul   $0x32,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%esi
add    $0x1,%esi
imul   $0x6400000,0x4(%rsp),%edx
06
mov    %rbp,%rdi
call   1060 <memset@plt>
lea    0xe1f(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
lea    0xe22(%rip),%rdi
jmp    12b2 <main+0x122>
lea    0xdce(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
cmp    $0x1,%eax
jne    12dd <main+0x14d>
mov    0x4(%rsp),%esi
test   %esi,%esi
jne    1200 <main+0x70>
lea    0xd52(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [50 x i8] c"how many 100MB blocks to allocate? 0 to leave >> \00"
@.str.1 = [4 x i8] c"%ud\00"
@.str.2 = [13 x i8] c"leaving, bye\00"
@INT_MAX = global i32 0
@.str.3 = [61 x i8] c"integer overflow, try allocating less than %d 100MB blocks\0A\0A\00"
@.str.4 = [38 x i8] c"Can't allocate this ammount of memory\00"
@.str.5 = [37 x i8] c"sucessful alloc, press enter to free\00"
@.str.6 = [14 x i8] c"memory freed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i32 1, i32* %2
br label %5
5: ; preds = %0, %51
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %2)
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %10, %5
%14 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %10
%17 = load i32, i32* @INT_MAX
%18 = load i32, i32* %2
%19 = udiv i32 %17, %18
%20 = icmp ugt i32 13107200, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @INT_MAX
%23 = udiv i32 %22, 13107200
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %51
25: ; preds = %16
%26 = load i32, i32* %2
%27 = mul i32 %26, 100
%28 = mul i32 %27, 131072
%29 = call i64 @calloc(i32 %28, i32 8)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %50
35: ; preds = %25
%36 = load i8*, i8** %4
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 50
%39 = add nsw i32 %38, 1
%40 = load i32, i32* %2
%41 = mul i32 %40, 100
%42 = mul i32 %41, 1048576
%43 = call i32 @memset(i8* %36, i32 %39, i32 %42)
%44 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 (...) @getchar()
%46 = call i32 (...) @getchar()
%47 = load i8*, i8** %4
%48 = call i32 @free(i8* %47)
%49 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %35, %33
br label %51
51: ; preds = %50, %21
br label %5
}

LLVM IR (-O2):
@.str = [50 x i8] c"how many 100MB blocks to allocate? 0 to leave >> \00"
@.str.1 = [4 x i8] c"%ud\00"
@.str.2 = [13 x i8] c"leaving, bye\00"
@INT_MAX = global i32 0
@.str.3 = [61 x i8] c"integer overflow, try allocating less than %d 100MB blocks\0A\0A\00"
@.str.4 = [38 x i8] c"Can't allocate this ammount of memory\00"
@.str.5 = [37 x i8] c"sucessful alloc, press enter to free\00"
@.str.6 = [14 x i8] c"memory freed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, 0
%7 = icmp ne i32 %4, 1
%8 = or i1 %7, %6
br i1 %8, label %9, label %12
9: ; preds = %39, %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %0, %39
%13 = phi i32 [ %42, %39 ], [ %5, %0 ]
%14 = load i32, i32* @INT_MAX
%15 = udiv i32 %14, %13
%16 = icmp ult i32 %15, 13107200
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = udiv i32 %14, 13107200
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %39
20: ; preds = %12
%21 = mul i32 %13, 13107200
%22 = call i64 @calloc(i32 %21, i32 8)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %39
27: ; preds = %20
%28 = call i32 (...) @rand()
%29 = srem i32 %28, 50
%30 = add nsw i32 %29, 1
%31 = load i32, i32* %1
%32 = mul i32 %31, 104857600
%33 = call i32 @memset(i8* %23, i32 %30, i32 %32)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 (...) @getchar()
%36 = call i32 (...) @getchar()
%37 = call i32 @free(i8* %23)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %25, %27, %17
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%42 = load i32, i32* %1
%43 = icmp eq i32 %42, 0
%44 = icmp ne i32 %41, 1
%45 = or i1 %44, %43
br i1 %45, label %9, label %12
}