int main(int argc, char *argv[]) {
  char *char_ptr;
  int *int_ptr;
  int mem_size;
  if (argc < 2)
    mem_size = 50;
  else
    mem_size = atoi(argv[1]);
  printf("\t[+] allocating %d bytes of memory on the heap for char_ptr\n",
         mem_size);
  char_ptr = (char *)errorchecked_malloc(mem_size);
  strcpy(char_ptr, "This is memory is located on the heap.");
  printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
  printf("\t[+] allocating 12 bytes of memory on the heap for int_ptr\n");
  int_ptr = (int *)errorchecked_malloc(12);
  *int_ptr = 31337;
  printf("int_ptr (%p) --> %d\n", int_ptr, *int_ptr);
  printf("\t[-] freeing char_ptr's heap memory...\n");
  free(char_ptr);
  printf("\t[+] allocating another 15 bytes for char_ptr\n");
  char_ptr = (char *)errorchecked_malloc(15);
  strcpy(char_ptr, "new memory");
  printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
  printf("\t[-] freeing int_ptr's heap memory...\n");
  free(int_ptr);
  printf("\t[-] freeing char_ptr's heap memory...\n");
  free(char_ptr);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    117c <main+0x2c>
movl   $0x32,-0x24(%rbp)
jmp    118e <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x0,%al
call   1060 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    $0x0,%al
call   1070 <errorchecked_malloc@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xc,%edi
mov    $0x0,%al
call   1070 <errorchecked_malloc@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x7a69,(%rax)
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xf,%edi
mov    $0x0,%al
call   1070 <errorchecked_malloc@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xecb(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x32,%ebx
cmp    $0x2,%edi
jl     116f <main+0x1f>
mov    0x8(%rsi),%rdi
xor    %eax,%eax
call   1060 <atoi@plt>
mov    %eax,%ebx
lea    0xe8a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
xor    %eax,%eax
call   1070 <errorchecked_malloc@plt>
movslq %eax,%rbx
movups 0xeab(%rip),%xmm0
movups %xmm0,(%rbx)
movups 0xeb1(%rip),%xmm0
movups %xmm0,0x10(%rbx)
movabs $0x2e706165682065,%rax
mov    %rax,0x1f(%rbx)
lea    0xeaf(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xed0(%rip),%rdi
call   1040 <puts@plt>
mov    $0xc,%edi
xor    %eax,%eax
call   1070 <errorchecked_malloc@plt>
movslq %eax,%r15
movl   $0x7a69,(%r15)
lea    0xe8e(%rip),%rdi
mov    %r15,%rsi
mov    $0x7a69,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf27(%rip),%r12
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xebc(%rip),%rdi
call   1040 <puts@plt>
mov    $0xf,%edi
xor    %eax,%eax
call   1070 <errorchecked_malloc@plt>
movslq %eax,%rbx
movabs $0x6f6d656d2077656e,%rax
65 6d 6f
mov    %rax,(%rbx)
movl   $0x79726f,0x7(%rbx)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeab(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [61 x i8] c"\09[+] allocating %d bytes of memory on the heap for char_ptr\0A\00"
@.str.1 = [39 x i8] c"This is memory is located on the heap.\00"
@.str.2 = [24 x i8] c"char_ptr (%p) --> '%s'\0A\00"
@.str.3 = [60 x i8] c"\09[+] allocating 12 bytes of memory on the heap for int_ptr\0A\00"
@.str.4 = [21 x i8] c"int_ptr (%p) --> %d\0A\00"
@.str.5 = [40 x i8] c"\09[-] freeing char_ptr's heap memory...\0A\00"
@.str.6 = [47 x i8] c"\09[+] allocating another 15 bytes for char_ptr\0A\00"
@.str.7 = [11 x i8] c"new memory\00"
@.str.8 = [39 x i8] c"\09[-] freeing int_ptr's heap memory...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 50, i32* %8
br label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) bitcast (i32 (...)* @atoi to i32 (i8*, ...)*)(i8* %15)
store i32 %16, i32* %8
br label %17
17: ; preds = %12, %11
%18 = load i32, i32* %8
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %8
%21 = call i32 (i32, ...) bitcast (i32 (...)* @errorchecked_malloc to i32 (i32, ...)*)(i32 %20)
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = call i8* @strcpy(i8* %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%26 = load i8*, i8** %6
%27 = load i8*, i8** %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 (i32, ...) bitcast (i32 (...)* @errorchecked_malloc to i32 (i32, ...)*)(i32 12)
%31 = sext i32 %30 to i64
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %7
%33 = load i32*, i32** %7
store i32 31337, i32* %33
%34 = load i32*, i32** %7
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32* %34, i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%39 = load i8*, i8** %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 (i32, ...) bitcast (i32 (...)* @errorchecked_malloc to i32 (i32, ...)*)(i32 15)
%42 = sext i32 %41 to i64
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %6
%44 = load i8*, i8** %6
%45 = call i8* @strcpy(i8* %44, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%46 = load i8*, i8** %6
%47 = load i8*, i8** %6
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.8, i64 0, i64 0))
%50 = load i32*, i32** %7
%51 = bitcast i32* %50 to i8*
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%53 = load i8*, i8** %6
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [61 x i8] c"\09[+] allocating %d bytes of memory on the heap for char_ptr\0A\00"
@.str.1 = [39 x i8] c"This is memory is located on the heap.\00"
@.str.2 = [24 x i8] c"char_ptr (%p) --> '%s'\0A\00"
@.str.4 = [21 x i8] c"int_ptr (%p) --> %d\0A\00"
@.str.7 = [11 x i8] c"new memory\00"
@str = [59 x i8] c"\09[+] allocating 12 bytes of memory on the heap for int_ptr\00"
@str.10 = [46 x i8] c"\09[+] allocating another 15 bytes for char_ptr\00"
@str.11 = [38 x i8] c"\09[-] freeing int_ptr's heap memory...\00"
@str.12 = [39 x i8] c"\09[-] freeing char_ptr's heap memory...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 (i8*, ...) bitcast (i32 (...)* @atoi to i32 (i8*, ...)*)(i8* %6)
br label %8
8: ; preds = %2, %4
%9 = phi i32 [ %7, %4 ], [ 50, %2 ]
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 (i32, ...) bitcast (i32 (...)* @errorchecked_malloc to i32 (i32, ...)*)(i32 %9)
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(39) %13, i8* align 1 dereferenceable(39) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 39, i1 false)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 (i32, ...) bitcast (i32 (...)* @errorchecked_malloc to i32 (i32, ...)*)(i32 12)
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
store i32 31337, i32* %18
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32* %18, i32 31337)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.12, i64 0, i64 0))
tail call void @free(i8* %13)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.10, i64 0, i64 0))
%22 = tail call i32 (i32, ...) bitcast (i32 (...)* @errorchecked_malloc to i32 (i32, ...)*)(i32 15)
%23 = sext i32 %22 to i64
%24 = inttoptr i64 %23 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(11) %24, i8* align 1 dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 11, i1 false)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %24)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.11, i64 0, i64 0))
%27 = inttoptr i64 %17 to i8*
tail call void @free(i8* %27)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.12, i64 0, i64 0))
tail call void @free(i8* %24)
ret i32 0
}