#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 */

/* Variables and functions */
void *malloc(size_t);
void *sp_aligned_malloc(size_t len) {
  void *ptr, *aligned_ptr;
  size_t addr;
  ptr = malloc(len + 64);
  if (ptr == NULL)
    return NULL;
  addr = (size_t)ptr;
  addr = 64 - (addr % 64);
  aligned_ptr = (void *)((char *)ptr + addr);
  *((void **)aligned_ptr - 1) = ptr;
  return aligned_ptr;
}


x86 assembly (clang, -O0):
<sp_aligned_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x40,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1145 <sp_aligned_malloc+0x35>
movq   $0x0,-0x8(%rbp)
jmp    1181 <sp_aligned_malloc+0x71>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
and    $0x3f,%rcx
mov    $0x40,%eax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x8(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sp_aligned_malloc>:
push   %rax
add    $0x40,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     1130 <sp_aligned_malloc+0x20>
mov    %rax,%rcx
and    $0xffffffffffffffc0,%rax
mov    %rcx,0x38(%rax)
add    $0x40,%rax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
define i8* @sp_aligned_malloc(i64 %0) {
%2 = add i64 %0, 64
%3 = tail call i8* @malloc(i64 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %1
%6 = ptrtoint i8* %3 to i64
%7 = and i64 %6, 63
%8 = sub nuw nsw i64 64, %7
%9 = getelementptr inbounds i8, i8* %3, i64 %8
%10 = getelementptr inbounds i8, i8* %9, i64 -8
%11 = bitcast i8* %10 to i8**
store i8* %3, i8** %11
br label %12
12: ; preds = %1, %5
%13 = phi i8* [ %9, %5 ], [ null, %1 ]
ret i8* %13
}