#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 */
struct mem_context {
  int dummy;
};

/* Variables and functions */
void *mem_alloc(struct mem_context *, int);
int /*<<< orphan*/ mem_context_free(struct mem_context *);
struct mem_context *mem_context_new();
int /*<<< orphan*/ mem_free(struct mem_context *, void *, int);
int /*<<< orphan*/ memset(void *, int, int);
int main() {
  struct mem_context *ctx = mem_context_new();
  int i, j;
  for (i = 1; i < 400; i++) {
    void *ptrs[1025];
    for (j = 0; j < 1025; j++) {
      ptrs[j] = mem_alloc(ctx, i);
      memset(ptrs[j], 0xFF, i);
    }
    for (j = 0; j < 1025; j++) {
      mem_free(ctx, ptrs[j], i);
    }
  }
  mem_context_free(ctx);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <mem_context_new@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x190,-0x14(%rbp)
jge    1224 <main+0xd4>
movl   $0x0,-0x18(%rbp)
cmpl   $0x401,-0x18(%rbp)
jge    11d7 <main+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <mem_alloc@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
mov    %rcx,-0x2020(%rbp,%rax,8)
ff
movslq -0x18(%rbp),%rax
mov    -0x2020(%rbp,%rax,8),%rdi
ff
mov    -0x14(%rbp),%edx
mov    $0xff,%esi
call   1040 <memset@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1188 <main+0x38>
movl   $0x0,-0x18(%rbp)
cmpl   $0x401,-0x18(%rbp)
jge    1211 <main+0xc1>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
mov    -0x2020(%rbp,%rax,8),%rsi
ff
mov    -0x14(%rbp),%edx
call   1030 <mem_free@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11de <main+0x8e>
jmp    1216 <main+0xc6>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <main+0x24>
mov    -0x10(%rbp),%rdi
call   1060 <mem_context_free@plt>
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2010,%rsp
xor    %eax,%eax
call   1050 <mem_context_new@plt>
mov    %rax,%r14
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <mem_alloc@plt>
mov    %rax,(%rsp,%rbx,8)
mov    %rax,%rdi
mov    $0xff,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
add    $0x1,%rbx
cmp    $0x401,%rbx
jne    1180 <main+0x30>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%rsp,%rbx,8),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1030 <mem_free@plt>
add    $0x1,%rbx
cmp    $0x401,%rbx
jne    11b0 <main+0x60>
add    $0x1,%ebp
cmp    $0x190,%ebp
jne    1170 <main+0x20>
mov    %r14,%rdi
call   1060 <mem_context_free@plt>
xor    %eax,%eax
add    $0x2010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.mem_context = type { i32 }
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.mem_context*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1025 x i8*]
store i32 0, i32* %1
%6 = call %struct.mem_context* (...) @mem_context_new()
store %struct.mem_context* %6, %struct.mem_context** %2
store i32 1, i32* %3
br label %7
7: ; preds = %46, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 400
br i1 %9, label %10, label %49
10: ; preds = %7
store i32 0, i32* %4
br label %11
11: ; preds = %27, %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 1025
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = load %struct.mem_context*, %struct.mem_context** %2
%16 = load i32, i32* %3
%17 = call i8* @mem_alloc(%struct.mem_context* %15, i32 %16)
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1025 x i8*], [1025 x i8*]* %5, i64 0, i64 %19
store i8* %17, i8** %20
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1025 x i8*], [1025 x i8*]* %5, i64 0, i64 %22
%24 = load i8*, i8** %23
%25 = load i32, i32* %3
%26 = call i32 @memset(i8* %24, i32 255, i32 %25)
br label %27
27: ; preds = %14
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %11
30: ; preds = %11
store i32 0, i32* %4
br label %31
31: ; preds = %42, %30
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 1025
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = load %struct.mem_context*, %struct.mem_context** %2
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1025 x i8*], [1025 x i8*]* %5, i64 0, i64 %37
%39 = load i8*, i8** %38
%40 = load i32, i32* %3
%41 = call i32 @mem_free(%struct.mem_context* %35, i8* %39, i32 %40)
br label %42
42: ; preds = %34
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
store i32 %44, i32* %4
br label %31
45: ; preds = %31
br label %46
46: ; preds = %45
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %7
49: ; preds = %7
%50 = load %struct.mem_context*, %struct.mem_context** %2
%51 = call i32 @mem_context_free(%struct.mem_context* %50)
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
%struct.mem_context = type { i32 }
define i32 @main() {
%1 = alloca [1025 x i8*]
%2 = tail call %struct.mem_context* (...) @mem_context_new()
%3 = bitcast [1025 x i8*]* %1 to i8*
br label %4
4: ; preds = %0, %20
%5 = phi i32 [ 1, %0 ], [ %21, %20 ]
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %11, %6 ]
%8 = tail call i8* @mem_alloc(%struct.mem_context* %2, i32 %5)
%9 = getelementptr inbounds [1025 x i8*], [1025 x i8*]* %1, i64 0, i64 %7
store i8* %8, i8** %9
%10 = tail call i32 @memset(i8* %8, i32 255, i32 %5)
%11 = add nuw nsw i64 %7, 1
%12 = icmp eq i64 %11, 1025
br i1 %12, label %13, label %6
13: ; preds = %6, %13
%14 = phi i64 [ %18, %13 ], [ 0, %6 ]
%15 = getelementptr inbounds [1025 x i8*], [1025 x i8*]* %1, i64 0, i64 %14
%16 = load i8*, i8** %15
%17 = tail call i32 @mem_free(%struct.mem_context* %2, i8* %16, i32 %5)
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, 1025
br i1 %19, label %20, label %13
20: ; preds = %13
%21 = add nuw nsw i32 %5, 1
%22 = icmp eq i32 %21, 400
br i1 %22, label %23, label %4
23: ; preds = %20
%24 = tail call i32 @mem_context_free(%struct.mem_context* %2)
ret i32 0
}