#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 *);
char *mmalloc(int);
int /*<<< orphan*/ printf(char *);
int rand();
int alloc_free_chain() {
  static char *p[100000];
  int i;
  int block_size;
  int rand_count;
  if ((rand() % 20) == 0)
    alloc_free_chain();
  rand_count = rand() % 100000;
  for (i = 0; i < rand_count; i++) {
    if ((rand() % 5) == 0)
      block_size = rand() % (1024 * 8);
    else
      block_size = rand() % 32;
    p[i] = 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    $0x186a0,%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   $0x14f8b589,%rdx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%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 [100000 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, 100000
store i32 %11, i32* %3
store i32 0, i32* %1
br label %12
12: ; preds = %40, %9
%13 = load i32, i32* %1
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %43
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 i8* @mmalloc(i32 %27)
%29 = load i32, i32* %1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [100000 x i8*], [100000 x i8*]* @alloc_free_chain.p, i64 0, i64 %30
store i8* %28, i8** %31
%32 = load i32, i32* %1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [100000 x i8*], [100000 x i8*]* @alloc_free_chain.p, i64 0, i64 %33
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %26
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %26
br label %40
40: ; preds = %39
%41 = load i32, i32* %1
%42 = add nsw i32 %41, 1
store i32 %42, i32* %1
br label %12
43: ; preds = %12
store i32 0, i32* %1
br label %44
44: ; preds = %61, %43
%45 = load i32, i32* %1
%46 = load i32, i32* %3
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %64
48: ; preds = %44
%49 = load i32, i32* %1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [100000 x i8*], [100000 x i8*]* @alloc_free_chain.p, i64 0, i64 %50
%52 = load i8*, i8** %51
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %60
54: ; preds = %48
%55 = load i32, i32* %1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [100000 x i8*], [100000 x i8*]* @alloc_free_chain.p, i64 0, i64 %56
%58 = load i8*, i8** %57
%59 = call i32 @mfree(i8* %58)
br label %60
60: ; preds = %54, %48
br label %61
61: ; preds = %60
%62 = load i32, i32* %1
%63 = add nsw i32 %62, 1
store i32 %63, i32* %1
br label %44
64: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
@alloc_free_chain.p = internal unnamed_addr global [100000 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, 100000
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %6
%11 = zext i32 %8 to i64
br label %15
12: ; preds = %28
br i1 %9, label %13, label %41
13: ; preds = %12
%14 = zext i32 %8 to i64
br label %31
15: ; preds = %10, %28
%16 = phi i64 [ 0, %10 ], [ %29, %28 ]
%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 i8* @mmalloc(i32 %22)
%24 = getelementptr inbounds [100000 x i8*], [100000 x i8*]* @alloc_free_chain.p, i64 0, i64 %16
store i8* %23, i8** %24
%25 = icmp eq i8* %23, null
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %15, %26
%29 = add nuw nsw i64 %16, 1
%30 = icmp eq i64 %29, %11
br i1 %30, label %12, label %15
31: ; preds = %13, %38
%32 = phi i64 [ 0, %13 ], [ %39, %38 ]
%33 = getelementptr inbounds [100000 x i8*], [100000 x i8*]* @alloc_free_chain.p, i64 0, i64 %32
%34 = load i8*, i8** %33
%35 = icmp eq i8* %34, null
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = tail call i32 @mfree(i8* %34)
br label %38
38: ; preds = %31, %36
%39 = add nuw nsw i64 %32, 1
%40 = icmp eq i64 %39, %14
br i1 %40, label %41, label %31
41: ; preds = %38, %6, %12
ret i32 0
}