#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*/ assert(int);
int /*<<< orphan*/ free(int /*<<< orphan*/);
int /*<<< orphan*/ malloc(int);
void *realloc(void *, int);
int main() {
  int i;
  void *p;
  for (i = 0; i < 100000; i++)
    free(malloc(40960));
  for (i = 0; i < 100000; i++) {
    p = realloc(NULL, 40960);
    p = realloc(p, 50000);
    p = realloc(p, 40000);
    p = realloc(p, 0);
    assert(!p);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x186a0,-0x8(%rbp)
jge    1182 <main+0x42>
mov    $0xa000,%edi
call   1050 <malloc@plt>
mov    %eax,%edi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <main+0x16>
movl   $0x0,-0x8(%rbp)
cmpl   $0x186a0,-0x8(%rbp)
jge    11fd <main+0xbd>
xor    %eax,%eax
mov    %eax,%edi
mov    $0xa000,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xc350,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x9c40,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1189 <main+0x49>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x186a0,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0xa000,%edi
call   1050 <malloc@plt>
mov    %eax,%edi
call   1030 <free@plt>
add    $0xffffffff,%ebx
jne    1150 <main+0x10>
mov    $0x186a0,%ebx
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    $0xa000,%esi
call   1060 <realloc@plt>
mov    %rax,%rdi
mov    $0xc350,%esi
call   1060 <realloc@plt>
mov    %rax,%rdi
mov    $0x9c40,%esi
call   1060 <realloc@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <realloc@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1040 <assert@plt>
add    $0xffffffff,%ebx
jne    1170 <main+0x30>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
br label %4
4: ; preds = %10, %0
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 100000
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = call i32 @malloc(i32 40960)
%9 = call i32 @free(i32 %8)
br label %10
10: ; preds = %7
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
store i32 %12, i32* %2
br label %4
13: ; preds = %4
store i32 0, i32* %2
br label %14
14: ; preds = %30, %13
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 100000
br i1 %16, label %17, label %33
17: ; preds = %14
%18 = call i8* @realloc(i8* null, i32 40960)
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = call i8* @realloc(i8* %19, i32 50000)
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = call i8* @realloc(i8* %21, i32 40000)
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = call i8* @realloc(i8* %23, i32 0)
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
%27 = xor i1 %26, true
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
br label %30
30: ; preds = %17
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %14
33: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
define i32 @main() {
br label %1
1: ; preds = %0, %1
%2 = phi i32 [ 0, %0 ], [ %5, %1 ]
%3 = tail call i32 @malloc(i32 40960)
%4 = tail call i32 @free(i32 %3)
%5 = add nuw nsw i32 %2, 1
%6 = icmp eq i32 %5, 100000
br i1 %6, label %7, label %1
7: ; preds = %1, %7
%8 = phi i32 [ %16, %7 ], [ 0, %1 ]
%9 = tail call i8* @realloc(i8* null, i32 40960)
%10 = tail call i8* @realloc(i8* %9, i32 50000)
%11 = tail call i8* @realloc(i8* %10, i32 40000)
%12 = tail call i8* @realloc(i8* %11, i32 0)
%13 = icmp eq i8* %12, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = add nuw nsw i32 %8, 1
%17 = icmp eq i32 %16, 100000
br i1 %17, label %18, label %7
18: ; preds = %7
ret i32 0
}