#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 */
size_t const SIZE_MAX;
int *calloc(size_t const, size_t const);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, ...);
int _calloc(char const *progname) {
  int *p;
  size_t i;
  const size_t cnt = 16;
  printf("%s: Testing calloc()\n", progname);
  if (calloc(SIZE_MAX, 2) != NULL || calloc(2, SIZE_MAX) != NULL) {
    printf("calloc() failed to detect overflow\n");
    return 1;
  }
  if ((p = calloc(sizeof(int), cnt)) == NULL) {
    printf("calloc() failed to allocate memory\n");
    return 1;
  }
  for (i = 0; i < cnt; i++)
    if (p[i] != 0) {
      printf("calloc() failed to clear memory\n");
      return 1;
    }
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x10,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%rdi
mov    $0x2,%esi
call   1050 <calloc@plt>
cmp    $0x0,%rax
jne    1192 <_calloc+0x62>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rsi
mov    $0x2,%edi
call   1050 <calloc@plt>
cmp    $0x0,%rax
je     11ac <_calloc+0x7c>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <_calloc+0x118>
mov    $0x4,%edi
mov    $0x10,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e3 <_calloc+0xb3>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <_calloc+0x118>
movq   $0x0,-0x20(%rbp)
cmpq   $0x10,-0x20(%rbp)
jae    1238 <_calloc+0x108>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     1222 <_calloc+0xf2>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1248 <_calloc+0x118>
jmp    1227 <_calloc+0xf7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11eb <_calloc+0xbb>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_calloc>:
push   %rax
mov    %rdi,%rsi
lea    0xeb5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%esi
xor    %edi,%edi
call   1060 <calloc@plt>
test   %rax,%rax
jne    1174 <_calloc+0x34>
mov    $0x2,%edi
xor    %esi,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1187 <_calloc+0x47>
lea    0xede(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret
mov    $0x4,%edi
mov    $0x10,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1206 <_calloc+0xc6>
cmpl   $0x0,(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x4(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x8(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0xc(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x10(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x14(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x18(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x1c(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x20(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x24(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x28(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x2c(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x30(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x34(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x38(%rax)
jne    11fa <_calloc+0xba>
cmpl   $0x0,0x3c(%rax)
je     1212 <_calloc+0xd2>
lea    0xe15(%rip),%rdi
jmp    117b <_calloc+0x3b>
lea    0xe29(%rip),%rdi
jmp    117b <_calloc+0x3b>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"%s: Testing calloc()\0A\00"
@SIZE_MAX = constant i64 0
@.str.1 = [36 x i8] c"calloc() failed to detect overflow\0A\00"
@.str.2 = [36 x i8] c"calloc() failed to allocate memory\0A\00"
@.str.3 = [33 x i8] c"calloc() failed to clear memory\0A\00"
define i32 @_calloc(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i64 16, i64* %6
%7 = load i8*, i8** %3
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i64, i64* @SIZE_MAX
%10 = call i32* @calloc(i64 %9, i64 2)
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = load i64, i64* @SIZE_MAX
%14 = call i32* @calloc(i64 2, i64 %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %12, %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %42
18: ; preds = %12
%19 = call i32* @calloc(i64 4, i64 16)
store i32* %19, i32** %4
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %42
23: ; preds = %18
store i64 0, i64* %5
br label %24
24: ; preds = %36, %23
%25 = load i64, i64* %5
%26 = icmp ult i64 %25, 16
br i1 %26, label %27, label %39
27: ; preds = %24
%28 = load i32*, i32** %4
%29 = load i64, i64* %5
%30 = getelementptr inbounds i32, i32* %28, i64 %29
%31 = load i32, i32* %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %2
br label %42
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load i64, i64* %5
%38 = add i64 %37, 1
store i64 %38, i64* %5
br label %24
39: ; preds = %24
%40 = load i32*, i32** %4
%41 = call i32 @free(i32* %40)
store i32 0, i32* %2
br label %42
42: ; preds = %39, %33, %21, %16
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [22 x i8] c"%s: Testing calloc()\0A\00"
@SIZE_MAX = constant i64 0
@str = [32 x i8] c"calloc() failed to clear memory\00"
@str.4 = [35 x i8] c"calloc() failed to allocate memory\00"
@str.5 = [35 x i8] c"calloc() failed to detect overflow\00"
define i32 @_calloc(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32* @calloc(i64 0, i64 2)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32* @calloc(i64 2, i64 0)
%7 = icmp eq i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %5, %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.5, i64 0, i64 0))
br label %82
10: ; preds = %5
%11 = tail call i32* @calloc(i64 4, i64 16)
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* %11
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %80
16: ; preds = %10
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
br label %82
18: ; preds = %13
%19 = getelementptr inbounds i32, i32* %11, i64 1
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %80
22: ; preds = %18
%23 = getelementptr inbounds i32, i32* %11, i64 2
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %80
26: ; preds = %22
%27 = getelementptr inbounds i32, i32* %11, i64 3
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %80
30: ; preds = %26
%31 = getelementptr inbounds i32, i32* %11, i64 4
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %80
34: ; preds = %30
%35 = getelementptr inbounds i32, i32* %11, i64 5
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %80
38: ; preds = %34
%39 = getelementptr inbounds i32, i32* %11, i64 6
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %80
42: ; preds = %38
%43 = getelementptr inbounds i32, i32* %11, i64 7
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %80
46: ; preds = %42
%47 = getelementptr inbounds i32, i32* %11, i64 8
%48 = load i32, i32* %47
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %80
50: ; preds = %46
%51 = getelementptr inbounds i32, i32* %11, i64 9
%52 = load i32, i32* %51
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %80
54: ; preds = %50
%55 = getelementptr inbounds i32, i32* %11, i64 10
%56 = load i32, i32* %55
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %80
58: ; preds = %54
%59 = getelementptr inbounds i32, i32* %11, i64 11
%60 = load i32, i32* %59
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %80
62: ; preds = %58
%63 = getelementptr inbounds i32, i32* %11, i64 12
%64 = load i32, i32* %63
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %80
66: ; preds = %62
%67 = getelementptr inbounds i32, i32* %11, i64 13
%68 = load i32, i32* %67
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %80
70: ; preds = %66
%71 = getelementptr inbounds i32, i32* %11, i64 14
%72 = load i32, i32* %71
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %80
74: ; preds = %70
%75 = getelementptr inbounds i32, i32* %11, i64 15
%76 = load i32, i32* %75
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = tail call i32 @free(i32* %11)
br label %82
80: ; preds = %74, %70, %66, %62, %58, %54, %50, %46, %42, %38, %34, %30, %26, %22, %18, %13
%81 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %82
82: ; preds = %78, %80, %16, %8
%83 = phi i32 [ 1, %8 ], [ 1, %16 ], [ 1, %80 ], [ 0, %78 ]
ret i32 %83
}