#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(unsigned long *);
unsigned long *malloc(int);
int /*<<< orphan*/ printf(char *);
int main() {
  int i;
  unsigned long *p, *j;
  j = p = malloc(4096 * 4);
  for (i = 0; i < 4096; i++) {
    *p = 0;
    p++;
  }
  p = j;
  for (i = 0; i < 4096; i++) {
    *p = (int)malloc(1024 * 4);
    if (!(*p))
      break;
    p++;
  }
  p = j;
  for (i = 0; i < 4096; i++) {
    free((void *)(*p));
    p++;
  }
  free(j);
  printf("mkfragment success\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x4000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x1000,-0x8(%rbp)
jge    118a <main+0x5a>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1158 <main+0x28>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x1000,-0x8(%rbp)
jge    11e7 <main+0xb7>
mov    $0x1000,%edi
call   1050 <malloc@plt>
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11cd <main+0x9d>
jmp    11e7 <main+0xb7>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1199 <main+0x69>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x1000,-0x8(%rbp)
jge    1229 <main+0xf9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11f6 <main+0xc6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x4000,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
mov    $0x8000,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1000,%edi
call   1060 <malloc@plt>
movslq %eax,%rcx
shl    $0x20,%rax
mov    %rcx,(%r14,%rbx,8)
je     1196 <main+0x56>
lea    0x1(%rbx),%rax
cmp    $0xfff,%ebx
mov    %rax,%rbx
jb     1170 <main+0x30>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    $0x1000,%ebx
jne    11a0 <main+0x60>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe3c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@str = [19 x i8] c"mkfragment success\00"
define i32 @main() {
%1 = tail call i64* @malloc(i32 16384)
%2 = bitcast i64* %1 to i8*
br label %3
3: ; preds = %0, %3
%4 = phi i32 [ %12, %3 ], [ 0, %0 ]
%5 = phi i64* [ %11, %3 ], [ %1, %0 ]
%6 = tail call i64* @malloc(i32 4096)
%7 = ptrtoint i64* %6 to i64
%8 = shl i64 %7, 32
%9 = ashr exact i64 %8, 32
store i64 %9, i64* %5
%10 = icmp ne i64 %8, 0
%11 = getelementptr inbounds i64, i64* %5, i64 1
%12 = add nuw nsw i32 %4, 1
%13 = icmp ult i32 %4, 4095
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %3, label %15
15: ; preds = %3, %15
%16 = phi i32 [ %22, %15 ], [ 0, %3 ]
%17 = phi i64* [ %21, %15 ], [ %1, %3 ]
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to i64*
%20 = tail call i32 @free(i64* %19)
%21 = getelementptr inbounds i64, i64* %17, i64 1
%22 = add nuw nsw i32 %16, 1
%23 = icmp eq i32 %22, 4096
br i1 %23, label %24, label %15
24: ; preds = %15
%25 = tail call i32 @free(i64* %1)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
ret i32 0
}