#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 */
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int random();
int /*<<< orphan*/ srandom(int);
int test1(void) {
  void **ptrs;
  int i, j;
  int size;
  int ret = 0;
  srandom(0x19730929);
  ptrs = malloc(1000 * sizeof(void *));
  for (i = 0; i < 1000; i++) {
    if ((ptrs[i] = malloc((random() % 0x10000))) == NULL) {
      printf("malloc random failed! %i\n", i);
      ++ret;
    }
  }
  for (i = 0; i < 10000; i++) {
    j = (random() % 1000);
    free(ptrs[j]);
    size = (random() % 0x10000);
    ptrs[j] = malloc(size);
    if (!ptrs[j]) {
      printf("malloc failed! %d\n", i);
      ++ret;
    }
    memset(ptrs[j], 0, size);
  }
  for (i = 0; i < 1000; i++) {
    free(ptrs[i]);
  }
  return ret;
}


x86 assembly (clang, -O0):
<test1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x18(%rbp)
mov    $0x19730929,%edi
call   1040 <srandom@plt>
mov    $0x1f40,%edi
call   1080 <malloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3e8,-0xc(%rbp)
jge    11f2 <test1+0x92>
mov    $0x0,%al
call   1070 <random@plt>
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1080 <malloc@plt>
mov    -0x8(%rbp),%rcx
movslq -0xc(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    11df <test1+0x7f>
mov    -0xc(%rbp),%esi
lea    0xe2f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11e4 <test1+0x84>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    118e <test1+0x2e>
movl   $0x0,-0xc(%rbp)
cmpl   $0x2710,-0xc(%rbp)
jge    12a1 <test1+0x141>
mov    $0x0,%al
call   1070 <random@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1070 <random@plt>
mov    $0x10000,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    127d <test1+0x11d>
mov    -0xc(%rbp),%esi
lea    0xdab(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11f9 <test1+0x99>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3e8,-0xc(%rbp)
jge    12d4 <test1+0x174>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12a8 <test1+0x148>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x19730929,%edi
call   1040 <srandom@plt>
mov    $0x1f40,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
lea    0xe77(%rip),%r14
xor    %ebp,%ebp
xor    %r15d,%r15d
jmp    119d <test1+0x3d>
add    $0x1,%rbp
cmp    $0x3e8,%rbp
je     11d7 <test1+0x77>
xor    %eax,%eax
call   1070 <random@plt>
lea    0xffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffff0000,%ecx
sub    %ecx,%eax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,(%r12,%rbp,8)
test   %rax,%rax
jne    1190 <test1+0x30>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
add    $0x1,%r15d
jmp    1190 <test1+0x30>
xor    %r13d,%r13d
jmp    11f9 <test1+0x99>
nopl   0x0(%rax)
mov    %rbp,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
add    $0x1,%r13d
cmp    $0x2710,%r13d
je     1270 <test1+0x110>
xor    %eax,%eax
call   1070 <random@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
movslq %eax,%r14
mov    (%r12,%r14,8),%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1070 <random@plt>
mov    %eax,%ebx
lea    0xffff(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
and    $0xffff0000,%eax
sub    %eax,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,(%r12,%r14,8)
test   %rax,%rax
jne    11e0 <test1+0x80>
lea    0xdbb(%rip),%rdi
mov    %r13d,%esi
call   1050 <printf@plt>
add    $0x1,%r15d
jmp    11e0 <test1+0x80>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    $0x3e8,%rbx
jne    1280 <test1+0x120>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"malloc random failed! %i\0A\00"
@.str.1 = [19 x i8] c"malloc failed! %d\0A\00"
define i32 @test1() {
%1 = alloca i8**
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %5
%6 = call i32 @srandom(i32 426969385)
%7 = call i8* @malloc(i32 8000)
%8 = bitcast i8* %7 to i8**
store i8** %8, i8*** %1
store i32 0, i32* %2
br label %9
9: ; preds = %27, %0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 1000
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = call i32 (...) @random()
%14 = srem i32 %13, 65536
%15 = call i8* @malloc(i32 %14)
%16 = load i8**, i8*** %1
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
store i8* %15, i8** %19
%20 = icmp eq i8* %15, null
br i1 %20, label %21, label %26
21: ; preds = %12
%22 = load i32, i32* %2
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %26
26: ; preds = %21, %12
br label %27
27: ; preds = %26
%28 = load i32, i32* %2
%29 = add nsw i32 %28, 1
store i32 %29, i32* %2
br label %9
30: ; preds = %9
store i32 0, i32* %2
br label %31
31: ; preds = %70, %30
%32 = load i32, i32* %2
%33 = icmp slt i32 %32, 10000
br i1 %33, label %34, label %73
34: ; preds = %31
%35 = call i32 (...) @random()
%36 = srem i32 %35, 1000
store i32 %36, i32* %3
%37 = load i8**, i8*** %1
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
%41 = load i8*, i8** %40
%42 = call i32 @free(i8* %41)
%43 = call i32 (...) @random()
%44 = srem i32 %43, 65536
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = call i8* @malloc(i32 %45)
%47 = load i8**, i8*** %1
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8*, i8** %47, i64 %49
store i8* %46, i8** %50
%51 = load i8**, i8*** %1
%52 = load i32, i32* %3
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = icmp ne i8* %55, null
br i1 %56, label %62, label %57
57: ; preds = %34
%58 = load i32, i32* %2
%59 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
br label %62
62: ; preds = %57, %34
%63 = load i8**, i8*** %1
%64 = load i32, i32* %3
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %63, i64 %65
%67 = load i8*, i8** %66
%68 = load i32, i32* %4
%69 = call i32 @memset(i8* %67, i32 0, i32 %68)
br label %70
70: ; preds = %62
%71 = load i32, i32* %2
%72 = add nsw i32 %71, 1
store i32 %72, i32* %2
br label %31
73: ; preds = %31
store i32 0, i32* %2
br label %74
74: ; preds = %84, %73
%75 = load i32, i32* %2
%76 = icmp slt i32 %75, 1000
br i1 %76, label %77, label %87
77: ; preds = %74
%78 = load i8**, i8*** %1
%79 = load i32, i32* %2
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8*, i8** %78, i64 %80
%82 = load i8*, i8** %81
%83 = call i32 @free(i8* %82)
br label %84
84: ; preds = %77
%85 = load i32, i32* %2
%86 = add nsw i32 %85, 1
store i32 %86, i32* %2
br label %74
87: ; preds = %74
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
@.str = [26 x i8] c"malloc random failed! %i\0A\00"
@.str.1 = [19 x i8] c"malloc failed! %d\0A\00"
define i32 @test1() {
%1 = tail call i32 @srandom(i32 426969385)
%2 = tail call dereferenceable_or_null(8000) i8* @malloc(i32 8000)
%3 = bitcast i8* %2 to i8**
br label %4
4: ; preds = %0, %16
%5 = phi i64 [ 0, %0 ], [ %18, %16 ]
%6 = phi i32 [ 0, %0 ], [ %17, %16 ]
%7 = tail call i32 (...) @random()
%8 = srem i32 %7, 65536
%9 = tail call i8* @malloc(i32 %8)
%10 = getelementptr inbounds i8*, i8** %3, i64 %5
store i8* %9, i8** %10
%11 = icmp eq i8* %9, null
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = trunc i64 %5 to i32
%14 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = add nsw i32 %6, 1
br label %16
16: ; preds = %4, %12
%17 = phi i32 [ %15, %12 ], [ %6, %4 ]
%18 = add nuw nsw i64 %5, 1
%19 = icmp eq i64 %18, 1000
br i1 %19, label %20, label %4
20: ; preds = %16, %36
%21 = phi i32 [ %37, %36 ], [ %17, %16 ]
%22 = phi i32 [ %39, %36 ], [ 0, %16 ]
%23 = tail call i32 (...) @random()
%24 = srem i32 %23, 1000
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %3, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i32 @free(i8* %27)
%29 = tail call i32 (...) @random()
%30 = srem i32 %29, 65536
%31 = tail call i8* @malloc(i32 %30)
store i8* %31, i8** %26
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %20
%34 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%35 = add nsw i32 %21, 1
br label %36
36: ; preds = %33, %20
%37 = phi i32 [ %21, %20 ], [ %35, %33 ]
%38 = tail call i32 @memset(i8* %31, i32 0, i32 %30)
%39 = add nuw nsw i32 %22, 1
%40 = icmp eq i32 %39, 10000
br i1 %40, label %41, label %20
41: ; preds = %36, %41
%42 = phi i64 [ %46, %41 ], [ 0, %36 ]
%43 = getelementptr inbounds i8*, i8** %3, i64 %42
%44 = load i8*, i8** %43
%45 = tail call i32 @free(i8* %44)
%46 = add nuw nsw i64 %42, 1
%47 = icmp eq i64 %46, 1000
br i1 %47, label %48, label %41
48: ; preds = %41
ret i32 %37
}