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 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);
  free(char_ptr);
  free(int_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    0xe92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe9a(%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    0xea2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x32,%ebx
cmp    $0x2,%edi
jl     116b <main+0x1b>
mov    0x8(%rsi),%rdi
xor    %eax,%eax
call   1060 <atoi@plt>
mov    %eax,%ebx
lea    0xe8e(%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 0xeaf(%rip),%xmm0
movups %xmm0,(%rbx)
movups 0xeb5(%rip),%xmm0
movups %xmm0,0x10(%rbx)
movl   $0x706165,0x1f(%rbx)
lea    0xeb6(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xecd(%rip),%rdi
call   1040 <puts@plt>
mov    $0xc,%edi
xor    %eax,%eax
call   1070 <errorchecked_malloc@plt>
movslq %eax,%r14
movl   $0x7a69,(%r14)
lea    0xe96(%rip),%rdi
mov    %r14,%rsi
mov    $0x7a69,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 = [35 x i8] c"this is memory located on the heap\00"
@.str.2 = [22 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"
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 ([35 x i8], [35 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 ([22 x i8], [22 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 = load i8*, i8** %6
%39 = load i32*, i32** %7
%40 = bitcast i32* %39 to i8*
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [61 x i8] c"\09[+] allocating %d bytes of memory on the heap for char_ptr\0A\00"
@.str.1 = [35 x i8] c"this is memory located on the heap\00"
@.str.2 = [22 x i8] c"char_ptr (%p) --> %s\0A\00"
@.str.4 = [21 x i8] c"int_ptr (%p) --> %d\0A\00"
@str = [59 x i8] c"\09[+] allocating 12 bytes of memory on the heap for int_ptr\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(35) %13, i8* align 1 dereferenceable(35) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i64 35, i1 false)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 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)
tail call void @free(i8* %13)
%20 = inttoptr i64 %17 to i8*
tail call void @free(i8* %20)
ret i32 0
}