#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*/ exit(int);
int /*<<< orphan*/ free(void *);
void *malloc(int);
void *do_work(void *ptr) {
  void **p = malloc(10000000 * sizeof(void *));
  for (size_t i = 0; i < 10000000; i++) {
    p[i] = malloc(16);
    if (!p[i]) {
      exit(1);
    }
  }
  for (size_t i = 0; i < 10000000; i++) {
    free(p[i]);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<do_work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x4c4b400,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x989680,-0x18(%rbp)
jae    11ac <do_work+0x7c>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1196 <do_work+0x66>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    119b <do_work+0x6b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1152 <do_work+0x22>
movq   $0x0,-0x20(%rbp)
cmpq   $0x989680,-0x20(%rbp)
jae    11e4 <do_work+0xb4>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b4 <do_work+0x84>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_work>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x4c4b400,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
test   %rax,%rax
je     119f <do_work+0x6f>
add    $0x1,%rbx
cmp    $0x989680,%rbx
jne    1150 <do_work+0x20>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    $0x989680,%rbx
jne    1180 <do_work+0x50>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
define i8* @do_work(i8* %0) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = call i8* @malloc(i32 80000000)
%7 = bitcast i8* %6 to i8**
store i8** %7, i8*** %3
store i64 0, i64* %4
br label %8
8: ; preds = %24, %1
%9 = load i64, i64* %4
%10 = icmp ult i64 %9, 10000000
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = call i8* @malloc(i32 16)
%13 = load i8**, i8*** %3
%14 = load i64, i64* %4
%15 = getelementptr inbounds i8*, i8** %13, i64 %14
store i8* %12, i8** %15
%16 = load i8**, i8*** %3
%17 = load i64, i64* %4
%18 = getelementptr inbounds i8*, i8** %16, i64 %17
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %11
br label %24
24: ; preds = %23
%25 = load i64, i64* %4
%26 = add i64 %25, 1
store i64 %26, i64* %4
br label %8
27: ; preds = %8
store i64 0, i64* %5
br label %28
28: ; preds = %37, %27
%29 = load i64, i64* %5
%30 = icmp ult i64 %29, 10000000
br i1 %30, label %31, label %40
31: ; preds = %28
%32 = load i8**, i8*** %3
%33 = load i64, i64* %5
%34 = getelementptr inbounds i8*, i8** %32, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %31
%38 = load i64, i64* %5
%39 = add i64 %38, 1
store i64 %39, i64* %5
br label %28
40: ; preds = %28
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
define i8* @do_work(i8* readnone returned %0) {
%2 = tail call dereferenceable_or_null(80000000) i8* @malloc(i32 80000000)
%3 = bitcast i8* %2 to i8**
br label %7
4: ; preds = %7
%5 = add nuw nsw i64 %8, 1
%6 = icmp eq i64 %5, 10000000
br i1 %6, label %15, label %7
7: ; preds = %1, %4
%8 = phi i64 [ 0, %1 ], [ %5, %4 ]
%9 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%10 = getelementptr inbounds i8*, i8** %3, i64 %8
store i8* %9, i8** %10
%11 = icmp eq i8* %9, null
br i1 %11, label %12, label %4
12: ; preds = %7
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %15
ret i8* %0
15: ; preds = %4, %15
%16 = phi i64 [ %20, %15 ], [ 0, %4 ]
%17 = getelementptr inbounds i8*, i8** %3, i64 %16
%18 = load i8*, i8** %17
%19 = tail call i32 @free(i8* %18)
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, 10000000
br i1 %21, label %14, label %15
}