#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*/ mfree(char *);
scalar_t__ mmalloc(int);
int /*<<< orphan*/ printf(char *);
int rand();
int alloc_free_chain() {
  static char *p[1000];
  int i;
  int block_size;
  int rand_count;
  if ((rand() % 20) == 0)
    alloc_free_chain();
  rand_count = rand() % 1000;
  for (i = 0; i < rand_count; i++) {
    if ((rand() % 5) == 0)
      block_size = rand() % (1024 * 8);
    else
      block_size = rand() % 32;
    p[i] = (char *)mmalloc(block_size);
    if (!p[i])
      printf("Error in alloc\n");
  }
  for (i = 0; i < rand_count; i++) {
    if (p[i])
      mfree(p[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<alloc_free_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1175 <alloc_free_chain+0x25>
call   1040 <alloc_free_chain@plt>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122a <alloc_free_chain+0xda>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11c9 <alloc_free_chain+0x79>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x2000,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
jmp    11db <alloc_free_chain+0x8b>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <mmalloc@plt>
mov    %rax,%rdx
movslq -0x4(%rbp),%rcx
lea    0x2e6f(%rip),%rax
mov    %rdx,(%rax,%rcx,8)
movslq -0x4(%rbp),%rcx
lea    0x2e60(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
jne    1217 <alloc_free_chain+0xc7>
lea    0xdee(%rip),%rdi
call   1030 <printf@plt>
jmp    121c <alloc_free_chain+0xcc>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    118e <alloc_free_chain+0x3e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    127a <alloc_free_chain+0x12a>
movslq -0x4(%rbp),%rcx
lea    0x2e18(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
je     1267 <alloc_free_chain+0x117>
movslq -0x4(%rbp),%rcx
lea    0x2e02(%rip),%rax
mov    (%rax,%rcx,8),%rdi
call   1060 <mfree@plt>
jmp    126c <alloc_free_chain+0x11c>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1231 <alloc_free_chain+0xe1>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_free_chain>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <rand@plt>
imul   $0xcccccccd,%eax,%eax
add    $0x19999998,%eax
ror    $0x2,%eax
cmp    $0xccccccc,%eax
ja     117c <alloc_free_chain+0x2c>
call   1040 <alloc_free_chain@plt>
xor    %eax,%eax
call   1070 <rand@plt>
movslq %eax,%rdx
imul   $0x10624dd3,%rdx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0x3e8,%eax,%eax
sub    %eax,%edx
test   %edx,%edx
jle    1259 <alloc_free_chain+0x109>
mov    %rdx,(%rsp)
mov    %edx,%r15d
lea    0x0(,%r15,8),%rbx
mov    $0x2000,%ebp
lea    0x2e9b(%rip),%r13
xor    %r14d,%r14d
jmp    11d9 <alloc_free_chain+0x89>
nopw   0x0(%rax,%rax,1)
add    $0x8,%r14
cmp    %r14,%rbx
je     1225 <alloc_free_chain+0xd5>
xor    %eax,%eax
call   1070 <rand@plt>
imul   $0xcccccccd,%eax,%r12d
add    $0x19999999,%r12d
xor    %eax,%eax
call   1070 <rand@plt>
cmp    $0x33333333,%r12d
mov    $0x20,%ecx
cmovb  %ebp,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1050 <mmalloc@plt>
mov    %rax,(%r14,%r13,1)
test   %rax,%rax
jne    11d0 <alloc_free_chain+0x80>
lea    0xde2(%rip),%rdi
call   1030 <puts@plt>
jmp    11d0 <alloc_free_chain+0x80>
cmpl   $0x0,(%rsp)
jle    1259 <alloc_free_chain+0x109>
shl    $0x3,%r15
xor    %ebx,%ebx
jmp    1249 <alloc_free_chain+0xf9>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x8,%rbx
cmp    %rbx,%r15
je     1259 <alloc_free_chain+0x109>
mov    (%rbx,%r13,1),%rdi
test   %rdi,%rdi
je     1240 <alloc_free_chain+0xf0>
call   1060 <mfree@plt>
jmp    1240 <alloc_free_chain+0xf0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@alloc_free_chain.p = internal global [1000 x i8*] zeroinitializer
@.str = [16 x i8] c"Error in alloc\0A\00"
define i32 @alloc_free_chain() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @rand()
%5 = srem i32 %4, 20
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @alloc_free_chain()
br label %9
9: ; preds = %7, %0
%10 = call i32 (...) @rand()
%11 = srem i32 %10, 1000
store i32 %11, i32* %3
store i32 0, i32* %1
br label %12
12: ; preds = %41, %9
%13 = load i32, i32* %1
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %44
16: ; preds = %12
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 (...) @rand()
%22 = srem i32 %21, 8192
store i32 %22, i32* %2
br label %26
23: ; preds = %16
%24 = call i32 (...) @rand()
%25 = srem i32 %24, 32
store i32 %25, i32* %2
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %2
%28 = call i64 @mmalloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = load i32, i32* %1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* @alloc_free_chain.p, i64 0, i64 %31
store i8* %29, i8** %32
%33 = load i32, i32* %1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* @alloc_free_chain.p, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = icmp ne i8* %36, null
br i1 %37, label %40, label %38
38: ; preds = %26
%39 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %26
br label %41
41: ; preds = %40
%42 = load i32, i32* %1
%43 = add nsw i32 %42, 1
store i32 %43, i32* %1
br label %12
44: ; preds = %12
store i32 0, i32* %1
br label %45
45: ; preds = %62, %44
%46 = load i32, i32* %1
%47 = load i32, i32* %3
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %65
49: ; preds = %45
%50 = load i32, i32* %1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* @alloc_free_chain.p, i64 0, i64 %51
%53 = load i8*, i8** %52
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = load i32, i32* %1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* @alloc_free_chain.p, i64 0, i64 %57
%59 = load i8*, i8** %58
%60 = call i32 @mfree(i8* %59)
br label %61
61: ; preds = %55, %49
br label %62
62: ; preds = %61
%63 = load i32, i32* %1
%64 = add nsw i32 %63, 1
store i32 %64, i32* %1
br label %45
65: ; preds = %45
ret i32 0
}

LLVM IR (-O2):
@alloc_free_chain.p = internal unnamed_addr global [1000 x i8*] zeroinitializer
@str = [15 x i8] c"Error in alloc\00"
define i32 @alloc_free_chain() {
%1 = tail call i32 (...) @rand()
%2 = srem i32 %1, 20
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @alloc_free_chain()
br label %6
6: ; preds = %4, %0
%7 = tail call i32 (...) @rand()
%8 = srem i32 %7, 1000
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %42
10: ; preds = %6
%11 = zext i32 %8 to i64
br label %15
12: ; preds = %29
br i1 %9, label %13, label %42
13: ; preds = %12
%14 = zext i32 %8 to i64
br label %32
15: ; preds = %10, %29
%16 = phi i64 [ 0, %10 ], [ %30, %29 ]
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 5
%19 = icmp eq i32 %18, 0
%20 = tail call i32 (...) @rand()
%21 = select i1 %19, i32 8192, i32 32
%22 = srem i32 %20, %21
%23 = tail call i64 @mmalloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* @alloc_free_chain.p, i64 0, i64 %16
store i8* %24, i8** %25
%26 = icmp eq i64 %23, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %15, %27
%30 = add nuw nsw i64 %16, 1
%31 = icmp eq i64 %30, %11
br i1 %31, label %12, label %15
32: ; preds = %13, %39
%33 = phi i64 [ 0, %13 ], [ %40, %39 ]
%34 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* @alloc_free_chain.p, i64 0, i64 %33
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = tail call i32 @mfree(i8* %35)
br label %39
39: ; preds = %32, %37
%40 = add nuw nsw i64 %33, 1
%41 = icmp eq i64 %40, %14
br i1 %41, label %42, label %32
42: ; preds = %39, %6, %12
ret i32 0
}