#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__ ptrdiff_t;

/* Variables and functions */
void *dir_malloc(size_t);
void *libsais_align_up(void *, size_t);
void *libsais_alloc_aligned(size_t size, size_t alignment) {
  void *address = dir_malloc(size + sizeof(short) + alignment - 1);
  if (address != NULL) {
    void *aligned_address = libsais_align_up(
        (void *)((ptrdiff_t)address + (ptrdiff_t)(sizeof(short))), alignment);
    ((short *)aligned_address)[-1] =
        (short)((ptrdiff_t)aligned_address - (ptrdiff_t)address);
    return aligned_address;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<libsais_alloc_aligned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
add    -0x18(%rbp),%rdi
sub    $0x1,%rdi
call   1030 <dir_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     118c <libsais_alloc_aligned+0x6c>
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <libsais_align_up@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %ax,%cx
mov    -0x28(%rbp),%rax
mov    %cx,-0x2(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1194 <libsais_alloc_aligned+0x74>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libsais_alloc_aligned>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
add    %rsi,%rdi
add    $0x1,%rdi
call   1030 <dir_malloc@plt>
test   %rax,%rax
je     1154 <libsais_alloc_aligned+0x34>
mov    %rax,%rbx
mov    %rax,%rdi
add    $0x2,%rdi
mov    %r14,%rsi
call   1040 <libsais_align_up@plt>
mov    %eax,%ecx
sub    %ebx,%ecx
mov    %cx,-0x2(%rax)
jmp    1156 <libsais_alloc_aligned+0x36>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @libsais_alloc_aligned(i64 %0, i64 %1) {
%3 = add i64 %0, 1
%4 = add i64 %3, %1
%5 = tail call i8* @dir_malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = ptrtoint i8* %5 to i64
%9 = add nsw i64 %8, 2
%10 = inttoptr i64 %9 to i8*
%11 = tail call i8* @libsais_align_up(i8* %10, i64 %1)
%12 = ptrtoint i8* %11 to i64
%13 = sub nsw i64 %12, %8
%14 = trunc i64 %13 to i16
%15 = getelementptr inbounds i8, i8* %11, i64 -2
%16 = bitcast i8* %15 to i16*
store i16 %14, i16* %16
br label %17
17: ; preds = %2, %7
%18 = phi i8* [ %11, %7 ], [ null, %2 ]
ret i8* %18
}