#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ kr_free(char *);
void *kr_malloc(size_t);
double kr_malloc_get_fragmentation_ratio();
int kr_malloc_get_rss();
int kr_malloc_used_memory();
void *kr_realloc(void *, size_t);
char *kr_strdup(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  void *pMalloc = NULL;
  size_t malloc_size = 100;
  pMalloc = kr_malloc(malloc_size);
  assert(pMalloc != NULL);
  pMalloc = kr_realloc(pMalloc, malloc_size * 2);
  assert(pMalloc != NULL);
  kr_free(pMalloc);
  char *pStrDup = kr_strdup("Tiger");
  assert(pStrDup != NULL);
  printf("pStrDup=[%s]\n", pStrDup);
  kr_free(pStrDup);
  int i = 0;
  char *paMalloc[100];
  for (i = 0; i < 100; i++) {
    paMalloc[i] = (char *)kr_malloc(malloc_size * i);
    assert(paMalloc[i] != NULL);
  }
  printf("used memory:[%u], fragmentation_ratio:[%f], rss:[%u]\n",
         kr_malloc_used_memory(), kr_malloc_get_fragmentation_ratio(),
         kr_malloc_get_rss());
  for (i = 0; i < 100; i++) {
    kr_free(paMalloc[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x64,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <kr_malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
call   1060 <kr_realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <kr_free@plt>
lea    0xdf8(%rip),%rdi
call   1080 <kr_strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <kr_free@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x64,-0x24(%rbp)
jge    129e <main+0x10e>
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
imul   %rax,%rdi
call   1030 <kr_malloc@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
mov    %rcx,-0x350(%rbp,%rax,8)
ff
movslq -0x24(%rbp),%rax
cmpq   $0x0,-0x350(%rbp,%rax,8)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    124c <main+0xbc>
mov    $0x0,%al
call   1090 <kr_malloc_used_memory@plt>
mov    %eax,-0x35c(%rbp)
mov    $0x0,%al
call   1050 <kr_malloc_get_fragmentation_ratio@plt>
movsd  %xmm0,-0x358(%rbp)
ff
mov    $0x0,%al
call   10b0 <kr_malloc_get_rss@plt>
mov    -0x35c(%rbp),%esi
movsd  -0x358(%rbp),%xmm0
ff
mov    %eax,%edx
lea    0xd3c(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x64,-0x24(%rbp)
jge    130f <main+0x17f>
movslq -0x24(%rbp),%rax
mov    -0x350(%rbp,%rax,8),%rdi
ff
call   10a0 <kr_free@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12e6 <main+0x156>
xor    %eax,%eax
add    $0x360,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x330,%rsp
mov    $0x64,%edi
call   1030 <kr_malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    $0xc8,%esi
mov    %rbx,%rdi
call   1060 <kr_realloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   10a0 <kr_free@plt>
lea    0xe1d(%rip),%rdi
call   1080 <kr_strdup@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
lea    0xe06(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   10a0 <kr_free@plt>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <kr_malloc@plt>
mov    %rax,0x0(%rbp)
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
add    $0x8,%rbp
add    $0x64,%rbx
cmp    $0x2710,%rbx
jne    1220 <main+0x90>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <kr_malloc_used_memory@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <kr_malloc_get_fragmentation_ratio@plt>
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   10b0 <kr_malloc_get_rss@plt>
lea    0xda3(%rip),%rdi
mov    %ebp,%esi
movsd  0x8(%rsp),%xmm0
mov    %eax,%edx
mov    $0x1,%al
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10(%rsp,%rbx,8),%rdi
call   10a0 <kr_free@plt>
add    $0x1,%rbx
cmp    $0x64,%rbx
jne    1290 <main+0x100>
xor    %eax,%eax
add    $0x330,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"Tiger\00"
@.str.1 = [14 x i8] c"pStrDup=[%s]\0A\00"
@.str.2 = [54 x i8] c"used memory:[%u], fragmentation_ratio:[%f], rss:[%u]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [100 x i8*]
store i32 0, i32* %1
store i8* null, i8** %2
store i64 100, i64* %3
%7 = load i64, i64* %3
%8 = call i8* @kr_malloc(i64 %7)
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %2
%14 = load i64, i64* %3
%15 = mul i64 %14, 2
%16 = call i8* @kr_realloc(i8* %13, i64 %15)
store i8* %16, i8** %2
%17 = load i8*, i8** %2
%18 = icmp ne i8* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i8*, i8** %2
%22 = call i32 @kr_free(i8* %21)
%23 = call i8* @kr_strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp ne i8* %24, null
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i8*, i8** %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %4
%31 = call i32 @kr_free(i8* %30)
store i32 0, i32* %5
store i32 0, i32* %5
br label %32
32: ; preds = %51, %0
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 100
br i1 %34, label %35, label %54
35: ; preds = %32
%36 = load i64, i64* %3
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = mul i64 %36, %38
%40 = call i8* @kr_malloc(i64 %39)
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 %42
store i8* %40, i8** %43
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 %45
%47 = load i8*, i8** %46
%48 = icmp ne i8* %47, null
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
br label %51
51: ; preds = %35
%52 = load i32, i32* %5
%53 = add nsw i32 %52, 1
store i32 %53, i32* %5
br label %32
54: ; preds = %32
%55 = call i32 (...) @kr_malloc_used_memory()
%56 = call double (...) @kr_malloc_get_fragmentation_ratio()
%57 = call i32 (...) @kr_malloc_get_rss()
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %55, double %56, i32 %57)
store i32 0, i32* %5
br label %59
59: ; preds = %68, %54
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 100
br i1 %61, label %62, label %71
62: ; preds = %59
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [100 x i8*], [100 x i8*]* %6, i64 0, i64 %64
%66 = load i8*, i8** %65
%67 = call i32 @kr_free(i8* %66)
br label %68
68: ; preds = %62
%69 = load i32, i32* %5
%70 = add nsw i32 %69, 1
store i32 %70, i32* %5
br label %59
71: ; preds = %59
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"Tiger\00"
@.str.1 = [14 x i8] c"pStrDup=[%s]\0A\00"
@.str.2 = [54 x i8] c"used memory:[%u], fragmentation_ratio:[%f], rss:[%u]\0A\00"
define i32 @main() {
%1 = alloca [100 x i8*]
%2 = tail call i8* @kr_malloc(i64 100)
%3 = icmp ne i8* %2, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i8* @kr_realloc(i8* %2, i64 200)
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 @kr_free(i8* %6)
%11 = tail call i8* @kr_strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%16 = tail call i32 @kr_free(i8* %11)
%17 = bitcast [100 x i8*]* %1 to i8*
br label %18
18: ; preds = %0, %18
%19 = phi i64 [ 0, %0 ], [ %26, %18 ]
%20 = mul nuw nsw i64 %19, 100
%21 = tail call i8* @kr_malloc(i64 %20)
%22 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %19
store i8* %21, i8** %22
%23 = icmp ne i8* %21, null
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = add nuw nsw i64 %19, 1
%27 = icmp eq i64 %26, 100
br i1 %27, label %28, label %18
28: ; preds = %18
%29 = tail call i32 (...) @kr_malloc_used_memory()
%30 = tail call double (...) @kr_malloc_get_fragmentation_ratio()
%31 = tail call i32 (...) @kr_malloc_get_rss()
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %29, double %30, i32 %31)
br label %33
33: ; preds = %28, %33
%34 = phi i64 [ 0, %28 ], [ %38, %33 ]
%35 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = tail call i32 @kr_free(i8* %36)
%38 = add nuw nsw i64 %34, 1
%39 = icmp eq i64 %38, 100
br i1 %39, label %40, label %33
40: ; preds = %33
ret i32 0
}