#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 *malloc(int);
char *realloc(char *, int);
int main(void) {
  char *p;
  int i;
  for (i = 0; i < 10000; i++) {
    p = malloc(10 + 10 * (i % 100));
    p = realloc(p, 500);
    p = realloc(p, 600);
    free(p);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2710,-0x14(%rbp)
jge    11a8 <main+0x78>
mov    -0x14(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
imul   $0xa,%edx,%edi
add    $0xa,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1f4,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x258,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <main+0x16>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x2710,%r14d
mov    $0xa,%bp
xor    %ebx,%ebx
movzwl %bx,%ebx
mov    %ebx,%eax
shr    $0x2,%eax
imul   $0x147b,%eax,%eax
shr    $0x11,%eax
imul   $0x3e8,%eax,%eax
mov    %ebp,%ecx
sub    %eax,%ecx
movzwl %cx,%edi
call   1040 <malloc@plt>
mov    %rax,%rdi
mov    $0x1f4,%esi
call   1050 <realloc@plt>
mov    %rax,%rdi
mov    $0x258,%esi
call   1050 <realloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
add    $0xa,%ebp
add    $0x1,%ebx
add    $0xffffffff,%r14d
jne    1140 <main+0x10>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @main() {
br label %1
1: ; preds = %0, %1
%2 = phi i32 [ 0, %0 ], [ %12, %1 ]
%3 = trunc i32 %2 to i16
%4 = urem i16 %3, 100
%5 = mul nuw nsw i16 %4, 10
%6 = add nuw nsw i16 %5, 10
%7 = zext i16 %6 to i32
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i8* @realloc(i8* %8, i32 500)
%10 = tail call i8* @realloc(i8* %9, i32 600)
%11 = tail call i32 @free(i8* %10)
%12 = add nuw nsw i32 %2, 1
%13 = icmp eq i32 %12, 10000
br i1 %13, label %14, label %1
14: ; preds = %1
ret i32 0
}