#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);
void foo(int n_alloc, int n_free) {
  void *a[n_alloc];
  int i;
  for (i = 0; i < n_alloc; ++i)
    a[i] = malloc(i);
  for (i = 0; i < n_free; ++i)
    free(a[i]);
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    118d <foo+0x6d>
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115c <foo+0x3c>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11bf <foo+0x9f>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1194 <foo+0x74>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %edi,%r12d
mov    %rsp,%r15
lea    0xf(,%r12,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
test   %edi,%edi
jle    1164 <foo+0x44>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1150 <foo+0x30>
test   %r14d,%r14d
jle    1182 <foo+0x62>
mov    %r14d,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
mov    (%r15,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1170 <foo+0x50>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @foo(i32 %0, i32 %1) {
%3 = zext i32 %0 to i64
%4 = alloca i8*, i64 %3
%5 = icmp sgt i32 %0, 0
br i1 %5, label %10, label %6
6: ; preds = %10, %2
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %24
8: ; preds = %6
%9 = zext i32 %1 to i64
br label %17
10: ; preds = %2, %10
%11 = phi i64 [ %15, %10 ], [ 0, %2 ]
%12 = trunc i64 %11 to i32
%13 = tail call i8* @malloc(i32 %12)
%14 = getelementptr inbounds i8*, i8** %4, i64 %11
store i8* %13, i8** %14
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %3
br i1 %16, label %6, label %10
17: ; preds = %8, %17
%18 = phi i64 [ 0, %8 ], [ %22, %17 ]
%19 = getelementptr inbounds i8*, i8** %4, i64 %18
%20 = load i8*, i8** %19
%21 = tail call i32 @free(i8* %20)
%22 = add nuw nsw i64 %18, 1
%23 = icmp eq i64 %22, %9
br i1 %23, label %24, label %17
24: ; preds = %17, %6
ret void
}