#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 */
void *calloc(int, int);
int /*<<< orphan*/ free(void *);
int main(int argc, char *argv[]) {
  int i;
  const int limit = 5;
  void *ptr[limit];
  for (i = 0; i < limit; ++i) {
    ptr[i] = calloc(3000, i);
  }
  for (i = 0; i < limit; ++i) {
    free(ptr[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x5,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    117c <main+0x5c>
mov    -0x14(%rbp),%esi
mov    $0xbb8,%edi
call   1040 <calloc@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
mov    %rcx,-0x50(%rbp,%rax,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114b <main+0x2b>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    11a9 <main+0x89>
movslq -0x14(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1183 <main+0x63>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0xbb8,%edi
xor    %esi,%esi
call   1040 <calloc@plt>
mov    %rax,%r14
mov    $0xbb8,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r15
mov    $0xbb8,%edi
mov    $0x2,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
mov    $0xbb8,%edi
mov    $0x3,%esi
call   1040 <calloc@plt>
mov    %rax,%r13
mov    $0xbb8,%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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 i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5, i32* %7
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %8
%10 = alloca i8*, i64 5
store i32 0, i32* %6
br label %11
11: ; preds = %20, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 5
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i32, i32* %6
%16 = call i8* @calloc(i32 3000, i32 %15)
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %10, i64 %18
store i8* %16, i8** %19
br label %20
20: ; preds = %14
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
br label %11
23: ; preds = %11
store i32 0, i32* %6
br label %24
24: ; preds = %33, %23
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 5
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %10, i64 %29
%31 = load i8*, i8** %30
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %27
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %24
36: ; preds = %24
store i32 0, i32* %3
%37 = load i8*, i8** %8
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @calloc(i32 3000, i32 0)
%4 = tail call dereferenceable_or_null(3000) i8* @calloc(i32 3000, i32 1)
%5 = tail call dereferenceable_or_null(6000) i8* @calloc(i32 3000, i32 2)
%6 = tail call dereferenceable_or_null(9000) i8* @calloc(i32 3000, i32 3)
%7 = tail call dereferenceable_or_null(12000) i8* @calloc(i32 3000, i32 4)
%8 = tail call i32 @free(i8* %3)
%9 = tail call i32 @free(i8* %4)
%10 = tail call i32 @free(i8* %5)
%11 = tail call i32 @free(i8* %6)
%12 = tail call i32 @free(i8* %7)
ret i32 0
}