#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*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *, void *);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int main() {
  int i;
  srand(0);
  void *p = malloc(1);
  for (i = 0; i < 10000; ++i) {
    malloc(rand() % 1000);
    usleep(100);
  }
  printf("malloc: %p\n", p);
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1050 <srand@plt>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jge    11c6 <main+0x66>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1060 <malloc@plt>
mov    $0x64,%edi
call   1080 <usleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    118b <main+0x2b>
mov    -0x10(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %edi,%edi
call   1050 <srand@plt>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0x2710,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <rand@plt>
mov    $0x64,%edi
call   1080 <usleep@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x20>
lea    0xe63(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"malloc: %p\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 @srand(i32 0)
%5 = call i8* @malloc(i32 1)
store i8* %5, i8** %3
store i32 0, i32* %2
br label %6
6: ; preds = %14, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 10000
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = call i32 (...) @rand()
%11 = srem i32 %10, 1000
%12 = call i8* @malloc(i32 %11)
%13 = call i32 @usleep(i32 100)
br label %14
14: ; preds = %9
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
store i32 %16, i32* %2
br label %6
17: ; preds = %6
%18 = load i8*, i8** %3
%19 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %3
%21 = call i32 @free(i8* %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"malloc: %p\0A\00"
define i32 @main() {
%1 = tail call i32 @srand(i32 0)
%2 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
br label %3
3: ; preds = %0, %3
%4 = phi i32 [ 0, %0 ], [ %7, %3 ]
%5 = tail call i32 (...) @rand()
%6 = tail call i32 @usleep(i32 100)
%7 = add nuw nsw i32 %4, 1
%8 = icmp eq i32 %7, 10000
br i1 %8, label %9, label %3
9: ; preds = %3
%10 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %2)
%11 = tail call i32 @free(i8* %2)
ret i32 0
}