#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__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int malloc_increment;
  printf("MEMLK - intentional memory leak program; Copyright Bradley Sadowsky, "
         "June 2017 - MIT License <bradley.sadowsky@gmail.com>\n");
  printf("Size (in bytes) to increment every iteration (Enter '0' for help): ");
  scanf("%d", &malloc_increment);
  if (malloc_increment == 0) {
    printf("MEMLK creates an intentional memory leak, and leaks X bytes every "
           "iteration until the allocation fails; it then divides X in half "
           "and continues\n");
    return 0;
  }
  while (1) {
    if (malloc(malloc_increment) == 0)
      malloc_increment = malloc_increment / 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
lea    0xf29(%rip),%rdi
call   1030 <printf@plt>
lea    0xf61(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1185 <main+0x55>
lea    0xf4a(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret
jmp    118a <main+0x5a>
mov    -0x8(%rbp),%edi
call   1040 <malloc@plt>
cmp    $0x0,%rax
jne    11aa <main+0x7a>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    118a <main+0x5a>

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xeff(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rdi
call   1040 <printf@plt>
lea    0xee4(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
je     119f <main+0x5f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1050 <malloc@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
jne    1180 <main+0x40>
mov    %edi,%eax
shr    $0x1f,%eax
add    %edi,%eax
sar    %eax
mov    %eax,0x4(%rsp)
mov    %eax,%edi
jmp    1180 <main+0x40>
lea    0xf1b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [123 x i8] c"MEMLK - intentional memory leak program; Copyright Bradley Sadowsky, June 2017 - MIT License <bradley.sadowsky@gmail.com>\0A\00"
@.str.1 = [68 x i8] c"Size (in bytes) to increment every iteration (Enter '0' for help): \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [145 x i8] c"MEMLK creates an intentional memory leak, and leaks X bytes every iteration until the allocation fails; it then divides X in half and continues\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @printf(i8* getelementptr inbounds ([123 x i8], [123 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
10: ; preds = %0
br label %11
11: ; preds = %10, %18
%12 = load i32, i32* %2
%13 = call i64 @malloc(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %2
%17 = sdiv i32 %16, 2
store i32 %17, i32* %2
br label %18
18: ; preds = %15, %11
br label %11
}

LLVM IR (-O2):
@.str.1 = [68 x i8] c"Size (in bytes) to increment every iteration (Enter '0' for help): \00"
@.str.2 = [3 x i8] c"%d\00"
@str = [122 x i8] c"MEMLK - intentional memory leak program; Copyright Bradley Sadowsky, June 2017 - MIT License <bradley.sadowsky@gmail.com>\00"
@str.4 = [144 x i8] c"MEMLK creates an intentional memory leak, and leaks X bytes every iteration until the allocation fails; it then divides X in half and continues\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([122 x i8], [122 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([144 x i8], [144 x i8]* @str.4, i64 0, i64 0))
ret i32 0
10: ; preds = %0, %15
%11 = phi i32 [ %16, %15 ], [ %6, %0 ]
%12 = call i64 @malloc(i32 %11)
%13 = icmp eq i64 %12, 0
%14 = load i32, i32* %1
br i1 %13, label %17, label %15
15: ; preds = %10, %17
%16 = phi i32 [ %18, %17 ], [ %14, %10 ]
br label %10
17: ; preds = %10
%18 = sdiv i32 %14, 2
store i32 %18, i32* %1
br label %15
}