#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 *fmsynth_memory_alloc(size_t alignment, size_t size) {
  void **place;
  uintptr_t addr = 0;
  void *ptr = malloc(alignment + size + sizeof(uintptr_t));
  if (!ptr) {
    return NULL;
  }
  addr = ((uintptr_t)ptr + sizeof(uintptr_t) + alignment) & ~(alignment - 1);
  place = (void **)addr;
  place[-1] = ptr;
  return (void *)addr;
}


x86 assembly (clang, -O0):
<fmsynth_memory_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0x8,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1155 <fmsynth_memory_alloc+0x45>
movq   $0x0,-0x8(%rbp)
jmp    1190 <fmsynth_memory_alloc+0x80>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
add    -0x10(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x8(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmsynth_memory_alloc>:
push   %rbx
mov    %rdi,%rbx
add    %rsi,%rdi
add    $0x8,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     1138 <fmsynth_memory_alloc+0x28>
lea    0x8(%rbx),%rcx
add    %rax,%rcx
neg    %rbx
and    %rcx,%rbx
mov    %rax,-0x8(%rbx)
jmp    113a <fmsynth_memory_alloc+0x2a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

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