#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 */
void *malloc(int);
int /*<<< orphan*/ printf(char *, int);
int random();
void alloc(void **pool, int amount) {
  int i;
  printf("Alloc %d\n", amount);
  for (i = 0; i < 1024; i++) {
    if (pool[i] == NULL) {
      pool[i] = malloc(random() % (1024 * 1024));
      amount--;
      if (amount == 0) {
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xeb7(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x400,-0x10(%rbp)
jge    11ca <alloc+0x9a>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11b7 <alloc+0x87>
mov    $0x0,%al
call   1040 <random@plt>
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11b2 <alloc+0x82>
jmp    11ca <alloc+0x9a>
jmp    11b7 <alloc+0x87>
jmp    11bc <alloc+0x8c>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1155 <alloc+0x25>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xec0(%rip),%rdi
call   1030 <printf@plt>
xor    %ebx,%ebx
jmp    115d <alloc+0x2d>
nopl   0x0(%rax)
add    $0x1,%rbx
cmp    $0x400,%rbx
je     118e <alloc+0x5e>
cmpq   $0x0,(%r14,%rbx,8)
jne    1150 <alloc+0x20>
xor    %eax,%eax
call   1040 <random@plt>
lea    0xfffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfff00000,%ecx
sub    %ecx,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14,%rbx,8)
add    $0xffffffff,%ebp
jne    1150 <alloc+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Alloc %d\0A\00"
define void @alloc(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 0, i32* %5
br label %8
8: ; preds = %33, %2
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 1024
br i1 %10, label %11, label %36
11: ; preds = %8
%12 = load i8**, i8*** %3
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8*, i8** %12, i64 %14
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %32
18: ; preds = %11
%19 = call i32 (...) @random()
%20 = srem i32 %19, 1048576
%21 = call i8* @malloc(i32 %20)
%22 = load i8**, i8*** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
store i8* %21, i8** %25
%26 = load i32, i32* %4
%27 = add nsw i32 %26, -1
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %18
br label %36
31: ; preds = %18
br label %32
32: ; preds = %31, %11
br label %33
33: ; preds = %32
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %8
36: ; preds = %30, %8
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"Alloc %d\0A\00"
define void @alloc(i8** nocapture %0, i32 %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %4
4: ; preds = %2, %16
%5 = phi i64 [ 0, %2 ], [ %18, %16 ]
%6 = phi i32 [ %1, %2 ], [ %17, %16 ]
%7 = getelementptr inbounds i8*, i8** %0, i64 %5
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %16
10: ; preds = %4
%11 = tail call i32 (...) @random()
%12 = srem i32 %11, 1048576
%13 = tail call i8* @malloc(i32 %12)
store i8* %13, i8** %7
%14 = add nsw i32 %6, -1
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %4, %10
%17 = phi i32 [ %14, %10 ], [ %6, %4 ]
%18 = add nuw nsw i64 %5, 1
%19 = icmp eq i64 %18, 1024
br i1 %19, label %20, label %4
20: ; preds = %10, %16
ret void
}