#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ int32_t;

/* Variables and functions */
void *malloc(size_t);
void *__attribute__((malloc)) __dpl_malloc_smart(size_t n) {
  int32_t *p;
  char *cp;
  void *ptr = NULL;
  ptr = malloc(n + sizeof(int32_t) + 1);
  if (ptr) {
    cp = (char *)ptr;
    cp[0] = 0x01;
    cp++;
    p = (int32_t *)cp;
    p[0] = (int32_t)n;
    p++;
  } else {
  }
  return (void *)p;
}


x86 assembly (clang, -O0):
<__dpl_malloc_smart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1183 <__dpl_malloc_smart+0x73>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    1188 <__dpl_malloc_smart+0x78>
jmp    1188 <__dpl_malloc_smart+0x78>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__dpl_malloc_smart>:
push   %rbx
mov    %rdi,%rbx
add    $0x9,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     112f <__dpl_malloc_smart+0x1f>
movb   $0x1,(%rax)
mov    %rbx,0x1(%rax)
add    $0x9,%rax
pop    %rbx
ret
pop    %rbx
ret

LLVM IR (-O0):
define noalias i8* @__dpl_malloc_smart(i64 %0) {
%2 = alloca i64
%3 = alloca i64*
%4 = alloca i8*
%5 = alloca i8*
store i64 %0, i64* %2
store i8* null, i8** %5
%6 = load i64, i64* %2
%7 = add i64 %6, 8
%8 = add i64 %7, 1
%9 = call i8* @malloc(i64 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %25
12: ; preds = %1
%13 = load i8*, i8** %5
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 1, i8* %15
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = bitcast i8* %18 to i64*
store i64* %19, i64** %3
%20 = load i64, i64* %2
%21 = load i64*, i64** %3
%22 = getelementptr inbounds i64, i64* %21, i64 0
store i64 %20, i64* %22
%23 = load i64*, i64** %3
%24 = getelementptr inbounds i64, i64* %23, i32 1
store i64* %24, i64** %3
br label %26
25: ; preds = %1
br label %26
26: ; preds = %25, %12
%27 = load i64*, i64** %3
%28 = bitcast i64* %27 to i8*
ret i8* %28
}

LLVM IR (-O2):
define noalias i8* @__dpl_malloc_smart(i64 %0) {
%2 = add i64 %0, 9
%3 = tail call i8* @malloc(i64 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %1
store i8 1, i8* %3
%6 = getelementptr inbounds i8, i8* %3, i64 1
%7 = bitcast i8* %6 to i64*
store i64 %0, i64* %7
%8 = getelementptr inbounds i8, i8* %3, i64 9
br label %9
9: ; preds = %1, %5
%10 = phi i8* [ %8, %5 ], [ undef, %1 ]
ret i8* %10
}