#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 getpid();
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(void) {
  int size = 0;
  int chunk_size = 512;
  void *p = NULL;
  while (1) {
    if ((p = malloc(chunk_size)) == NULL) {
      printf("out of memory!!\n");
      break;
    }
    memset(p, 1, chunk_size);
    size += chunk_size;
    printf("[%d] - memory is allocated [%8d] bytes \n", getpid(), size);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x200,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119e <main+0x4e>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e1 <main+0x91>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1050 <memset@plt>
mov    -0xc(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1175 <main+0x25>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x200,%ebx
mov    $0x200,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11c6 <main+0x66>
lea    0xe81(%rip),%r14
nop
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x200,%edi
call   1070 <malloc@plt>
add    $0x200,%ebx
test   %rax,%rax
jne    1180 <main+0x20>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"out of memory!!\0A\00"
@.str.1 = [41 x i8] c"[%d] - memory is allocated [%8d] bytes \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
store i32 512, i32* %3
store i8* null, i8** %4
br label %5
5: ; preds = %0, %11
%6 = load i32, i32* %3
%7 = call i8* @malloc(i32 %6)
store i8* %7, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %22
11: ; preds = %5
%12 = load i8*, i8** %4
%13 = load i32, i32* %3
%14 = call i32 @memset(i8* %12, i32 1, i32 %13)
%15 = load i32, i32* %3
%16 = load i32, i32* %2
%17 = add nsw i32 %16, %15
store i32 %17, i32* %2
%18 = call i32 (...) @getpid()
%19 = load i32, i32* %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = call i32 @sleep(i32 1)
br label %5
22: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [41 x i8] c"[%d] - memory is allocated [%8d] bytes \0A\00"
@str = [16 x i8] c"out of memory!!\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %5
3: ; preds = %5, %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret i32 0
5: ; preds = %0, %5
%6 = phi i8* [ %13, %5 ], [ %1, %0 ]
%7 = phi i32 [ %9, %5 ], [ 0, %0 ]
%8 = tail call i32 @memset(i8* %6, i32 1, i32 512)
%9 = add nuw nsw i32 %7, 512
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %9)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%14 = icmp eq i8* %13, null
br i1 %14, label %3, label %5
}