void checkmem(char *p, int size) {
  char *begin;
  char *end;
  begin = p - strlen("_mem_begin_") - 1 - sizeof(int);
  if (strcmp(begin, "_mem_begin_"))
    die("checkmem: memory corrupted - invalid head sign");
  if (*(int *)(begin + strlen("_mem_begin_") + 1) != size)
    die("checkmem: memory corrupted - invalid size");
  end = begin + size +
        (strlen("_mem_begin_") + 1 + sizeof(int) + strlen("mem_end") + 1) -
        strlen("mem_end") - 1;
  if (strcmp(end, "mem_end"))
    die("checkmem: memory corrupted - invalid end sign");
}


x86 assembly (clang, -O0):
<checkmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0xfffffffffffffff5,%rax
add    $0xffffffffffffffff,%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
je     116a <checkmem+0x4a>
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <die@plt>
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%eax
cmp    -0xc(%rbp),%eax
je     1188 <checkmem+0x68>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <die@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0x18,%rax
add    $0xfffffffffffffff9,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xeb7(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
je     11ca <checkmem+0xaa>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <die@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkmem>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    -0x10(%rdi),%rbx
lea    0xecb(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     114f <checkmem+0x2f>
lea    0xec4(%rip),%rdi
xor    %eax,%eax
call   1040 <die@plt>
cmp    %r14d,-0x4(%rbp)
je     1163 <checkmem+0x43>
lea    0xedf(%rip),%rdi
xor    %eax,%eax
call   1040 <die@plt>
movslq %r14d,%rax
lea    (%rbx,%rax,1),%rdi
add    $0x10,%rdi
lea    0xef0(%rip),%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
je     1190 <checkmem+0x70>
lea    0xee8(%rip),%rdi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <die@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"_mem_begin_\00"
@.str.1 = [47 x i8] c"checkmem: memory corrupted - invalid head sign\00"
@.str.2 = [42 x i8] c"checkmem: memory corrupted - invalid size\00"
@.str.3 = [8 x i8] c"mem_end\00"
@.str.4 = [46 x i8] c"checkmem: memory corrupted - invalid end sign\00"
define void @checkmem(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 -11
%9 = getelementptr inbounds i8, i8* %8, i64 -1
%10 = getelementptr inbounds i8, i8* %9, i64 -4
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @strcmp(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 11
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
%22 = load i32, i32* %4
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
%27 = load i8*, i8** %5
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = getelementptr inbounds i8, i8* %30, i64 24
%32 = getelementptr inbounds i8, i8* %31, i64 -7
%33 = getelementptr inbounds i8, i8* %32, i64 -1
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 @strcmp(i8* %34, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %26
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"_mem_begin_\00"
@.str.1 = [47 x i8] c"checkmem: memory corrupted - invalid head sign\00"
@.str.2 = [42 x i8] c"checkmem: memory corrupted - invalid size\00"
@.str.3 = [8 x i8] c"mem_end\00"
@.str.4 = [46 x i8] c"checkmem: memory corrupted - invalid end sign\00"
define void @checkmem(i8* nocapture %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 -16
%4 = tail call i32 @strcmp(i8* %3, i8* dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8, i8* %0, i64 -4
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, %1
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = tail call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = sext i32 %1 to i64
%17 = getelementptr inbounds i8, i8* %3, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 24
%19 = getelementptr inbounds i8, i8* %18, i64 -7
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = tail call i32 @strcmp(i8* %20, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = tail call i32 (i8*, ...) bitcast (i32 (...)* @die to i32 (i8*, ...)*)(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
ret void
}