#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 *img_buf_memalign(size_t align, size_t size) {
  void *addr, *x = NULL;
  addr = malloc(size + align - 1 + sizeof(size_t));
  if (addr) {
    x = (void *)(((size_t)((unsigned char *)addr + sizeof(size_t)) +
                  (((int)align) - 1)) &
                 (size_t) - ((int)align));
    ((size_t *)x)[-1] = (size_t)addr;
  }
  return x;
}


x86 assembly (clang, -O0):
<img_buf_memalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x8(%rbp),%rdi
sub    $0x1,%rdi
add    $0x8,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1181 <img_buf_memalign+0x71>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    -0x8(%rbp),%rcx
mov    %ecx,%edx
xor    %ecx,%ecx
sub    %edx,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x8(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<img_buf_memalign>:
push   %rbx
mov    %rdi,%rbx
add    %rsi,%rdi
add    $0x7,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     114e <img_buf_memalign+0x3e>
lea    -0x1(%rbx),%ecx
movslq %ecx,%rcx
lea    (%rax,%rcx,1),%rdx
add    $0x8,%rdx
movabs $0xffffffff00000000,%rcx
ff ff ff
imul   %rbx,%rcx
sar    $0x20,%rcx
and    %rdx,%rcx
mov    %rax,-0x8(%rcx)
jmp    1150 <img_buf_memalign+0x40>
xor    %ecx,%ecx
mov    %rcx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @img_buf_memalign(i64 %0, i64 %1) {
%3 = add i64 %0, 7
%4 = add i64 %3, %1
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %21, label %7
7: ; preds = %2
%8 = getelementptr inbounds i8, i8* %5, i64 8
%9 = ptrtoint i8* %8 to i64
%10 = shl i64 %0, 32
%11 = add i64 %10, -4294967296
%12 = ashr exact i64 %11, 32
%13 = add i64 %12, %9
%14 = mul i64 %0, -4294967296
%15 = ashr exact i64 %14, 32
%16 = and i64 %13, %15
%17 = inttoptr i64 %16 to i8*
%18 = ptrtoint i8* %5 to i64
%19 = inttoptr i64 %16 to i64*
%20 = getelementptr inbounds i64, i64* %19, i64 -1
store i64 %18, i64* %20
br label %21
21: ; preds = %7, %2
%22 = phi i8* [ %17, %7 ], [ null, %2 ]
ret i8* %22
}