#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);
int posix_memalign(void **, size_t, size_t);
void *malloc_aligned(size_t len, unsigned alignment) {
  if (!alignment) {
    return malloc(len);
  } else {
    void *h;
    int ret = posix_memalign(&h, (size_t)(alignment * sizeof(float)),
                             len + alignment);
    if (ret != 0 || h == NULL)
      return NULL;
    return (void *)((char *)h + alignment - 2 * sizeof(float));
  }
}


x86 assembly (clang, -O0):
<malloc_aligned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    114b <malloc_aligned+0x2b>
mov    -0x10(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    119e <malloc_aligned+0x7e>
mov    -0x14(%rbp),%eax
mov    %eax,%esi
shl    $0x2,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%eax
add    %rax,%rdx
lea    -0x20(%rbp),%rdi
call   1040 <posix_memalign@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    117f <malloc_aligned+0x5f>
cmpq   $0x0,-0x20(%rbp)
jne    118c <malloc_aligned+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    119e <malloc_aligned+0x7e>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
add    $0xfffffffffffffff8,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_aligned>:
mov    %rdi,%rdx
test   %esi,%esi
je     1165 <malloc_aligned+0x45>
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
lea    0x0(,%rbx,4),%rsi
add    %rbx,%rdx
lea    0x8(%rsp),%rdi
call   1040 <posix_memalign@plt>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
lea    -0x8(%rcx,%rbx,1),%rdx
cmove  %rcx,%rdx
xor    %ecx,%ecx
test   %eax,%eax
cmove  %rdx,%rcx
mov    %rcx,%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    %rdx,%rdi
jmp    1030 <malloc@plt>

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

LLVM IR (-O2):
define i8* @malloc_aligned(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @malloc(i64 %0)
br label %20
7: ; preds = %2
%8 = bitcast i8** %3 to i8*
%9 = zext i32 %1 to i64
%10 = shl nuw nsw i64 %9, 2
%11 = add i64 %9, %0
%12 = call i32 @posix_memalign(i8** %3, i64 %10, i64 %11)
%13 = icmp ne i32 %12, 0
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
%16 = select i1 %13, i1 true, i1 %15
%17 = getelementptr inbounds i8, i8* %14, i64 %9
%18 = getelementptr inbounds i8, i8* %17, i64 -8
%19 = select i1 %16, i8* null, i8* %18
br label %20
20: ; preds = %7, %5
%21 = phi i8* [ %19, %7 ], [ %6, %5 ]
ret i8* %21
}