void freemem(void *vp) {
  char *p = vp;
  int size;
  if (!p)
    return;
  size = get_mem_size(vp);
  checkmem(p, size);
  p -= (strlen("_mem_begin_") + 1 + sizeof(int));
  strcpy(p, "__free_");
  strcpy(p + size +
             (strlen("_mem_begin_") + 1 + sizeof(int) + strlen("mem_end") + 1) -
             strlen("mem_end") - 1,
         "__free_");
  free(p);
}


x86 assembly (clang, -O0):
<freemem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <freemem+0x24>
jmp    11c8 <freemem+0x88>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1060 <get_mem_size@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1050 <checkmem@plt>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffff0,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
add    $0x18,%rdi
add    $0xfffffffffffffff9,%rdi
add    $0xffffffffffffffff,%rdi
lea    0xe46(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freemem>:
test   %rdi,%rdi
je     1174 <freemem+0x44>
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <get_mem_size@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <checkmem@plt>
lea    -0x10(%rbx),%rdi
movabs $0x5f656572665f5f,%rax
mov    %rax,-0x10(%rbx)
movslq %ebp,%rcx
mov    %rax,(%rbx,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <free@plt>
ret

LLVM IR (-O0):
@.str = [8 x i8] c"__free_\00"
define void @freemem(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
br label %28
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 (i8*, ...) bitcast (i32 (...)* @get_mem_size to i32 (i8*, ...)*)(i8* %10)
store i32 %11, i32* %4
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @checkmem to i32 (i8*, i32, ...)*)(i8* %12, i32 %13)
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 -16
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = call i8* @strcpy(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 24
%24 = getelementptr inbounds i8, i8* %23, i64 -7
%25 = getelementptr inbounds i8, i8* %24, i64 -1
%26 = call i8* @strcpy(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%27 = load i8*, i8** %3
br label %28
28: ; preds = %9, %8
ret void
}

LLVM IR (-O2):
define void @freemem(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %14, label %3
3: ; preds = %1
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @get_mem_size to i32 (i8*, ...)*)(i8* %0)
%5 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @checkmem to i32 (i8*, i32, ...)*)(i8* %0, i32 %4)
%6 = getelementptr inbounds i8, i8* %0, i64 -16
%7 = bitcast i8* %6 to i64*
store i64 26851609172926303, i64* %7
%8 = sext i32 %4 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = getelementptr inbounds i8, i8* %9, i64 24
%11 = getelementptr inbounds i8, i8* %10, i64 -7
%12 = getelementptr inbounds i8, i8* %11, i64 -1
%13 = bitcast i8* %12 to i64*
store i64 26851609172926303, i64* %13
tail call void @free(i8* %6)
br label %14
14: ; preds = %1, %3
ret void
}