#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 */
int EINVAL;
void *aligned_alloc(size_t, size_t);
int errno;
int posix_memalign(void **res, size_t align, size_t len) {
  if (align < sizeof(void *))
    return EINVAL;
  void *mem = aligned_alloc(align, len);
  if (!mem)
    return errno;
  *res = mem;
  return 0;
}


x86 assembly (clang, -O0):
<posix_memalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x8,-0x18(%rbp)
jae    1140 <posix_memalign+0x30>
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    117f <posix_memalign+0x6f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <aligned_alloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116d <posix_memalign+0x5d>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    117f <posix_memalign+0x6f>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_memalign>:
push   %rbx
cmp    $0x7,%rsi
ja     1120 <posix_memalign+0x10>
mov    0x2eba(%rip),%rax
jmp    1141 <posix_memalign+0x31>
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1030 <aligned_alloc@plt>
test   %rax,%rax
je     113a <posix_memalign+0x2a>
mov    %rax,(%rbx)
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = 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 = icmp ult i64 %9, 8
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* %4
br label %24
13: ; preds = %3
%14 = load i64, i64* %6
%15 = load i64, i64* %7
%16 = call i8* @aligned_alloc(i64 %14, i64 %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = load i32, i32* @errno
store i32 %20, i32* %4
br label %24
21: ; preds = %13
%22 = load i8*, i8** %8
%23 = load i8**, i8*** %5
store i8* %22, i8** %23
store i32 0, i32* %4
br label %24
24: ; preds = %21, %19, %11
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @posix_memalign(i8** nocapture writeonly %0, i64 %1, i64 %2) {
%4 = icmp ult i64 %1, 8
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = load i32, i32* @EINVAL
br label %13
7: ; preds = %3
%8 = tail call i8* @aligned_alloc(i64 %1, i64 %2)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load i32, i32* @errno
br label %13
12: ; preds = %7
store i8* %8, i8** %0
br label %13
13: ; preds = %10, %12, %5
%14 = phi i32 [ %6, %5 ], [ 0, %12 ], [ %11, %10 ]
ret i32 %14
}