int main(void) {
  unsigned lifetime[17];
  void *blk[17];
  int i, j, k;
  dump_heap();
  memset(lifetime, 0, sizeof(lifetime));
  memset(blk, 0, sizeof(blk));
  for (i = 0; i < 1000; i++) {
    printf("Pass %6u\n", i);
    for (j = 0; j < 17; j++) {
      if (lifetime[j] != 0) {
        (lifetime[j])--;
        continue;
      }
      if (blk[j] != ((void *)0)) {
        kfree(blk[j]);
        blk[j] = ((void *)0);
      }
      k = rand() % 40960 + 1;
      blk[j] = kmalloc(k);
      if (blk[j] == ((void *)0))
        printf("failed to alloc %u bytes\n", k);
      else
        lifetime[j] = rand() % 21;
    }
  }
  printf("\n\n");
  dump_heap();
  for (j = 0; j < 17; j++) {
    if (blk[j] != ((void *)0)) {
      kfree(blk[j]);
      blk[j] = ((void *)0);
    }
    (lifetime[j]) = 0;
  }
  dump_heap();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <dump_heap@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x44,%edx
call   1050 <memset@plt>
lea    -0xe0(%rbp),%rdi
xor    %esi,%esi
mov    $0x88,%edx
call   1050 <memset@plt>
movl   $0x0,-0xe4(%rbp)
cmpl   $0x3e8,-0xe4(%rbp)
jge    12fb <main+0x19b>
mov    -0xe4(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xe8(%rbp)
cmpl   $0x11,-0xe8(%rbp)
jge    12e2 <main+0x182>
movslq -0xe8(%rbp),%rax
cmpl   $0x0,-0x50(%rbp,%rax,4)
je     120a <main+0xaa>
movslq -0xe8(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x50(%rbp,%rax,4)
jmp    12ce <main+0x16e>
movslq -0xe8(%rbp),%rax
cmpq   $0x0,-0xe0(%rbp,%rax,8)
je     1249 <main+0xe9>
movslq -0xe8(%rbp),%rax
mov    -0xe0(%rbp,%rax,8),%rdi
ff
mov    $0x0,%al
call   1060 <kfree@plt>
movslq -0xe8(%rbp),%rax
movq   $0x0,-0xe0(%rbp,%rax,8)
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0xa000,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0xec(%rbp)
mov    -0xec(%rbp),%edi
mov    $0x0,%al
call   1070 <kmalloc@plt>
movslq %eax,%rcx
movslq -0xe8(%rbp),%rax
mov    %rcx,-0xe0(%rbp,%rax,8)
ff
movslq -0xe8(%rbp),%rax
cmpq   $0x0,-0xe0(%rbp,%rax,8)
jne    12af <main+0x14f>
mov    -0xec(%rbp),%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c9 <main+0x169>
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x15,%ecx
cltd
idiv   %ecx
movslq -0xe8(%rbp),%rax
mov    %edx,-0x50(%rbp,%rax,4)
jmp    12ce <main+0x16e>
mov    -0xe8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe8(%rbp)
jmp    11d4 <main+0x74>
jmp    12e7 <main+0x187>
mov    -0xe4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe4(%rbp)
jmp    11a6 <main+0x46>
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <dump_heap@plt>
movl   $0x0,-0xe8(%rbp)
cmpl   $0x11,-0xe8(%rbp)
jge    1389 <main+0x229>
movslq -0xe8(%rbp),%rax
cmpq   $0x0,-0xe0(%rbp,%rax,8)
je     1366 <main+0x206>
movslq -0xe8(%rbp),%rax
mov    -0xe0(%rbp,%rax,8),%rdi
ff
mov    $0x0,%al
call   1060 <kfree@plt>
movslq -0xe8(%rbp),%rax
movq   $0x0,-0xe0(%rbp,%rax,8)
movslq -0xe8(%rbp),%rax
movl   $0x0,-0x50(%rbp,%rax,4)
mov    -0xe8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xe8(%rbp)
jmp    131a <main+0x1ba>
mov    $0x0,%al
call   1030 <dump_heap@plt>
xor    %eax,%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xe0,%rsp
xor    %eax,%eax
call   1040 <dump_heap@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x30(%rsp)
movl   $0x0,0x40(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xc0(%rsp)
movq   $0x0,0xd0(%rsp)
lea    0xe22(%rip),%r14
lea    0xe25(%rip),%r15
xor    %r12d,%r12d
jmp    1201 <main+0xa1>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    $0x3e8,%r12d
je     12c5 <main+0x165>
mov    %r14,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %ebp,%ebp
jmp    1230 <main+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffff,%eax
mov    %eax,(%rsp,%rbp,4)
add    $0x1,%rbp
cmp    $0x11,%rbp
je     11f0 <main+0x90>
mov    (%rsp,%rbp,4),%eax
test   %eax,%eax
jne    1220 <main+0xc0>
mov    0x50(%rsp,%rbp,8),%rdi
test   %rdi,%rdi
je     1248 <main+0xe8>
xor    %eax,%eax
call   1060 <kfree@plt>
xor    %eax,%eax
call   1080 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x2e,%rcx
add    %edx,%ecx
shl    $0xd,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebx
add    $0x1,%ebx
mov    %ebx,%edi
xor    %eax,%eax
call   1070 <kmalloc@plt>
cltq
mov    %rax,0x50(%rsp,%rbp,8)
test   %eax,%eax
je     12b4 <main+0x154>
xor    %eax,%eax
call   1080 <rand@plt>
cltq
imul   $0x30c30c31,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rcx,%rdx,4),%ecx
sub    %ecx,%eax
mov    %eax,(%rsp,%rbp,4)
jmp    1226 <main+0xc6>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1226 <main+0xc6>
lea    0xd56(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <dump_heap@plt>
xor    %ebx,%ebx
jmp    12f1 <main+0x191>
nopl   0x0(%rax)
movl   $0x0,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x11,%rbx
je     130d <main+0x1ad>
mov    0x50(%rsp,%rbx,8),%rdi
test   %rdi,%rdi
je     12e0 <main+0x180>
xor    %eax,%eax
call   1060 <kfree@plt>
movq   $0x0,0x50(%rsp,%rbx,8)
jmp    12e0 <main+0x180>
xor    %eax,%eax
call   1040 <dump_heap@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Pass %6u\0A\00"
@.str.1 = [26 x i8] c"failed to alloc %u bytes\0A\00"
@.str.2 = [3 x i8] c"\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [17 x i32]
%3 = alloca [17 x i8*]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @dump_heap()
%8 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 0
%9 = bitcast i32* %8 to i8*
%10 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 0
%11 = bitcast i8** %10 to i8*
store i32 0, i32* %4
br label %12
12: ; preds = %78, %0
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 1000
br i1 %14, label %15, label %81
15: ; preds = %12
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 0, i32* %5
br label %18
18: ; preds = %74, %15
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 17
br i1 %20, label %21, label %77
21: ; preds = %18
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %29
%31 = load i32, i32* %30
%32 = add i32 %31, -1
store i32 %32, i32* %30
br label %74
33: ; preds = %21
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %35
%37 = load i8*, i8** %36
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %48
39: ; preds = %33
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 (i8*, ...) bitcast (i32 (...)* @kfree to i32 (i8*, ...)*)(i8* %43)
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %46
store i8* null, i8** %47
br label %48
48: ; preds = %39, %33
%49 = call i32 (...) @rand()
%50 = srem i32 %49, 40960
%51 = add nsw i32 %50, 1
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = call i32 (i32, ...) bitcast (i32 (...)* @kmalloc to i32 (i32, ...)*)(i32 %52)
%54 = sext i32 %53 to i64
%55 = inttoptr i64 %54 to i8*
%56 = load i32, i32* %5
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %57
store i8* %55, i8** %58
%59 = load i32, i32* %5
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %60
%62 = load i8*, i8** %61
%63 = icmp eq i8* %62, null
br i1 %63, label %64, label %67
64: ; preds = %48
%65 = load i32, i32* %6
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %65)
br label %73
67: ; preds = %48
%68 = call i32 (...) @rand()
%69 = srem i32 %68, 21
%70 = load i32, i32* %5
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %71
store i32 %69, i32* %72
br label %73
73: ; preds = %67, %64
br label %74
74: ; preds = %73, %27
%75 = load i32, i32* %5
%76 = add nsw i32 %75, 1
store i32 %76, i32* %5
br label %18
77: ; preds = %18
br label %78
78: ; preds = %77
%79 = load i32, i32* %4
%80 = add nsw i32 %79, 1
store i32 %80, i32* %4
br label %12
81: ; preds = %12
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%83 = call i32 (...) @dump_heap()
store i32 0, i32* %5
br label %84
84: ; preds = %106, %81
%85 = load i32, i32* %5
%86 = icmp slt i32 %85, 17
br i1 %86, label %87, label %109
87: ; preds = %84
%88 = load i32, i32* %5
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %89
%91 = load i8*, i8** %90
%92 = icmp ne i8* %91, null
br i1 %92, label %93, label %102
93: ; preds = %87
%94 = load i32, i32* %5
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %95
%97 = load i8*, i8** %96
%98 = call i32 (i8*, ...) bitcast (i32 (...)* @kfree to i32 (i8*, ...)*)(i8* %97)
%99 = load i32, i32* %5
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 %100
store i8* null, i8** %101
br label %102
102: ; preds = %93, %87
%103 = load i32, i32* %5
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds [17 x i32], [17 x i32]* %2, i64 0, i64 %104
store i32 0, i32* %105
br label %106
106: ; preds = %102
%107 = load i32, i32* %5
%108 = add nsw i32 %107, 1
store i32 %108, i32* %5
br label %84
109: ; preds = %84
%110 = call i32 (...) @dump_heap()
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"Pass %6u\0A\00"
@.str.1 = [26 x i8] c"failed to alloc %u bytes\0A\00"
@str = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca [17 x i32]
%2 = alloca [17 x i8*]
%3 = bitcast [17 x i32]* %1 to i8*
%4 = bitcast [17 x i8*]* %2 to i8*
%5 = tail call i32 (...) @dump_heap()
br label %6
6: ; preds = %0, %38
%7 = phi i32 [ 0, %0 ], [ %39, %38 ]
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %6, %35
%10 = phi i64 [ 0, %6 ], [ %36, %35 ]
%11 = getelementptr inbounds [17 x i32], [17 x i32]* %1, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = add i32 %12, -1
store i32 %15, i32* %11
br label %35
16: ; preds = %9
%17 = getelementptr inbounds [17 x i8*], [17 x i8*]* %2, i64 0, i64 %10
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 (i8*, ...) bitcast (i32 (...)* @kfree to i32 (i8*, ...)*)(i8* %18)
br label %22
22: ; preds = %20, %16
%23 = tail call i32 (...) @rand()
%24 = srem i32 %23, 40960
%25 = add nsw i32 %24, 1
%26 = tail call i32 (i32, ...) bitcast (i32 (...)* @kmalloc to i32 (i32, ...)*)(i32 %25)
%27 = sext i32 %26 to i64
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %17
%29 = icmp eq i32 %26, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %35
32: ; preds = %22
%33 = tail call i32 (...) @rand()
%34 = srem i32 %33, 21
store i32 %34, i32* %11
br label %35
35: ; preds = %30, %32, %14
%36 = add nuw nsw i64 %10, 1
%37 = icmp eq i64 %36, 17
br i1 %37, label %38, label %9
38: ; preds = %35
%39 = add nuw nsw i32 %7, 1
%40 = icmp eq i32 %39, 1000
br i1 %40, label %41, label %6
41: ; preds = %38
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%43 = tail call i32 (...) @dump_heap()
br label %44
44: ; preds = %41, %51
%45 = phi i64 [ 0, %41 ], [ %53, %51 ]
%46 = getelementptr inbounds [17 x i8*], [17 x i8*]* %2, i64 0, i64 %45
%47 = load i8*, i8** %46
%48 = icmp eq i8* %47, null
br i1 %48, label %51, label %49
49: ; preds = %44
%50 = tail call i32 (i8*, ...) bitcast (i32 (...)* @kfree to i32 (i8*, ...)*)(i8* %47)
store i8* null, i8** %46
br label %51
51: ; preds = %49, %44
%52 = getelementptr inbounds [17 x i32], [17 x i32]* %1, i64 0, i64 %45
store i32 0, i32* %52
%53 = add nuw nsw i64 %45, 1
%54 = icmp eq i64 %53, 17
br i1 %54, label %55, label %44
55: ; preds = %51
%56 = tail call i32 (...) @dump_heap()
ret i32 0
}