#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*/ free(char *);
char *realloc(char *, int);
int main(int argc, char *argv[]) {
  int count = 20;
  char *ptr = NULL;
  while (count--) {
    ptr = realloc(ptr, (1 << 20));
    ptr = realloc(ptr, 1);
  }
  free(ptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x14,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1182 <main+0x62>
mov    -0x20(%rbp),%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    1145 <main+0x25>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %edi,%edi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x100000,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 20, i32* %6
store i8* null, i8** %7
br label %8
8: ; preds = %12, %2
%9 = load i32, i32* %6
%10 = add nsw i32 %9, -1
store i32 %10, i32* %6
%11 = icmp ne i32 %9, 0
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i8*, i8** %7
%14 = call i8* @realloc(i8* %13, i32 1048576)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = call i8* @realloc(i8* %15, i32 1)
store i8* %16, i8** %7
br label %8
17: ; preds = %8
%18 = load i8*, i8** %7
%19 = call i32 @free(i8* %18)
ret i32 0
}

LLVM IR (-O2):
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @realloc(i8* null, i32 1048576)
%4 = tail call i8* @realloc(i8* %3, i32 1)
%5 = tail call i8* @realloc(i8* %4, i32 1048576)
%6 = tail call i8* @realloc(i8* %5, i32 1)
%7 = tail call i8* @realloc(i8* %6, i32 1048576)
%8 = tail call i8* @realloc(i8* %7, i32 1)
%9 = tail call i8* @realloc(i8* %8, i32 1048576)
%10 = tail call i8* @realloc(i8* %9, i32 1)
%11 = tail call i8* @realloc(i8* %10, i32 1048576)
%12 = tail call i8* @realloc(i8* %11, i32 1)
%13 = tail call i8* @realloc(i8* %12, i32 1048576)
%14 = tail call i8* @realloc(i8* %13, i32 1)
%15 = tail call i8* @realloc(i8* %14, i32 1048576)
%16 = tail call i8* @realloc(i8* %15, i32 1)
%17 = tail call i8* @realloc(i8* %16, i32 1048576)
%18 = tail call i8* @realloc(i8* %17, i32 1)
%19 = tail call i8* @realloc(i8* %18, i32 1048576)
%20 = tail call i8* @realloc(i8* %19, i32 1)
%21 = tail call i8* @realloc(i8* %20, i32 1048576)
%22 = tail call i8* @realloc(i8* %21, i32 1)
%23 = tail call i8* @realloc(i8* %22, i32 1048576)
%24 = tail call i8* @realloc(i8* %23, i32 1)
%25 = tail call i8* @realloc(i8* %24, i32 1048576)
%26 = tail call i8* @realloc(i8* %25, i32 1)
%27 = tail call i8* @realloc(i8* %26, i32 1048576)
%28 = tail call i8* @realloc(i8* %27, i32 1)
%29 = tail call i8* @realloc(i8* %28, i32 1048576)
%30 = tail call i8* @realloc(i8* %29, i32 1)
%31 = tail call i8* @realloc(i8* %30, i32 1048576)
%32 = tail call i8* @realloc(i8* %31, i32 1)
%33 = tail call i8* @realloc(i8* %32, i32 1048576)
%34 = tail call i8* @realloc(i8* %33, i32 1)
%35 = tail call i8* @realloc(i8* %34, i32 1048576)
%36 = tail call i8* @realloc(i8* %35, i32 1)
%37 = tail call i8* @realloc(i8* %36, i32 1048576)
%38 = tail call i8* @realloc(i8* %37, i32 1)
%39 = tail call i8* @realloc(i8* %38, i32 1048576)
%40 = tail call i8* @realloc(i8* %39, i32 1)
%41 = tail call i8* @realloc(i8* %40, i32 1048576)
%42 = tail call i8* @realloc(i8* %41, i32 1)
%43 = tail call i32 @free(i8* %42)
ret i32 0
}