#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ compute(int);
int /*<<< orphan*/ debug(char *, int, int, int);
int /*<<< orphan*/ free(char *);
void test_calloc() {
  int i, j;
  char *buffer[10];
  for (i = 0; i < 10; i++) {
    int alloc_size = (1 + i) * 1024;
    debug("\tloop %d/%d: allocating %d bytes\n", i, 10, alloc_size);
    buffer[i] = calloc(alloc_size, sizeof(uint8_t));
    for (j = 0; j < (1 + i) * 1024; j++) {
      buffer[i][j] = 'a';
    }
    compute(50000);
    free(buffer[i]);
    compute(10000);
  }
}


x86 assembly (clang, -O0):
<test_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0xa,-0x4(%rbp)
jge    11ff <test_calloc+0xbf>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
shl    $0xa,%eax
mov    %eax,-0x64(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x64(%rbp),%ecx
lea    0xe8e(%rip),%rdi
mov    $0xa,%edx
call   1040 <debug@plt>
mov    -0x64(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
movslq -0x4(%rbp),%rax
mov    %rcx,-0x60(%rbp,%rax,8)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
shl    $0xa,%ecx
cmp    %ecx,%eax
jge    11cf <test_calloc+0x8f>
movslq -0x4(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rax
movslq -0x8(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    119c <test_calloc+0x5c>
mov    $0xc350,%edi
call   1060 <compute@plt>
movslq -0x4(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rdi
call   1030 <free@plt>
mov    $0x2710,%edi
call   1060 <compute@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114f <test_calloc+0xf>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_calloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x400,%ebx
xor    %esi,%esi
lea    0xe9a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
lea    0x1(%rsi),%r12
mov    %r14,%rdi
mov    $0xa,%edx
mov    %ebx,%ecx
call   1040 <debug@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x61,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    $0xc350,%edi
call   1070 <compute@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0x2710,%edi
call   1070 <compute@plt>
add    $0x400,%rbx
mov    %r12,%rsi
cmp    $0xa,%r12
jne    1170 <test_calloc+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"\09loop %d/%d: allocating %d bytes\0A\00"
define void @test_calloc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10 x i8*]
%4 = alloca i32
store i32 0, i32* %1
br label %5
5: ; preds = %45, %0
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 10
br i1 %7, label %8, label %48
8: ; preds = %5
%9 = load i32, i32* %1
%10 = add nsw i32 1, %9
%11 = mul nsw i32 %10, 1024
store i32 %11, i32* %4
%12 = load i32, i32* %1
%13 = load i32, i32* %4
%14 = call i32 @debug(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %12, i32 10, i32 %13)
%15 = load i32, i32* %4
%16 = call i8* @calloc(i32 %15, i32 4)
%17 = load i32, i32* %1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %18
store i8* %16, i8** %19
store i32 0, i32* %2
br label %20
20: ; preds = %34, %8
%21 = load i32, i32* %2
%22 = load i32, i32* %1
%23 = add nsw i32 1, %22
%24 = mul nsw i32 %23, 1024
%25 = icmp slt i32 %21, %24
br i1 %25, label %26, label %37
26: ; preds = %20
%27 = load i32, i32* %1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = load i32, i32* %2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 97, i8* %33
br label %34
34: ; preds = %26
%35 = load i32, i32* %2
%36 = add nsw i32 %35, 1
store i32 %36, i32* %2
br label %20
37: ; preds = %20
%38 = call i32 @compute(i32 50000)
%39 = load i32, i32* %1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 @free(i8* %42)
%44 = call i32 @compute(i32 10000)
br label %45
45: ; preds = %37
%46 = load i32, i32* %1
%47 = add nsw i32 %46, 1
store i32 %47, i32* %1
br label %5
48: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"\09loop %d/%d: allocating %d bytes\0A\00"
define void @test_calloc() {
br label %1
1: ; preds = %0, %1
%2 = phi i64 [ 0, %0 ], [ %6, %1 ]
%3 = trunc i64 %2 to i32
%4 = shl nuw nsw i64 %2, 10
%5 = add nuw nsw i64 %4, 1024
%6 = add nuw nsw i64 %2, 1
%7 = trunc i64 %6 to i32
%8 = shl nuw nsw i32 %7, 10
%9 = tail call i32 @debug(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %3, i32 10, i32 %8)
%10 = tail call i8* @calloc(i32 %8, i32 4)
%11 = tail call i32 @compute(i32 50000)
%12 = tail call i32 @free(i8* %10)
%13 = tail call i32 @compute(i32 10000)
%14 = icmp eq i64 %6, 10
br i1 %14, label %15, label %1
15: ; preds = %1
ret void
}