#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 */
unsigned char *alloc_test_calloc(int, int);
int /*<<< orphan*/ alloc_test_free(unsigned char *);
int alloc_test_get_allocated();
int /*<<< orphan*/ alloc_test_set_limit(int);
int /*<<< orphan*/ assert(int);
void test_calloc(void) {
  unsigned char *block;
  int i;
  assert(alloc_test_get_allocated() == 0);
  block = alloc_test_calloc(16, 64);
  assert(alloc_test_get_allocated() == 1024);
  assert(block != NULL);
  for (i = 0; i < 1024; ++i) {
    assert(block[i] == 0);
  }
  alloc_test_free(block);
  assert(alloc_test_get_allocated() == 0);
  alloc_test_set_limit(1);
  block = alloc_test_calloc(1024, 1);
  assert(block != NULL);
  assert(alloc_test_get_allocated() == 1024);
  assert(alloc_test_calloc(1024, 1) == NULL);
  assert(alloc_test_get_allocated() == 1024);
  alloc_test_free(block);
}


x86 assembly (clang, -O0):
<test_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x10,%edi
mov    $0x40,%esi
call   1070 <alloc_test_calloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x400,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x400,-0xc(%rbp)
jge    11eb <test_calloc+0x9b>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b4 <test_calloc+0x64>
mov    -0x8(%rbp),%rdi
call   1040 <alloc_test_free@plt>
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x1,%edi
call   1030 <alloc_test_set_limit@plt>
mov    $0x400,%edi
mov    $0x1,%esi
call   1070 <alloc_test_calloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x400,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x400,%edi
mov    $0x1,%esi
call   1070 <alloc_test_calloc@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x400,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <alloc_test_free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_calloc>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    $0x10,%edi
mov    $0x40,%esi
call   1070 <alloc_test_calloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
cmp    $0x400,%eax
sete   %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
cmpb   $0x0,(%r14,%rbx,1)
sete   %dil
call   1060 <assert@plt>
add    $0x1,%rbx
cmp    $0x400,%rbx
jne    11b0 <test_calloc+0x60>
mov    %r14,%rdi
call   1040 <alloc_test_free@plt>
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    $0x1,%edi
call   1030 <alloc_test_set_limit@plt>
mov    $0x400,%edi
mov    $0x1,%esi
call   1070 <alloc_test_calloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
cmp    $0x400,%eax
sete   %dil
call   1060 <assert@plt>
mov    $0x400,%edi
mov    $0x1,%esi
call   1070 <alloc_test_calloc@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
cmp    $0x400,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <alloc_test_free@plt>

LLVM IR (-O0):
define void @test_calloc() {
%1 = alloca i8*
%2 = alloca i32
%3 = call i32 (...) @alloc_test_get_allocated()
%4 = icmp eq i32 %3, 0
%5 = zext i1 %4 to i32
%6 = call i32 @assert(i32 %5)
%7 = call i8* @alloc_test_calloc(i32 16, i32 64)
store i8* %7, i8** %1
%8 = call i32 (...) @alloc_test_get_allocated()
%9 = icmp eq i32 %8, 1024
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %1
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
store i32 0, i32* %2
br label %16
16: ; preds = %29, %0
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 1024
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = load i8*, i8** %1
%21 = load i32, i32* %2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
br label %29
29: ; preds = %19
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
br label %16
32: ; preds = %16
%33 = load i8*, i8** %1
%34 = call i32 @alloc_test_free(i8* %33)
%35 = call i32 (...) @alloc_test_get_allocated()
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = call i32 @alloc_test_set_limit(i32 1)
%40 = call i8* @alloc_test_calloc(i32 1024, i32 1)
store i8* %40, i8** %1
%41 = load i8*, i8** %1
%42 = icmp ne i8* %41, null
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = call i32 (...) @alloc_test_get_allocated()
%46 = icmp eq i32 %45, 1024
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = call i8* @alloc_test_calloc(i32 1024, i32 1)
%50 = icmp eq i8* %49, null
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = call i32 (...) @alloc_test_get_allocated()
%54 = icmp eq i32 %53, 1024
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i8*, i8** %1
%58 = call i32 @alloc_test_free(i8* %57)
ret void
}

LLVM IR (-O2):
define void @test_calloc() {
%1 = tail call i32 (...) @alloc_test_get_allocated()
%2 = icmp eq i32 %1, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i8* @alloc_test_calloc(i32 16, i32 64)
%6 = tail call i32 (...) @alloc_test_get_allocated()
%7 = icmp eq i32 %6, 1024
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i8* %5, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
br label %13
13: ; preds = %0, %13
%14 = phi i64 [ 0, %0 ], [ %20, %13 ]
%15 = getelementptr inbounds i8, i8* %5, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = add nuw nsw i64 %14, 1
%21 = icmp eq i64 %20, 1024
br i1 %21, label %22, label %13
22: ; preds = %13
%23 = tail call i32 @alloc_test_free(i8* %5)
%24 = tail call i32 (...) @alloc_test_get_allocated()
%25 = icmp eq i32 %24, 0
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = tail call i32 @alloc_test_set_limit(i32 1)
%29 = tail call i8* @alloc_test_calloc(i32 1024, i32 1)
%30 = icmp ne i8* %29, null
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = tail call i32 (...) @alloc_test_get_allocated()
%34 = icmp eq i32 %33, 1024
%35 = zext i1 %34 to i32
%36 = tail call i32 @assert(i32 %35)
%37 = tail call i8* @alloc_test_calloc(i32 1024, i32 1)
%38 = icmp eq i8* %37, null
%39 = zext i1 %38 to i32
%40 = tail call i32 @assert(i32 %39)
%41 = tail call i32 (...) @alloc_test_get_allocated()
%42 = icmp eq i32 %41, 1024
%43 = zext i1 %42 to i32
%44 = tail call i32 @assert(i32 %43)
%45 = tail call i32 @alloc_test_free(i8* %29)
ret void
}