#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 size_t alignment;

/* Variables and functions */
int EINVAL;
int ENOMEM;
void *malloc(size_t);
int posix_memalign(void **memptr, size_t alignment, size_t size) {
  void *mem;
  if (alignment % 2 != 0 || alignment % sizeof(void *) != 0)
    return EINVAL;
  if ((mem = malloc(size + alignment)) == NULL)
    return ENOMEM;
  *memptr =
      (void *)(((unsigned long)mem) % (alignment) == 0
                   ? ((unsigned long)mem)
                   : ((unsigned long)mem) +
                         ((alignment) - ((unsigned long)mem) % (alignment)));
  return 0;
}


x86 assembly (clang, -O0):
<posix_memalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    1148 <posix_memalign+0x38>
mov    -0x18(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     1159 <posix_memalign+0x49>
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <posix_memalign+0xce>
mov    -0x20(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1185 <posix_memalign+0x75>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <posix_memalign+0xce>
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    $0x0,%rdx
jne    11a8 <posix_memalign+0x98>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11cc <posix_memalign+0xbc>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%rax
xor    %edx,%edx
divq   -0x18(%rbp)
mov    -0x38(%rbp),%rax
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_memalign>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
test   $0x7,%bl
je     1127 <posix_memalign+0x17>
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
jmp    1173 <posix_memalign+0x63>
mov    %rdi,%r14
add    %rbx,%rdx
mov    %rdx,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     1150 <posix_memalign+0x40>
mov    %rax,%rcx
or     %rbx,%rax
shr    $0x20,%rax
je     115b <posix_memalign+0x4b>
mov    %rcx,%rax
xor    %edx,%edx
div    %rbx
jmp    1161 <posix_memalign+0x51>
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
jmp    1173 <posix_memalign+0x63>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
sub    %rdx,%rbx
test   %rdx,%rdx
cmove  %rdx,%rbx
add    %rcx,%rbx
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @posix_memalign(i8** %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i64, i64* %6
%10 = urem i64 %9, 2
%11 = icmp ne i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %3
%13 = load i64, i64* %6
%14 = urem i64 %13, 8
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12, %3
%17 = load i32, i32* @EINVAL
store i32 %17, i32* %4
br label %49
18: ; preds = %12
%19 = load i64, i64* %7
%20 = load i64, i64* %6
%21 = add i64 %19, %20
%22 = call i8* @malloc(i64 %21)
store i8* %22, i8** %8
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32, i32* @ENOMEM
store i32 %25, i32* %4
br label %49
26: ; preds = %18
%27 = load i8*, i8** %8
%28 = ptrtoint i8* %27 to i64
%29 = load i64, i64* %6
%30 = urem i64 %28, %29
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i8*, i8** %8
%34 = ptrtoint i8* %33 to i64
br label %45
35: ; preds = %26
%36 = load i8*, i8** %8
%37 = ptrtoint i8* %36 to i64
%38 = load i64, i64* %6
%39 = load i8*, i8** %8
%40 = ptrtoint i8* %39 to i64
%41 = load i64, i64* %6
%42 = urem i64 %40, %41
%43 = sub i64 %38, %42
%44 = add i64 %37, %43
br label %45
45: ; preds = %35, %32
%46 = phi i64 [ %34, %32 ], [ %44, %35 ]
%47 = inttoptr i64 %46 to i8*
%48 = load i8**, i8*** %5
store i8* %47, i8** %48
store i32 0, i32* %4
br label %49
49: ; preds = %45, %24, %16
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @posix_memalign(i8** nocapture writeonly %0, i64 %1, i64 %2) {
%4 = and i64 %1, 7
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = load i32, i32* @EINVAL
br label %22
8: ; preds = %3
%9 = add i64 %2, %1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = load i32, i32* @ENOMEM
br label %22
14: ; preds = %8
%15 = ptrtoint i8* %10 to i64
%16 = urem i64 %15, %1
%17 = icmp eq i64 %16, 0
%18 = sub i64 %1, %16
%19 = select i1 %17, i64 0, i64 %18
%20 = add i64 %19, %15
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %0
br label %22
22: ; preds = %14, %12, %6
%23 = phi i32 [ %7, %6 ], [ %13, %12 ], [ 0, %14 ]
ret i32 %23
}