#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 *aligned_alloc(size_t, size_t);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, char const *, int, char *);
char *strerror(int /*<<< orphan*/);
void *do_aligned_alloc(size_t alignment, size_t size, const char *func,
                       int line) {
  void *mem;
  if ((size % alignment) != 0) {
    size = ((size / alignment) + 1) * alignment;
  }
  mem = aligned_alloc(alignment, size);
  if (mem == NULL) {
    printf("%s: aligned_alloc() line %d failed: %s\n", func, line,
           strerror(errno));
    return NULL;
  }
  return mem;
}


x86 assembly (clang, -O0):
<do_aligned_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
cmp    $0x0,%rdx
je     1176 <do_aligned_alloc+0x46>
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
add    $0x1,%rax
imul   -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <aligned_alloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d1 <do_aligned_alloc+0xa1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <do_aligned_alloc+0xa9>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_aligned_alloc>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,%rax
or     %rdi,%rax
shr    $0x20,%rax
je     1150 <do_aligned_alloc+0x20>
mov    %rsi,%rax
xor    %edx,%edx
div    %rdi
jmp    1156 <do_aligned_alloc+0x26>
mov    %esi,%eax
xor    %edx,%edx
div    %edi
add    $0x1,%rax
imul   %rdi,%rax
test   %rdx,%rdx
cmove  %rsi,%rax
mov    %rax,%rsi
call   1040 <aligned_alloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1198 <do_aligned_alloc+0x68>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe76(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <printf@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"%s: aligned_alloc() line %d failed: %s\0A\00"
@errno = global i32 0
define i8* @do_aligned_alloc(i64 %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i64 %0, i64* %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i64, i64* %7
%12 = load i64, i64* %6
%13 = urem i64 %11, %12
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load i64, i64* %7
%17 = load i64, i64* %6
%18 = udiv i64 %16, %17
%19 = add i64 %18, 1
%20 = load i64, i64* %6
%21 = mul i64 %19, %20
store i64 %21, i64* %7
br label %22
22: ; preds = %15, %4
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = call i8* @aligned_alloc(i64 %23, i64 %24)
store i8* %25, i8** %10
%26 = load i8*, i8** %10
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %29, i32 %30, i8* %32)
store i8* null, i8** %5
br label %36
34: ; preds = %22
%35 = load i8*, i8** %10
store i8* %35, i8** %5
br label %36
36: ; preds = %34, %28
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
@.str = [40 x i8] c"%s: aligned_alloc() line %d failed: %s\0A\00"
@errno = global i32 0
define i8* @do_aligned_alloc(i64 %0, i64 %1, i8* %2, i32 %3) {
%5 = urem i64 %1, %0
%6 = udiv i64 %1, %0
%7 = icmp eq i64 %5, 0
%8 = add i64 %6, 1
%9 = mul i64 %8, %0
%10 = select i1 %7, i64 %1, i64 %9
%11 = tail call i8* @aligned_alloc(i64 %0, i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %4
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %3, i8* %15)
br label %17
17: ; preds = %4, %13
ret i8* %11
}