#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 */
int posix_memalign(void **, size_t, size_t);
void *bh_align_malloc(size_t size, size_t align) {
  void *buf = NULL;
  void *ptr = NULL;
  int res;
  align = (align + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
  size = (size + align - 1) & ~(align - 1);
  res = posix_memalign(&ptr, align, size);
  buf = res ? NULL : ptr;
  return buf;
}


x86 assembly (clang, -O0):
<bh_align_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
sub    $0x1,%rax
and    $0xfffffffffffffff8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1030 <posix_memalign@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     118c <bh_align_malloc+0x7c>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1194 <bh_align_malloc+0x84>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bh_align_malloc>:
push   %rax
movq   $0x0,(%rsp)
add    $0x7,%rsi
and    $0xfffffffffffffff8,%rsi
lea    (%rdi,%rsi,1),%rax
add    $0xffffffffffffffff,%rax
mov    %rsi,%rdx
neg    %rdx
and    %rax,%rdx
mov    %rsp,%rdi
call   1030 <posix_memalign@plt>
test   %eax,%eax
jne    1144 <bh_align_malloc+0x34>
mov    (%rsp),%rax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i8* @bh_align_malloc(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i64 %0, i64* %3
store i64 %1, i64* %4
store i8* null, i8** %5
store i8* null, i8** %6
%8 = load i64, i64* %4
%9 = add i64 %8, 8
%10 = sub i64 %9, 1
%11 = and i64 %10, -8
store i64 %11, i64* %4
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = add i64 %12, %13
%15 = sub i64 %14, 1
%16 = load i64, i64* %4
%17 = sub i64 %16, 1
%18 = xor i64 %17, -1
%19 = and i64 %15, %18
store i64 %19, i64* %3
%20 = load i64, i64* %4
%21 = load i64, i64* %3
%22 = call i32 @posix_memalign(i8** %6, i64 %20, i64 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %2
br label %28
26: ; preds = %2
%27 = load i8*, i8** %6
br label %28
28: ; preds = %26, %25
%29 = phi i8* [ null, %25 ], [ %27, %26 ]
store i8* %29, i8** %5
%30 = load i8*, i8** %5
ret i8* %30
}

LLVM IR (-O2):
define i8* @bh_align_malloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = add i64 %1, 7
%6 = and i64 %5, -8
%7 = add i64 %0, -1
%8 = add i64 %7, %6
%9 = sub i64 0, %6
%10 = and i64 %8, %9
%11 = call i32 @posix_memalign(i8** %3, i64 %6, i64 %10)
%12 = icmp eq i32 %11, 0
%13 = load i8*, i8** %3
%14 = select i1 %12, i8* %13, i8* null
ret i8* %14
}