#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 *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  for (int i = 1;; i++) {
    int *p = malloc(1 * 1024 * 1024);
    if (p == NULL) {
      printf("Out of memory!\n");
      return 0;
    }
    *p = 50;
    printf("Allocated a total of %d MB.\n", i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    $0x100000,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1165 <main+0x45>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
movl   $0x32,(%rax)
mov    -0x8(%rbp),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1136 <main+0x16>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1174 <main+0x44>
mov    $0x1,%ebx
lea    0xeb1(%rip),%r14
nop
movl   $0x32,(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebx
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
jne    1150 <main+0x20>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Out of memory!\0A\00"
@.str.1 = [29 x i8] c"Allocated a total of %d MB.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
store i32 0, i32* %1
store i32 1, i32* %2
br label %4
4: ; preds = %14, %0
%5 = call i32* @malloc(i32 1048576)
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
ret i32 0
10: ; preds = %4
%11 = load i32*, i32** %3
store i32 50, i32* %11
%12 = load i32, i32* %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
store i32 %16, i32* %2
br label %4
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"Allocated a total of %d MB.\0A\00"
@str = [15 x i8] c"Out of memory!\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 1048576)
%2 = icmp eq i32* %1, null
br i1 %2, label %10, label %3
3: ; preds = %0, %3
%4 = phi i32* [ %8, %3 ], [ %1, %0 ]
%5 = phi i32 [ %7, %3 ], [ 1, %0 ]
store i32 50, i32* %4
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = add nuw nsw i32 %5, 1
%8 = tail call i32* @malloc(i32 1048576)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %3
10: ; preds = %3, %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
ret i32 0
}