#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;
int ENOMEM;
void *memalign(size_t, size_t);
int posix_memalign(void **memptr, size_t alignment, size_t size) {
  if (alignment % sizeof(void *) != 0)
    return EINVAL;
  void *mem = memalign(alignment, size);
  if (mem != NULL) {
    *memptr = mem;
    return 0;
  } else
    return ENOMEM;
}


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)
mov    -0x18(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     1147 <posix_memalign+0x37>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1186 <posix_memalign+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <memalign@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     117a <posix_memalign+0x6a>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1186 <posix_memalign+0x76>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_memalign>:
push   %rbx
test   $0x7,%sil
je     1122 <posix_memalign+0x12>
mov    0x2eba(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1030 <memalign@plt>
test   %rax,%rax
je     113c <posix_memalign+0x2c>
mov    %rax,(%rbx)
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e9d(%rip),%rax
jmp    111e <posix_memalign+0xe>

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

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 %14
8: ; preds = %3
%9 = tail call i8* @memalign(i64 %1, i64 %2)
%10 = icmp eq i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %8
store i8* %9, i8** %0
br label %14
12: ; preds = %8
%13 = load i32, i32* @ENOMEM
br label %14
14: ; preds = %11, %12, %6
%15 = phi i32 [ %7, %6 ], [ 0, %11 ], [ %13, %12 ]
ret i32 %15
}