#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *);
int main() {
  char *some_memory;
  int size_to_allocate = (1024);
  int megs_obtained = 0;
  int ks_obtained = 0;
  while (1) {
    for (ks_obtained = 0; ks_obtained < 1024; ks_obtained++) {
      some_memory = (char *)malloc(size_to_allocate);
      if (some_memory == NULL)
        exit(EXIT_FAILURE);
      sprintf(some_memory, "Hello World");
    }
    megs_obtained++;
    printf("Now allocated %d Megabytes\n", megs_obtained);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x400,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jge    11bb <main+0x7b>
mov    -0x14(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119d <main+0x5d>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116b <main+0x2b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
jmp    1164 <main+0x24>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r15d,%r15d
movabs $0x6f57206f6c6c6548,%rbx
20 57 6f
lea    0xec1(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    $0x400,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x400,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     118f <main+0x5f>
mov    %rbx,(%rax)
movl   $0x646c72,0x8(%rax)
add    $0xffffffff,%ebp
jne    1160 <main+0x30>
add    $0x1,%r15d
mov    %r14,%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
jmp    1150 <main+0x20>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [12 x i8] c"Hello World\00"
@.str.1 = [28 x i8] c"Now allocated %d Megabytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 1024, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %0, %25
store i32 0, i32* %5
br label %7
7: ; preds = %22, %6
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 1024
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = load i32, i32* %3
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %2
%14 = load i8*, i8** %2
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %10
%20 = load i8*, i8** %2
%21 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %7
25: ; preds = %7
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %6
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [12 x i8] c"Hello World\00"
@.str.1 = [28 x i8] c"Now allocated %d Megabytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
br label %1
1: ; preds = %14, %0
%2 = phi i32 [ 0, %0 ], [ %15, %14 ]
br label %3
3: ; preds = %1, %10
%4 = phi i32 [ 0, %1 ], [ %12, %10 ]
%5 = tail call i64 @malloc(i32 1024)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %3
%11 = inttoptr i64 %5 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(12) %11, i8* align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i64 12, i1 false)
%12 = add nuw nsw i32 %4, 1
%13 = icmp eq i32 %12, 1024
br i1 %13, label %14, label %3
14: ; preds = %10
%15 = add nuw nsw i32 %2, 1
%16 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %1
}