#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int);
int /*<<< orphan*/ free(char *);
int random();
int /*<<< orphan*/ stderr;
void random_test(void) {
  char *ptrs[50000];
  int i;
  for (i = 0; i < 50000; ++i) {
    int j;
    int n = 1 + random() % 10;
    int elem = 1 + random() % 100;
    int size = n * elem;
    ptrs[i] = (char *)calloc(n, elem);
    if (ptrs[i] == NULL)
      break;
    for (j = 0; j < size; ++j) {
      if (ptrs[i][j] != '\0') {
        fprintf(stderr, "byte not cleared (size %d, element %d, byte %d)", size,
                i, j);
        exit(1);
      }
      ptrs[i][j] = '\xff';
    }
  }
  while (i-- > 0)
    free(ptrs[i]);
}


x86 assembly (clang, -O0):
<random_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x61aa0,%rsp
movl   $0x0,-0x61a84(%rbp)
cmpl   $0xc350,-0x61a84(%rbp)
jge    12ae <random_test+0x15e>
mov    $0x0,%al
call   1060 <random@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x61a8c(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x61a90(%rbp)
mov    -0x61a8c(%rbp),%eax
imul   -0x61a90(%rbp),%eax
mov    %eax,-0x61a94(%rbp)
mov    -0x61a8c(%rbp),%edi
mov    -0x61a90(%rbp),%esi
call   1040 <calloc@plt>
mov    %rax,%rcx
movslq -0x61a84(%rbp),%rax
mov    %rcx,-0x61a80(%rbp,%rax,8)
ff
movslq -0x61a84(%rbp),%rax
cmpq   $0x0,-0x61a80(%rbp,%rax,8)
jne    11f6 <random_test+0xa6>
jmp    12ae <random_test+0x15e>
movl   $0x0,-0x61a88(%rbp)
mov    -0x61a88(%rbp),%eax
cmp    -0x61a94(%rbp),%eax
jge    1295 <random_test+0x145>
movslq -0x61a84(%rbp),%rax
mov    -0x61a80(%rbp,%rax,8),%rax
ff
movslq -0x61a88(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1267 <random_test+0x117>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x61a94(%rbp),%edx
mov    -0x61a84(%rbp),%ecx
mov    -0x61a88(%rbp),%r8d
lea    0xda8(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq -0x61a84(%rbp),%rax
mov    -0x61a80(%rbp,%rax,8),%rax
ff
movslq -0x61a88(%rbp),%rcx
movb   $0xff,(%rax,%rcx,1)
mov    -0x61a88(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61a88(%rbp)
jmp    1200 <random_test+0xb0>
jmp    129a <random_test+0x14a>
mov    -0x61a84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61a84(%rbp)
jmp    1165 <random_test+0x15>
jmp    12b3 <random_test+0x163>
mov    -0x61a84(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x61a84(%rbp)
cmp    $0x0,%eax
jle    12e6 <random_test+0x196>
movslq -0x61a84(%rbp),%rax
mov    -0x61a80(%rbp,%rax,8),%rdi
ff
call   1030 <free@plt>
jmp    12b3 <random_test+0x163>
add    $0x61aa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_test>:
push   %rbp
push   %r14
push   %rbx
sub    $0x61a80,%rsp
xor    %ebx,%ebx
jmp    1171 <random_test+0x21>
nop
add    $0x1,%rbx
cmp    $0xc350,%rbx
je     1210 <random_test+0xc0>
xor    %eax,%eax
call   1060 <random@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%r14d
add    $0x1,%r14d
xor    %eax,%eax
call   1060 <random@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0x1,%ebp
mov    %r14d,%edi
mov    %ebp,%esi
call   1040 <calloc@plt>
mov    %rax,(%rsp,%rbx,8)
test   %rax,%rax
je     120a <random_test+0xba>
imul   %r14d,%ebp
test   %ebp,%ebp
jle    1160 <random_test+0x10>
mov    %ebp,%ecx
xor    %r8d,%r8d
cs nopw 0x0(%rax,%rax,1)
nop
cmpb   $0x0,(%rax,%r8,1)
jne    1240 <random_test+0xf0>
movb   $0xff,(%rax,%r8,1)
add    $0x1,%r8
cmp    %r8,%rcx
jne    11f0 <random_test+0xa0>
jmp    1160 <random_test+0x10>
test   %ebx,%ebx
jg     1215 <random_test+0xc5>
jmp    1234 <random_test+0xe4>
mov    $0xc350,%ebx
add    $0x1,%rbx
nopl   0x0(%rax)
mov    -0x10(%rsp,%rbx,8),%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1220 <random_test+0xd0>
add    $0x61a80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [48 x i8] c"byte not cleared (size %d, element %d, byte %d)\00"
define void @random_test() {
%1 = alloca [50000 x i8*]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %2
br label %7
7: ; preds = %68, %0
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 50000
br i1 %9, label %10, label %71
10: ; preds = %7
%11 = call i32 (...) @random()
%12 = srem i32 %11, 10
%13 = add nsw i32 1, %12
store i32 %13, i32* %4
%14 = call i32 (...) @random()
%15 = srem i32 %14, 100
%16 = add nsw i32 1, %15
store i32 %16, i32* %5
%17 = load i32, i32* %4
%18 = load i32, i32* %5
%19 = mul nsw i32 %17, %18
store i32 %19, i32* %6
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = call i64 @calloc(i32 %20, i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %25
store i8* %23, i8** %26
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %10
br label %71
33: ; preds = %10
store i32 0, i32* %3
br label %34
34: ; preds = %64, %33
%35 = load i32, i32* %3
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %67
38: ; preds = %34
%39 = load i32, i32* %2
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %40
%42 = load i8*, i8** %41
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %38
%50 = load i32, i32* @stderr
%51 = load i32, i32* %6
%52 = load i32, i32* %2
%53 = load i32, i32* %3
%54 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %51, i32 %52, i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %38
%57 = load i32, i32* %2
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %58
%60 = load i8*, i8** %59
%61 = load i32, i32* %3
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 -1, i8* %63
br label %64
64: ; preds = %56
%65 = load i32, i32* %3
%66 = add nsw i32 %65, 1
store i32 %66, i32* %3
br label %34
67: ; preds = %34
br label %68
68: ; preds = %67
%69 = load i32, i32* %2
%70 = add nsw i32 %69, 1
store i32 %70, i32* %2
br label %7
71: ; preds = %32, %7
br label %72
72: ; preds = %76, %71
%73 = load i32, i32* %2
%74 = add nsw i32 %73, -1
store i32 %74, i32* %2
%75 = icmp sgt i32 %73, 0
br i1 %75, label %76, label %82
76: ; preds = %72
%77 = load i32, i32* %2
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %78
%80 = load i8*, i8** %79
%81 = call i32 @free(i8* %80)
br label %72
82: ; preds = %72
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [48 x i8] c"byte not cleared (size %d, element %d, byte %d)\00"
define void @random_test() {
%1 = alloca [50000 x i8*]
%2 = bitcast [50000 x i8*]* %1 to i8*
br label %3
3: ; preds = %0, %34
%4 = phi i64 [ 0, %0 ], [ %35, %34 ]
%5 = tail call i32 (...) @random()
%6 = srem i32 %5, 10
%7 = add nsw i32 %6, 1
%8 = tail call i32 (...) @random()
%9 = srem i32 %8, 100
%10 = add nsw i32 %9, 1
%11 = mul nsw i32 %10, %7
%12 = tail call i64 @calloc(i32 %7, i32 %10)
%13 = inttoptr i64 %12 to i8*
%14 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %4
store i8* %13, i8** %14
%15 = icmp eq i64 %12, 0
br i1 %15, label %37, label %16
16: ; preds = %3
%17 = icmp sgt i32 %11, 0
br i1 %17, label %18, label %34
18: ; preds = %16
%19 = zext i32 %11 to i64
br label %20
20: ; preds = %18, %31
%21 = phi i64 [ 0, %18 ], [ %32, %31 ]
%22 = getelementptr inbounds i8, i8* %13, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %20
%26 = trunc i64 %4 to i32
%27 = trunc i64 %21 to i32
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %26, i32 %27)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %20
store i8 -1, i8* %22
%32 = add nuw nsw i64 %21, 1
%33 = icmp eq i64 %32, %19
br i1 %33, label %34, label %20
34: ; preds = %31, %16
%35 = add nuw nsw i64 %4, 1
%36 = icmp eq i64 %35, 50000
br i1 %36, label %40, label %3
37: ; preds = %3
%38 = trunc i64 %4 to i32
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %34, %37
%41 = phi i64 [ %4, %37 ], [ 50000, %34 ]
%42 = and i64 %41, 4294967295
br label %43
43: ; preds = %40, %43
%44 = phi i64 [ %42, %40 ], [ %45, %43 ]
%45 = add nsw i64 %44, -1
%46 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %1, i64 0, i64 %45
%47 = load i8*, i8** %46
%48 = tail call i32 @free(i8* %47)
%49 = icmp ugt i64 %44, 1
br i1 %49, label %43, label %50
50: ; preds = %43, %37
ret void
}