#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 */
scalar_t__ atol(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, size_t);
int main(int argc, char *argv[]) {
  size_t max = (argc > 1) ? (size_t)atol(argv[1])
                          : ((size_t)256 * 1024) * ((size_t)1024 * 1024);
  for (size_t n = 1; n <= max; n *= 2) {
    printf("attempting malloc of %zu bytes \n", n);
    fflush(0);
    void *ptr = malloc(n);
    if (ptr == NULL) {
      printf("malloc of %zu bytes failed \n", n);
      fflush(0);
      return 1;
    } else {
      printf("malloc of %zu bytes succeeded \n", n);
      fflush(0);
      memset(ptr, 0, n);
      printf("memset of %zu bytes succeeded \n", n);
      fflush(0);
      free(ptr);
    }
  }
  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   $0x1,-0x8(%rbp)
jle    1196 <main+0x36>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atol@plt>
mov    %rax,-0x30(%rbp)
jmp    11a9 <main+0x49>
movabs $0x4000000000,%rax
mov    %rax,-0x30(%rbp)
jmp    11a9 <main+0x49>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     1275 <main+0x115>
mov    -0x20(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1219 <main+0xb9>
mov    -0x20(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <main+0x11c>
mov    -0x20(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x20(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1264 <main+0x104>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b9 <main+0x59>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%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
jl     1188 <main+0x28>
mov    0x8(%rsi),%rdi
call   1080 <atol@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1192 <main+0x32>
xor    %ebp,%ebp
jmp    1234 <main+0xd4>
movabs $0x4000000000,%r14
mov    $0x1,%ebx
lea    0xe62(%rip),%r15
lea    0xeb9(%rip),%r13
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1219 <main+0xb9>
mov    %rax,%r12
lea    0xe65(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    %rbx,%rbx
cmp    %r14,%rbx
jbe    11b0 <main+0x50>
jmp    1234 <main+0xd4>
lea    0xe01(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"attempting malloc of %zu bytes \0A\00"
@.str.1 = [29 x i8] c"malloc of %zu bytes failed \0A\00"
@.str.2 = [32 x i8] c"malloc of %zu bytes succeeded \0A\00"
@.str.3 = [32 x i8] c"memset of %zu bytes succeeded \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @atol(i8* %14)
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %11
%18 = phi i64 [ %15, %11 ], [ 274877906944, %16 ]
store i64 %18, i64* %6
store i64 1, i64* %7
br label %19
19: ; preds = %48, %17
%20 = load i64, i64* %7
%21 = load i64, i64* %6
%22 = icmp ule i64 %20, %21
br i1 %22, label %23, label %51
23: ; preds = %19
%24 = load i64, i64* %7
%25 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %24)
%26 = call i32 @fflush(i32 0)
%27 = load i64, i64* %7
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = load i64, i64* %7
%33 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 %32)
%34 = call i32 @fflush(i32 0)
store i32 1, i32* %3
br label %52
35: ; preds = %23
%36 = load i64, i64* %7
%37 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %36)
%38 = call i32 @fflush(i32 0)
%39 = load i8*, i8** %8
%40 = load i64, i64* %7
%41 = call i32 @memset(i8* %39, i32 0, i64 %40)
%42 = load i64, i64* %7
%43 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %42)
%44 = call i32 @fflush(i32 0)
%45 = load i8*, i8** %8
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %35
br label %48
48: ; preds = %47
%49 = load i64, i64* %7
%50 = mul i64 %49, 2
store i64 %50, i64* %7
br label %19
51: ; preds = %19
store i32 0, i32* %3
br label %52
52: ; preds = %51, %31
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [33 x i8] c"attempting malloc of %zu bytes \0A\00"
@.str.1 = [29 x i8] c"malloc of %zu bytes failed \0A\00"
@.str.2 = [32 x i8] c"malloc of %zu bytes succeeded \0A\00"
@.str.3 = [32 x i8] c"memset of %zu bytes succeeded \0A\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 @atol(i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %29, label %9
9: ; preds = %2, %4
%10 = phi i64 [ %7, %4 ], [ 274877906944, %2 ]
br label %11
11: ; preds = %9, %20
%12 = phi i64 [ %27, %20 ], [ 1, %9 ]
%13 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = tail call i32 @fflush(i32 0)
%15 = tail call i8* @malloc(i64 %12)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 %12)
%19 = tail call i32 @fflush(i32 0)
br label %29
20: ; preds = %11
%21 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%22 = tail call i32 @fflush(i32 0)
%23 = tail call i32 @memset(i8* %15, i32 0, i64 %12)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %12)
%25 = tail call i32 @fflush(i32 0)
%26 = tail call i32 @free(i8* %15)
%27 = shl i64 %12, 1
%28 = icmp ugt i64 %27, %10
br i1 %28, label %29, label %11
29: ; preds = %20, %4, %17
%30 = phi i32 [ 1, %17 ], [ 0, %4 ], [ 0, %20 ]
ret i32 %30
}