#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(int *);
int *malloc(unsigned int);
int /*<<< orphan*/ memset(char *, char, unsigned int);
int /*<<< orphan*/ printf(char *);
int main() {
  unsigned int i;
  char *p;
  free(malloc(1));
  int *k;
  k = (int *)malloc(10000 * sizeof(int));
  printf("Alloc\n");
  for (i = 4000; i < 5000; i++) {
    p = malloc(i);
    memset(p, 'A', i);
    k[i] = (unsigned int)p;
  }
  printf("Freeing\n");
  for (i = 4000; i < 5000; i++) {
    free((void *)k[i]);
  }
  free(k);
  printf("OK\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x9c40,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfa0,-0x8(%rbp)
cmpl   $0x1388,-0x8(%rbp)
jae    11ca <main+0x8a>
mov    -0x8(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x41,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1182 <main+0x42>
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfa0,-0x8(%rbp)
cmpl   $0x1388,-0x8(%rbp)
jae    1208 <main+0xc8>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
movslq (%rax,%rcx,4),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11dd <main+0x9d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x9c40,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfa0,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x41,%esi
mov    %ebx,%edx
call   1050 <memset@plt>
mov    %r15d,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    $0x1388,%rbx
jne    1180 <main+0x40>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfa0,%ebx
nopl   0x0(%rax,%rax,1)
movslq (%r14,%rbx,4),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    $0x1388,%rbx
jne    11c0 <main+0x80>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe29(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"Alloc\0A\00"
@.str.1 = [9 x i8] c"Freeing\0A\00"
@.str.2 = [4 x i8] c"OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32* @malloc(i32 1)
%6 = call i32 @free(i32* %5)
%7 = call i32* @malloc(i32 40000)
store i32* %7, i32** %4
%8 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 4000, i32* %2
br label %9
9: ; preds = %25, %0
%10 = load i32, i32* %2
%11 = icmp ult i32 %10, 5000
br i1 %11, label %12, label %28
12: ; preds = %9
%13 = load i32, i32* %2
%14 = call i32* @malloc(i32 %13)
%15 = bitcast i32* %14 to i8*
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = load i32, i32* %2
%18 = call i32 @memset(i8* %16, i8 signext 65, i32 %17)
%19 = load i8*, i8** %3
%20 = ptrtoint i8* %19 to i32
%21 = load i32*, i32** %4
%22 = load i32, i32* %2
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
store i32 %20, i32* %24
br label %25
25: ; preds = %12
%26 = load i32, i32* %2
%27 = add i32 %26, 1
store i32 %27, i32* %2
br label %9
28: ; preds = %9
%29 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 4000, i32* %2
br label %30
30: ; preds = %43, %28
%31 = load i32, i32* %2
%32 = icmp ult i32 %31, 5000
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load i32*, i32** %4
%35 = load i32, i32* %2
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = inttoptr i64 %39 to i8*
%41 = bitcast i8* %40 to i32*
%42 = call i32 @free(i32* %41)
br label %43
43: ; preds = %33
%44 = load i32, i32* %2
%45 = add i32 %44, 1
store i32 %45, i32* %2
br label %30
46: ; preds = %30
%47 = load i32*, i32** %4
%48 = call i32 @free(i32* %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@str = [6 x i8] c"Alloc\00"
@str.3 = [8 x i8] c"Freeing\00"
@str.4 = [3 x i8] c"OK\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 1)
%2 = tail call i32 @free(i32* %1)
%3 = tail call i32* @malloc(i32 40000)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %5
5: ; preds = %0, %5
%6 = phi i64 [ 4000, %0 ], [ %14, %5 ]
%7 = trunc i64 %6 to i32
%8 = tail call i32* @malloc(i32 %7)
%9 = bitcast i32* %8 to i8*
%10 = tail call i32 @memset(i8* %9, i8 signext 65, i32 %7)
%11 = ptrtoint i32* %8 to i64
%12 = trunc i64 %11 to i32
%13 = getelementptr inbounds i32, i32* %3, i64 %6
store i32 %12, i32* %13
%14 = add nuw nsw i64 %6, 1
%15 = icmp eq i64 %14, 5000
br i1 %15, label %16, label %5
16: ; preds = %5
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 4000, %16 ], [ %25, %18 ]
%20 = getelementptr inbounds i32, i32* %3, i64 %19
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = tail call i32 @free(i32* %23)
%25 = add nuw nsw i64 %19, 1
%26 = icmp eq i64 %25, 5000
br i1 %26, label %27, label %18
27: ; preds = %18
%28 = tail call i32 @free(i32* %3)
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.4, i64 0, i64 0))
ret i32 0
}