#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 */
long long atoll(char *);
int errno;
char *malloc(long long);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, long long);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
scalar_t__ system(char *);
int main(int argc, char **argv) {
  int i;
  unsigned long long mem = (4LL * 1024 * 1024 * 1024);
  unsigned long long allocated = 0;
  if (argc > 1)
    mem = 1024LL * atoll(argv[1]);
  if (mem < 0)
    mem = (4LL * 1024 * 1024 * 1024);
  printf("testing with %u GB\n", mem / 1024 / 1024 / 1024);
  for (i = 0; allocated <= mem; i++) {
    char *p = malloc((100LL * 1024 * 1024));
    if (!p) {
      printf("can't allocate more\n");
      break;
    }
    memset(p, 0, (100LL * 1024 * 1024));
    allocated += (100LL * 1024 * 1024);
    printf("allocated = %u MB\n", allocated / 1024 / 1024);
    if (system("free")) {
      int e = errno;
      printf("can't execute free errno=%d %s\n", e, strerror(e));
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movabs $0x100000000,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ab <main+0x4b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoll@plt>
shl    $0xa,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11c4 <main+0x64>
movabs $0x100000000,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
shr    $0xa,%rsi
shr    $0xa,%rsi
shr    $0xa,%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
ja     12b7 <main+0x157>
mov    $0x6400000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1223 <main+0xc3>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b7 <main+0x157>
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x6400000,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%rax
add    $0x6400000,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
shr    $0xa,%rsi
shr    $0xa,%rsi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdda(%rip),%rdi
call   1030 <system@plt>
cmp    $0x0,%rax
je     12a4 <main+0x144>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%esi
mov    %rax,%rdx
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b7 <main+0x157>
jmp    12a9 <main+0x149>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e9 <main+0x89>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     118f <main+0x1f>
mov    0x8(%rsi),%rdi
call   1070 <atoll@plt>
mov    %rax,%r12
shl    $0xa,%r12
jmp    1199 <main+0x29>
movabs $0x100000000,%r12
mov    %r12,%rsi
shr    $0x1e,%rsi
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x6400000,%ebx
lea    0xe5a(%rip),%r14
lea    0xe66(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    -0x6400000(%rbx),%rax
cmp    %r12,%rax
ja     1250 <main+0xe0>
mov    $0x6400000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1244 <main+0xd4>
mov    $0x6400000,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbx,%rsi
shr    $0x14,%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <system@plt>
add    $0x6400000,%rbx
test   %rax,%rax
je     11d0 <main+0x60>
mov    0x2dba(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xdf6(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1250 <main+0xe0>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"testing with %u GB\0A\00"
@.str.1 = [21 x i8] c"can't allocate more\0A\00"
@.str.2 = [19 x i8] c"allocated = %u MB\0A\00"
@.str.3 = [5 x i8] c"free\00"
@errno = global i32 0
@.str.4 = [32 x i8] c"can't execute free errno=%d %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 4294967296, i64* %7
store i64 0, i64* %8
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @atoll(i8* %16)
%18 = mul nsw i64 1024, %17
store i64 %18, i64* %7
br label %19
19: ; preds = %13, %2
%20 = load i64, i64* %7
%21 = icmp ult i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i64 4294967296, i64* %7
br label %23
23: ; preds = %22, %19
%24 = load i64, i64* %7
%25 = udiv i64 %24, 1024
%26 = udiv i64 %25, 1024
%27 = udiv i64 %26, 1024
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %27)
store i32 0, i32* %6
br label %29
29: ; preds = %57, %23
%30 = load i64, i64* %8
%31 = load i64, i64* %7
%32 = icmp ule i64 %30, %31
br i1 %32, label %33, label %60
33: ; preds = %29
%34 = call i8* @malloc(i64 104857600)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %60
39: ; preds = %33
%40 = load i8*, i8** %9
%41 = call i32 @memset(i8* %40, i32 0, i64 104857600)
%42 = load i64, i64* %8
%43 = add i64 %42, 104857600
store i64 %43, i64* %8
%44 = load i64, i64* %8
%45 = udiv i64 %44, 1024
%46 = udiv i64 %45, 1024
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %46)
%48 = call i64 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %39
%51 = load i32, i32* @errno
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = load i32, i32* %10
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %52, i8* %54)
br label %60
56: ; preds = %39
br label %57
57: ; preds = %56
%58 = load i32, i32* %6
%59 = add nsw i32 %58, 1
store i32 %59, i32* %6
br label %29
60: ; preds = %50, %37, %29
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"testing with %u GB\0A\00"
@.str.2 = [19 x i8] c"allocated = %u MB\0A\00"
@.str.3 = [5 x i8] c"free\00"
@errno = global i32 0
@.str.4 = [32 x i8] c"can't execute free errno=%d %s\0A\00"
@str = [20 x i8] c"can't allocate more\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @atoll(i8* %6)
%8 = shl nsw i64 %7, 10
br label %9
9: ; preds = %4, %2
%10 = phi i64 [ %8, %4 ], [ 4294967296, %2 ]
%11 = lshr i64 %10, 30
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %11)
br label %13
13: ; preds = %21, %9
%14 = phi i64 [ 0, %9 ], [ %23, %21 ]
%15 = icmp ugt i64 %14, %10
br i1 %15, label %32, label %16
16: ; preds = %13
%17 = tail call dereferenceable_or_null(104857600) i8* @malloc(i64 104857600)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %32
21: ; preds = %16
%22 = tail call i32 @memset(i8* %17, i32 0, i64 104857600)
%23 = add i64 %14, 104857600
%24 = lshr exact i64 %23, 20
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %24)
%26 = tail call i64 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %13, label %28
28: ; preds = %21
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %29, i8* %30)
br label %32
32: ; preds = %13, %19, %28
ret i32 0
}