#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*/ debug_init();
void *do_alloc(int, char *);
int /*<<< orphan*/ do_free(void *);
void **malloc(int);
int /*<<< orphan*/ print_stats();
int /*<<< orphan*/ printf(char *, char *);
int random();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int i, j;
  char astr[64];
  int max_iter = 30;
  int max_alloc_types = 20;
  void **p;
  if (argc < (1 + 1)) {
    printf("Usage: %s <max_iters>\n", argv[0]);
    return 0;
  }
  sscanf(argv[1], "%d", &max_iter);
  p = malloc(sizeof(void *) * max_iter);
  srandom(time(NULL));
  debug_init();
  for (i = 0; i < max_iter; i++) {
    j = random() % max_alloc_types;
    sprintf(astr, "alloc %d", j);
    p[i] = do_alloc(20, astr);
  }
  for (i = 0; i < max_iter; i++) {
    if (random() % 2 == 0)
      do_free(p[i]);
  }
  print_stats();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1e,-0x64(%rbp)
movl   $0x14,-0x68(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11fd <main+0x4d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe14(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <main+0x14b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe11(%rip),%rsi
lea    -0x64(%rbp),%rdx
call   10b0 <sscanf@plt>
movslq -0x64(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x70(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
mov    $0x0,%al
call   1050 <debug_init@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x64(%rbp),%eax
jge    129e <main+0xee>
mov    $0x0,%al
call   1090 <random@plt>
cltd
idivl  -0x68(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xda9(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0x60(%rbp),%rsi
mov    $0x14,%edi
call   1070 <do_alloc@plt>
mov    %rax,%rdx
mov    -0x70(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1246 <main+0x96>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x64(%rbp),%eax
jge    12ed <main+0x13d>
mov    $0x0,%al
call   1090 <random@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12da <main+0x12a>
mov    -0x70(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   10c0 <do_free@plt>
jmp    12df <main+0x12f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a5 <main+0xf5>
mov    $0x0,%al
call   1040 <print_stats@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
movl   $0x1e,0xc(%rsp)
cmp    $0x1,%edi
jg     11dc <main+0x2c>
mov    (%rsi),%rsi
lea    0xe2e(%rip),%rdi
call   1060 <printf@plt>
jmp    12bb <main+0x10b>
mov    0x8(%rsi),%rdi
lea    0xe36(%rip),%rsi
lea    0xc(%rsp),%rdx
call   10b0 <sscanf@plt>
mov    0xc(%rsp),%edi
shl    $0x3,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
xor    %eax,%eax
call   1050 <debug_init@plt>
cmpl   $0x0,0xc(%rsp)
jle    12b4 <main+0x104>
lea    0xdf0(%rip),%r14
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1090 <random@plt>
movslq %eax,%rdx
imul   $0x66666667,%rdx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
shl    $0x2,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   10d0 <sprintf@plt>
mov    $0x14,%edi
mov    %r12,%rsi
call   1070 <do_alloc@plt>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     1230 <main+0x80>
test   %eax,%eax
jle    12b4 <main+0x104>
xor    %ebx,%ebx
jmp    129e <main+0xee>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jge    12b4 <main+0x104>
xor    %eax,%eax
call   1090 <random@plt>
test   $0x1,%al
jne    1290 <main+0xe0>
mov    (%r15,%rbx,8),%rdi
call   10c0 <do_free@plt>
jmp    1290 <main+0xe0>
xor    %eax,%eax
call   1040 <print_stats@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: %s <max_iters>\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [9 x i8] c"alloc %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 30, i32* %9
store i32 20, i32* %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 0, i32* %3
br label %74
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = mul i64 8, %25
%27 = trunc i64 %26 to i32
%28 = call i8** @malloc(i32 %27)
store i8** %28, i8*** %11
%29 = call i32 @time(i32* null)
%30 = call i32 @srandom(i32 %29)
%31 = call i32 (...) @debug_init()
store i32 0, i32* %6
br label %32
32: ; preds = %49, %19
%33 = load i32, i32* %6
%34 = load i32, i32* %9
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %52
36: ; preds = %32
%37 = call i32 (...) @random()
%38 = load i32, i32* %10
%39 = srem i32 %37, %38
store i32 %39, i32* %7
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* %7
%42 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%44 = call i8* @do_alloc(i32 20, i8* %43)
%45 = load i8**, i8*** %11
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
store i8* %44, i8** %48
br label %49
49: ; preds = %36
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 1
store i32 %51, i32* %6
br label %32
52: ; preds = %32
store i32 0, i32* %6
br label %53
53: ; preds = %69, %52
%54 = load i32, i32* %6
%55 = load i32, i32* %9
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %72
57: ; preds = %53
%58 = call i32 (...) @random()
%59 = srem i32 %58, 2
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i8**, i8*** %11
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
%67 = call i32 @do_free(i8* %66)
br label %68
68: ; preds = %61, %57
br label %69
69: ; preds = %68
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %53
72: ; preds = %53
%73 = call i32 (...) @print_stats()
store i32 0, i32* %3
br label %74
74: ; preds = %72, %14
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [23 x i8] c"Usage: %s <max_iters>\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [9 x i8] c"alloc %d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
store i32 30, i32* %4
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %52
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%15 = load i32, i32* %4
%16 = shl i32 %15, 3
%17 = call i8** @malloc(i32 %16)
%18 = call i32 @time(i32* null)
%19 = call i32 @srandom(i32 %18)
%20 = call i32 (...) @debug_init()
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %25, label %50
23: ; preds = %25
%24 = icmp sgt i32 %33, 0
br i1 %24, label %36, label %50
25: ; preds = %11, %25
%26 = phi i64 [ %32, %25 ], [ 0, %11 ]
%27 = call i32 (...) @random()
%28 = srem i32 %27, 20
%29 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = call i8* @do_alloc(i32 20, i8* %5)
%31 = getelementptr inbounds i8*, i8** %17, i64 %26
store i8* %30, i8** %31
%32 = add nuw nsw i64 %26, 1
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br i1 %35, label %25, label %23
36: ; preds = %23, %45
%37 = phi i64 [ %46, %45 ], [ 0, %23 ]
%38 = call i32 (...) @random()
%39 = and i32 %38, 1
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = getelementptr inbounds i8*, i8** %17, i64 %37
%43 = load i8*, i8** %42
%44 = call i32 @do_free(i8* %43)
br label %45
45: ; preds = %36, %41
%46 = add nuw nsw i64 %37, 1
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = icmp slt i64 %46, %48
br i1 %49, label %36, label %50
50: ; preds = %45, %11, %23
%51 = call i32 (...) @print_stats()
br label %52
52: ; preds = %50, %8
ret i32 0
}