void *getmem(int size) {
  char *p;
  char *mem;
  p = (char *)malloc(
      (strlen("_mem_begin_") + 1 + sizeof(int) + strlen("mem_end") + 1) + size);
  if (!p)
    die("getmem: no more memory (%d)", size);
  strcpy(p, "_mem_begin_");
  p += strlen("_mem_begin_") + 1;
  *(int *)p = size;
  p += sizeof(int);
  mem = p;
  memset(mem, 0, size);
  p += size;
  strcpy(p, "mem_end");
  checkmem(mem, size);
  return mem;
}


x86 assembly (clang, -O0):
<getmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movslq -0x4(%rbp),%rdi
add    $0x18,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1188 <getmem+0x38>
mov    -0x4(%rbp),%esi
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1060 <die@plt>
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x4(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    $0x0,%al
call   1070 <checkmem@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getmem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
movslq %edi,%r14
lea    0x18(%r14),%rdi
call   1040 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    116f <getmem+0x2f>
lea    0xe9b(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <die@plt>
movabs $0x6765625f6d656d5f,%rax
62 65 67
mov    %rax,0x0(%rbp)
movl   $0x5f6e69,0x8(%rbp)
mov    %r15d,0xc(%rbp)
mov    %rbp,%rbx
add    $0x10,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
movabs $0x646e655f6d656d,%rax
mov    %rax,0x10(%rbp,%r14,1)
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <checkmem@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [28 x i8] c"getmem: no more memory (%d)\00"
@.str.1 = [12 x i8] c"_mem_begin_\00"
define i8* @getmem(i32 %0) {
%2 = sext i32 %0 to i64
%3 = add nsw i64 %2, 24
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @die to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %8
8: ; preds = %6, %1
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(12) %4, i8* align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 12, i1 false)
%9 = getelementptr inbounds i8, i8* %4, i64 12
%10 = bitcast i8* %9 to i32*
store i32 %0, i32* %10
%11 = getelementptr inbounds i8, i8* %4, i64 16
tail call void @llvm.memset.p0i8.i64(i8* align 1 %11, i8 0, i64 %2, i1 false)
%12 = getelementptr inbounds i8, i8* %11, i64 %2
%13 = bitcast i8* %12 to i64*
store i64 28268879342822765, i64* %13
%14 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @checkmem to i32 (i8*, i32, ...)*)(i8* %11, i32 %0)
ret i8* %11
}